View difference between Paste ID: 9Udd0uH7 and pTzVdVdP
SHOW: | | - or go back to the newest paste.
1
#include <sys/socket.h>
2
#include <sys/ioctl.h>
3
#include <sys/time.h>
4
5
#include <asm/types.h>
6
7
#include <math.h>
8
#include <string.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12
#include <signal.h>
13
#include <arpa/inet.h>
14
15
#include <linux/if_packet.h>
16
#include <linux/if_ether.h>
17
#include <linux/if_arp.h>
18
19
#define BUF_SIZE 42
20
#define DEVICE "eth1"
21
#define ETH_P_NULL 0x0
22
#define ETH_MAC_LEN ETH_ALEN
23
#define	ETH_ARP 0x0806
24
25-
int s = 0; /*Socketdescriptor*/
25+
int s = -1; /*Socketdescriptor*/
26
void* buffer = NULL;
27
long total_packets = 0;
28
long answered_packets = 0;
29
30
void sigint(int signum);
31
32
struct __attribute__((packed)) arp_header
33
{
34
	unsigned short arp_hd;
35
	unsigned short arp_pr;
36
	unsigned char arp_hdl;
37
	unsigned char arp_prl;
38
	unsigned short arp_op;
39
	unsigned char arp_sha[6];
40
	unsigned char arp_spa[4];
41
	unsigned char arp_dha[6];
42
	unsigned char arp_dpa[4];
43
};
44
int main(void) {
45
	buffer = (void*)malloc(BUF_SIZE); /*Buffer for Ethernet Frame*/
46
	unsigned char* etherhead = buffer;	/*Pointer to Ethenet Header*/
47
	struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to
48
	                                                  ethernet header*/
49
	unsigned char* arphead = buffer + 14;
50
	struct arp_header *ah;
51
	unsigned char src_mac[6];	 /*our MAC address*/
52
53
	struct ifreq ifr;
54
	struct sockaddr_ll socket_address;
55
	int ifindex = 0;	 /*Ethernet Interface index*/
56
	int i;
57
	int length;	 /*length of received packet*/
58
	int sent;
59
60
	printf("Server started, entering initialiation phase...\n");
61
62
	/*open socket*/
63
	s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
64
	if (s == -1) {
65
		perror("socket():");
66
		exit(1);
67
	}
68
	printf("Successfully opened socket: %i\n", s);
69
70
	/*retrieve ethernet interface index*/
71
	strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
72
	if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
73
		perror("SIOCGIFINDEX");
74
		exit(1);
75
	}
76
	ifindex = ifr.ifr_ifindex;
77
	printf("Successfully got interface index: %i\n", ifindex);
78
79
	/*retrieve corresponding MAC*/
80
	if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
81
		perror("SIOCGIFINDEX");
82
		exit(1);
83
	}
84
	for (i = 0; i < 6; i++) {
85
		src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
86
	}
87
	printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
88
	       src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);
89
90
	/*prepare sockaddr_ll*/
91
	socket_address.sll_family = PF_PACKET;
92
	socket_address.sll_protocol = htons(ETH_P_ARP);
93
	socket_address.sll_ifindex = ifindex;
94
	socket_address.sll_hatype = ARPHRD_ETHER;
95
	socket_address.sll_pkttype = 0; //PACKET_OTHERHOST;
96
	socket_address.sll_halen = 0;
97
	socket_address.sll_addr[6] = 0x00;
98
	socket_address.sll_addr[7] = 0x00;
99
	/*establish signal handler*/
100
	signal(SIGINT, sigint);
101
	printf("Successfully established signal handler for SIGINT\n");
102
	printf("We are in production state, waiting for incoming packets....\n");
