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