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