File Coverage

send_packet_linux.c
Criterion Covered Total %
statement 9 16 56.2
branch 4 8 50.0
condition n/a
subroutine n/a
pod n/a
total 13 24 54.1


line stmt bran cond sub pod time code
1             /*
2             Perl ARP Extension
3             Send the packet
4             Linux code
5              
6             Programmed by Bastian Ballmann
7             Last update: 11.05.2022
8              
9             This program is free software; you can redistribute
10             it and/or modify it under the terms of the
11             GNU General Public License version 2 as published
12             by the Free Software Foundation.
13              
14             This program is distributed in the hope that it will
15             be useful, but WITHOUT ANY WARRANTY; without even
16             the implied warranty of MERCHANTABILITY or FITNESS
17             FOR A PARTICULAR PURPOSE.
18             See the GNU General Public License for more details.
19             */
20              
21             #include
22             #include
23             #include
24             #include
25             #include
26             #include
27             #include
28             #include "arp.h"
29              
30 1           int send_packet_linux(const char *dev, u_char *packet, u_int packetsize)
31             {
32             struct sockaddr addr;
33             int sock;
34              
35 1 50         if(strlen(dev) == 0)
36             {
37 0           printf("dev is undefined. Terminating.\n");
38 0           return 0;
39             }
40              
41 1 50         if(packetsize == 0)
42             {
43 0           printf("packetsize is zero. Terminating.\n");
44 0           return 0;
45             }
46              
47             // Create socket descriptor
48 1 50         if( ( sock = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0 )
49             {
50 0           perror("socket");
51 0           return 0;
52             }
53              
54             // Set dev and send the packet
55 1           strncpy(addr.sa_data, dev, sizeof(addr.sa_data));
56              
57 1 50         if( (sendto(sock, packet, packetsize, 0, &addr, sizeof(struct sockaddr))) < 0 )
58             {
59 1           perror("send");
60 1           return 0;
61             }
62              
63 0           close(sock);
64 1           return 1;
65             }