tcpconnect.c revision 146251
1/*- 2 * Copyright (c) 2004-2005 Robert N. M. Watson 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/tools/regression/netinet/tcpconnect/tcpconnect.c 146251 2005-05-16 00:54:47Z rwatson $ 27 */ 28 29#include <sys/types.h> 30#include <sys/socket.h> 31 32#include <netinet/in.h> 33 34#include <arpa/inet.h> 35 36#include <errno.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <unistd.h> 41 42static void 43usage(void) 44{ 45 46 fprintf(stderr, "tcpconnect server port\n"); 47 fprintf(stderr, "tcpconnect client ip port count\n"); 48 exit(-1); 49} 50 51static void 52tcpconnect_server(int argc, char *argv[]) 53{ 54 int listen_sock, accept_sock; 55 struct sockaddr_in sin; 56 char *dummy; 57 long port; 58 59 if (argc != 1) 60 usage(); 61 62 bzero(&sin, sizeof(sin)); 63 sin.sin_len = sizeof(sin); 64 sin.sin_family = AF_INET; 65 sin.sin_addr.s_addr = htonl(INADDR_ANY); 66 67 port = strtoul(argv[0], &dummy, 10); 68 if (port < 1 || port > 65535 || *dummy != '\0') 69 usage(); 70 sin.sin_port = htons(port); 71 72 listen_sock = socket(PF_INET, SOCK_STREAM, 0); 73 if (listen_sock == -1) 74 errx(-1, "socket: %s", strerror(errno)); 75 76 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) 77 errx(-1, "bind: %s", strerror(errno)); 78 79 if (listen(listen_sock, -1) == -1) 80 errx(-1, "listen: %s", strerror(errno)); 81 82 while (1) { 83 accept_sock = accept(listen_sock, NULL, NULL); 84 close(accept_sock); 85 } 86} 87 88static void 89tcpconnect_client(int argc, char *argv[]) 90{ 91 struct sockaddr_in sin; 92 long count, i, port; 93 char *dummy; 94 int sock; 95 96 if (argc != 3) 97 usage(); 98 99 bzero(&sin, sizeof(sin)); 100 sin.sin_len = sizeof(sin); 101 sin.sin_family = AF_INET; 102 if (inet_aton(argv[0], &sin.sin_addr) == 0) 103 errx(-1, "listen: %x", strerror(errno)); 104 105 port = strtoul(argv[1], &dummy, 10); 106 if (port < 1 || port > 65535 || *dummy != '\0') 107 usage(); 108 sin.sin_port = htons(port); 109 110 count = strtoul(argv[2], &dummy, 10); 111 if (count < 1 || count > 100000 || *dummy != '\0') 112 usage(); 113 114 for (i = 0; i < count; i++) { 115 sock = socket(PF_INET, SOCK_STREAM, 0); 116 if (sock == -1) 117 errx(-1, "socket: %s", strerror(errno)); 118 119#ifdef NONBLOCK 120 if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) 121 errx(-1, "fcntl(F_SETFL): %s", strerror(errno)); 122 123 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 124 && errno != EINPROGRESS) 125 errx(-1, "connect: %s", strerror(errno)); 126#else 127 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) 128 errx(-1, "connect: %s", strerror(errno)); 129#endif 130 131 close(sock); 132 } 133} 134 135int 136main(int argc, char *argv[]) 137{ 138 139 if (argc < 2) 140 usage(); 141 142 if (strcmp(argv[1], "server") == 0) 143 tcpconnect_server(argc - 2, argv + 2); 144 else if (strcmp(argv[1], "client") == 0) 145 tcpconnect_client(argc - 2, argv + 2); 146 else 147 usage(); 148 149 exit(0); 150} 151