ARP.xs

filter: Statementstmt 21.4% 15 / 70 Branchbran 3.1% 1 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 15.6% 16 / 102 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« send_packet_linux.c ↑ Summary arp_lookup_linux.c »
1 /*
2 Perl ARP Extension
3 Create and send an arp packets, lookup mac addresses
4  
5 Programmed by Bastian Ballmann
6 Last update: 27.04.2020
7  
8 This program is free software; you can redistribute
9 it and/or modify it under the terms of the
10 GNU General Public License version 2 as published
11 by the Free Software Foundation.
12  
13 This program is distributed in the hope that it will
14 be useful, but WITHOUT ANY WARRANTY; without even
15 the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE.
17 See the GNU General Public License for more details.
18 */
19  
20 #include "EXTERN.h"
21 #include "perl.h"
22 #include "XSUB.h"
23  
24 #include "ppport.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <net/ethernet.h>
30 #ifdef BSD
31 #else
32 #include <netinet/ether.h>
33 #endif
34 #include <net/if.h>
35 #include <arpa/inet.h>
36 #include "arp.h"
37  
38 MODULE = Net::ARP PACKAGE = Net::ARP
39  
40 int
41 send_packet(dev, sip, dip, smac, dmac, type)
42 const char *dev;
43 const char *sip;
44 const char *dip;
45 const char *smac;
46 const char *dmac;
47 const char *type;
48  
49 CODE:
50 int uid;
51 2 unsigned int packetsize = sizeof(struct my_arphdr) + sizeof(struct ether_header);
52 2 unsigned char packet[packetsize];
53 2 struct ether_header *ethhdr = (struct ether_header *)packet;
54 2 struct my_arphdr *arp = (struct my_arphdr *)(packet + sizeof(struct ether_header));
55 u_short op;
56 in_addr_t ipaddr;
57  
58 2 RETVAL = 1;
59  
60 // Are you root?
61 2 uid = getuid();
62 2 ▶ if(uid != 0)
Branch: 1 branch
BranchTrueFalseTotal
if(uid != 0) 2 0 2
63 {
64 2 printf("You must have UID 0 instead of %d.\n",uid);
65 2 exit(0);
66 }
67  
68 // Initialize packet buffer
69 0 memset(packet,0,packetsize);
70  
71 // What's the ARP operation type?
72 0 ▶ if(!strcmp(type,"request"))
Branch: 1 branch
BranchTrueFalseTotal
if(!strcmp(type,"request")) 0 0 0
73 {
74 0 op = ARPOP_REQUEST;
75 }
76 0 ▶ else if(!strcmp(type,"reply"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"reply")) 0 0 0
77 {
78 0 op = ARPOP_REPLY;
79 }
80 0 ▶ else if(!strcmp(type,"revrequest"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"revrequest")) 0 0 0
81 {
82 0 op = ARPOP_REVREQUEST;
83 }
84 0 ▶ else if(!strcmp(type,"revreply"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"revreply")) 0 0 0
85 {
86 0 op = ARPOP_REVREPLY;
87 }
88 0 ▶ else if(!strcmp(type,"invrequest"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"invrequest")) 0 0 0
89 {
90 0 op = ARPOP_INVREQUEST;
91 }
92 0 ▶ else if(!strcmp(type,"invreply"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"invreply")) 0 0 0
93 {
94 0 op = ARPOP_INVREPLY;
95 }
96 else
97 {
98 0 printf("Unknown ARP operation\n");
99 0 RETVAL = 0;
100 }
101  
102 0 ▶ if(smac == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(smac == NULL) 0 0 0
103 {
104 0 printf("Parameter smac is NULL! Terminating.\n");
105 0 RETVAL = 0;
106 }
107  
108 0 ▶ if(dmac == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(dmac == NULL) 0 0 0
109 {
110 0 printf("Parameter dmac is NULL! Terminating.\n");
111 0 RETVAL = 0;
112 }
113  
114 // Found a dollar sign?
115 0 ▶ if(strchr(smac,36))
Branch: 1 branch
BranchTrueFalseTotal
if(strchr(smac,36)) 0 0 0
116 {
117 0 printf("Found a $ char in smac! Terminating.\n");
118 0 RETVAL = 0;
119 }
120  
121 0 ▶ if(strchr(dmac,36))
Branch: 1 branch
BranchTrueFalseTotal
if(strchr(dmac,36)) 0 0 0
122 {
123 0 printf("Found a $ char in dmac! Terminating.\n");
124 0 RETVAL = 0;
125 }
126  
127 0 ▶ if(ether_aton(smac) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(ether_aton(smac) == NULL) 0 0 0
128 {
129 0 printf("Invalid source mac address! Terminating.\n");
130 0 RETVAL = 0;
131 }
132  
133 0 ▶ if(ether_aton(dmac) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(ether_aton(dmac) == NULL) 0 0 0
134 {
135 0 printf("Invalid destination mac address! Terminating.\n");
136 0 RETVAL = 0;
137 }
138  
139 // Check ips
140 0 ▶ if(inet_addr(sip) == INADDR_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(inet_addr(sip) == INADDR_NONE) 0 0 0
141 {
142 0 printf("Invalid source ip address! Terminating.\n");
143 0 RETVAL = 0;
144 }
145  
146 0 ▶ if(inet_addr(dip) == INADDR_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(inet_addr(dip) == INADDR_NONE) 0 0 0
147 {
148 0 printf("Invalid destination ip address! Terminating.\n");
149 0 RETVAL = 0;
150 }
151  
152 // Construct and send packet
153 0 ▶ if(RETVAL != 0)
Branch: 1 branch
BranchTrueFalseTotal
if(RETVAL != 0) 0 0 0
154 {
155 // Ethernet header
156 0 memcpy(ethhdr->ether_dhost,(u_char *)ether_aton(dmac),ETHER_ADDR_LEN); // Destination MAC
157 0 memcpy(ethhdr->ether_shost,(u_char *)ether_aton(smac),ETHER_ADDR_LEN); // Source MAC
158 0 ethhdr->ether_type = htons(ETHERTYPE_ARP); // ARP protocol
159  
160 // ARP header
161 0 arp->hw_type = htons(ARPHDR_ETHER); // Hardware address type
162 0 arp->proto_type = htons(ETH_P_IP); // Protocol address type
163 0 arp->ha_len = ETH_ALEN; // Hardware address length
164 0 arp->pa_len = IP_ALEN; // Protocol address length
165 0 arp->opcode = htons(op); // ARP operation
166 0 memcpy(arp->source_add,(u_char *)ether_aton(smac),ETH_ALEN); // Source MAC
167 0 memcpy(arp->dest_add,(u_char *)ether_aton(dmac),ETH_ALEN); // Destination MAC
168 0 ipaddr = inet_addr(sip);
169 0 memcpy(arp->source_ip, (u_char *)&ipaddr, IP_ALEN); // Source IP
170  
171 0 ipaddr = inet_addr(dip);
172 0 memcpy(arp->dest_ip, (u_char *)&ipaddr, IP_ALEN); // Destination IP
173  
174  
175  
176 // Run packet!! Run!
177 // FreeBSD code
178 if(SOCK_TYPE == SOCK_RAW)
179 {
180 RETVAL = send_packet_bsd(dev,packet,packetsize);
181 }
182  
183 // Linux code
184 else
185 {
186 0 RETVAL = send_packet_linux(dev,packet,packetsize);
187 }
188 }
189  
190 OUTPUT:
191 RETVAL
192  
193 char *
194 get_mac(dev)
195 const char *dev;
196 CODE:
197 1 char tmp[HEX_HW_ADDR_LEN] = "unknown";
198  
199 if(SOCK_TYPE == SOCK_RAW)
200 {
201 get_mac_bsd(dev,tmp);
202 }
203 else
204 {
205 1 get_mac_linux(dev,tmp);
206 }
207  
208 1 RETVAL = tmp;
209  
210 OUTPUT:
211 RETVAL
212  
213  
214 char *
215 arp_lookup(dev, ip)
216 const char *dev;
217 const char *ip;
218  
219 CODE:
220 3 char tmp[HEX_HW_ADDR_LEN] = "unknown";
221  
222 if(SOCK_TYPE == SOCK_RAW)
223 {
224 arp_lookup_bsd(dev,ip,tmp);
225 }
226 else
227 {
228 3 arp_lookup_linux(dev,ip,tmp);
229 }
230  
231 3 RETVAL = tmp;
232  
233 OUTPUT:
234 RETVAL
235  
« send_packet_linux.c ↑ Summary arp_lookup_linux.c »