3 * sock.c (C) 1995-1998 Darren Reed
5 * See the IPFILTER.LICENCE file for details on licencing.
9 static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: sock.c,v 2.8.4.7 2007/09/13 07:19:34 darrenr Exp $";
12 #include <sys/param.h>
13 #include <sys/types.h>
16 #if defined(__NetBSD__) && defined(__vax__)
18 * XXX need to declare boolean_t for _KERNEL <sys/files.h>
19 * which ends up including <sys/device.h> for vax. See PR#32907
20 * for further details.
22 typedef int boolean_t;
27 #if (__FreeBSD_version >= 300000)
28 # include <sys/dirent.h>
34 # include <machine/lock.h>
40 # include <sys/smp_lock.h>
42 # include <sys/file.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #if !defined(ultrix) && !defined(hpux) && !defined(__osf__)
55 #include <sys/systm.h>
56 #include <sys/session.h>
59 #include <sys/sysctl.h>
60 #include <sys/filedesc.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/tcp.h>
69 #if defined(__FreeBSD__)
70 # include "radix_ipf.h"
73 # include <net/route.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
93 # define KMEM _PATH_KMEM
98 # define KERNEL _PATH_UNIX
102 # define KMEM "/dev/kmem"
105 # define KERNEL "/vmunix"
110 static struct proc *getproc __P((void));
112 static struct kinfo_proc *getproc __P((void));
116 int kmemcpy(buf, pos, n)
122 off_t offset = (u_long)pos;
125 kfd = open(KMEM, O_RDONLY);
127 if (lseek(kfd, offset, SEEK_SET) == -1)
132 if (read(kfd, buf, n) == -1)
140 struct nlist names[4] = {
152 static struct proc *getproc()
155 pid_t pid = getpid();
158 n = nlist(KERNEL, names);
161 fprintf(stderr, "nlist(%#x) == %d\n", names, n);
164 if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
166 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
169 siz = nproc * sizeof(struct proc);
170 if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1)
172 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
173 names[0].n_value, &p, sizeof(p));
176 proc = (struct proc *)malloc(siz);
177 if (KMCPY(proc, p, siz) == -1)
179 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
186 for (n = nproc; n; n--, p++)
196 struct tcpcb *find_tcp(fd, ti)
207 if (!(p = getproc()))
209 up = (struct user *)malloc(sizeof(*up));
211 if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1)
213 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea);
217 if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1)
219 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value);
224 o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
225 if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
227 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
228 up->u_ofile, o, sizeof(*o));
231 f = (struct file *)calloc(1, sizeof(*f));
232 if (KMCPY(f, o[fd], sizeof(*f)) == -1)
234 fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n",
235 up->u_ofile[fd], f, sizeof(*f));
239 s = (struct socket *)calloc(1, sizeof(*s));
240 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
242 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n",
243 o[fd], s, sizeof(*s));
247 i = (struct inpcb *)calloc(1, sizeof(*i));
248 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
250 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n",
251 s->so_pcb, i, sizeof(*i));
255 t = (struct tcpcb *)calloc(1, sizeof(*t));
256 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
258 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n",
259 i->inp_ppcb, t, sizeof(*t));
262 return (struct tcpcb *)i->inp_ppcb;
265 static struct kinfo_proc *getproc()
267 static struct kinfo_proc kp;
268 pid_t pid = getpid();
274 mib[2] = KERN_PROC_PID;
278 if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1)
287 struct tcpcb *find_tcp(tfd, ti)
295 struct kinfo_proc *p;
298 if (!(p = getproc()))
301 fd = (struct filedesc *)malloc(sizeof(*fd));
304 #if defined( __FreeBSD_version) && __FreeBSD_version >= 500013
305 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1)
307 fprintf(stderr, "read(%#lx,%#lx) failed\n",
308 (u_long)p, (u_long)p->ki_fd);
313 if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1)
315 fprintf(stderr, "read(%#lx,%#lx) failed\n",
316 (u_long)p, (u_long)p->kp_proc.p_fd);
328 o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
329 if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
331 fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",
332 (u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o));
335 f = (struct file *)calloc(1, sizeof(*f));
336 if (KMCPY(f, o[tfd], sizeof(*f)) == -1)
338 fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n",
339 (u_long)o[tfd], (u_long)f, (u_long)sizeof(*f));
343 s = (struct socket *)calloc(1, sizeof(*s));
344 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
346 fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n",
347 (u_long)f->f_data, (u_long)s, (u_long)sizeof(*s));
351 i = (struct inpcb *)calloc(1, sizeof(*i));
352 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
354 fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n",
355 (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i));
359 t = (struct tcpcb *)calloc(1, sizeof(*t));
360 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
362 fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n",
363 (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t));
366 return (struct tcpcb *)i->inp_ppcb;
381 #endif /* BSD < 199301 */
383 int do_socket(dev, mtu, ti, gwip)
389 struct sockaddr_in rsin, lsin;
390 struct tcpcb *t, tcb;
394 printf("Dest. Port: %d\n", ti->ti_dport);
396 fd = socket(AF_INET, SOCK_STREAM, 0);
403 if (fcntl(fd, F_SETFL, FNDELAY) == -1)
409 bzero((char *)&lsin, sizeof(lsin));
410 lsin.sin_family = AF_INET;
411 bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
412 sizeof(struct in_addr));
413 if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
419 (void) getsockname(fd, (struct sockaddr *)&lsin, &len);
420 ti->ti_sport = lsin.sin_port;
421 printf("sport %d\n", ntohs(lsin.sin_port));
423 nfd = initdevice(dev, 1);
427 if (!(t = find_tcp(fd, ti)))
430 bzero((char *)&rsin, sizeof(rsin));
431 rsin.sin_family = AF_INET;
432 bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
433 sizeof(struct in_addr));
434 rsin.sin_port = ti->ti_dport;
435 if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
436 errno != EINPROGRESS)
441 KMCPY(&tcb, t, sizeof(tcb));
442 ti->ti_win = tcb.rcv_adv;
443 ti->ti_seq = tcb.snd_nxt - 1;
444 ti->ti_ack = tcb.rcv_nxt;
446 if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1)
448 (void)write(fd, "Hello World\n", 12);