Several extremely useful Winsock support functions can help you get information about peers, make DNS queries, and convert between various data formats that are supported with Winsock.
To get address information for a connection that a socket is currently connected with, you can use the getpeername() function:
int getpeername (SOCKET s, struct sockaddr *name, int *namelen);
The first parameter is the socket for which you want information, the name parameter is a pointer to a SOCKADDR structure (which will be a SOCKADDR_IN for TCP/IP), and namelen is a pointer to its length.
To retrieve address information for the local interface of a connected socket, the following function is defined:
int getsockname (SOCKET s, struct sockaddr *name, int *namelen);
The getsockname() function takes the same parameters as the getpeername() function, except that the name parameter will return local address information, rather than the remote connection.
To get your device's host name, you can call the aptly named gethostname() function:
int gethostname (char *name, int namelen);
The name parameter is a pointer to a character buffer that will receive the name, and namelen is the length of the name buffer specified.
To set the device's host name, use the sethostname() function, which is defined as follows:
int sethostname(char *pName, int cName);
This function takes two parameters. The first is a pointer to a buffer that contains the new host name, and the second is the cName parameter, which specifies the buffer's length.
The inet_addr function is used to convert an IP "dot" address (e.g., 192.158.0.0) into an unsigned long value:
unsigned long inet_addr (const char *cp);
It takes a single parameter, a pointer to a character buffer cp containing the IP "dot" address string.
If you want to perform the inverse function, use inet_ntoa(), which will convert an address to a string:
char * inet_ntoa (struct in_addr in);
Here, you pass in an address structure; typically, a SOCKADDR_IN that defines the address you want to convert.
The next set of support functions we will look at deals with host name resolution. All Winsock functions dealing with host addresses and names use a HOSTENT data structure. This structure contains all the available information about an individual host. It is defined as follows:
struct hostent { char *h_name; char **h_aliases; short h_addrtype; short h_length; char **h_addr_list; };
The h_name field contains the official name of the host. The h_aliases field points to an array of alternative host name string pointers that are terminated by a NULL pointer value. The h_addrtype field is the type of address being returned, and will be either AF_INET or AF_IRDA. The h_length field is the length in bytes of each address in the h_addr_list array. The h_addr_list array is a null-terminated array of network addresses in network byte order.
To get host information for a network device by name, use the following:
struct hostent *gethostbyname(const char *name);
The only parameter needed is the name of the host for which you want information.
Use the following to get information about a host by address:
struct hostent *gethostbyaddr(const char *addr, int len, int type);
Here, you need to pass in a bit more information about the host. The first parameter is the address structure, with information about the host you want to query. This will typically be the value that is returned from calling the inet_addr() function. The len parameter is the length in bytes of the address, and the type parameter is the type of address?either AF_INET or AF_IDRA.
The final set of support functions deals with converting values from host byte order to network byte order.
To convert a long value to/from host byte order to network byte order, use the following functions:
u_long htonl (u_long hostlong); u_long ntohl (u_long netlong);
Convert a short value to/from host byte order to network byte order as follows:
u_short htons (u_short hostshort); u_short ntohs (u_short netshort);