1/* -*- c-basic-offset: 8; -*-
2 *
3 * Copyright (c) 1993 W. Richard Stevens.  All rights reserved.
4 * Permission to use or modify this software and its documentation only for
5 * educational purposes and without fee is hereby granted, provided that
6 * the above copyright notice appear in all copies.  The author makes no
7 * representations about the suitability of this software for any purpose.
8 * It is provided "as is" without express or implied warranty.
9 */
10
11
12#include "config.h"   /* configuration options for current OS */
13
14#include <stdio.h>
15#ifdef HAVE_SYS_TYPES_H
16#include <sys/types.h>
17#endif
18#include <errno.h>
19#include <sys/param.h>
20#include <sys/socket.h>
21
22#ifdef HAVE_SYS_SELECT_H
23#include <sys/select.h>
24#endif
25
26#ifndef	HAVE_ADDRINFO_STRUCT
27#include "addrinfo.h"
28#endif
29
30#include <sys/uio.h>
31#ifdef	__bsdi__
32#include <machine/endian.h> /* required before tcp.h, for BYTE_ORDER */
33#endif
34#include <netinet/tcp.h>	   /* TCP_NODELAY */
35#include <netdb.h>	   /* getservbyname(), gethostbyname() */
36#include <netinet/in.h>
37#include <arpa/inet.h>
38#include <string.h>
39#include <stdlib.h>
40#include <unistd.h>
41
42
43
44/* Older resolvers do not have gethostbyname2() */
45#ifndef	HAVE_GETHOSTBYNAME2
46#define	gethostbyname2(host,family)		gethostbyname((host))
47#endif
48
49/* This avoids a warning with glibc compilation */
50#ifndef errno
51extern int errno;
52#endif
53
54
55/* Miscellaneous constants */
56#define	MAXLINE	     4096	/* max text line length */
57#define	MAXSOCKADDR  128	/* max socket address structure size */
58#define	BUFFSIZE     8192	/* buffer size for reads and writes */
59
60/* stdin and stdout file descriptors */
61#define STDIN_FILENO  0
62#define STDOUT_FILENO 1
63
64#define	min(a,b)	((a) < (b) ? (a) : (b))
65#define	max(a,b)	((a) > (b) ? (a) : (b))
66
67/* declare global variables */
68extern int		bindport;
69extern int		broadcast;
70extern int		cbreak;
71extern int		chunkwrite;
72extern int		client;
73extern int		connectudp;
74extern int		crlf;
75extern int		debug;
76extern int		dofork;
77extern int		dontroute;
78extern char		foreignip[];
79extern int		foreignport;
80extern int		halfclose;
81extern int		ignorewerr;
82extern int		iptos;
83extern int		ipttl;
84extern char		joinip[];
85extern int		keepalive;
86extern long		linger;
87extern int		listenq;
88extern char		localip[];
89extern int		maxseg;
90extern int		mcastttl;
91extern int		msgpeek;
92extern int		nodelay;
93extern int		nbuf;
94extern int		onesbcast;
95extern int		pauseclose;
96extern int		pauseinit;
97extern int		pauselisten;
98extern int		pauserw;
99extern int		reuseaddr;
100extern int		reuseport;
101extern int		readlen;
102extern int		writelen;
103extern int		recvdstaddr;
104extern int		rcvbuflen;
105extern int		sndbuflen;
106extern long		rcvtimeo;
107extern long		sndtimeo;
108extern char	       *rbuf;
109extern char	       *wbuf;
110extern int		server;
111extern int		sigio;
112extern int		sourcesink;
113extern int		sroute_cnt;
114extern int		udp;
115extern int		urgwrite;
116extern int		verbose;
117extern int		usewritev;
118
119extern struct sockaddr_in	cliaddr, servaddr;
120
121/* Earlier versions of gcc under SunOS 4.x have problems passing arguments
122   that are structs (as opposed to pointers to structs).  This shows up
123   with inet_ntoa, whose argument is a "struct in_addr". */
124
125#if	defined(sun) && defined(__GNUC__) && defined(GCC_STRUCT_PROBLEM)
126#define	INET_NTOA(foo)	inet_ntoa(&foo)
127#else
128#define	INET_NTOA(foo)	inet_ntoa(foo)
129#endif
130
131				/* function prototypes */
132void	buffers(int);
133int     cliopen(char *, char *);
134int	crlf_add(char *, int, const char *, int);
135int	crlf_strip(char *, int, const char *, int);
136void	join_mcast(int, struct sockaddr_in *);
137void	loop_tcp(int);
138void	loop_udp(int);
139void	pattern(char *, int);
140int		servopen(char *, char *);
141void	sink_tcp(int);
142void	sink_udp(int);
143void	source_tcp(int);
144void	source_udp(int);
145void	sroute_doopt(int, char *);
146void	sroute_set(int);
147void	sleep_us(unsigned int);
148void	sockopts(int, int);
149ssize_t	dowrite(int, const void *, size_t);
150
151void	TELL_WAIT(void);
152void	TELL_PARENT(pid_t);
153void	WAIT_PARENT(void);
154void	TELL_CHILD(pid_t);
155void	WAIT_CHILD(void);
156
157void	 err_dump(const char *, ...);
158void	 err_msg(const char *, ...);
159void	 err_quit(const char *, ...);
160void	 err_ret(const char *, ...);
161void	 err_sys(const char *, ...);
162
163ssize_t	 writen(int, const void *, size_t);
164