tcpconnect.c revision 136613
1136337Srwatson/*-
2136337Srwatson * Copyright (c) 2004 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 136613 2004-10-17 11:07:03Z ru $
27136337Srwatson */
28136337Srwatson
29136337Srwatson#include <sys/types.h>
30136337Srwatson#include <sys/socket.h>
31136337Srwatson
32136337Srwatson#include <netinet/in.h>
33136337Srwatson
34136337Srwatson#include <arpa/inet.h>
35136337Srwatson
36136337Srwatson#include <stdio.h>
37136337Srwatson#include <stdlib.h>
38136337Srwatson#include <string.h>
39136337Srwatson#include <unistd.h>
40136337Srwatson
41136337Srwatsonstatic void
42136613Sruusage(void)
43136337Srwatson{
44136337Srwatson
45136337Srwatson	fprintf(stderr, "tcpconnect server port\n");
46136337Srwatson	fprintf(stderr, "tcpconnect client ip port count\n");
47136337Srwatson	exit(-1);
48136337Srwatson}
49136337Srwatson
50136337Srwatsonstatic void
51136337Srwatsontcpconnect_server(int argc, char *argv[])
52136337Srwatson{
53136337Srwatson	int listen_sock, accept_sock;
54136337Srwatson	struct sockaddr_in sin;
55136337Srwatson	char *dummy;
56136337Srwatson	long port;
57136337Srwatson
58136337Srwatson	if (argc != 1)
59136337Srwatson		usage();
60136337Srwatson
61136337Srwatson	bzero(&sin, sizeof(sin));
62136337Srwatson	sin.sin_len = sizeof(sin);
63136337Srwatson	sin.sin_family = AF_INET;
64136337Srwatson	sin.sin_addr.s_addr = htonl(INADDR_ANY);
65136337Srwatson
66136337Srwatson	port = strtoul(argv[0], &dummy, 10);
67136337Srwatson	if (port < 1 || port > 65535 || *dummy != '\0')
68136337Srwatson		usage();
69136337Srwatson	sin.sin_port = htons(port);
70136337Srwatson
71136337Srwatson	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
72136337Srwatson	if (listen_sock == -1) {
73136337Srwatson		perror("socket");
74136337Srwatson		exit(-1);
75136337Srwatson	}
76136337Srwatson
77136337Srwatson	if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
78136337Srwatson		perror("bind");
79136337Srwatson		exit(-1);
80136337Srwatson	}
81136337Srwatson
82136337Srwatson	if (listen(listen_sock, -1) == -1) {
83136337Srwatson		perror("listen");
84136337Srwatson		exit(1);
85136337Srwatson	}
86136337Srwatson
87136337Srwatson	while (1) {
88136337Srwatson		accept_sock = accept(listen_sock, NULL, NULL);
89136337Srwatson		close(accept_sock);
90136337Srwatson	}
91136337Srwatson}
92136337Srwatson
93136337Srwatsonstatic void
94136337Srwatsontcpconnect_client(int argc, char *argv[])
95136337Srwatson{
96136337Srwatson	struct sockaddr_in sin;
97136337Srwatson	long count, i, port;
98136337Srwatson	char *dummy;
99136337Srwatson	int sock;
100136337Srwatson
101136337Srwatson	if (argc != 3)
102136337Srwatson		usage();
103136337Srwatson
104136337Srwatson	bzero(&sin, sizeof(sin));
105136337Srwatson	sin.sin_len = sizeof(sin);
106136337Srwatson	sin.sin_family = AF_INET;
107136337Srwatson	if (inet_aton(argv[0], &sin.sin_addr) == 0) {
108136337Srwatson		perror(argv[0]);
109136337Srwatson		exit(-1);
110136337Srwatson	}
111136337Srwatson
112136337Srwatson	port = strtoul(argv[1], &dummy, 10);
113136337Srwatson	if (port < 1 || port > 65535 || *dummy != '\0')
114136337Srwatson		usage();
115136337Srwatson	sin.sin_port = htons(port);
116136337Srwatson
117136337Srwatson	count = strtoul(argv[2], &dummy, 10);
118136337Srwatson	if (count < 1 || count > 100000 || *dummy != '\0')
119136337Srwatson		usage();
120136337Srwatson
121136337Srwatson	for (i = 0; i < count; i++) {
122136337Srwatson		sock = socket(PF_INET, SOCK_STREAM, 0);
123136337Srwatson		if (sock == -1) {
124136337Srwatson			perror("socket");
125136337Srwatson			exit(-1);
126136337Srwatson		}
127136337Srwatson
128136337Srwatson		if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
129136337Srwatson			perror("connect");
130136337Srwatson			exit(-1);
131136337Srwatson		}
132136337Srwatson
133136337Srwatson		close(sock);
134136337Srwatson	}
135136337Srwatson}
136136337Srwatson
137136337Srwatsonint
138136337Srwatsonmain(int argc, char *argv[])
139136337Srwatson{
140136337Srwatson
141136337Srwatson	if (argc < 2)
142136337Srwatson		usage();
143136337Srwatson
144136337Srwatson	if (strcmp(argv[1], "server") == 0)
145136337Srwatson		tcpconnect_server(argc - 2, argv + 2);
146136337Srwatson	else if (strcmp(argv[1], "client") == 0)
147136337Srwatson		tcpconnect_client(argc - 2, argv + 2);
148136337Srwatson	else
149136337Srwatson		usage();
150136337Srwatson
151136337Srwatson	exit(0);
152136337Srwatson}
153