-
Notifications
You must be signed in to change notification settings - Fork 15
/
icmp.c
130 lines (113 loc) · 3.08 KB
/
icmp.c
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
/* pingcheck - Check connectivity of interfaces in OpenWRT
*
* Copyright (C) 2015 Bruno Randolf <br1@einfach.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "main.h"
/* keep libc includes before linux headers for musl compatibility */
#include <netinet/in.h>
#include <err.h>
#include <linux/icmp.h>
#include <linux/if.h>
#include <linux/ip.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static int pid = -1;
/* standard 1s complement checksum */
static unsigned short checksum(void* b, int len)
{
unsigned short* buf = b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2) {
sum += *buf++;
}
if (len == 1) {
sum += *(unsigned char*)buf;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
int icmp_init(const char* ifname)
{
int ret;
pid = getpid();
int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd == -1) {
warn("Could not open socket");
return -1;
}
if (ifname != NULL) {
if (strlen(ifname) >= IFNAMSIZ) {
fprintf(stderr, "icmp_init: ifname too long");
return -1;
}
struct ifreq ifr;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
if (ret < 0) {
warn("Could not bind to '%s'", ifname);
close(fd);
return -1;
}
}
return fd;
}
bool icmp_echo_send(int fd, int dst_ip, int cnt)
{
char buf[500];
int ret;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = 0;
addr.sin_addr.s_addr = dst_ip;
struct icmphdr* icmp = (struct icmphdr*)buf;
icmp->type = ICMP_ECHO;
icmp->code = 0;
icmp->un.echo.id = htons(pid + fd);
icmp->un.echo.sequence = htons(cnt);
icmp->checksum = 0;
icmp->checksum = checksum(buf, sizeof(struct icmphdr));
ret = sendto(fd, &buf, sizeof(struct icmphdr), 0, (struct sockaddr*)&addr,
sizeof(addr));
if (ret <= 0) {
warn("sendto");
return false;
}
return true;
}
int icmp_echo_receive(int fd)
{
char buf[500];
int ret;
ret = recv(fd, buf, sizeof(buf), 0);
if (ret < (int)(sizeof(struct icmphdr) + sizeof(struct iphdr))) {
warn("received packet too short");
return -1;
}
struct iphdr* ip = (struct iphdr*)buf;
struct icmphdr* icmp = (struct icmphdr*)(buf + ip->ihl * 4);
int csum_recv = icmp->checksum;
icmp->checksum = 0; // need to zero before calculating checksum
int csum_calc = checksum(icmp, sizeof(struct icmphdr));
int received_fd = ntohs(icmp->un.echo.id) - pid;
if (csum_recv == csum_calc && // checksum correct
icmp->type == ICMP_ECHOREPLY && // correct type
received_fd >= 0) { // handle could be valid
return received_fd;
}
return -1;
}