Browse code

기본 파일 추가

Ikseon Kang authored on05/05/2019 08:21:13
Showing4 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
1
+all:
2
+	gcc -o main src/main.c src/lib.c -I./ -fpack-struct=1
0 3
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
+}
0 60
new file mode 100644
... ...
@@ -0,0 +1,31 @@
1
+#include <stdio.h>
2
+#include <string.h>
3
+#include <unistd.h>
4
+#include <sys/types.h>
5
+#include <sys/socket.h>
6
+#include <sys/ioctl.h>
7
+#include <linux/if.h>
8
+#include <linux/if_packet.h>
9
+#include <linux/if_ether.h>
10
+#include <netinet/in.h>
11
+#include <arpa/inet.h>
12
+
13
+#ifndef __linux__
14
+  #pragma pack(push, 1)
15
+#endif
16
+struct ether_header
17
+{
18
+  uint8_t  ether_dhost[6];    /* destination eth addr */
19
+  uint8_t  ether_shost[6];    /* source ether addr  */
20
+  uint16_t ether_type;    /* packet type ID field */
21
+}
22
+#ifndef __linux__
23
+  ;
24
+  #pragma pack(pop)
25
+#else
26
+__attribute__ ((__packed__));
27
+#endif
28
+
29
+int send_wol(const char *interface, unsigned char *buf, int len);
30
+void make_wol_packet(char *buf, const char *dstmac);
31
+void make_ether_packet(char *buf, const char *dstmac);
0 32
new file mode 100644
... ...
@@ -0,0 +1,30 @@
1
+/*!
2
+ *  @file    main.c
3
+ *  @author  Kang IkSeon (ikseon1026@gmail.com)
4
+ *
5
+ *  @internal
6
+ *       Created:  2018년 01월 30일
7
+ *     Copyright:  Copyright (c) 2018, Kang IkSeon
8
+ */
9
+
10
+#include <stdio.h>
11
+#include <stdint.h>
12
+
13
+#include "src/lib.h"
14
+
15
+#define PACKET_LEN 116
16
+
17
+int main() {
18
+  int n, len;
19
+  unsigned char packet[PACKET_LEN];
20
+  char dmac[] = {0x08, 0x62, 0x66, 0x7c, 0xed, 0x6c};
21
+
22
+  printf ("ether size : %zu\n", sizeof(struct ether_header));
23
+
24
+  make_ether_packet(packet, dmac);
25
+  make_wol_packet(packet + sizeof(struct ether_header), dmac);
26
+
27
+  send_wol("enp0s25", packet, PACKET_LEN);
28
+
29
+  return 0;
30
+}