· 7 years ago · Feb 06, 2018, 09:10 PM
1/**
2 * CClient Implementation
3 * Listens to UDP broadcasts on port 7447
4 * And sends mac_address using TCP to port 7777
5 * For debugging purposes use the command
6 * gcc -Wall client.c
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <sys/ioctl.h>
15#include <net/if.h>
16#include <unistd.h>
17#include <errno.h>
18#include <arpa/inet.h>
19#include <netinet/in.h>
20#include <netdb.h>
21
22
23#define BUFLEN 512
24#define SERVER_ADDR "192.168.1.122"
25#define SERVER_PORT 7777
26#define PORT 7447
27
28unsigned char mac_address[6] = {0};
29
30void die(char *s) {
31 perror(s);
32 exit(1);
33}
34
35int display_error(char *s) {
36 perror(s);
37 return 1;
38}
39
40char *get_client_mac_address() {
41 struct ifreq ifr;
42 struct ifconf ifc;
43 char buf[BUFLEN];
44 int success = 0, sock;
45
46 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
47 die("[-] Socket creation failed");
48
49 }
50 ifc.ifc_len = sizeof(buf);
51 ifc.ifc_buf = buf;
52 if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
53 die("[-] Could not use IOCTL");
54
55 struct ifreq* it = ifc.ifc_req;
56 const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
57
58 for (; it != end; ++it) {
59 strcpy(ifr.ifr_name, it->ifr_name);
60 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0)
61 if (! (ifr.ifr_flags & IFF_LOOPBACK)) // don't count loopback
62 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
63 success = 1;
64 break;
65 }
66 else
67 die("[-] Error while looping through interfaces");
68 }
69
70 if (success)
71 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
72
73 close(sock);
74 printf("> Successfully received Local MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n",
75 (unsigned char) mac_address[0],
76 (unsigned char) mac_address[1],
77 (unsigned char) mac_address[2],
78 (unsigned char) mac_address[3],
79 (unsigned char) mac_address[4],
80 (unsigned char) mac_address[5]);
81 return (char *) &mac_address;
82}
83
84int send_mac_address_to_server(char *mac_address) {
85 struct sockaddr_in sock_addr;
86 int sock_size = sizeof(sock_addr);
87 int sock_desc, recv_len;
88 char buf[BUFLEN];
89
90 if ((sock_desc = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) {
91 display_error("Socket creation failed");
92 return 1;
93 }
94
95 memset((char *) &sock_addr, 0, sizeof(sock_addr));
96 sock_addr.sin_family = AF_INET;
97 sock_addr.sin_port = htons(SERVER_PORT);
98 sock_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
99
100 if (connect(sock_desc, (struct sockaddr*) &sock_addr, sock_size) == -1)
101 return display_error("TCP socket bind failed");
102
103 struct timeval tv;
104 tv.tv_sec = 20; /* 20 Secs Timeout */
105 tv.tv_usec = 0;
106
107 if(setsockopt(sock_desc, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(tv)) < 0)
108 return display_error("[-] Timed out while connecting to server");
109
110 if(send(sock_desc, mac_address, strlen(mac_address), 0) == -1)
111 return display_error("[-] Could not send mac_address to server");
112}
113
114int main() {
115
116 char *mac_address = get_client_mac_address();
117
118 struct sockaddr_in sock_addr;
119 int sock_size = sizeof(sock_addr);
120 int sock_desc, recv_len;
121 char buf[BUFLEN];
122
123 if ((sock_desc = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
124 die("Socket creation failed");
125
126 memset((char *) &sock_addr, 0, sizeof(sock_addr));
127 sock_addr.sin_family = AF_INET;
128 sock_addr.sin_port = htons(PORT);
129 sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
130
131 if (bind(sock_desc, (struct sockaddr*) &sock_addr, sock_size) == -1)
132 die("Error in bind");
133
134 while (1) {
135 printf("[*] Waiting for the data ...");
136 fflush(stdout);
137 /* Recieving data, blocking call */
138 if ((recv_len = recvfrom(sock_desc,
139 buf,
140 BUFLEN,
141 0,
142 (struct sockaddr *) &sock_addr,
143 &sock_size)) == -1 ) {
144 display_error("Error in recieving ...");
145 continue;
146 }
147 printf("Data received from server is :: %s \n", buf);
148 send_mac_address_to_server(mac_address);
149 }
150
151 return 0;
152}