sock.c revision 57109
1/* $FreeBSD: head/contrib/ipfilter/ipsend/sock.c 57109 2000-02-10 03:17:51Z peter $ */
2/*
3 * sock.c (C) 1995-1998 Darren Reed
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and due credit is given
7 * to the original author and the contributors.
8 */
9#if !defined(lint)
10static const char sccsid[] = "@(#)sock.c	1.2 1/11/96 (C)1995 Darren Reed";
11static const char rcsid[] = "@(#)$Id: sock.c,v 2.1 1999/08/04 17:31:16 darrenr Exp $";
12#endif
13#include <stdio.h>
14#include <unistd.h>
15#include <string.h>
16#include <stdlib.h>
17#include <stddef.h>
18#include <pwd.h>
19#include <sys/types.h>
20#include <sys/time.h>
21#include <sys/param.h>
22#include <sys/stat.h>
23#ifndef	ultrix
24#include <fcntl.h>
25#endif
26#if (__FreeBSD_version >= 300000)
27# include <sys/dirent.h>
28#else
29# include <sys/dir.h>
30#endif
31#define _KERNEL
32#define	KERNEL
33#ifdef	ultrix
34# undef	LOCORE
35# include <sys/smp_lock.h>
36#endif
37#include <sys/file.h>
38#undef  _KERNEL
39#undef  KERNEL
40#include <nlist.h>
41#include <sys/user.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/proc.h>
45#if !defined(ultrix) && !defined(hpux)
46# include <kvm.h>
47#endif
48#ifdef sun
49#include <sys/systm.h>
50#include <sys/session.h>
51#endif
52#if BSD >= 199103
53#include <sys/sysctl.h>
54#include <sys/filedesc.h>
55#include <paths.h>
56#endif
57#include <math.h>
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/ip.h>
61#include <netinet/tcp.h>
62#include <net/if.h>
63#include <net/route.h>
64#include <netinet/ip_var.h>
65#include <netinet/in_pcb.h>
66#include <netinet/tcp_timer.h>
67#include <netinet/tcp_var.h>
68#include "ipsend.h"
69
70int	nproc;
71struct	proc	*proc;
72
73#ifndef	KMEM
74# ifdef	_PATH_KMEM
75#  define	KMEM	_PATH_KMEM
76# endif
77#endif
78#ifndef	KERNEL
79# ifdef	_PATH_UNIX
80#  define	KERNEL	_PATH_UNIX
81# endif
82#endif
83#ifndef	KMEM
84# define	KMEM	"/dev/kmem"
85#endif
86#ifndef	KERNEL
87# define	KERNEL	"/vmunix"
88#endif
89
90
91#if BSD < 199103
92static	struct	proc	*getproc __P((void));
93#else
94static	struct	kinfo_proc	*getproc __P((void));
95#endif
96
97
98int	kmemcpy(buf, pos, n)
99char	*buf;
100void	*pos;
101int	n;
102{
103	static	int	kfd = -1;
104	off_t	offset = (u_long)pos;
105
106	if (kfd == -1)
107		kfd = open(KMEM, O_RDONLY);
108
109	if (lseek(kfd, offset, SEEK_SET) == -1)
110	    {
111		perror("lseek");
112		return -1;
113	    }
114	if (read(kfd, buf, n) == -1)
115	    {
116		perror("read");
117		return -1;
118	    }
119	return n;
120}
121
122struct	nlist	names[4] = {
123	{ "_proc" },
124	{ "_nproc" },
125#ifdef	ultrix
126	{ "_u" },
127#else
128	{ NULL },
129#endif
130	{ NULL }
131	};
132
133#if BSD < 199103
134static struct proc *getproc()
135{
136	struct	proc	*p;
137	pid_t	pid = getpid();
138	int	siz, n;
139
140	n = nlist(KERNEL, names);
141	if (n != 0)
142	    {
143		fprintf(stderr, "nlist(%#x) == %d\n", names, n);
144		return NULL;
145	    }
146	if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
147	    {
148		fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
149		return NULL;
150	    }
151	siz = nproc * sizeof(struct proc);
152	if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1)
153	    {
154		fprintf(stderr, "read(%#x,%#x,%d) proc\n",
155			names[0].n_value, &p, sizeof(p));
156		return NULL;
157	    }
158	proc = (struct proc *)malloc(siz);
159	if (KMCPY(proc, p, siz) == -1)
160	    {
161		fprintf(stderr, "read(%#x,%#x,%d) proc\n",
162			p, proc, siz);
163		return NULL;
164	    }
165
166	p = proc;
167
168	for (n = nproc; n; n--, p++)
169		if (p->p_pid == pid)
170			break;
171	if (!n)
172		return NULL;
173
174	return p;
175}
176
177
178struct	tcpcb	*find_tcp(fd, ti)
179int	fd;
180struct	tcpiphdr *ti;
181{
182	struct	tcpcb	*t;
183	struct	inpcb	*i;
184	struct	socket	*s;
185	struct	user	*up;
186	struct	proc	*p;
187	struct	file	*f, **o;
188
189	if (!(p = getproc()))
190		return NULL;
191printf("fl %x ty %x cn %d mc %d\n",
192f->f_flag, f->f_type, f->f_count, f->f_msgcount);
193	up = (struct user *)malloc(sizeof(*up));
194#ifndef	ultrix
195	if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1)
196	    {
197		fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea);
198		return NULL;
199	    }
200#else
201	if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1)
202	    {
203		fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value);
204		return NULL;
205	    }
206#endif
207
208	o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
209	if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
210	    {
211		fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
212			up->u_ofile, o, sizeof(*o));
213		return NULL;
214	    }
215	f = (struct file *)calloc(1, sizeof(*f));
216	if (KMCPY(f, o[fd], sizeof(*f)) == -1)
217	    {
218		fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n",
219			up->u_ofile[fd], f, sizeof(*f));
220		return NULL;
221	    }
222
223	s = (struct socket *)calloc(1, sizeof(*s));
224	if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
225	    {
226		fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n",
227			o[fd], s, sizeof(*s));
228		return NULL;
229	    }
230
231	i = (struct inpcb *)calloc(1, sizeof(*i));
232	if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
233	    {
234		fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n",
235			s->so_pcb, i, sizeof(*i));
236		return NULL;
237	    }
238
239	t = (struct tcpcb *)calloc(1, sizeof(*t));
240	if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
241	    {
242		fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n",
243			i->inp_ppcb, t, sizeof(*t));
244		return NULL;
245	    }
246	return (struct tcpcb *)i->inp_ppcb;
247}
248#else
249static struct kinfo_proc *getproc()
250{
251	static	struct	kinfo_proc kp;
252	pid_t	pid = getpid();
253	int	mib[4];
254	size_t	n;
255
256	mib[0] = CTL_KERN;
257	mib[1] = KERN_PROC;
258	mib[2] = KERN_PROC_PID;
259	mib[3] = pid;
260
261	n = sizeof(kp);
262	if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1)
263	    {
264		perror("sysctl");
265		return NULL;
266	    }
267	return &kp;
268}
269
270
271struct	tcpcb	*find_tcp(tfd, ti)
272int	tfd;
273struct	tcpiphdr *ti;
274{
275	struct	tcpcb	*t;
276	struct	inpcb	*i;
277	struct	socket	*s;
278	struct	filedesc	*fd;
279	struct	kinfo_proc	*p;
280	struct	file	*f, **o;
281
282	if (!(p = getproc()))
283		return NULL;
284
285	fd = (struct filedesc *)malloc(sizeof(*fd));
286	if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1)
287	    {
288		fprintf(stderr, "read(%#lx,%#lx) failed\n",
289			(u_long)p, (u_long)p->kp_proc.p_fd);
290		return NULL;
291	    }
292
293	o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
294	if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
295	    {
296		fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",
297			(u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o));
298		return NULL;
299	    }
300	f = (struct file *)calloc(1, sizeof(*f));
301	if (KMCPY(f, o[tfd], sizeof(*f)) == -1)
302	    {
303		fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n",
304			(u_long)o[tfd], (u_long)f, (u_long)sizeof(*f));
305		return NULL;
306	    }
307
308	s = (struct socket *)calloc(1, sizeof(*s));
309	if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
310	    {
311		fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n",
312			(u_long)f->f_data, (u_long)s, (u_long)sizeof(*s));
313		return NULL;
314	    }
315
316	i = (struct inpcb *)calloc(1, sizeof(*i));
317	if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
318	    {
319		fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n",
320			(u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i));
321		return NULL;
322	    }
323
324	t = (struct tcpcb *)calloc(1, sizeof(*t));
325	if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
326	    {
327		fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n",
328			(u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t));
329		return NULL;
330	    }
331	return (struct tcpcb *)i->inp_ppcb;
332}
333#endif /* BSD < 199301 */
334
335int	do_socket(dev, mtu, ti, gwip)
336char	*dev;
337int	mtu;
338struct	tcpiphdr *ti;
339struct	in_addr	gwip;
340{
341	struct	sockaddr_in	rsin, lsin;
342	struct	tcpcb	*t, tcb;
343	int	fd, nfd, len;
344
345	printf("Dest. Port: %d\n", ti->ti_dport);
346
347	fd = socket(AF_INET, SOCK_STREAM, 0);
348	if (fd == -1)
349	    {
350		perror("socket");
351		return -1;
352	    }
353
354	if (fcntl(fd, F_SETFL, FNDELAY) == -1)
355	    {
356		perror("fcntl");
357		return -1;
358	    }
359
360	bzero((char *)&lsin, sizeof(lsin));
361	lsin.sin_family = AF_INET;
362	bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
363	      sizeof(struct in_addr));
364	if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
365	    {
366		perror("bind");
367		return -1;
368	    }
369	len = sizeof(lsin);
370	(void) getsockname(fd, (struct sockaddr *)&lsin, &len);
371	ti->ti_sport = lsin.sin_port;
372	printf("sport %d\n", ntohs(lsin.sin_port));
373	nfd = initdevice(dev, ntohs(lsin.sin_port), 1);
374
375	if (!(t = find_tcp(fd, ti)))
376		return -1;
377
378	bzero((char *)&rsin, sizeof(rsin));
379	rsin.sin_family = AF_INET;
380	bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
381	      sizeof(struct in_addr));
382	rsin.sin_port = ti->ti_dport;
383	if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
384	    errno != EINPROGRESS)
385	    {
386		perror("connect");
387		return -1;
388	    }
389	KMCPY(&tcb, t, sizeof(tcb));
390	ti->ti_win = tcb.rcv_adv;
391	ti->ti_seq = tcb.snd_nxt - 1;
392	ti->ti_ack = tcb.rcv_nxt;
393
394	if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1)
395		return -1;
396	(void)write(fd, "Hello World\n", 12);
397	sleep(2);
398	close(fd);
399	return 0;
400}
401