sock.c (344833) | sock.c (363769) |
---|---|
1/* $FreeBSD: stable/11/contrib/ipfilter/ipsend/sock.c 344833 2019-03-06 02:37:25Z cy $ */ | 1/* $FreeBSD: stable/11/contrib/ipfilter/ipsend/sock.c 363769 2020-08-02 04:25:36Z cy $ */ |
2/* 3 * sock.c (C) 1995-1998 Darren Reed 4 * 5 * See the IPFILTER.LICENCE file for details on licencing. 6 * 7 */ 8#if !defined(lint) 9static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed"; --- 6 unchanged lines hidden (view full) --- 16#if defined(__NetBSD__) && defined(__vax__) 17/* 18 * XXX need to declare boolean_t for _KERNEL <sys/files.h> 19 * which ends up including <sys/device.h> for vax. See PR#32907 20 * for further details. 21 */ 22typedef int boolean_t; 23#endif | 2/* 3 * sock.c (C) 1995-1998 Darren Reed 4 * 5 * See the IPFILTER.LICENCE file for details on licencing. 6 * 7 */ 8#if !defined(lint) 9static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed"; --- 6 unchanged lines hidden (view full) --- 16#if defined(__NetBSD__) && defined(__vax__) 17/* 18 * XXX need to declare boolean_t for _KERNEL <sys/files.h> 19 * which ends up including <sys/device.h> for vax. See PR#32907 20 * for further details. 21 */ 22typedef int boolean_t; 23#endif |
24#ifndef ultrix | |
25#include <fcntl.h> | 24#include <fcntl.h> |
26#endif 27#if (__FreeBSD_version >= 300000) | |
28# include <sys/dirent.h> | 25# include <sys/dirent.h> |
29#else 30# include <sys/dir.h> 31#endif | |
32# ifdef __NetBSD__ 33# include <machine/lock.h> 34# endif 35# ifdef __FreeBSD__ 36# define _WANT_FILE 37# else 38# define _KERNEL 39# define KERNEL 40# endif | 26# ifdef __NetBSD__ 27# include <machine/lock.h> 28# endif 29# ifdef __FreeBSD__ 30# define _WANT_FILE 31# else 32# define _KERNEL 33# define KERNEL 34# endif |
41# ifdef ultrix 42# undef LOCORE 43# include <sys/smp_lock.h> 44# endif | |
45# include <sys/file.h> 46# ifdef __FreeBSD__ 47# undef _WANT_FILE 48# else 49# undef _KERNEL 50# undef KERNEL 51# endif 52#include <nlist.h> 53#include <sys/user.h> 54#include <sys/socket.h> 55#include <sys/socketvar.h> 56#include <sys/proc.h> | 35# include <sys/file.h> 36# ifdef __FreeBSD__ 37# undef _WANT_FILE 38# else 39# undef _KERNEL 40# undef KERNEL 41# endif 42#include <nlist.h> 43#include <sys/user.h> 44#include <sys/socket.h> 45#include <sys/socketvar.h> 46#include <sys/proc.h> |
57#if !defined(ultrix) && !defined(hpux) && !defined(__osf__) | |
58# include <kvm.h> | 47# include <kvm.h> |
59#endif | |
60#ifdef sun 61#include <sys/systm.h> 62#include <sys/session.h> 63#endif | 48#ifdef sun 49#include <sys/systm.h> 50#include <sys/session.h> 51#endif |
64#if BSD >= 199103 | |
65#include <sys/sysctl.h> 66#include <sys/filedesc.h> 67#include <paths.h> | 52#include <sys/sysctl.h> 53#include <sys/filedesc.h> 54#include <paths.h> |
68#endif | |
69#include <math.h> 70#include <netinet/in.h> 71#include <netinet/in_systm.h> 72#include <netinet/ip.h> 73#include <netinet/tcp.h> 74#include <net/if.h> 75# include <net/route.h> 76#include <netinet/ip_var.h> --- 59 unchanged lines hidden (view full) --- 136 return -1; 137 } 138 return n; 139} 140 141struct nlist names[4] = { 142 { "_proc" }, 143 { "_nproc" }, | 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> --- 59 unchanged lines hidden (view full) --- 122 return -1; 123 } 124 return n; 125} 126 127struct nlist names[4] = { 128 { "_proc" }, 129 { "_nproc" }, |
144#ifdef ultrix 145 { "_u" }, 146#else | |
147 { NULL }, | 130 { NULL }, |
148#endif | |
149 { NULL } 150 }; 151 | 131 { NULL } 132 }; 133 |
152#if BSD < 199103 153static struct proc *getproc() 154{ 155 struct proc *p; 156 pid_t pid = getpid(); 157 int siz, n; 158 159 n = nlist(KERNEL, names); 160 if (n != 0) 161 { 162 fprintf(stderr, "nlist(%#x) == %d\n", names, n); 163 return NULL; 164 } 165 if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1) 166 { 167 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value); 168 return NULL; 169 } 170 siz = nproc * sizeof(struct proc); 171 if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1) 172 { 173 fprintf(stderr, "read(%#x,%#x,%d) proc\n", 174 names[0].n_value, &p, sizeof(p)); 175 return NULL; 176 } 177 proc = (struct proc *)malloc(siz); 178 if (KMCPY(proc, p, siz) == -1) 179 { 180 fprintf(stderr, "read(%#x,%#x,%d) proc\n", 181 p, proc, siz); 182 return NULL; 183 } 184 185 p = proc; 186 187 for (n = nproc; n; n--, p++) 188 if (p->p_pid == pid) 189 break; 190 if (!n) 191 return NULL; 192 193 return p; 194} 195 196 197struct tcpcb *find_tcp(fd, ti) 198 int fd; 199 struct tcpiphdr *ti; 200{ 201 struct tcpcb *t; 202 struct inpcb *i; 203 struct socket *s; 204 struct user *up; 205 struct proc *p; 206 struct file *f, **o; 207 208 if (!(p = getproc())) 209 return NULL; 210 up = (struct user *)malloc(sizeof(*up)); 211#ifndef ultrix 212 if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1) 213 { 214 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea); 215 return NULL; 216 } 217#else 218 if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1) 219 { 220 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value); 221 return NULL; 222 } 223#endif 224 225 o = (struct file **)calloc(up->u_lastfile + 1, sizeof(*o)); 226 if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1) 227 { 228 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n", 229 up->u_ofile, o, sizeof(*o)); 230 return NULL; 231 } 232 f = (struct file *)calloc(1, sizeof(*f)); 233 if (KMCPY(f, o[fd], sizeof(*f)) == -1) 234 { 235 fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n", 236 up->u_ofile[fd], f, sizeof(*f)); 237 return NULL; 238 } 239 240 s = (struct socket *)calloc(1, sizeof(*s)); 241 if (KMCPY(s, f->f_data, sizeof(*s)) == -1) 242 { 243 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n", 244 o[fd], s, sizeof(*s)); 245 return NULL; 246 } 247 248 i = (struct inpcb *)calloc(1, sizeof(*i)); 249 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1) 250 { 251 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n", 252 s->so_pcb, i, sizeof(*i)); 253 return NULL; 254 } 255 256 t = (struct tcpcb *)calloc(1, sizeof(*t)); 257 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1) 258 { 259 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n", 260 i->inp_ppcb, t, sizeof(*t)); 261 return NULL; 262 } 263 return (struct tcpcb *)i->inp_ppcb; 264} 265#else | |
266static struct kinfo_proc *getproc() 267{ 268 static struct kinfo_proc kp; 269 pid_t pid = getpid(); 270 int mib[4]; 271 size_t n; 272 273 mib[0] = CTL_KERN; --- 23 unchanged lines hidden (view full) --- 297 struct file *f, **o; 298 299 if (!(p = getproc())) 300 return NULL; 301 302 fd = (struct filedesc *)malloc(sizeof(*fd)); 303 if (fd == NULL) 304 return NULL; | 134static struct kinfo_proc *getproc() 135{ 136 static struct kinfo_proc kp; 137 pid_t pid = getpid(); 138 int mib[4]; 139 size_t n; 140 141 mib[0] = CTL_KERN; --- 23 unchanged lines hidden (view full) --- 165 struct file *f, **o; 166 167 if (!(p = getproc())) 168 return NULL; 169 170 fd = (struct filedesc *)malloc(sizeof(*fd)); 171 if (fd == NULL) 172 return NULL; |
305#if defined( __FreeBSD_version) && __FreeBSD_version >= 500013 | 173#if defined( __FreeBSD_version) |
306 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1) 307 { 308 fprintf(stderr, "read(%#lx,%#lx) failed\n", 309 (u_long)p, (u_long)p->ki_fd); 310 free(fd); 311 return NULL; 312 } 313#else --- 60 unchanged lines hidden (view full) --- 374 if (s != NULL) 375 free(s); 376 if (i != NULL) 377 free(i); 378 if (t != NULL) 379 free(t); 380 return NULL; 381} | 174 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1) 175 { 176 fprintf(stderr, "read(%#lx,%#lx) failed\n", 177 (u_long)p, (u_long)p->ki_fd); 178 free(fd); 179 return NULL; 180 } 181#else --- 60 unchanged lines hidden (view full) --- 242 if (s != NULL) 243 free(s); 244 if (i != NULL) 245 free(i); 246 if (t != NULL) 247 free(t); 248 return NULL; 249} |
382#endif /* BSD < 199301 */ | |
383 384int do_socket(dev, mtu, ti, gwip) 385 char *dev; 386 int mtu; 387 struct tcpiphdr *ti; 388 struct in_addr gwip; 389{ 390 struct sockaddr_in rsin, lsin; --- 63 unchanged lines hidden --- | 250 251int do_socket(dev, mtu, ti, gwip) 252 char *dev; 253 int mtu; 254 struct tcpiphdr *ti; 255 struct in_addr gwip; 256{ 257 struct sockaddr_in rsin, lsin; --- 63 unchanged lines hidden --- |