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