libdht
Functions
utils.h File Reference

DHT utilities. More...

#include <sys/socket.h>
#include <arpa/inet.h>
Include dependency graph for utils.h:

Go to the source code of this file.

Functions

const char * hex (const unsigned char id[20])
 Return the hexadecimal representation of a 160-bit value. More...
 
int from_hex (const char *s, unsigned char id[20])
 Convert a hexadecimal representation string to a 160-bit value. More...
 
const char * sockaddr_fmt (const struct sockaddr *sa, socklen_t addrlen)
 Format socket address. More...
 
int sockaddr_cmp (const struct sockaddr *s1, const struct sockaddr *s2)
 Compare two socket addresses. More...
 
const char * compactaddr_fmt (const unsigned char *ip, size_t len)
 Format compact address information. More...
 

Detailed Description

DHT utilities.

Function Documentation

const char* compactaddr_fmt ( const unsigned char *  ip,
size_t  len 
)

Format compact address information.

Returns the given compact address address information as a human-readable string. A compact address is a 4-byte IPv4 address in network byte order followed by a 2-byte port number in network byte order, or a 16-byte IPv6 address in network byte order followed by a 2-byte port number in network byte order.

Parameters
ipThe compact address string buffer
lenLength of the compact address string.
Returns
The address string of NULL if the compact address is invalid.
int from_hex ( const char *  s,
unsigned char  id[20] 
)

Convert a hexadecimal representation string to a 160-bit value.

Scans the hexadecimal string s and converts it to a binary info-hash.

Parameters
sThe hexadecimal string.
idThe buffer into which the info-hash will be returned.
Returns
0 on success, -1 if the string is invalid.
const char* hex ( const unsigned char  id[20])

Return the hexadecimal representation of a 160-bit value.

Formats the given binary info-hash into an hexadecimal string. The string is returned in a statically allocated buffer, which subsequent calls will overwrite and is therefore not reentrant.

Parameters
idThe 160-bit value to convert.
Returns
Hexadecimal string.
int sockaddr_cmp ( const struct sockaddr *  s1,
const struct sockaddr *  s2 
)

Compare two socket addresses.

Parameters
s1First socket address.
s2Second socket address.
Returns
-1 if s1 < s2, 0 if s1 == s2, 1 if s1 > s2.
const char* sockaddr_fmt ( const struct sockaddr *  sa,
socklen_t  addrlen 
)

Format socket address.

Returns the socket address as a human-readable string. This function supports AF_INET and AF_INET6 socket address families. The string is returned in a statically allocated buffer, which subsequent calls will overwrite, and is therefore not reentrant.

Parameters
saThe socket address
addrlenLength of the socket address structure
Returns
The address string.