1 // (C) Copyright 2015 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 #include <stdio.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <errno.h>
24 #include <netinet/in.h>
25 #include <unistd.h>
26 #include <netdb.h>
27 #include <string.h>
28 #include <sys/xattr.h>
29 
30 int main(int argc, char* argv[])
31 {
32 
33 	int sock;
34 	char message[255] = "hello";
35 	struct sockaddr_in server_addr;
36 	char* label_in;
37 	char* label_out;
38 	char* attr_out = "security.SMACK64IPOUT";
39 	char* attr_in = "security.SMACK64IPIN";
40 	char out[256];
41 	int port;
42 
43 	struct timeval timeout;
44 	timeout.tv_sec = 15;
45 	timeout.tv_usec = 0;
46 
47 	struct hostent*  host = gethostbyname("localhost");
48 
49 	if (argc != 4)
50 	{
51 		perror("Client: Arguments missing, please provide socket labels");
52 		return 2;
53 	}
54 
55 	port = atoi(argv[1]);
56 	label_in = argv[2];
57 	label_out = argv[3];
58 
59 	if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
60 	{
61 		perror("Client: Socket failure");
62 		return 2;
63 	}
64 
65 
66 	if(fsetxattr(sock, attr_out, label_out, strlen(label_out), 0) < 0)
67 	{
68 		perror("Client: Unable to set attribute SMACK64IPOUT");
69 		return 2;
70 	}
71 
72 	if(fsetxattr(sock, attr_in, label_in, strlen(label_in), 0) < 0)
73 	{
74 		perror("Client: Unable to set attribute SMACK64IPIN");
75 		return 2;
76 	}
77 
78 	server_addr.sin_family = AF_INET;
79 	server_addr.sin_port = htons(port);
80 	bcopy((char*) host->h_addr, (char*) &server_addr.sin_addr.s_addr,host->h_length);
81 	bzero(&(server_addr.sin_zero),8);
82 
83 	if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
84 	{
85 		perror("Client: Set timeout failed\n");
86 		return 2;
87 	}
88 
89 	if (connect(sock, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
90 	{
91     		perror("Client: Connection failure");
92 			close(sock);
93         	return 1;
94 	}
95 
96 
97 	if(write(sock, message, strlen(message)) < 0)
98 	{
99 		perror("Client: Error sending data\n");
100 		close(sock);
101 		return 1;
102 	}
103 	close(sock);
104 	return 0;
105 }
106 
107 
108 
109 
110 
111 
112