Created
April 8, 2018 21:24
-
-
Save tom-code/14d40c5b88e45c1958a88ee66991a950 to your computer and use it in GitHub Desktop.
macOS raw net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/socket.h> | |
| #include <net/bpf.h> | |
| #include <net/if.h> | |
| #include <net/ethernet.h> | |
| #include <errno.h> | |
| #include <fcntl.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #include <stdio.h> | |
| #include <sys/ioctl.h> | |
| int open_bpf() { | |
| char buf[100]; | |
| int bpf = -1; | |
| for (int i=0; i<99; i++) { | |
| sprintf(buf, "/dev/bpf%d", i); | |
| bpf = open(buf, O_RDWR); | |
| if (bpf != -1) { | |
| return bpf; | |
| } | |
| } | |
| return -1; | |
| } | |
| int main(int argc, const char * argv[]) { | |
| int s; | |
| unsigned char packet[100]; | |
| int d; | |
| d = open_bpf(); | |
| if (d == -1) { | |
| printf("bpf open failed\n"); | |
| return 1; | |
| } | |
| printf("bpf opened %d\n", d); | |
| struct ifreq iface; | |
| strcpy(iface.ifr_name, "en0"); | |
| if (ioctl(d, BIOCSETIF, &iface) > 0) { | |
| printf("can't bind\n"); | |
| return 1; | |
| } | |
| uint flag = 1; | |
| if (ioctl(d, BIOCGHDRCMPLT, &flag) == -1) { | |
| printf("e1 %s\n", strerror(errno)); | |
| } | |
| struct ether_header he; | |
| he.ether_type = 0; | |
| he.ether_dhost[0] = 10; | |
| he.ether_dhost[1] = 10; | |
| he.ether_dhost[2] = 10; | |
| he.ether_dhost[3] = 10; | |
| he.ether_shost[0] = 10; | |
| he.ether_shost[1] = 10; | |
| int r = write(d, &he, 100); | |
| printf("write res %d", r); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment