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