| Winsock & .NET | Winsock | < More TCP/IP Programming Interfaces (APIs) | Linux Socket Index | Socket & Client-server Design Considerations > |


 

 

 

 

 

NETWORK PROGRAMMING

LINUX SOCKET PART 5: APIs & HEADER FILES

 

 

 

 

 

Menu

 

Network Story 1

Network Story 2

Network Story 3

Network Story 4

Network Story 5

Network Story 6

Socket Example 1

Socket Example 2

Socket Example 3

Socket Example 4

Socket Example 5

Socket Example 6

Socket Example 7

Advanced TCP/IP 1

Advanced TCP/IP 2

Advanced TCP/IP 3

Advanced TCP/IP 4

Advanced TCP/IP 5

 

My Training Period:  xx  hours

 

Note:  Program examples if any, compiled using gcc on Linux Fedora Core 3 machine with several update, as normal user.  The Fedora machine used for the testing having the "No Stack Execute" disabled and the SELinux set to default configuration.

 

sendto() and recvfrom() for DATAGRAM (UDP)

  • Since datagram sockets aren’t connected to a remote host, we need to give the destination address before we send a packet.  The prototype is:

int sendto(int sockfd, const void *msg, int len, unsigned int flags, const

struct sockaddr *to, int tolen);

  • This call is basically the same as the call to send() with the addition of two other pieces of information.

  • to is a pointer to a struct sockaddr (which you’ll probably have as a struct sockaddr_in and cast it at the last minute) which contains the destination IP address and port.

  • tolen can simply be set to sizeof(struct sockaddr).

  • Just like with send(), sendto() returns the number of bytes actually sent (which, again, might be less than the number of bytes you told it to send!), or -1 on error.

  • Equally similar are recv() and recvfrom().  The prototype of recvfrom() is:

int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct

sockaddr *from, int *fromlen);

  • Again, this is just like recv() with the addition of a couple fields.

  • from is a pointer to a local struct sockaddr that will be filled with the IP address and port of the originating machine.

  • fromlen is a pointer to a local int that should be initialized to sizeof(struct sockaddr).  When the function returns, fromlen will contain the length of the address actually stored in from. recvfrom() returns the number of bytes received, or -1 on error (with errno set accordingly).

  • Remember, if you connect() a datagram socket, you can then simply use send() and recv() for all your transactions.

  • The socket itself is still a datagram socket and the packets still use UDP, but the socket interface will automatically add the destination and source information for you.

A sample of the client socket call flow

socket()

connect()

while (x)

{

   write()

   read()

}

close()

A sample of the server socket call flow

socket()

bind()

listen()

while (1)

{

   accept()

   while (x)

{

      read()

      write()

}

   close()

}

close()

 

 

 

 

 

 

 

 

 

 

 

 

 

Network Integers versus Host Integers

04, 03, 02, 01.  This is a little endian.

htons()

Host to network short.

ntohs()

Network to host short.

htonl()

Host to network long.

ntohl()

Network to host long.

 

Table 7

 struct sockaddr_in ina

ina.sin_addr.s_addr = inet_addr("10.12.110.57");

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

struct sockaddr_in my_addr;

/* host byte order */

my_addr.sin_family = AF_INET;

/* short, network byte order */

my_addr.sin_port = htons(MYPORT);

inet_aton("10.12.110.57", &(my_addr.sin_addr));

/* zero the rest of the struct */

memset(&(my_addr.sin_zero), 0, 8);

printf("%s", inet_ntoa(ina.sin_addr));

char *a1, *a2;

...

...

a1 = inet_ntoa(ina1.sin_addr); /* this is 192.168.4.1 */

a2 = inet_ntoa(ina2.sin_addr); /* this is 10.11.110.55 */

printf("address 1: %s\n", a1);

printf("address 2: %s\n", a2);

address 1: 10.11.110.55

address 2: 10.11.110.55

 

---------------------------------------------------SOME SUMMARY----------------------------------------------

 

Socket Library Functions

  1. Startup / close.

  2. Data transfer.

  3. Options control.

  4. Other.

  1. Host address.

  2. Ports for services.

  3. Other.

  1. Data conversion.

  2. Address manipulation.

  3. Error handling.

Primary Socket Calls

 

socket()

Create a new socket and return its descriptor.

bind()

Associate a socket with a port and address.

listen()

Establish queue for connection requests.

accept()

Accept a connection request.

connect()

Initiate a connection to a remote host.

recv()

Receive data from a socket descriptor.

send()

Send data to a socket descriptor.

read()

Reads from files, devices, sockets etc.

write()

Writes to files, devices, sockets etc.

close()

“One-way” close of a socket descriptor.

shutdown()

Allows you to cut off communication in a certain direction, or both ways just like close() does.

 

Table 8

 

Network Database Administration functions

Socket Utility Functions

 

ntohs()/ntohl()

