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 * $FreeBSD$ 33 */ 34 35 #include <arpa/inet.h> 36 #include <netinet/in.h> 37 #include <sys/types.h> 38 #include <sys/socket.h> 39 40 #include <err.h> 41 #include <sysexits.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #define buflen 80 50 51 /* 52 * Setup a TCP server listening on a port for connections, all of 53 * which subseuqently have their user cookie set. 54 */ 55 int 56 main(int argc, char **argv) 57 { 58 struct sockaddr_in srv; 59 int sock, accepted, port, cookie; 60 int ret; 61 char recvbuf[buflen]; 62 63 if (argc != 3) { 64 fprintf(stderr, "Usage: %s port cookie\n", argv[0]); 65 exit(2); 66 } 67 68 port = atoi(argv[1]); 69 cookie = atoi(argv[2]); 70 71 sock = socket(PF_INET, SOCK_STREAM, 0); 72 if (sock < 0) 73 err(EXIT_FAILURE, "socket"); 74 75 srv.sin_len = sizeof(srv); 76 srv.sin_family = AF_INET; 77 srv.sin_port = htons(port); 78 srv.sin_addr.s_addr = INADDR_ANY; 79 80 if (bind(sock, (struct sockaddr *)&srv, srv.sin_len) < 0) 81 err(EX_OSERR, "failed to bind to port %d", port); 82 83 if (listen(sock, 5) < 0) 84 err(EX_OSERR, "failed to listen on socket"); 85 86 ret = setsockopt(sock, SOL_SOCKET, SO_USER_COOKIE, &cookie, sizeof(cookie)); 87 if (ret < 0) 88 err(EX_OSERR, "setsockopt(SO_USER_COOKIE)"); 89 90 while (1) { 91 92 accepted = accept(sock, NULL, 0); 93 94 if (accepted < 0) 95 err(EX_OSERR, "accept failed"); 96 97 ret = setsockopt(accepted, SOL_SOCKET, SO_USER_COOKIE, 98 &cookie, sizeof(cookie)); 99 if (ret < 0) 100 err(EX_OSERR, "setsockopt(SO_USER_COOKIE)"); 101 102 ret = read(accepted, &recvbuf, buflen); 103 104 if (ret < 0) 105 warn("failed read"); 106 107 close(accepted); 108 } 109 110 return (0); 111 } 112