summaryrefslogtreecommitdiff
path: root/icmp.c
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-07-23 16:23:56 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-07-25 18:07:49 +0200
commitb582cce69853d0a562dd0171914426887e854966 (patch)
tree9b5a1c13eb766b8ab4d406d62fa84ec5303e2b46 /icmp.c
parent5722c47aa3402f1458da9eec332153a454a540b7 (diff)
Use raw sockets for server
No more buffering, but auto-reply needs to be turned off; see run_server.sh.
Diffstat (limited to 'icmp.c')
-rw-r--r--icmp.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/icmp.c b/icmp.c
index e64580c..63436ac 100644
--- a/icmp.c
+++ b/icmp.c
@@ -2,6 +2,7 @@
#include <stddef.h>
#include <string.h>
#include <assert.h>
+#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
@@ -99,10 +100,6 @@ struct icmp_reply icmp_communicate(int sock, const char *ip_address, int seqnum,
int icmp_send_echo_reply(const char *ip_address, int id, int seqnum, const void *data_, size_t length) {
const uint8_t *data = (const uint8_t*)data_;
- int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
- int zero = 0;
- assert(setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &zero, sizeof zero) >= 0);
-
struct sockaddr_in addr;
make_sockaddr(&addr, ip_address);
@@ -120,9 +117,14 @@ int icmp_send_echo_reply(const char *ip_address, int id, int seqnum, const void
msg.checksum = ~compute_checksum(&msg, total_length);
- if (sendto(sock, &msg, ICMP_PAYLOAD_OFFSET + length, 0, (struct sockaddr*)&addr, sizeof addr) < 0) {
- return -1;
- }
+ int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
+ // The below is only necessary for sending on IPPROTO_RAW sockets
+ // int zero = 0;
+ // assert(setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &zero, sizeof zero) >= 0);
+
+ int ret = sendto(sock, &msg, ICMP_PAYLOAD_OFFSET + length, 0, (struct sockaddr*)&addr, sizeof addr);
+ close(sock);
+ if (ret < 0) return -1;
return 0;
}