]>
git.saurik.com Git - apple/xnu.git/blob - tests/bpflib.c
2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
34 #include <sys/socket.h>
38 #define PRIVATE_EXTERN __private_extern__
47 bpf_set_timeout(int fd
, struct timeval
* tv_p
)
49 return ioctl(fd
, BIOCSRTIMEOUT
, tv_p
);
53 bpf_get_blen(int fd
, int * blen
)
55 return ioctl(fd
, BIOCGBLEN
, blen
);
59 bpf_set_header_complete(int fd
, u_int header_complete
)
61 return ioctl(fd
, BIOCSHDRCMPLT
, &header_complete
);
65 bpf_set_see_sent(int fd
, u_int see_sent
)
67 return ioctl(fd
, BIOCSSEESENT
, &see_sent
);
71 bpf_dispose(int bpf_fd
)
86 for (i
= 0; true; i
++) {
87 snprintf(bpfdev
, sizeof(bpfdev
), "/dev/bpf%d", i
);
88 fd
= open(bpfdev
, O_RDWR
, 0);
92 (void) ioctl(fd
, BIOCSETTC
, &tc
);
93 #endif /* SO_TC_CTL */
104 bpf_setif(int fd
, const char * en_name
)
108 strlcpy(ifr
.ifr_name
, en_name
, sizeof(ifr
.ifr_name
));
109 return ioctl(fd
, BIOCSETIF
, &ifr
);
113 bpf_set_immediate(int fd
, u_int value
)
115 return ioctl(fd
, BIOCIMMEDIATE
, &value
);
119 bpf_filter_receive_none(int fd
)
121 struct bpf_insn insns
[] = {
122 BPF_STMT(BPF_RET
+ BPF_K
, 0),
124 struct bpf_program prog
;
126 prog
.bf_len
= sizeof(insns
) / sizeof(struct bpf_insn
);
127 prog
.bf_insns
= insns
;
128 return ioctl(fd
, BIOCSETF
, &prog
);
132 bpf_arp_filter(int fd
, int type_offset
, int type
, u_int pkt_size
)
134 struct bpf_insn insns
[] = {
135 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, type_offset
),
136 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, type
, 0, 1),
137 BPF_STMT(BPF_RET
+ BPF_K
, pkt_size
),
138 BPF_STMT(BPF_RET
+ BPF_K
, 0),
140 struct bpf_program prog
;
142 prog
.bf_len
= sizeof(insns
) / sizeof(struct bpf_insn
);
143 prog
.bf_insns
= insns
;
144 return ioctl(fd
, BIOCSETF
, &prog
);
148 #include <net/if_arp.h>
149 #include <net/ethernet.h>
150 #include <netinet/if_ether.h>
154 bpf_read_continuously(int fd
, u_int blen
)
157 char * rxbuf
= malloc(blen
);
159 printf("rx buf len is %d\n", blen
);
161 n
= read(fd
, rxbuf
, blen
);
163 perror("bpf_read_continuously");
169 print_data(rxbuf
, n
);
174 main(int argc
, char * argv
[])
177 char * en_name
= "en0";
181 perror("no bpf devices");
188 (void)bpf_set_immediate(fd
, 1);
189 if (bpf_arp_filter(fd
, 12, ETHERTYPE_ARP
,
190 sizeof(struct ether_arp
) + sizeof(struct ether_header
))
192 perror("bpf_arp_filter");
194 if (bpf_setif(fd
, en_name
) < 0) {
195 perror("bpf_attach");
199 if (bpf_get_blen(fd
, &bpf_blen
) < 0) {
200 perror("bpf_get_blen");
203 bpf_read_continuously(fd
, bpf_blen
);