From 143cff86f5358c9fdb98d77dd19f023a23857c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Fri, 25 Nov 2016 15:48:08 +0100 Subject: [PATCH] Add fake client sending IHUs --- .gitignore | 1 + utils/ihu_replier.c | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 utils/ihu_replier.c diff --git a/.gitignore b/.gitignore index 1a3ed07..6095945 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ projet.pdf jeanhubert cfg* +*.bin diff --git a/utils/ihu_replier.c b/utils/ihu_replier.c new file mode 100644 index 0000000..7c33837 --- /dev/null +++ b/utils/ihu_replier.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const int BUFFER_SIZE = 4096; + +int main(void) { + int sock = socket(PF_INET6, SOCK_DGRAM, 0); + if(sock < 0) { + perror("Could not create socket"); + exit(1); + } + + int reuseVal = 1; + if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseVal, sizeof(reuseVal)) + < 0) { + perror("Could not set reuseaddr"); + exit(1); + } + + struct sockaddr_in6 sockIn; + memset(&sockIn, 0, sizeof(sockIn)); + sockIn.sin6_family = AF_INET6; + sockIn.sin6_port = htons(1212); + if(bind(sock, (struct sockaddr*)&sockIn, sizeof(sockIn))) { + perror("Could not bind socket to port 8080"); + exit(1); + } + + char* buffer = (char*)malloc(sizeof(char) * BUFFER_SIZE); + while(1) { + struct sockaddr fromAddr; + socklen_t fromAddr_len; + ssize_t readBytes = recvfrom(sock, buffer, BUFFER_SIZE, + 0, &fromAddr, &fromAddr_len); + if(readBytes < 0) { + perror("WARNING: Could not read from socket"); + continue; + } + + uint8_t outPck[24]; + memset(outPck, 0, 24); + outPck[0] = 57; + outPck[1] = 0; + *(uint16_t*)(outPck+2) = htonl(10); + outPck[12] = 2; + outPck[13] = 8; + memcpy(outPck+14, buffer+4, 8); + + printf("%d\n", fromAddr.sa_family); + char sAddr[54]; + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)(&fromAddr); + inet_ntop(AF_INET6, &addr6->sin6_addr, sAddr, 54); + printf("Sending [%s]:%d\n", sAddr, addr6->sin6_port); + sendto(sock, outPck, 22, 0, &fromAddr, fromAddr_len); + } + + if(close(sock) < 0) { + perror("Could not destroy socket"); + exit(1); + } + return 0; +} +