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