103
104
	while (1) {
105
		/*Wait for incoming packet...*/
106
		length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
107
		if (length == -1)
108
			{
109
				perror("recvfrom():");
110
				exit(1);
111
			}
112
		if(ntohs(eh->h_proto) == ETH_P_ARP)
113
			{
114
115
				unsigned char buf_arp_dha[6];
116
				unsigned char buf_arp_dpa[4];
117
118
				ah = (struct arp_header *)arphead;
119
				if(ntohs(ah->arp_op) != ARPOP_REQUEST)
120
					continue;
121
				printf("buffer is---------------- %s \n",(char*)ah);
122
				printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
123
				printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
124
				printf("OPERATION : %x \n", ah->arp_op);
125
				printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
126
				       ah->arp_sha[0],
127
				       ah->arp_sha[1],
128
				       ah->arp_sha[2],
129
				       ah->arp_sha[3],
130
				       ah->arp_sha[4],
131
				       ah->arp_sha[5]
132
				       );
133
				printf("SENDER IP address: %02d:%02d:%02d:%02d\n",
134
				       ah->arp_spa[0],
135
				       ah->arp_spa[1],
136
				       ah->arp_spa[2],
137
				       ah->arp_spa[3]
138
				       );
139
				#if 0
140
				if(ah->arp_spa[0]==10&&ah->arp_spa[1]==00&&ah->arp_spa[2]==00&&ah->arp_spa[3]==01)
141
					{
142
						printf("Sender ip is .............bam bam..........................................\n");
143
						system("sudo arp -s 10.0.0.1  00:1e:73:91:04:0d");
144
					}
145
				#endif
146
				printf("TARGET MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
147
				       ah->arp_dha[0],
148
				       ah->arp_dha[1],
149
				       ah->arp_dha[2],
150
				       ah->arp_dha[3],
151
				       ah->arp_dha[4],
152
				       ah->arp_dha[5]
153
				       );
154
				printf("TARGET IP address: %02d:%02d:%02d:%02d\n",
155
				       ah->arp_dpa[0],
156
				       ah->arp_dpa[1],
157
				       ah->arp_dpa[2],
158
				       ah->arp_dpa[3]
159
				       );
160
161
				printf("+++++++++++++++++++++++++++++++++++++++\n" );
162
				printf("ETHER DST MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
163
				       eh->h_dest[0],
164
				       eh->h_dest[1],
165
				       eh->h_dest[2],
166
				       eh->h_dest[3],
167
				       eh->h_dest[4],
168
				       eh->h_dest[5]
169
				       );
170
				printf("ETHER SRC MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
171
				       eh->h_source[0],
172
				       eh->h_source[1],
173
				       eh->h_source[2],
174
				       eh->h_source[3],
175
				       eh->h_source[4],
176
				       eh->h_source[5]
177
				       );
178
				memcpy( (void*)etherhead, (const void*)(etherhead+ETH_MAC_LEN),
179
				        ETH_MAC_LEN);
180
				memcpy( (void*)(etherhead+ETH_MAC_LEN), (const void*)src_mac,
181
				        ETH_MAC_LEN);
182
				eh->h_proto = htons(ETH_P_ARP);
183
				printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& \n");
184
				printf("ETHER DST MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
185
				       eh->h_dest[0],
186
				       eh->h_dest[1],
187
				       eh->h_dest[2],
188
				       eh->h_dest[3],
189
				       eh->h_dest[4],
190
				       eh->h_dest[5]
191
				       );
192
				printf("ETHER SRC MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
193
				       eh->h_source[0],
194
				       eh->h_source[1],
195
				       eh->h_source[2],
196
				       eh->h_source[3],
197
				       eh->h_source[4],
198
				       eh->h_source[5]
199
				       );
200
				//ah->arp_hd = ntohs(ah->arp_hd);
201
				//ah->arp_pr = ntohs(ah->arp_pr);
202
203
				ah->arp_op = htons(ARPOP_REPLY);
204
205
				buf_arp_dpa[0] = ah->arp_dpa[0];
206
				buf_arp_dpa[1] = ah->arp_dpa[1];
207
				buf_arp_dpa[2] = ah->arp_dpa[2];
208
				buf_arp_dpa[3] = ah->arp_dpa[3];
209
210
				ah->arp_dha[0] = ah->arp_sha[0];
211
				ah->arp_dha[1] = ah->arp_sha[1];
212
				ah->arp_dha[2] = ah->arp_sha[2];
213
				ah->arp_dha[3] = ah->arp_sha[3];
214
				ah->arp_dha[4] = ah->arp_sha[4];
215
				ah->arp_dha[5] = ah->arp_sha[5];
216
217
				ah->arp_dpa[0] = ah->arp_spa[0];
218
				ah->arp_dpa[1] = ah->arp_spa[1];
219
				ah->arp_dpa[2] = ah->arp_spa[2];
220
				ah->arp_dpa[3] = ah->arp_spa[3];
221
222
				ah->arp_spa[0] = buf_arp_dpa[0];
223
				ah->arp_spa[1] = buf_arp_dpa[1];
224
				ah->arp_spa[2] = buf_arp_dpa[2];
225
				ah->arp_spa[3] = buf_arp_dpa[3];
226
				//change the sender mac address
227
				ah->arp_sha[0] = 0x00;
228
				ah->arp_sha[1] = 0x1e;
229
				ah->arp_sha[2] = 0x73;
230
				ah->arp_sha[3] = 0x78;
231
				ah->arp_sha[4] = 0x9a;
232
				ah->arp_sha[5] = 0x0d;
233
234
				socket_address.sll_addr[0] = eh->h_dest[0];
235
				socket_address.sll_addr[1] = eh->h_dest[1];
236
				socket_address.sll_addr[2] = eh->h_dest[2];
237
				socket_address.sll_addr[3] = eh->h_dest[3];
238
				socket_address.sll_addr[4] = eh->h_dest[4];
239
				socket_address.sll_addr[5] = eh->h_dest[5];
240
				printf("=======================================\n" );
241
				printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
242
				       ah->arp_sha[0],
243
				       ah->arp_sha[1],
244
				       ah->arp_sha[2],
245
				       ah->arp_sha[3],
246
				       ah->arp_sha[4],
247
				       ah->arp_sha[5]
248
				       );
