Integrate parts of meta-intel-iot-security
[AGL/meta-agl.git] / meta-security / recipes-test / udp-smack-test / files / udp_client.c
1 // (C) Copyright 2015 Intel Corporation\r
2 //\r
3 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
4 // of this software and associated documentation files (the "Software"), to deal\r
5 // in the Software without restriction, including without limitation the rights\r
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7 // copies of the Software, and to permit persons to whom the Software is\r
8 // furnished to do so, subject to the following conditions:\r
9 //\r
10 // The above copyright notice and this permission notice shall be included in\r
11 // all copies or substantial portions of the Software.\r
12 //\r
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19 // THE SOFTWARE.\r
20 #include <sys/socket.h>\r
21 #include <stdio.h>\r
22 #include <netinet/in.h>\r
23 #include <netdb.h>\r
24 #include <string.h>\r
25 \r
26 int main(int argc, char* argv[])\r
27 {\r
28         char* message = "hello";\r
29         int sock, ret;\r
30         struct sockaddr_in server_addr;\r
31         struct hostent*  host = gethostbyname("localhost");\r
32         char* label;\r
33         char* attr = "security.SMACK64IPOUT";\r
34         int port;\r
35         if (argc != 3)\r
36         {\r
37                 perror("Client: Argument missing, please provide port and  label for SMACK64IPOUT");\r
38                 return 2;\r
39         }\r
40 \r
41         port = atoi(argv[1]);\r
42         label = argv[2];\r
43         sock = socket(AF_INET, SOCK_DGRAM,0);\r
44         if(sock < 0)\r
45         {\r
46                 perror("Client: Socket failure");\r
47                 return 2;\r
48         }\r
49         \r
50 \r
51         if(fsetxattr(sock, attr, label, strlen(label),0) < 0)\r
52         {\r
53                 perror("Client: Unable to set attribute ");\r
54                 return 2;\r
55         }\r
56 \r
57 \r
58         server_addr.sin_family = AF_INET;\r
59         server_addr.sin_port = htons(port);\r
60         bcopy((char*) host->h_addr, (char*) &server_addr.sin_addr.s_addr,host->h_length);\r
61         bzero(&(server_addr.sin_zero),8);\r
62         \r
63         ret = sendto(sock, message, strlen(message),0,(const struct sockaddr*)&server_addr,\r
64                         sizeof(struct sockaddr_in));\r
65 \r
66         close(sock);\r
67         if(ret < 0)\r
68         {\r
69                 perror("Client: Error sending message\n");\r
70                 return 1;\r
71         }\r
72         \r
73         return 0;\r
74 }\r
75 \r