Integrate parts of meta-intel-iot-security
[AGL/meta-agl.git] / meta-security / recipes-test / udp-smack-test / files / udp_server.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         int sock,ret;\r
29         struct sockaddr_in server_addr, client_addr;\r
30         socklen_t len;\r
31         char message[5];\r
32         char* label;\r
33         char* attr = "security.SMACK64IPIN";\r
34         int port;\r
35 \r
36         if(argc != 3)\r
37         {\r
38                 perror("Server: Argument missing, please provide port and label for SMACK64IPIN");\r
39                 return 2;\r
40         }\r
41         \r
42         port = atoi(argv[1]);\r
43         label = argv[2];\r
44 \r
45         struct timeval timeout;\r
46         timeout.tv_sec = 15;\r
47         timeout.tv_usec = 0;\r
48 \r
49         sock = socket(AF_INET,SOCK_DGRAM,0);\r
50         if(sock < 0)\r
51         {\r
52                 perror("Server: Socket error");\r
53                 return 2;\r
54         }\r
55         \r
56 \r
57         if(fsetxattr(sock, attr, label, strlen(label), 0) < 0)\r
58         {\r
59                 perror("Server: Unable to set attribute ");\r
60                 return 2;\r
61         }\r
62 \r
63         server_addr.sin_family = AF_INET;         \r
64         server_addr.sin_port = htons(port);     \r
65         server_addr.sin_addr.s_addr = INADDR_ANY; \r
66         bzero(&(server_addr.sin_zero),8); \r
67         \r
68 \r
69         if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)\r
70         {\r
71                 perror("Server: Set timeout failed\n");\r
72                 return 2;\r
73         }\r
74 \r
75         if(bind(sock, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0)\r
76         {\r
77                 perror("Server: Bind failure");\r
78                 return 2;\r
79         }\r
80 \r
81         len = sizeof(client_addr);\r
82         ret = recvfrom(sock, message, sizeof(message), 0, (struct sockaddr*)&client_addr,\r
83                                         &len);\r
84         close(sock);\r
85         if(ret < 0)\r
86         {\r
87                 perror("Server: Error receiving");\r
88                 return 1;\r
89 \r
90         }\r
91         return 0;\r
92 }\r
93 \r