249
				printf("SENDER IP address: %02d:%02d:%02d:%02d\n",
250
				       ah->arp_spa[0],
251
				       ah->arp_spa[1],
252
				       ah->arp_spa[2],
253
				       ah->arp_spa[3]
254
				       );
255
				if((ah->arp_spa[0]==10 && ah->arp_spa[1]==0 && ah->arp_spa[2]==0 && ah->arp_spa[3]==1))
256
					printf("------------------------------------------10.0.0.1-----------------------------------------\n");
257
				printf("TARGET MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
258
				       ah->arp_dha[0],
259
				       ah->arp_dha[1],
260
				       ah->arp_dha[2],
261
				       ah->arp_dha[3],
262
				       ah->arp_dha[4],
263
				       ah->arp_dha[5]
264
				       );
265
				printf("TARGET IP address: %02d:%02d:%02d:%02d\n",
266
				       ah->arp_dpa[0],
267
				       ah->arp_dpa[1],
268
				       ah->arp_dpa[2],
269
				       ah->arp_dpa[3]
270
				       );
271
				printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
272
				printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
273
				printf("OPERATION : %x \n", ah->arp_op);
274
275
				sent = sendto(s, buffer, BUF_SIZE, 0, (struct
276
				                                       sockaddr*)&socket_address, sizeof(socket_address));
277
				if (sent == -1)
278
					{
279
						perror("sendto():");
280
						exit(1);
281
					}
282
283
				answered_packets++;
284
285
			}
286
287
		total_packets++;
288
289
	}
290
}
291
void sigint(int signum) {
292
	/*Clean up.......*/
293
294
	struct ifreq ifr;
295
296
	if (s == -1)
297
		return;
298
299
	strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
300
	ioctl(s, SIOCGIFFLAGS, &ifr);
301
	ifr.ifr_flags &= ~IFF_PROMISC;
302
	ioctl(s, SIOCSIFFLAGS, &ifr);
303
	close(s);
304
305
	free(buffer);
306
307
	printf("Server terminating....\n");
308
309
	printf("Totally received: %ld packets\n", total_packets);
310
	printf("Answered %ld packets\n", answered_packets);
311
	exit(0);
312
}