-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharp.c
More file actions
175 lines (133 loc) · 3.69 KB
/
arp.c
File metadata and controls
175 lines (133 loc) · 3.69 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "arp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hw_addrs.h"
struct ip_to_mac self_list[MAX_IF];
int total_self_entries=0;
struct sockaddr_in eth0_ip;
int eth0_index;
unsigned char eth0_mac[IF_HADDR]={};
void arp_init()
{
struct hwa_info *hwa, *hwahead;
struct sockaddr *sa;
struct sockaddr_in *si;
char *ptr;
int i, prflag;
printf("\n");
for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) {
printf("%s :%s", hwa->if_name, ((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n");
if(!strcmp(hwa->if_name,"eth0")||(hwa->ip_alias == IP_ALIAS))
{
si= (struct sockaddr_in* )hwa->ip_addr;
self_list[total_self_entries].ip = si->sin_addr.s_addr;
memcpy(self_list[total_self_entries].mac,hwa->if_haddr,IF_HADDR);
if(!(strcmp(hwa->if_name,"eth0")))
{
eth0_index = hwa->if_index;
eth0_ip= *(struct sockaddr_in*) hwa->ip_addr;
memcpy(eth0_mac,hwa->if_haddr,IF_HADDR);
}
total_self_entries++;
}
if ( (sa = hwa->ip_addr) != NULL)
printf(" IP addr = %s\n", (char *)Sock_ntop_host(sa, sizeof(*sa)));
prflag = 0;
i = 0;
do {
if (hwa->if_haddr[i] != '\0') {
prflag = 1;
break;
}
} while (++i < IF_HADDR);
if (prflag) {
printf(" HW addr = ");
ptr = hwa->if_haddr;
i = IF_HADDR;
do {
printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
} while (--i > 0);
}
printf("\n interface index = %d\n\n", hwa->if_index);
}
// memset(ð0,0,sizeof(struct sockaddr_in));
printf("\n[INFO]eth0 ip recognized %s \n",(char*)inet_ntoa(eth0_ip.sin_addr));
free_hwa_info(hwahead);
}
int main()
{
int i=0,j=0;
int maxfdp;
fd_set rset;
int sockfd; /*socketdescriptor*/
int connfd;
int domainfd;
struct sockaddr_un servaddr,cliaddr;
int clilen = sizeof(cliaddr);
arp_init();
/*printing self Table*/
printf("\nTotal %d entries in self table\n",total_self_entries);
for(i=0;i<total_self_entries;i++)
{
printf("ip:%s mac: ",(char *)inet_ntoa(*(struct in_addr*)(&self_list[i].ip)));
for(j=0;j<IF_HADDR;j++)
{
printf("%.2x:",self_list[i].mac[j]);
}
printf("\b \n");
}
unlink(UNIX_D_PATH);
bzero(&servaddr,sizeof(servaddr));
servaddr.sun_family= AF_LOCAL;
strcpy(servaddr.sun_path,UNIX_D_PATH);
domainfd= socket(AF_LOCAL,SOCK_STREAM,0);
if (domainfd == -1) {
perror("Unable to create socket:");
exit(EXIT_FAILURE);
}
sockfd = socket(PF_PACKET, SOCK_RAW, htons(2159));
if (sockfd == -1) {
perror("Unable to create socket:");
exit(EXIT_FAILURE);
}
if(bind(domainfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
perror("bind error on domainfd:");
exit(EXIT_FAILURE);
}
if(listen(domainfd,LISTENQ)<0)
{
perror("listen error on domainfd:");
exit(EXIT_FAILURE);
}
FD_ZERO(&rset);
while(1)
{
FD_ZERO(&rset);
FD_SET(sockfd,&rset);
FD_SET(domainfd,&rset);
if(sockfd>domainfd)
maxfdp=sockfd+1;
else
maxfdp=domainfd+1;
select(maxfdp,&rset,NULL,NULL,NULL);
if(FD_ISSET(sockfd,&rset))
{
recv_process_pf_packet(sockfd);
}
if(FD_ISSET(domainfd,&rset))
{
connfd = accept(domainfd,(struct sockaddr *)&cliaddr,&clilen);
if(errno==EINTR)
continue;
if(connfd<0)
{
perror("accept error:");
continue;
}
process_app_con(sockfd,connfd);
}
}
return 0;
}