Professional Documents
Culture Documents
AF_INET domain
As a general rule, you will always probably need to include the following header files:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h> // for name resolution
struct sockaddr {
unsigned short sa_family; // Address family, AF_xxx
char sa_data[14]; // 14 octets
};
sa_data contains the socket parameters. In practice, we use the struct sockaddr_in which provides easy access to the fields.
Pointers towards sockaddr and sockaddr_in can always be swapped (or casted).
struct sockaddr_in {
short int sin_family; // AF_INET
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // IP address
unsigned char sin_zero[8]; // 8 null char
};
struct in_addr {
unsigned long s_addr; // 4 octets for IP address (IPv4)
};
Note:
The fields sin_port and sin_addr must be in "Network Byte Order". You thus have to use the functions htons, htonl,
ntohs, and ntohl to do the appropriate conversions.
Converts the string cp given in "a.b.c.d" format into a unsigned long integer (value returned by the function).
Converts the string cp given in "a.b.c.d" format into the in unsigned long integer
Universität Basel, CS221- Computer Networks and Security – Christophe Jelger - 2005 Page 1
Example:
my_socket.sin_family = AF_INET;
my_socket.sin_port = htons(80); // conversion host -> network (80 is the port for HTTP)
my_socket.sin_addr.s_addr = inet_addr("130.79.44.193"); // or inet_aton( "130.79.44.193", &(my_socket.sin_addr) );
memset( &(my_socket.sin_zero), '\0', 8); // set to zero
Name resolution
Name resolution functions permits to retrieve the IP address associated with a name (e.g. 217.12.3.11 for
www.yahoo.ch).
struct hostent {
char *h_name; // main name
char **h_aliases; // alternative names (aliases)
int h_addrtype; // address type (usually AF_INET)
int h_length; // length of address (in octets)
char **h_addr_list; // alternate addresses (in Network Byte Order)
};
#define h_addr h_addr_list[0] // fist address of h_addr_list
This function permits to obtain the IP address(es) associated with a given name.
Example:
/* aliases */
Universität Basel, CS221- Computer Networks and Security – Christophe Jelger - 2005 Page 2
Socket creation and connection management
Socket creation: domain = AF_INET, type = SOCK_STREAM (TCP) or SOCK_DGRAM (UDP), protocol = 0.
In all the prototypes given below, sockfd is a socket descriptor obtained via the socket() call.
Bind the socket to a certain port and IP address. If sock_info.sin_port = htons(0), the system will assign to the
socket a free port. If sock_info.sin_addr.s_addr = INADDR_ANY, the system will assign to the socket an IP
address of the machine.
The socket sockfd is set in a state where it waits for incoming connections. The integer max_number specifies
the maximum size of the queue of connections waiting to be accept()ed.
An incoming connection must be accept()ed. sockfd is the socket descriptor used in the listen() call, and
sock_info will contain the information related to the incoming connection.
CAREFUL: accept() returns a new socket descriptor that is used to send/receive data. The original socket
continues to be used only to detect new incoming connections.
Example:
.
Universität Basel, CS221- Computer Networks and Security – Christophe Jelger - 2005 Page 3
Sending/receiving data: blocking calls !
int send( int sockfd, const void *msg, int length, int flags);
Send length octets of the data stored at msg on the socket sockfd of type SOCK_STREAM.
int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen);
Same as send() but with a SOCK_DGRAM socket. The struct pointed by to contains the IP address and the
UDP port of the destination.
int recv( int sockfd, void *buffer, int length, unsigned int flags);
Wait for a maximum of length octets of data on the SOCK_STREAM socket sockfd – write data to buffer.
int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);
Same as recv() but with a SOCK_DGRAM socket. The struct pointed by from will contain the IP address and
the UDP port of the sender.
Some details about the integer flags. It can take the following values, or 0 for default behavior.
Restrain the use of the socket sockfd according to the value of how: 0 = reception not allowed, 1 = emission not allowed,
2 = reception and emission not allowed. WARNING: shutdown() does not close a socket!
int select( int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
Note: the following header files must be included <sys/time.h> and <unistd.h>
select() monitors a set of descriptors to detect if write or read operations have been performed or must be performed.
The following MACROs are used to manipulate the sets of descriptors.
We remind you that all the functions descriptions are available on Unix systems via the man pages.
Source: Beej's guide to network programming (http://www.ecst.csuchico.edu/~beej/guide/net/).
Book: R. Stevens, B. Fenner, and A. Rudoff, UNIX Network Programming, Vol. 1, 3rd edition, Addison-Wesley.
Universität Basel, CS221- Computer Networks and Security – Christophe Jelger - 2005 Page 4