3 * sock.c (C) 1995-1998 Darren Reed
5 * See the IPFILTER.LICENCE file for details on licencing.
8 # include <sys/ptimers.h>
16 #include <sys/types.h>
18 #include <sys/param.h>
23 #if (__FreeBSD_version >= 300000)
24 # include <sys/dirent.h>
32 # include <sys/smp_lock.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
42 #if !defined(ultrix) && !defined(hpux)
46 #include <sys/systm.h>
47 #include <sys/session.h>
50 #include <sys/sysctl.h>
51 #include <sys/filedesc.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/tcp.h>
60 #include <net/route.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/tcp_timer.h>
64 #include <netinet/tcp_var.h>
68 static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed";
69 static const char rcsid[] = "@(#)$Id: sock.c,v 2.1.4.5 2002/02/22 15:32:58 darrenr Exp $";
78 # define KMEM _PATH_KMEM
83 # define KERNEL _PATH_UNIX
87 # define KMEM "/dev/kmem"
90 # define KERNEL "/vmunix"
95 static struct proc *getproc __P((void));
97 static struct kinfo_proc *getproc __P((void));
101 int kmemcpy(buf, pos, n)
107 off_t offset = (u_long)pos;
110 kfd = open(KMEM, O_RDONLY);
112 if (lseek(kfd, offset, SEEK_SET) == -1)
117 if (read(kfd, buf, n) == -1)
125 struct nlist names[4] = {
137 static struct proc *getproc()
140 pid_t pid = getpid();
143 n = nlist(KERNEL, names);
146 fprintf(stderr, "nlist(%#x) == %d\n", names, n);
149 if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
151 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
154 siz = nproc * sizeof(struct proc);
155 if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1)
157 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
158 names[0].n_value, &p, sizeof(p));
161 proc = (struct proc *)malloc(siz);
162 if (KMCPY(proc, p, siz) == -1)
164 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
171 for (n = nproc; n; n--, p++)
181 struct tcpcb *find_tcp(fd, ti)
192 if (!(p = getproc()))
194 up = (struct user *)malloc(sizeof(*up));
196 if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1)
198 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea);
202 if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1)
204 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value);
209 o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
210 if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
212 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
213 up->u_ofile, o, sizeof(*o));
216 f = (struct file *)calloc(1, sizeof(*f));
217 if (KMCPY(f, o[fd], sizeof(*f)) == -1)
219 fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n",
220 up->u_ofile[fd], f, sizeof(*f));
224 s = (struct socket *)calloc(1, sizeof(*s));
225 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
227 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n",
228 o[fd], s, sizeof(*s));
232 i = (struct inpcb *)calloc(1, sizeof(*i));
233 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
235 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n",
236 s->so_pcb, i, sizeof(*i));
240 t = (struct tcpcb *)calloc(1, sizeof(*t));
241 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
243 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n",
244 i->inp_ppcb, t, sizeof(*t));
247 return (struct tcpcb *)i->inp_ppcb;
250 static struct kinfo_proc *getproc()
252 static struct kinfo_proc kp;
253 pid_t pid = getpid();
259 mib[2] = KERN_PROC_PID;
263 if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1)
272 struct tcpcb *find_tcp(tfd, ti)
280 struct kinfo_proc *p;
283 if (!(p = getproc()))
286 fd = (struct filedesc *)malloc(sizeof(*fd));
287 #if defined( __FreeBSD_version) && __FreeBSD_version >= 500013
288 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1)
290 fprintf(stderr, "read(%#lx,%#lx) failed\n",
291 (u_long)p, (u_long)p->ki_fd);
295 if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1)
297 fprintf(stderr, "read(%#lx,%#lx) failed\n",
298 (u_long)p, (u_long)p->kp_proc.p_fd);
303 o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
304 if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
306 fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",
307 (u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o));
310 f = (struct file *)calloc(1, sizeof(*f));
311 if (KMCPY(f, o[tfd], sizeof(*f)) == -1)
313 fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n",
314 (u_long)o[tfd], (u_long)f, (u_long)sizeof(*f));
318 s = (struct socket *)calloc(1, sizeof(*s));
319 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
321 fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n",
322 (u_long)f->f_data, (u_long)s, (u_long)sizeof(*s));
326 i = (struct inpcb *)calloc(1, sizeof(*i));
327 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
329 fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n",
330 (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i));
334 t = (struct tcpcb *)calloc(1, sizeof(*t));
335 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
337 fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n",
338 (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t));
341 return (struct tcpcb *)i->inp_ppcb;
343 #endif /* BSD < 199301 */
345 int do_socket(dev, mtu, ti, gwip)
351 struct sockaddr_in rsin, lsin;
352 struct tcpcb *t, tcb;
355 printf("Dest. Port: %d\n", ti->ti_dport);
357 fd = socket(AF_INET, SOCK_STREAM, 0);
364 if (fcntl(fd, F_SETFL, FNDELAY) == -1)
370 bzero((char *)&lsin, sizeof(lsin));
371 lsin.sin_family = AF_INET;
372 bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
373 sizeof(struct in_addr));
374 if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
380 (void) getsockname(fd, (struct sockaddr *)&lsin, &len);
381 ti->ti_sport = lsin.sin_port;
382 printf("sport %d\n", ntohs(lsin.sin_port));
383 nfd = initdevice(dev, ntohs(lsin.sin_port), 1);
385 if (!(t = find_tcp(fd, ti)))
388 bzero((char *)&rsin, sizeof(rsin));
389 rsin.sin_family = AF_INET;
390 bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
391 sizeof(struct in_addr));
392 rsin.sin_port = ti->ti_dport;
393 if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
394 errno != EINPROGRESS)
399 KMCPY(&tcb, t, sizeof(tcb));
400 ti->ti_win = tcb.rcv_adv;
401 ti->ti_seq = tcb.snd_nxt - 1;
402 ti->ti_ack = tcb.rcv_nxt;
404 if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1)
406 (void)write(fd, "Hello World\n", 12);