tcpconnect.c revision 136337
1/*- 2 * Copyright (c) 2004 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 136337 2004-10-09 20:58:28Z 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 <stdio.h> 37#include <stdlib.h> 38#include <string.h> 39#include <unistd.h> 40 41static void 42usage(volid) 43{ 44 45 fprintf(stderr, "tcpconnect server port\n"); 46 fprintf(stderr, "tcpconnect client ip port count\n"); 47 exit(-1); 48} 49 50static void 51tcpconnect_server(int argc, char *argv[]) 52{ 53 int listen_sock, accept_sock; 54 struct sockaddr_in sin; 55 char *dummy; 56 long port; 57 58 if (argc != 1) 59 usage(); 60 61 bzero(&sin, sizeof(sin)); 62 sin.sin_len = sizeof(sin); 63 sin.sin_family = AF_INET; 64 sin.sin_addr.s_addr = htonl(INADDR_ANY); 65 66 port = strtoul(argv[0], &dummy, 10); 67 if (port < 1 || port > 65535 || *dummy != '\0') 68 usage(); 69 sin.sin_port = htons(port); 70 71 listen_sock = socket(PF_INET, SOCK_STREAM, 0); 72 if (listen_sock == -1) { 73 perror("socket"); 74 exit(-1); 75 } 76 77 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) { 78 perror("bind"); 79 exit(-1); 80 } 81 82 if (listen(listen_sock, -1) == -1) { 83 perror("listen"); 84 exit(1); 85 } 86 87 while (1) { 88 accept_sock = accept(listen_sock, NULL, NULL); 89 close(accept_sock); 90 } 91} 92 93static void 94tcpconnect_client(int argc, char *argv[]) 95{ 96 struct sockaddr_in sin; 97 long count, i, port; 98 char *dummy; 99 int sock; 100 101 if (argc != 3) 102 usage(); 103 104 bzero(&sin, sizeof(sin)); 105 sin.sin_len = sizeof(sin); 106 sin.sin_family = AF_INET; 107 if (inet_aton(argv[0], &sin.sin_addr) == 0) { 108 perror(argv[0]); 109 exit(-1); 110 } 111 112 port = strtoul(argv[1], &dummy, 10); 113 if (port < 1 || port > 65535 || *dummy != '\0') 114 usage(); 115 sin.sin_port = htons(port); 116 117 count = strtoul(argv[2], &dummy, 10); 118 if (count < 1 || count > 100000 || *dummy != '\0') 119 usage(); 120 121 for (i = 0; i < count; i++) { 122 sock = socket(PF_INET, SOCK_STREAM, 0); 123 if (sock == -1) { 124 perror("socket"); 125 exit(-1); 126 } 127 128 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) { 129 perror("connect"); 130 exit(-1); 131 } 132 133 close(sock); 134 } 135} 136 137int 138main(int argc, char *argv[]) 139{ 140 141 if (argc < 2) 142 usage(); 143 144 if (strcmp(argv[1], "server") == 0) 145 tcpconnect_server(argc - 2, argv + 2); 146 else if (strcmp(argv[1], "client") == 0) 147 tcpconnect_client(argc - 2, argv + 2); 148 else 149 usage(); 150 151 exit(0); 152} 153