Fix ihu_replier

This commit is contained in:
Théophile Bastian 2016-11-28 15:18:27 +01:00
parent 68347f8b0c
commit 50975cd0e0

View file

@ -11,7 +11,7 @@
const int BUFFER_SIZE = 4096; const int BUFFER_SIZE = 4096;
int main(void) { int main(int argc, char** argv) {
int sock = socket(PF_INET6, SOCK_DGRAM, 0); int sock = socket(PF_INET6, SOCK_DGRAM, 0);
if(sock < 0) { if(sock < 0) {
perror("Could not create socket"); perror("Could not create socket");
@ -25,10 +25,17 @@ int main(void) {
exit(1); exit(1);
} }
if(argc < 3) {
fprintf(stderr, "Gimmeh my port and ID!\n");
exit(1);
}
int port = atoi(argv[1]);
char id = atoi(argv[2]);
struct sockaddr_in6 sockIn; struct sockaddr_in6 sockIn;
memset(&sockIn, 0, sizeof(sockIn)); memset(&sockIn, 0, sizeof(sockIn));
sockIn.sin6_family = AF_INET6; sockIn.sin6_family = AF_INET6;
sockIn.sin6_port = htons(1212); sockIn.sin6_port = htons(port);
if(bind(sock, (struct sockaddr*)&sockIn, sizeof(sockIn))) { if(bind(sock, (struct sockaddr*)&sockIn, sizeof(sockIn))) {
perror("Could not bind socket to port 8080"); perror("Could not bind socket to port 8080");
exit(1); exit(1);
@ -37,7 +44,7 @@ int main(void) {
char* buffer = (char*)malloc(sizeof(char) * BUFFER_SIZE); char* buffer = (char*)malloc(sizeof(char) * BUFFER_SIZE);
while(1) { while(1) {
struct sockaddr fromAddr; struct sockaddr fromAddr;
socklen_t fromAddr_len; socklen_t fromAddr_len = sizeof(struct sockaddr_in6);
ssize_t readBytes = recvfrom(sock, buffer, BUFFER_SIZE, ssize_t readBytes = recvfrom(sock, buffer, BUFFER_SIZE,
0, &fromAddr, &fromAddr_len); 0, &fromAddr, &fromAddr_len);
if(readBytes < 0) { if(readBytes < 0) {
@ -50,15 +57,16 @@ int main(void) {
outPck[0] = 57; outPck[0] = 57;
outPck[1] = 0; outPck[1] = 0;
*(uint16_t*)(outPck+2) = htonl(10); *(uint16_t*)(outPck+2) = htonl(10);
outPck[11] = id;
outPck[12] = 2; outPck[12] = 2;
outPck[13] = 8; outPck[13] = 8;
memcpy(outPck+14, buffer+4, 8); memcpy(outPck+14, buffer+4, 8);
printf("%d\n", fromAddr.sa_family); printf("%d v4=%d v6=%d\n", fromAddr.sa_family, AF_INET, AF_INET6);
char sAddr[54]; char sAddr[54];
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)(&fromAddr); struct sockaddr_in6* addr6 = (struct sockaddr_in6*)(&fromAddr);
inet_ntop(AF_INET6, &addr6->sin6_addr, sAddr, 54); inet_ntop(AF_INET6, &addr6->sin6_addr, sAddr, 54);
printf("Sending [%s]:%d\n", sAddr, addr6->sin6_port); printf("Sending [%s]:%d\n", sAddr, ntohs(addr6->sin6_port));
sendto(sock, outPck, 22, 0, &fromAddr, fromAddr_len); sendto(sock, outPck, 22, 0, &fromAddr, fromAddr_len);
} }