Trusted Solaris Developer's Guide

TCP/IP Client

To request the service, the client program connects to the server, sends a request, and waits for the meeting message. If the connection is closed before a message is received, the client exits because there is no meeting at its sensitivity label. If a message is received, the client uses t6recvfrom(3NSL) to obtain the message. Code to process the information is not shown in the example.

This first part of the program sets up data structures for the client request and server response.

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <tsol/label.h>
#include <tsix/t6attrs.h>

char *clnt_req = "Request Meeting Info";

main(int argc, char **argv)
{
	int sock, retval;
	char buf[256];
	int buflen = 256;
	int num;
	struct sockaddr_in serv_addr;
	struct hostent *hostent;
	bslabel_t *bsl;
	t6mask_t new_mask, sl_mask = T6M_SL;
	t6attr_t handle;
	char    *string = (char *)0;

This next main program segment processes the command-line argc and argv inputs to get the host name and port number of the server and establishes a connection.

	if (argc != 2) {
		printf("Usage: %s host\n", argv[0]);
		exit (1);
	}
	if ((hostent = gethostbyname(argv[1])) == NULL) {
		perror("gethostbyname");
		exit(1);
	}

	memset((void *) &serv_addr, 0, sizeof (serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(10000);
	memcpy((void *) &serv_addr.sin_addr,
		(void *) hostent->h_addr_list[0], hostent->h_length);

	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}
	if (connect(sock, (struct sockaddr *)&serv_addr,
		sizeof (serv_addr)) < 0) {
		perror("connect");
		exit(1);
	}
	printf("Connected\n");
	if ((handle = t6alloc_blk(sl_mask)) == NULL) {
		printf("t6attr_alloc: no memory");
		exit(1);
	}

This next main program segment sends the request to the server. The request is sent at the sensitivity label at which the client process is executing. When the server processes the request, it sends back meeting information for the sensitivity label at which the request is made only. The t6recvfrom(3NSL) routine receives the meeting information.

/* Send a request to server */
	write(sock, clnt_req, strlen(clnt_req));

	if ((num = t6recvfrom(sock, buf, buflen, 0, 0, 0, handle,
		&new_mask)) < 0) {
		perror("t6recvfrom");
		exit (1);
	} else if (num == 0) {
		printf("Connection closed, nothing matches.\n");
		exit(0);
	} else
		printf("Received Reply\n");

	retval = bsltos(bsl, &string, 0, LONG_WORDS);
	printf("Retval = %d, Sensitivity label = %s\n", retval, string);
	printf("Message = %s\n", buf);
}