diff options
Diffstat (limited to 'recipes-mac/smack/tcp-smack-test/tcp_server.c')
| -rw-r--r-- | recipes-mac/smack/tcp-smack-test/tcp_server.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/recipes-mac/smack/tcp-smack-test/tcp_server.c b/recipes-mac/smack/tcp-smack-test/tcp_server.c new file mode 100644 index 0000000..9285dc6 --- /dev/null +++ b/recipes-mac/smack/tcp-smack-test/tcp_server.c | |||
| @@ -0,0 +1,118 @@ | |||
| 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 <string.h> | ||
| 27 | |||
| 28 | int main(int argc, char* argv[]) | ||
| 29 | { | ||
| 30 | |||
| 31 | int sock; | ||
| 32 | int clientsock; | ||
| 33 | char message[255]; | ||
| 34 | socklen_t client_length; | ||
| 35 | struct sockaddr_in server_addr, client_addr; | ||
| 36 | char* label_in; | ||
| 37 | char* attr_in = "security.SMACK64IPIN"; | ||
| 38 | int port; | ||
| 39 | |||
| 40 | struct timeval timeout; | ||
| 41 | timeout.tv_sec = 15; | ||
| 42 | timeout.tv_usec = 0; | ||
| 43 | |||
| 44 | if (argc != 3) | ||
| 45 | { | ||
| 46 | perror("Server: Argument missing please provide port and label for SMACK64IPIN"); | ||
| 47 | return 2; | ||
| 48 | } | ||
| 49 | |||
| 50 | port = atoi(argv[1]); | ||
| 51 | label_in = argv[2]; | ||
| 52 | bzero(message,255); | ||
| 53 | |||
| 54 | |||
| 55 | if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) | ||
| 56 | { | ||
| 57 | perror("Server: Socket failure"); | ||
| 58 | return 2; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | if(fsetxattr(sock, attr_in, label_in, strlen(label_in),0) < 0) | ||
| 63 | { | ||
| 64 | perror("Server: Unable to set attribute ipin 2"); | ||
| 65 | return 2; | ||
| 66 | } | ||
| 67 | |||
| 68 | server_addr.sin_family = AF_INET; | ||
| 69 | server_addr.sin_port = htons(port); | ||
| 70 | server_addr.sin_addr.s_addr = INADDR_ANY; | ||
| 71 | bzero(&(server_addr.sin_zero),8); | ||
| 72 | |||
| 73 | if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) | ||
| 74 | { | ||
| 75 | perror("Server: Set timeout failed\n"); | ||
| 76 | return 2; | ||
| 77 | } | ||
| 78 | |||
| 79 | if(bind(sock, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) | ||
| 80 | { | ||
| 81 | perror("Server: Bind failure "); | ||
| 82 | return 2; | ||
| 83 | } | ||
| 84 | |||
| 85 | listen(sock, 1); | ||
| 86 | client_length = sizeof(client_addr); | ||
| 87 | |||
| 88 | clientsock = accept(sock,(struct sockaddr*) &client_addr, &client_length); | ||
| 89 | |||
| 90 | if (clientsock < 0) | ||
| 91 | { | ||
| 92 | perror("Server: Connection failed"); | ||
| 93 | close(sock); | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | if(fsetxattr(clientsock, "security.SMACK64IPIN", label_in, strlen(label_in),0) < 0) | ||
| 99 | { | ||
| 100 | perror(" Server: Unable to set attribute ipin 2"); | ||
| 101 | close(sock); | ||
| 102 | return 2; | ||
| 103 | } | ||
| 104 | |||
| 105 | if(read(clientsock, message, 254) < 0) | ||
| 106 | { | ||
| 107 | perror("Server: Error when reading from socket"); | ||
| 108 | close(clientsock); | ||
| 109 | close(sock); | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | close(clientsock); | ||
| 115 | close(sock); | ||
| 116 | |||
| 117 | return 0; | ||
| 118 | } | ||
