Browse code

기본 파일 추가

Ikseon Kang authored on05/05/2019 08:21:13
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,59 @@
1
+#include "src/lib.h"
2
+
3
+int send_wol(const char *interface, unsigned char *buf, int len) {
4
+	struct sockaddr_ll sll;
5
+	struct ifreq ifr;
6
+	int sock, arp_source_ip, arp_target_ip;
7
+
8
+	if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
9
+		perror("socket");
10
+		return 1;
11
+	}
12
+
13
+	memset(&ifr, 0x00, sizeof(ifr));
14
+
15
+	strcpy(ifr.ifr_name, interface);
16
+	if(ioctl(sock, SIOCGIFINDEX, &ifr) < 0 ) {
17
+		perror("ioctl ");
18
+		return 1;
19
+	}
20
+
21
+	memset(&sll, 0x00, sizeof(sll));
22
+	sll.sll_family = PF_PACKET;
23
+	sll.sll_ifindex = ifr.ifr_ifindex;
24
+	sll.sll_protocol = htons(ETH_P_ALL);
25
+
26
+	if(sendto(sock, buf, len, 0,
27
+    (struct sockaddr *)&sll, sizeof(sll)) < 0) {
28
+
29
+    perror("sendto ");
30
+	}
31
+
32
+	return 0;
33
+}
34
+
35
+void make_ether_packet(char *buf, const char *dstmac) {
36
+  struct ether_header *ether_header = (struct ether_header *)buf;
37
+  const char broadmac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
38
+  const char srcmac[6] = {0x00, 0x90, 0x27, 0x85, 0xcf, 0x01};
39
+
40
+  memcpy(ether_header->ether_dhost, broadmac, 6);
41
+  memcpy(ether_header->ether_shost, srcmac, 6);
42
+
43
+  ether_header->ether_type = htons(0x0842);
44
+}
45
+
46
+void make_wol_packet(char *buf, const char *dstmac) {
47
+  int offset = 0;
48
+  const char broadmac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
49
+
50
+  memcpy(buf, broadmac, sizeof(broadmac));
51
+
52
+  offset += sizeof(broadmac);
53
+
54
+  for (int i=0; i<16; i++) {
55
+    memcpy(buf + offset, dstmac, 6);
56
+
57
+    offset += 6;
58
+  }
59
+}