Convert short/long from network byte order (big endian) to host byte order.

htons()/htonl()

Convert short/long from host byte order to network byte order.

inet_ntoa()/inet_addr()

Convert 32-bit IP address (network byte order to/from a dotted decimal string).

perror()

Print error message (based on “errno”) to stderr.

herror()

Print error message for gethostbyname() to stderr (used with DNS).

 

Table 9

 

Primary Header Files

<sys/types.h>

Prerequisite typedefs.

<errno.h>

Names for “errno” values (error numbers).

<sys/socket.h>

struct sockaddr; system prototypes and constants.

<netdb.h.h>

Network info lookup prototypes and structures.

<netinet/in.h>

struct sockaddr_in; byte ordering macros.

<arpa/inet.h>

Utility function prototypes.

 

Table 10

 

Ancillary Socket Topics

  1. get/setsockopt() - keepalive, reuse, nodelay.

  2. fcntl() - async signals, blocking.

  3. ioctl() - file, socket, routing, interface options.

Socket header files

  1. Macro definitions.

  2. Data type definitions.

  3. Structure definitions.

  4. Function prototypes.

Header file name

Description

<arpa/inet.h>

Defines prototypes for those network library routines that convert Internet address and dotted-decimal notation, for example, inet_makeaddr().

<arpa/nameser.h>

Defines Internet name server macros and structures that are needed when the system uses the resolver routines.

<error.h>

Defines macros and variables for error reporting.

<fcntl.h>

Defines prototypes, macros, variables, and structures for control-type functions, for example, fcntl().

<net/if.h>

Defines prototypes, macros, variables, and the ifreq and ifconf structures that are associated with ioctl() requests that affect interfaces.

<net/route.h>

Defines prototypes, macros, variables, and the rtentry and rtconf structures that are associated with ioctl() requests that affect routing entries.

<netdb.h>

Contains data definitions for the network library routines.  Defines the following structures:

  • hostent and hostent_data.

  • netent and netent_data.

  • servent and servent_data.

  • protoent and protoent_data.

<netinet/in.h>

Defines prototypes, macros, variables, and the sockaddr_in structure to use with Internet domain sockets.

<netinet/ip.h>

Defines macros, variables, and structures that are associated with setting IP options.

<netinet/ip_icmp.h>

Defines macros, variables, and structures that are associated with the Internet Control Message Protocol (ICMP).

<netinet/tcp.h>

Defines macros, variables, and structures that are associated with setting TCP options.

<netns/idp.h>

Defines IPX packet header. May be needed in AF_NS socket applications.

<netns/ipx.h>

Defines ioctl structures for IPX ioctl() requests.  May be needed in AF_NS socket applications.

<netns/ns.h>

Defines AF_NS socket structures and options.  You must include this file in AF_NS socket applications.

<netns/sp.h>

Defines SPX packet header.  May be needed in AF_NS socket applications.

<nettel/tel.h>

Defines sockaddr_tel structure and related structures and macros. You must include this file in AF_TELEPHONY socket applications.

<resolv.h>

Contains macros and structures that are used by the resolver routines.

<ssl.h>

Defines Secure Sockets Layer (SSL) prototypes, macros, variables, and the following structures:

  • SSLInit

  • SSLHandle

<sys/ioctl.h>

Defines prototypes, macros, variables, and structures for I/O control-type functions, for example, ioctl().

<sys/param.h>

Defines some limits to system fields, in addition to miscellaneous macros and prototypes.

<sys/signal.h>

Defines additional macros, types, structures, and functions that are used by signal routines.

<sys/socket.h>

Defines socket prototypes, macros, variables, and the following structures:

  • sockaddr

  • msghdr

  • linger

You must include this file in all socket applications.

<sys/time.h>

Defines prototypes, macros, variables, and structures that are associated with time functions.

<sys/types.h>

Defines various data types.  Also includes prototypes, macros, variables, and structures that are associated with the select() function.  You must include this file in all socket applications.

<sys/uio.h>

Defines prototypes, macros, variables, and structures that are associated with I/O functions.

<sys/un.h>

Defines prototypes, macros, variables, and the sockaddr_un structure to use with UNIX domain sockets.

<unistd.h>

Contains macros and structures that are defined by the integrated file system.  Needed when the system uses the read() and write() system functions.

 

Table 11: Header files for Linux sockets APIs

 

Continue on next Module…More in-depth discussion about TCP/IP suite is given in Advanced TCP/IP Socket programming.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

 

  1. Check the best selling C/C++, Networking, Linux and Open Source books at Amazon.com.

  2. Protocol sequence diagram examples.

  3. Another site for protocols information.

  4. RFCs.

  5. GCC, GDB and other related tools.

 

 

 

 

 


 

| Winsock & .NET | Winsock | < More TCP/IP Programming Interfaces (APIs) | Linux Socket Index | Socket & Client-server Design Considerations > |