Add fake client sending IHUs

This commit is contained in:
Théophile Bastian 2016-11-25 15:48:08 +01:00
parent 6730d8ba85
commit 143cff86f5
2 changed files with 72 additions and 0 deletions

1
.gitignore vendored
View file

@ -31,3 +31,4 @@
projet.pdf projet.pdf
jeanhubert jeanhubert
cfg* cfg*
*.bin

71
utils/ihu_replier.c Normal file
View file

@ -0,0 +1,71 @@
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdint.h>
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;
}