diff options
Diffstat (limited to 'server.c')
-rw-r--r-- | server.c | 91 |
1 files changed, 50 insertions, 41 deletions
@@ -2,16 +2,12 @@ #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> - -typedef uint8_t u_int8_t; -typedef uint16_t u_int16_t; -typedef uint32_t u_int32_t; -#include <libnetfilter_log/libnetfilter_log.h> - +#include <netinet/ip_icmp.h> #include "icmp.h" #include "util.h" @@ -20,7 +16,8 @@ struct state { int socket; }; -static int icmp_callback(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, struct nflog_data *ldata, void *state_) { +#if 0 +static int icmp_callback(struct nflog_data *ldata, void *state_) { (void)gh; (void)nfmsg; struct state *state = (struct state*)state_; @@ -47,53 +44,65 @@ static int icmp_callback(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, stru return 0; } +#endif int main(void) { - struct nflog_handle *h = nflog_open(); - if (!h) { - perror("nflog_open"); - return 1; - } - if (nflog_unbind_pf(h, AF_INET) < 0) { - perror("nflog_unbind_pf"); - return 1; - } - if (nflog_bind_pf(h, AF_INET) < 0) { - perror("nflog_bind_pf"); - return 1; - } - - struct nflog_g_handle *qh = nflog_bind_group(h, 0); - if (!qh) { - fprintf(stderr, "nflog_bind_group: no handle for group 0\n"); - return 1; - } - - if (nflog_set_mode(qh, NFULNL_COPY_PACKET, 0xffff) < 0) { - fprintf(stderr, "nflog_set_mode: can't set packet copy mode\n"); - return 1; - } - - struct state state; + // struct state state; // state.socket = icmp_open_socket(); // if (state.socket < 0) { // perror("icmp_open_socket"); // return 1; // } - nflog_callback_register(qh, &icmp_callback, &state); + int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); - int fd = nflog_fd(h); + struct icmp_echo msg; - char buf[4096]; + char buf[MAX_IP_PACKET_SIZE]; while (true) { - ssize_t nr = recv(fd, buf, sizeof buf, 0); + 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; - nflog_handle_packet(h, buf, nr); - } + // buf now contains received data starting at the IP header + + printf("Full packet received:\n"); + xxd(buf, nr); - nflog_unbind_group(qh); - nflog_close(h); + struct iphdr *hdr = (struct iphdr*)buf; + int hdr_len = hdr->ihl * 4; + uint32_t saddr = hdr->saddr; + + 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)); + + 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"); + } + } } |