-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathca.cpp
More file actions
151 lines (122 loc) · 3.48 KB
/
ca.cpp
File metadata and controls
151 lines (122 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <pcap/pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>
#ifndef __linux__
#include <net/if_dl.h>
#else
#include <netinet/ether.h>
#endif
#include <string>
#include <net/ethernet.h>
#include "common.hpp"
#include "ether.hpp"
void usage(char* prog_name)
{
printf("cell advertisor\n");
printf("%s [interface]\n", prog_name);
}
int
main(int argc, char** argv)
{
debug = true;
if (argc != 2) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
uint8_t dst_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
struct ether_addr src_mac;
memset(&src_mac, 0, sizeof(src_mac));
std::vector<std::string> if_list = get_ifname_list();
char ifname[IFNAMSIZ];
memset(ifname, 0, sizeof(ifname));
strncpy(ifname, argv[1], strlen(argv[1]));
bool is_loopback;
#ifndef __linux__
is_loopback = strncmp(ifname, "lo0", strlen("lo0"));
#else
is_loopback = strncmp(ifname, "lo", strlen("lo"));
#endif
if (is_loopback == 0) {
MESG("ca cant use with lo0.");
exit(EXIT_FAILURE);
}
std::string str_ifname = ifname;
if (!is_exist_if(if_list, str_ifname)) {
MESG("A selected interface is not exist.");
exit(EXIT_FAILURE);
}
#ifndef __linux__
struct ifaddrs *ifs;
struct ifaddrs *ifp;
struct sockaddr_dl* dl;
if (getifaddrs(&ifs) != 0) {
PERROR("getifaddrs");
MESG("unabe to get interface info for %s", ifname);
return false;
}
for (ifp=ifs; ifp; ifp=ifp->ifa_next) {
int ifp_family = ifp->ifa_addr->sa_family;
if (ifp->ifa_addr == NULL) {
continue;
} else if (ifp_family != AF_LINK) {
continue;
}
dl = (struct sockaddr_dl*)ifp->ifa_addr;
if (strncmp(ifname, dl->sdl_data, dl->sdl_nlen) == 0) {
memcpy(&src_mac, LLADDR(dl), ETHER_ADDR_LEN);
break;
}
}
freeifaddrs(ifs);
#else
{
int fd;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, ifname, strlen(ifname));
ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
memcpy(&src_mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
}
#endif
printf("src:%s\n", ether_ntoa(&src_mac));
printf("dst:%s\n", ether_ntoa(&src_mac));
char pcapbuf[PCAP_ERRBUF_SIZE];
memset(pcapbuf, 0, sizeof(pcapbuf));
pcap_t* handler = pcap_create(ifname, pcapbuf);
if (handler == NULL) {
std::cout << "Cannot open device " << ifname << "." << std::endl;
std::cout << pcapbuf << std::endl;
exit(EXIT_FAILURE);
}
if (pcap_activate(handler) != 0) {
pcap_perror(handler, (char*)"ERROR");
exit(EXIT_FAILURE);
}
uint8_t advbuf[64];
memset(advbuf, 0, sizeof(advbuf));
struct ether_header* eth = (struct ether_header*)advbuf;
memcpy(eth->ether_dhost, dst_mac, sizeof(dst_mac));
memcpy(eth->ether_shost, &src_mac, sizeof(src_mac));
eth->ether_type = htons(ETHERTYPE_IP);
eth->ether_type = htons(0x0101);
// 0101-01FF exp number
int ret;
while ( 1 ) {
ret =pcap_inject(handler, advbuf, 64);
//perror("pcap_inject");
if(ret < 0) std::cout << "Failed to inject" << std::endl;
sleep(10);
}
return 0;
}