1 /* 2 * Copyright (c) 2016 Limelight Networks 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * substantially similar to the "NO WARRANTY" disclaimer below 13 * ("Disclaimer") and any redistribution must be conditioned upon 14 * including a substantially similar Disclaimer requirement for further 15 * binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGES. 29 * 30 * Authors: George Neville-Neil 31 */ 32 33 #include <arpa/inet.h> 34 #include <netinet/in.h> 35 #include <sys/types.h> 36 #include <sys/socket.h> 37 38 #include <err.h> 39 #include <sysexits.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #define buflen 80 48 49 /* 50 * Setup a TCP server listening on a port for connections, all of 51 * which subseuqently have their user cookie set. 52 */ 53 int 54 main(int argc, char **argv) 55 { 56 struct sockaddr_in srv; 57 int sock, accepted, port, cookie; 58 int ret; 59 char recvbuf[buflen]; 60 61 if (argc != 3) { 62 fprintf(stderr, "Usage: %s port cookie\n", argv[0]); 63 exit(2); 64 } 65 66 port = atoi(argv[1]); 67 cookie = atoi(argv[2]); 68 69 sock = socket(PF_INET, SOCK_STREAM, 0); 70 if (sock < 0) 71 err(EXIT_FAILURE, "socket"); 72 73 memset(&srv, 0, sizeof(srv)); 74 srv.sin_len = sizeof(srv); 75 srv.sin_family = AF_INET; 76 srv.sin_port = htons(port); 77 srv.sin_addr.s_addr = INADDR_ANY; 78 79 if (bind(sock, (struct sockaddr *)&srv, srv.sin_len) < 0) 80 err(EX_OSERR, "failed to bind to port %d", port); 81 82 if (listen(sock, 5) < 0) 83 err(EX_OSERR, "failed to listen on socket"); 84 85 ret = setsockopt(sock, SOL_SOCKET, SO_USER_COOKIE, &cookie, sizeof(cookie)); 86 if (ret < 0) 87 err(EX_OSERR, "setsockopt(SO_USER_COOKIE)"); 88 89 while (1) { 90 91 accepted = accept(sock, NULL, 0); 92 93 if (accepted < 0) 94 err(EX_OSERR, "accept failed"); 95 96 ret = setsockopt(accepted, SOL_SOCKET, SO_USER_COOKIE, 97 &cookie, sizeof(cookie)); 98 if (ret < 0) 99 err(EX_OSERR, "setsockopt(SO_USER_COOKIE)"); 100 101 ret = read(accepted, &recvbuf, buflen); 102 103 if (ret < 0) 104 warn("failed read"); 105 106 close(accepted); 107 } 108 109 return (0); 110 } 111