diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-07-23 21:44:07 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-07-25 18:07:49 +0200 |
commit | 9f2fccfdc2eae83efbde1e3ae94a2cc220537983 (patch) | |
tree | 16a19a1877d7cfdd39767de0cfaf312adc398d1b /server.c | |
parent | b582cce69853d0a562dd0171914426887e854966 (diff) |
Better ICMP code encapsulation
Diffstat (limited to 'server.c')
-rw-r--r-- | server.c | 107 |
1 files changed, 18 insertions, 89 deletions
@@ -1,108 +1,37 @@ #include <stdio.h> #include <stdbool.h> -#include <stddef.h> #include <stdlib.h> -#include <string.h> #include <unistd.h> -#include <stdint.h> -#include <arpa/inet.h> -#include <netinet/ip.h> -#include <netinet/ip_icmp.h> -#include "icmp.h" +#include "icmp_server.h" #include "util.h" -struct state { - int socket; -}; - -#if 0 -static int icmp_callback(struct nflog_data *ldata, void *state_) { - (void)gh; (void)nfmsg; - - struct state *state = (struct state*)state_; - - uint8_t *ip_start; // received packet, starting at the IP buffer - int ip_len = nflog_get_payload(ldata, (char**)&ip_start); - - struct iphdr *hdr = (struct iphdr*)ip_start; - int hdr_len = hdr->ihl * 4; - uint32_t saddr = hdr->saddr; - - struct icmp_echo *msg = (struct icmp_echo*)(ip_start + hdr_len); - int msg_len = ip_len - hdr_len; - printf("Received: type %u code %u id %hu seqnum %hu payload:\n", - (unsigned)msg->type, (unsigned)msg->code, msg->id, msg->seqnum); - xxd(msg->payload, msg_len - offsetof(struct icmp_echo, payload)); - - uint8_t *saddr_bytes = (uint8_t*)&saddr; - char ip_address[16]; - sprintf(ip_address, "%u.%u.%u.%u", saddr_bytes[0], saddr_bytes[1], saddr_bytes[2], saddr_bytes[3]); - if (icmp_send_echo_reply(ip_address, msg->id, msg->seqnum, "dank je wel", 11) < 0) { - perror("icmp_send_echo_reply"); - } - - return 0; -} -#endif - - int main(void) { - // struct state state; - // state.socket = icmp_open_socket(); - // if (state.socket < 0) { - // perror("icmp_open_socket"); - // return 1; - // } - - int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); - - struct icmp_echo msg; + int sock = icmp_server_open_socket(); + if (sock < 0) { + perror("icmp_server_open_socket"); + return 1; + } - char buf[MAX_IP_PACKET_SIZE]; while (true) { - struct iovec iov1; - memset(&iov1, 0, sizeof iov1); - iov1.iov_base = buf; - iov1.iov_len = sizeof buf; - - struct sockaddr_in addr; - - struct msghdr recv_msghdr; - memset(&recv_msghdr, 0, sizeof recv_msghdr); - recv_msghdr.msg_name = &addr; - recv_msghdr.msg_namelen = sizeof addr; - recv_msghdr.msg_iov = &iov1; - recv_msghdr.msg_iovlen = 1; - - ssize_t nr = recvmsg(sock, &recv_msghdr, 0); - if (nr < 0) break; - - // buf now contains received data starting at the IP header - - printf("Full packet received:\n"); - xxd(buf, nr); - - struct iphdr *hdr = (struct iphdr*)buf; - int hdr_len = hdr->ihl * 4; - uint32_t saddr = hdr->saddr; + struct icmp_server_incoming msg = icmp_server_receive(sock); + if (msg.data == NULL) { + perror("icmp_server_communicate"); + return 1; + } - struct icmp_echo *msg = (struct icmp_echo*)(buf + hdr_len); - int msg_len = nr - hdr_len; - printf("Received: type %u code %u id %hu seqnum %hu payload:\n", - (unsigned)msg->type, (unsigned)msg->code, msg->id, msg->seqnum); - xxd(msg->payload, msg_len - offsetof(struct icmp_echo, payload)); + printf("Received: type %d id %d seqnum %d data:\n", msg.type, msg.id, msg.seqnum); + xxd(msg.data, msg.length); - if (msg->type != ICMP_ECHO) { + if (msg.type != ICMP_ECHO) { printf("Not an ICMP_ECHO, ignoring\n"); continue; } - uint8_t *saddr_bytes = (uint8_t*)&saddr; - char ip_address[16]; - sprintf(ip_address, "%u.%u.%u.%u", saddr_bytes[0], saddr_bytes[1], saddr_bytes[2], saddr_bytes[3]); - if (icmp_send_echo_reply(ip_address, msg->id, msg->seqnum, "dank je wel", 11) < 0) { - perror("icmp_send_echo_reply"); + if (icmp_server_send_reply(sock, msg.source_addr, msg.id, msg.seqnum, "dank je wel", 11) < 0) { + perror("icmp_server_send_reply"); } } + + close(sock); } |