tcpconnect.c revision 174032
1136337Srwatson/*- 2146251Srwatson * Copyright (c) 2004-2005 Robert N. M. Watson 3136337Srwatson * All rights reserved. 4136337Srwatson * 5136337Srwatson * Redistribution and use in source and binary forms, with or without 6136337Srwatson * modification, are permitted provided that the following conditions 7136337Srwatson * are met: 8136337Srwatson * 1. Redistributions of source code must retain the above copyright 9136337Srwatson * notice, this list of conditions and the following disclaimer. 10136337Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11136337Srwatson * notice, this list of conditions and the following disclaimer in the 12136337Srwatson * documentation and/or other materials provided with the distribution. 13136337Srwatson * 14136337Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15136337Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16136337Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17136337Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18136337Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19136337Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20136337Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21136337Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22136337Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23136337Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24136337Srwatson * SUCH DAMAGE. 25136337Srwatson * 26136337Srwatson * $FreeBSD: head/tools/regression/netinet/tcpconnect/tcpconnect.c 174032 2007-11-28 20:33:58Z bz $ 27136337Srwatson */ 28136337Srwatson 29136337Srwatson#include <sys/types.h> 30136337Srwatson#include <sys/socket.h> 31136337Srwatson 32136337Srwatson#include <netinet/in.h> 33174032Sbz#include <netinet/tcp.h> 34136337Srwatson 35136337Srwatson#include <arpa/inet.h> 36136337Srwatson 37136843Srwatson#include <errno.h> 38174032Sbz#include <fcntl.h> 39136337Srwatson#include <stdio.h> 40136337Srwatson#include <stdlib.h> 41136337Srwatson#include <string.h> 42136337Srwatson#include <unistd.h> 43136337Srwatson 44174032Sbz 45136337Srwatsonstatic void 46136613Sruusage(void) 47136337Srwatson{ 48136337Srwatson 49136337Srwatson fprintf(stderr, "tcpconnect server port\n"); 50174032Sbz fprintf(stderr, "tcpconnect client ip port count [nonblock] [tcpmd5]\n"); 51136337Srwatson exit(-1); 52136337Srwatson} 53136337Srwatson 54136337Srwatsonstatic void 55136337Srwatsontcpconnect_server(int argc, char *argv[]) 56136337Srwatson{ 57136337Srwatson int listen_sock, accept_sock; 58136337Srwatson struct sockaddr_in sin; 59136337Srwatson char *dummy; 60136337Srwatson long port; 61136337Srwatson 62136337Srwatson if (argc != 1) 63136337Srwatson usage(); 64136337Srwatson 65136337Srwatson bzero(&sin, sizeof(sin)); 66136337Srwatson sin.sin_len = sizeof(sin); 67136337Srwatson sin.sin_family = AF_INET; 68136337Srwatson sin.sin_addr.s_addr = htonl(INADDR_ANY); 69136337Srwatson 70136337Srwatson port = strtoul(argv[0], &dummy, 10); 71136337Srwatson if (port < 1 || port > 65535 || *dummy != '\0') 72136337Srwatson usage(); 73136337Srwatson sin.sin_port = htons(port); 74136337Srwatson 75136337Srwatson listen_sock = socket(PF_INET, SOCK_STREAM, 0); 76136843Srwatson if (listen_sock == -1) 77136843Srwatson errx(-1, "socket: %s", strerror(errno)); 78136337Srwatson 79136843Srwatson if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) 80136843Srwatson errx(-1, "bind: %s", strerror(errno)); 81136337Srwatson 82136843Srwatson if (listen(listen_sock, -1) == -1) 83136843Srwatson errx(-1, "listen: %s", strerror(errno)); 84136337Srwatson 85136337Srwatson while (1) { 86136337Srwatson accept_sock = accept(listen_sock, NULL, NULL); 87136337Srwatson close(accept_sock); 88136337Srwatson } 89136337Srwatson} 90136337Srwatson 91136337Srwatsonstatic void 92136337Srwatsontcpconnect_client(int argc, char *argv[]) 93136337Srwatson{ 94136337Srwatson struct sockaddr_in sin; 95136337Srwatson long count, i, port; 96136337Srwatson char *dummy; 97136337Srwatson int sock; 98174032Sbz int nonblock = 0, md5enable = 0; 99136337Srwatson 100174032Sbz if (argc < 3 || argc > 5) 101136337Srwatson usage(); 102174032Sbz for (i=3; i < argc; i++) { 103174032Sbz if (strcmp(argv[i], "nonblock") == 0) 104174032Sbz nonblock = 1; 105174032Sbz if (strcmp(argv[i], "tcpmd5") == 0) 106174032Sbz md5enable = 1; 107174032Sbz } 108136337Srwatson 109136337Srwatson bzero(&sin, sizeof(sin)); 110136337Srwatson sin.sin_len = sizeof(sin); 111136337Srwatson sin.sin_family = AF_INET; 112136843Srwatson if (inet_aton(argv[0], &sin.sin_addr) == 0) 113136843Srwatson errx(-1, "listen: %x", strerror(errno)); 114136337Srwatson 115136337Srwatson port = strtoul(argv[1], &dummy, 10); 116136337Srwatson if (port < 1 || port > 65535 || *dummy != '\0') 117136337Srwatson usage(); 118136337Srwatson sin.sin_port = htons(port); 119136337Srwatson 120136337Srwatson count = strtoul(argv[2], &dummy, 10); 121136337Srwatson if (count < 1 || count > 100000 || *dummy != '\0') 122136337Srwatson usage(); 123136337Srwatson 124136337Srwatson for (i = 0; i < count; i++) { 125136337Srwatson sock = socket(PF_INET, SOCK_STREAM, 0); 126136843Srwatson if (sock == -1) 127136843Srwatson errx(-1, "socket: %s", strerror(errno)); 128136337Srwatson 129174032Sbz /* No warning in default case on ENOPROTOOPT. */ 130174032Sbz if (setsockopt(sock, IPPROTO_TCP, TCP_MD5SIG, 131174032Sbz &md5enable, sizeof(md5enable)) != 0) { 132174032Sbz if (errno == ENOPROTOOPT && md5enable > 0) 133174032Sbz err(-1, "setsockopt(TCP_MD5SIG)"); 134174032Sbz else if (errno != ENOPROTOOPT) 135174032Sbz warn("setsockopt(TCP_MD5SIG)"); 136174032Sbz } 137146251Srwatson 138174032Sbz if (nonblock) { 139174032Sbz if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) 140174032Sbz errx(-1, "fcntl(F_SETFL): %s", strerror(errno)); 141136337Srwatson 142174032Sbz if (connect(sock, (struct sockaddr *)&sin, 143174032Sbz sizeof(sin)) == -1 && errno != EINPROGRESS) 144174032Sbz errx(-1, "connect: %s", strerror(errno)); 145174032Sbz } else { 146174032Sbz if (connect(sock, (struct sockaddr *)&sin, 147174032Sbz sizeof(sin)) == -1) 148174032Sbz errx(-1, "connect: %s", strerror(errno)); 149174032Sbz } 150174032Sbz 151136337Srwatson close(sock); 152136337Srwatson } 153136337Srwatson} 154136337Srwatson 155136337Srwatsonint 156136337Srwatsonmain(int argc, char *argv[]) 157136337Srwatson{ 158136337Srwatson 159136337Srwatson if (argc < 2) 160136337Srwatson usage(); 161136337Srwatson 162136337Srwatson if (strcmp(argv[1], "server") == 0) 163136337Srwatson tcpconnect_server(argc - 2, argv + 2); 164136337Srwatson else if (strcmp(argv[1], "client") == 0) 165136337Srwatson tcpconnect_client(argc - 2, argv + 2); 166136337Srwatson else 167136337Srwatson usage(); 168136337Srwatson 169136337Srwatson exit(0); 170136337Srwatson} 171