1 /* $FreeBSD: src/contrib/ipfilter/ipsend/sock.c,v 1.3.2.4 2003/03/01 03:55:53 darrenr Exp $ */
2 /* $DragonFly: src/contrib/ipfilter/ipsend/sock.c,v 1.3 2005/06/22 02:08:19 dillon Exp $ */
4 * sock.c (C) 1995-1998 Darren Reed
6 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(__sgi) && (IRIX > 602)
9 # include <sys/ptimers.h>
17 #include <sys/types.h>
19 #include <sys/param.h>
24 #if (__FreeBSD_version >= 300000)
25 # include <sys/dirent.h>
33 # include <sys/smp_lock.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
43 #if !defined(ultrix) && !defined(hpux)
47 #include <sys/systm.h>
48 #include <sys/session.h>
51 #include <sys/sysctl.h>
52 #include <sys/filedesc.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/tcp.h>
61 #include <net/route.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/tcp_timer.h>
65 #include <netinet/tcp_var.h>
69 static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed";
70 static const char rcsid[] = "@(#)$Id: sock.c,v 2.1.4.6 2002/12/06 11:40:36 darrenr Exp $";
79 # define KMEM _PATH_KMEM
84 # define KERNEL _PATH_UNIX
88 # define KMEM "/dev/kmem"
91 # define KERNEL "/vmunix"
96 static struct proc *getproc __P((void));
98 static struct kinfo_proc *getproc __P((void));
102 int kmemcpy(buf, pos, n)
108 off_t offset = (u_long)pos;
111 kfd = open(KMEM, O_RDONLY);
113 if (lseek(kfd, offset, SEEK_SET) == -1)
118 if (read(kfd, buf, n) == -1)
126 struct nlist names[4] = {
138 static struct proc *getproc()
141 pid_t pid = getpid();
144 n = nlist(KERNEL, names);
147 fprintf(stderr, "nlist(%#x) == %d\n", names, n);
150 if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
152 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
155 siz = nproc * sizeof(struct proc);
156 if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1)
158 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
159 names[0].n_value, &p, sizeof(p));
162 proc = (struct proc *)malloc(siz);
163 if (KMCPY(proc, p, siz) == -1)
165 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
172 for (n = nproc; n; n--, p++)
182 struct tcpcb *find_tcp(fd, ti)
194 if (!(p = getproc()))
196 up = (struct user *)malloc(sizeof(*up));
198 if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1)
200 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea);
204 if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1)
206 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value);
211 o = (struct fdnode *)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
212 if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
214 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
215 up->u_ofile, o, sizeof(*o));
218 f = (struct file *)calloc(1, sizeof(*f));
219 if (KMCPY(f, o[fd].fp, sizeof(*f)) == -1)
221 fprintf(stderr, "read(%#x,%#x,%d) - o[fd].fp - failed\n",
222 up->u_ofile[fd], f, sizeof(*f));
226 s = (struct socket *)calloc(1, sizeof(*s));
227 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
229 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n",
230 o[fd], s, sizeof(*s));
234 i = (struct inpcb *)calloc(1, sizeof(*i));
235 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
237 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n",
238 s->so_pcb, i, sizeof(*i));
242 t = (struct tcpcb *)calloc(1, sizeof(*t));
243 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
245 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n",
246 i->inp_ppcb, t, sizeof(*t));
249 return (struct tcpcb *)i->inp_ppcb;
252 static struct kinfo_proc *getproc()
254 static struct kinfo_proc kp;
255 pid_t pid = getpid();
261 mib[2] = KERN_PROC_PID;
265 if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1)
273 struct tcpcb *find_tcp(tfd, ti)
281 struct kinfo_proc *p;
285 if (!(p = getproc()))
288 fd = (struct filedesc *)malloc(sizeof(*fd));
289 #if defined( __FreeBSD_version) && __FreeBSD_version >= 500013
290 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1)
292 fprintf(stderr, "read(%#lx,%#lx) failed\n",
293 (u_long)p, (u_long)p->ki_fd);
297 if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1)
299 fprintf(stderr, "read(%#lx,%#lx) failed\n",
300 (u_long)p, (u_long)p->kp_proc.p_fd);
305 o = (struct fdnode *)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
306 if (KMCPY(o, fd->fd_files, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
308 fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",
309 (u_long)fd->fd_files, (u_long)o, (u_long)sizeof(*o));
312 f = (struct file *)calloc(1, sizeof(*f));
313 if (KMCPY(f, o[tfd].fp, sizeof(*f)) == -1)
315 fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd].fp - failed\n",
316 (u_long)o[tfd].fp, (u_long)f, (u_long)sizeof(*f));
320 s = (struct socket *)calloc(1, sizeof(*s));
321 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
323 fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n",
324 (u_long)f->f_data, (u_long)s, (u_long)sizeof(*s));
328 i = (struct inpcb *)calloc(1, sizeof(*i));
329 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
331 fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n",
332 (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i));
336 t = (struct tcpcb *)calloc(1, sizeof(*t));
337 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
339 fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n",
340 (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t));
343 return (struct tcpcb *)i->inp_ppcb;
345 #endif /* BSD < 199301 */
347 int do_socket(dev, mtu, ti, gwip)
353 struct sockaddr_in rsin, lsin;
354 struct tcpcb *t, tcb;
357 printf("Dest. Port: %d\n", ti->ti_dport);
359 fd = socket(AF_INET, SOCK_STREAM, 0);
366 if (fcntl(fd, F_SETFL, FNDELAY) == -1)
372 bzero((char *)&lsin, sizeof(lsin));
373 lsin.sin_family = AF_INET;
374 bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
375 sizeof(struct in_addr));
376 if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
382 (void) getsockname(fd, (struct sockaddr *)&lsin, &len);
383 ti->ti_sport = lsin.sin_port;
384 printf("sport %d\n", ntohs(lsin.sin_port));
385 nfd = initdevice(dev, ntohs(lsin.sin_port), 1);
387 if (!(t = find_tcp(fd, ti)))
390 bzero((char *)&rsin, sizeof(rsin));
391 rsin.sin_family = AF_INET;
392 bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
393 sizeof(struct in_addr));
394 rsin.sin_port = ti->ti_dport;
395 if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
396 errno != EINPROGRESS)
401 KMCPY(&tcb, t, sizeof(tcb));
402 ti->ti_win = tcb.rcv_adv;
403 ti->ti_seq = tcb.snd_nxt - 1;
404 ti->ti_ack = tcb.rcv_nxt;
406 if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1)
408 (void)write(fd, "Hello World\n", 12);