From b582cce69853d0a562dd0171914426887e854966 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 23 Jul 2018 16:23:56 +0200 Subject: Use raw sockets for server No more buffering, but auto-reply needs to be turned off; see run_server.sh. --- icmp.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'icmp.c') diff --git a/icmp.c b/icmp.c index e64580c..63436ac 100644 --- a/icmp.c +++ b/icmp.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -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; } -- cgit v1.2.3-70-g09d2