Introduction #
VxWorks Networking #
- Network programming allows users to:
- Build services.
- Create distributed applications.
- VxWorks network programming tools:
- Berkeley sockets.
- zbuf Socket API.
- Sun RPC (Remote Procedure Call).
Network Components #
Ports #
- Abstract destination point within a node.
- TCP/UDP intertask communication:
- Data is sent by writing to a remote port.
- Data is received by reading from a local port.
Packet Encapsulation #
Sockets #
Socket Overview #
- Programmatic interface to internet protocols.
- Protocol specified when socket created (e.g., UDP or TCP).
- Server binds its socket to a well known port.
- Client’s port number dynamically assigned by the system.
Ports #
- Socket address consists of:
- An internet address (node socket is on).
- A port number (destination within that node).
- Port identified by a short integer. VxWorks port usage conventions: ```bash * 0 - 1023 Reserved for system services (e.g.,rlogin, telnet, etc.). * 1024 - 5000 Dynamically allocated. * > 5000 User defined. ```
- Unique to each machine and protocol.
Socket Address #
- Generic socket address:
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* protocol specific address data */
};
- Socket address structure used by Internet Protocol:
struct sockaddr_in {
short sin_family; /* AF_INET */
u_short sin_port; /* port number */
struct in_addr sin_addr; /* internet address */
char sin_zero[8]; /* padding, must be zeroed out */
};
VxWorks supports only Internet sockets.
Network Byte Ordering #
- Fields in the struct sockaddr_in must be put in network byte order (big-endian).
- Macros to convert long/short integers between the host and the network byte ordering:
htonl( ) host to network long.
htons( ) host to network short.
ntohl( ) network to host long.
ntohs( ) network to host short.
Caveat - User Data #
- To send data in a system independent way:
- Sender converts data from its system-dependent format to some standard format.
- Receiver converts data from the standard format to its system-dependent format.
- The standard format used must handle:
- Any standard data types used (e.g., int, short, float, etc.).
- Data structure alignment.
- One such facility, XDR, will be discussed in the RPC section of this chapter.
Creating a Socket #
int socket (domain, type, protocol)
domain Must be PF_INET.
type Typically SOCK_DGRAM (UDP) or SOCK_STREAM (TCP).
protocol Socket protocol (typically 0).
- Opens a socket (analogous to open( ) for files).
- Returns a socket file descriptor or ERROR.
Binding a Socket #
- To bind a socket to a well known address:
STATUS bind (sockFd, pAdrs, adrsLen)
sockFd Socket descriptor returned from socket()
pAdrs Pointer to a struct sockaddr to which to bind this socket
adrsLen sizeof (struct sockaddr)
- Typically only called by server
Example Server Stub #
1 #define PORT_NUM (USHORT)5001;
2 struct sockaddr_in myAddr;
3 int mySocket;
4 ...
5 mySocket = socket (PF_INET, SOCK_DGRAM,0);
6 if (mySocket == ERROR)
7 return (ERROR);
8
9 bzero (&myAddr, sizeof (struct sockaddr_in));
10 myAddr.sin_family = AF_INET;
11 myAddr.sin_port = htons (PORT_NUM);
12 myAddr.sin_addr.s_addr = INADDR_ANY;
13
14 if (bind ( mySocket, (struct sockaddr *)&myAddr,
15 sizeof (myAddr)) == ERROR)
16 {
17 close (mySocket);
18 return (ERROR);
19 }
UDP Sockets Programming #
UDP Socket Overview #
Sending Data on UDP Sockets #
int sendto (sockFd, pBuf, bufLen, flags, pDestAdrs, destLen)
sockFd Socket to send data from
pBuf Address of data to send
bufLen Length of data in bytes
flags Special actions to be taken
pDestAdrs Pointer to struct sockaddr containing destination address
destLen sizeof (struct sockaddr)
- Returns the number of bytes sent or ERROR.
Receiving Data on UDP Sockets #
int recvfrom (sockFd, pBuf, buflen, flags, pFromAdrs, pFromLen)
sockFd Socket to receive data from.
pBuf Buffer to hold incoming data.
buflen Maximum number of bytes to read.
flags Flags for special handling of data.
pFromAdrs Pointer to struct sockaddr. Routine supplies internet address of sender.
pFromLen Pointer to integer. Must be initialized to sizeof (struct sockaddr).
- Blocks until data available to receive.
- Returns number of bytes received or ERROR.
TCP Sockets Programming #
TCP Socket Overview #
- TCP is connection based (like making a phone call).
- Concurrent servers are often implemented:
TCP Server Overview #
1 /* master server */
2 masterFd = socket (PF_INET, SOCK_STREAM, 0)
3 /* fill in server’s sockaddr struct */
4 bind (...) /* bind to well-known port */
5 listen (...) /* configure request queue */
6 FOREVER
7 {
8 clientFd = accept (masterFd, ...)
9 taskSpawn (..., slaveSrv, clientFd, ...)
10 }
1 /* slave server */
2 slaveSrv(clientFd, ...)
3 {
4 read (clientFd, ...) /* read request */
5 serviceClient ()
6 write (clientFd, ...) /* send reply */
7 close (clientFd)
8 }
TCP Client Overview #
1 /* TCP Client */
2 fd = socket (PF_INET, SOCK_STREAM, 0)
3
4 /* fill in sockaddr with server’s address */
5
6 connect (fd, ...) /* request service */
7
8 write (fd, ...) /* send request */
9 read (fd, ...) /* read reply */
10
11 close (fd) /* terminate connection */
Server Initialization #
- Before accepting connections, server must:
- Create a socket (socket( )).
- Bind the socket to a well known address (bind( )).
- Establish a connection request queue:
STATUS listen (sockFd, queueLen)
sockFd Socket descriptor returned from socket( ).
queueLen Nominal length of connection request queue.
Accepting Connections #
int accept (sockFd, pAdrs, pAdrsLen)
sockFd Servers socket (returned from socket( )).
pAdrs Pointer to a struct sockaddr through which the client’s address is returned.
pAdrsLen Pointer to length of address.
- Blocks until connection request occurs.
- Returns new socket file descriptor (connected to the client) or ERROR.
- Original socket, sockFd, is unconnected and ready to accept other connection requests.
Requesting Connections #
- To connect to the server, the client calls:
STATUS connect (sockFd, pAdrs, adrsLen)
sockFd Client’s socket descriptor.
pAdrs Pointer to server’s socket address.
adrsLen sizeof (struct sockaddr)
- Blocks until connection is established or timeout.
- Returns ERROR on timeout or if no server is bound to pAdrs.
Exchanging Data #
- read()/write() may be used to exchange data:
- Caveat: TCP is stream oriented.
- write( ) may write only part of message if I/O is nonblocking.
- read( ) may read more or less than one message.
Cleaning up a Stream Socket #
- When done using a socket, close( ) it:
- Frees resources associated with socket.
- Attempts to deliver any remaining data.
- Causes read( ) from peer socket to return 0.
- Can also use shutdown( ) to terminate output, while still receiving data from peer socket.
Setting Socket Options #
- Options can be enabled on a per socket basis, including:
- Don’t delay write( )’s to coalesce small TCP packets.
- Enable UDP broadcasts.
- Linger on close( ) until data is sent.
- bind( ) to an address already in use.
- Change the size of the send/receive buffers.
- Consult UNIX man pages on setsockopt( ) for details.
- To make a socket non-blocking:
int val = 1; /* Set to 0 for blocking I/O */
ioctl (sock, FIONBIO, &val);
zbuf Socket API #
- Improves application performance by minimizing data copies through buffer loaning.
- Application must manage buffers.
- zbuf application can communicate with a standard socket application.
- Supports TCP and UDP protocols.
- See zbufLib and zbufSockLib for details.
- Proprietary API.
RPC #
RPC Overview #
- RPC (Remote Procedure Call) provides a standard way to invoke procedures on a remote machine.
- For more information about RPC, see:
- Appendix.
- TCP/IP Illustrated Volume I (Stevens).
- Power Programming with RPC (O’Reilly & Associates).
- Documentation and source code can be found in wind/target/unsupported/rpc4.0
RPC Client - Server Model #
VxWorks and rpcgen #
- rpcgen is a RPC protocol compiler.
- From a specification of the remote procedures, rpcgen creates:
- A client stub.
- A server stub.
- The XDR routines for packing/unpacking data structures. Not created if all parameters/return values are standard data types.
- A header file for inclusion by client and server.
- Each VxWorks task accessing RPC calls using code produced by rpcgen must first initialize access.
STATUS rpcTaskInit( )
Summary #
- Transport layer network Protocols: ```c TCP Stream-oriented, reliable port-to-port communication. UDP Packet-oriented, non-reliable port-to-port communication. ```
- Sockets as the interface to network protocols:
- UDP transport protocol
- TCP transport protocol
- Configurable socket options
- zbuf socket API.
- Client/server programming strategies for distributed applications.