AMD64 - Fix format conversions and other warnings.
[dragonfly.git] / contrib / ipfilter / ipsd / sdlpi.c
blobc08fe697798868a222be179fce877371aa2ab175
1 /*
2 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
4 * See the IPFILTER.LICENCE file for details on licencing.
6 */
8 #include <stdio.h>
9 #include <netdb.h>
10 #include <ctype.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <errno.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <sys/timeb.h>
17 #include <sys/socket.h>
18 #include <sys/file.h>
19 #include <sys/ioctl.h>
20 #include <sys/stropts.h>
22 #include <sys/pfmod.h>
23 #include <sys/bufmod.h>
24 #include <sys/dlpi.h>
26 #include <net/if.h>
27 #include <netinet/in.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/ip.h>
30 #include <netinet/if_ether.h>
31 #include <netinet/ip_var.h>
32 #include <netinet/udp.h>
33 #include <netinet/udp_var.h>
34 #include <netinet/tcp.h>
35 #include <netinet/tcpip.h>
37 #include "ip_compat.h"
39 #ifndef lint
40 static char snitid[] = "%W% %G% (C)1995 Darren Reed";
41 #endif
43 #define BUFSPACE 32768
45 static int solfd;
48 * Be careful to only include those defined in the flags option for the
49 * interface are included in the header size.
51 static int timeout;
54 void nullbell()
56 return 0;
60 int ack_recv(ep)
61 char *ep;
63 struct tcpiphdr tip;
64 tcphdr_t *tcp;
65 ip_t *ip;
67 ip = (ip_t *)&tip;
68 tcp = (tcphdr_t *)(ip + 1);
69 bcopy(ep, (char *)ip, sizeof(*ip));
70 bcopy(ep + (ip->ip_hl << 2), (char *)tcp, sizeof(*tcp));
72 if (ip->ip_off & 0x1fff != 0)
73 return 0;
74 if (0 == detect(ip, tcp))
75 return 1;
76 return 0;
80 int readloop(fd, port, dst)
81 int fd, port;
82 struct in_addr dst;
84 static u_char buf[BUFSPACE];
85 register u_char *bp, *cp, *bufend;
86 register struct sb_hdr *hp;
87 register int cc;
88 struct strbuf dbuf;
89 ether_header_t eh;
90 time_t now = time(NULL);
91 int flags = 0, i, done = 0;
93 fd = solfd;
94 dbuf.len = 0;
95 dbuf.buf = buf;
96 dbuf.maxlen = sizeof(buf);
98 * no control data buffer...
100 while (1) {
101 (void) signal(SIGALRM, nullbell);
102 alarm(1);
103 i = getmsg(fd, NULL, &dbuf, &flags);
104 alarm(0);
105 (void) signal(SIGALRM, nullbell);
107 cc = dbuf.len;
108 if ((time(NULL) - now) > timeout)
109 return done;
110 if (i == -1)
111 if (errno == EINTR)
112 continue;
113 else
114 break;
115 bp = buf;
116 bufend = buf + cc;
118 * loop through each snapshot in the chunk
120 while (bp < bufend) {
122 * get past bufmod header
124 hp = (struct sb_hdr *)bp;
125 cp = (u_char *)((char *)bp + sizeof(*hp));
126 bcopy(cp, (char *)&eh, sizeof(eh));
128 * next snapshot
130 bp += hp->sbh_totlen;
131 cc -= hp->sbh_totlen;
133 if (eh.ether_type != ETHERTYPE_IP)
134 continue;
136 cp += sizeof(eh);
137 done += ack_recv(cp);
139 alarm(1);
141 perror("getmsg");
142 exit(-1);
145 int initdevice(device, tout)
146 char *device;
147 int tout;
149 struct strioctl si;
150 struct timeval to;
151 struct ifreq ifr;
152 struct packetfilt pfil;
153 u_long if_flags;
154 u_short *fwp = pfil.Pf_Filter;
155 char devname[16], *s, buf[256];
156 int i, offset, fd, snaplen= 58, chunksize = BUFSPACE;
158 (void) sprintf(devname, "/dev/%s", device);
160 s = devname + 5;
161 while (*s && !isdigit(*s))
162 s++;
163 if (!*s)
165 fprintf(stderr, "bad device name %s\n", devname);
166 exit(-1);
168 i = atoi(s);
169 *s = '\0';
171 * For reading
173 if ((fd = open(devname, O_RDWR)) < 0)
175 fprintf(stderr, "O_RDWR(0) ");
176 perror(devname);
177 exit(-1);
179 if (dlattachreq(fd, i) == -1 || dlokack(fd, buf) == -1)
181 fprintf(stderr, "DLPI error\n");
182 exit(-1);
184 dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
185 dlbindack(fd, buf);
187 * read full headers
189 if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
191 fprintf(stderr, "DLIOCRAW error\n");
192 exit(-1);
195 * Create some filter rules for our TCP watcher. We only want ethernet
196 * pacets which are IP protocol and only the TCP packets from IP.
198 offset = 6;
199 *fwp++ = ENF_PUSHWORD + offset;
200 *fwp++ = ENF_PUSHLIT | ENF_CAND;
201 *fwp++ = htons(ETHERTYPE_IP);
202 *fwp++ = ENF_PUSHWORD + sizeof(struct ether_header)/sizeof(short)+4;
203 *fwp++ = ENF_PUSHLIT | ENF_AND;
204 *fwp++ = htons(0x00ff);
205 *fwp++ = ENF_PUSHLIT | ENF_COR;
206 *fwp++ = htons(IPPROTO_TCP);
207 *fwp++ = ENF_PUSHWORD + sizeof(struct ether_header)/sizeof(short)+4;
208 *fwp++ = ENF_PUSHLIT | ENF_AND;
209 *fwp++ = htons(0x00ff);
210 *fwp++ = ENF_PUSHLIT | ENF_CAND;
211 *fwp++ = htons(IPPROTO_UDP);
212 pfil.Pf_FilterLen = (fwp - &pfil.Pf_Filter[0]);
214 * put filter in place.
217 if (ioctl(fd, I_PUSH, "pfmod") == -1)
219 perror("ioctl: I_PUSH pf");
220 exit(1);
222 if (strioctl(fd, PFIOCSETF, -1, sizeof(pfil), (char *)&pfil) == -1)
224 perror("ioctl: PFIOCSETF");
225 exit(1);
229 * arrange to get messages from the NIT STREAM and use NIT_BUF option
231 if (ioctl(fd, I_PUSH, "bufmod") == -1)
233 perror("ioctl: I_PUSH bufmod");
234 exit(1);
236 i = 128;
237 strioctl(fd, SBIOCSSNAP, -1, sizeof(i), (char *)&i);
239 * set the timeout
241 to.tv_sec = 1;
242 to.tv_usec = 0;
243 if (strioctl(fd, SBIOCSTIME, -1, sizeof(to), (char *)&to) == -1)
245 perror("strioctl(SBIOCSTIME)");
246 exit(-1);
249 * flush read queue
251 if (ioctl(fd, I_FLUSH, FLUSHR) == -1)
253 perror("I_FLUSHR");
254 exit(-1);
256 timeout = tout;
257 solfd = fd;
258 return fd;