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