Docsity

The UNIX Socket Protocol - Introduction to Operating Systems | EECS 678, Lab Reports of Operating Systems

Partial preview of the text

Download The UNIX Socket Protocol - Introduction to Operating Systems | EECS 678 and more Lab Reports Operating Systems in PDF only on Docsity!

The Unix Socket Protocol

Michael Jantz

Prasad Kulkarni

Introduction

โ— In this lab, we will use the Unix socket protocol to send data

from a client to a server and back to the client.

โ— Download the starter code for this lab:

  • (^) http://people.eecs.ku.edu/~mjantz/678labs/lab04/lab04.tar

โ— As you work through this lab, the following man pages might

be helpful:

  • (^) socket(2), unix(7), tcp(7), bind(2), listen(2), accept(2), connect(2), read(2), write(2)
  • (^) Man pages come in different sections. System calls are all in section
    1. To access a page from a section other than section 1, give the section as an argument (e.g. man 2 socket)

Client / Server Model

โ— (^) In this lab, we will create two programs which communicate using the client / server model. โ— (^) In this model, the server establishes a handshake socket with a particular address. Client programs create a session socket , which attempts to connect to the handshake socket. When the handshake socket receives a request to connect, the server accepts the connection and creates another session socket which is connected to the client's session socket. These session sockets can be used to exchange data between the client and server processes. Client 1 Client 2 HS Socket SS1 SS Client 1 SS Client 2 SS Connect Connect Server Accept Connected Session 1 Connected Session 2

Creating Sockets

โ— Sockets are created using the socket() system call:

  • (^) int socket(int domain, int type, int protocol) โ—

The domain parameter specifies a communication domain;

this selects the protocol family which we will use.

  • (^) The socket protocol governs how a socket is accessed and how it can be used. For this lab, we will use the PF_UNIX protocol family.

โ— The type parameter specifies the communication semantics.

  • (^) e.g. SOCK_STREAM. More on this later.

โ— The protocol parameter specifies a particular protocol to be

used with the socket (within the protocol family selected).

  • (^) Most protocol families only have one associated protocol option, and thus, this parameter is usually 0.

Streaming Sockets

โ— The SOCK_STREAM argument specifies that we will use the

socket as a two-way connection-based socket.

โ— This is in contrast to datagram sockets, which do not

establish safe connections and support packet broadcast

(sending to all endpoints in a network)

โ— This requires that we establish a connection between the two

socket endpoints before exchanging data.

โ— In order to establish a connection, we must declare a

common address that both endpoints can refer to.

Establishing an Address

โ— (^) Unix sockets are addressed by creating a file on the file system which socket endpoints refer to.

  • (^) As a point of comparison, the IPv4 and IPv6 socket families can be used to communicate between processes on different machines. They use an IP address and port number to address socket endpoints. โ— (^) The format of a Unix socket address is given in the Unix manual: #define UNIX_PATH_MAX 108 struct sockaddr_un { sa_family_t sun_family; /* AF_UNIX / char sun_path[UNIX_PATH_MAX]; / pathname */ } โ— (^) In both the client and server, populate the sockaddr_un structure with appropriate values:
  • (^) sun_family should be set to the predefined macro AF_UNIX (e.g. saun.sun_family = AF_UNIX)
  • (^) sun_path should be a common pathname. Use strcpy to copy this pathname.

Binding to an Address

โ— Use the bind() system call to bind a socket to an address:

  • (^) int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
  • (^) sockfd is the file descriptor referring to the socket you just created (record the return value of socket() to get this file descriptor)
  • (^) addr is the address you wish to bind the socket to. Observe that there is only one bind() system call for every socket protocol. Socket protocols typically have different address formats. Therefore, you will have to cast the sockaddr_un structure to a sockaddr structure.
  • (^) addrlen is the size of the address structure. You can get this easily using the builtin sizeof() function, e.g. โ— (^) sizeof(saun)
  • (^) bind() returns 0 on success. On error, -1 is returned and errno is set appropriately.

Getting the Socket to Listen

โ— (^) Now that we have the socket bound to an address, we need to tell the operating system that this socket is a handshake socket (i.e. one that does not transfer data, but only listens for sockets trying to connect). โ— (^) This is performed with the listen system call:

  • (^) int listen(int sockfd, int backlog)
  • (^) sockfd will be the file descriptor of the socket you've already created
  • (^) backlog is the maximum length to which the queue of pending connections to sockfd may grow. It is irrelevant for our example (any positive integer will do), but is necessary for the following reason: โ— (^) When the client sends a connection request to the server, the server receives the request, sends the client an acknowledgement packet, and puts the client in a pending connections queue. โ— (^) When the client receives the acknowledgement packet, it will send another packet to the server acknowledging this. Only when the server receives this packet will the client be taken off the pending connections queue, and the connection established. โ— (^) Without a maximum length for the pending queue, a hostile client could very easily drain the resources of a server by sending many connection requests without ever sending the final acknowledgement packet.
  • (^) listen() returns 0 on success. On error, -1 is returned and errno is set appropriately.

Connecting the Client

โ— Now that the server is setup, time to connect the client:
  • (^) Create another Unix socket in client.c using the socket() system call.
  • (^) If you have not done so already, populate the sockaddr_un struct (saun) with the same values you had used for the server
  • (^) Now, call connect() to tell the client to attempt to connect to the server: โ— (^) int connect(int sockfd, struct sockaddr *addr, socklen_t addrlen) โ— (^) sockfd should be the file descriptor for the newly created socket โ— (^) addr is the socket address you wish to attempt a connection on. Remember to cast the sockaddr_un variable in the client here. โ— (^) addrlen is the size of the address struct passed to this system call. This can be obtained easily with sizeof(saun). โ— (^) On success, 0 is returned. On error, -1 is returned, and errno is set appropriately.
  • (^) If the connection succeeds, the socket pointed to by sockfd will now be connected to another socket endpoint at the address provided (in our case, the socket returned by the accept() call in the server).

An Uppercase String Converter

โ— (^) At this point, each the client and the server should have access to a file descriptor that points to a connected socket endpoint. โ— (^) In this lab, we will use these sockets to implement a server which performs uppercase string conversion. The client will send several strings to the server, the server will convert these strings to uppercase, and then send the converted strings back to the client. This is very similar to what you will do in the first class project. โ— (^) I have predefined an array of strings to pass at the top of client.c.

  • (^) The array is called strs. As it is defined, strs[i] refers to the i'th string in strs. โ— (^) You should use a for loop in client.c to iterate over the strings in the strs array, send them to the server, and receive them from the server. โ— (^) The server should read the strings until no more are sent, convert each string to uppercase (using the predefined convert_string), and send each string back to the client.

Finishing Up

โ— (^) When you are done, uncomment the printf statements in each the client and the server. When you run both the client and the server (note you should always start the server first), your output should look like this: -bash-3.2$ ./server RECEIVED: this is the first string from the client SENDING: THIS IS THE FIRST STRING FROM THE CLIENT RECEIVED: this is the second string from the client SENDING: THIS IS THE SECOND STRING FROM THE CLIENT RECEIVED: this is the third string from the client SENDING: THIS IS THE THIRD STRING FROM THE CLIENT -bash-3.2$ ./client SENDING: this is the first string from the client RECEIVED: THIS IS THE FIRST STRING FROM THE CLIENT SENDING: this is the second string from the client RECEIVED: THIS IS THE SECOND STRING FROM THE CLIENT SENDING: this is the third string from the client RECEIVED: THIS IS THE THIRD STRING FROM THE CLIENT