860e91ba85e0070b3deb854f8a8e9952bceca480
Stefan Schuermans add IPv6 support to Blinken...

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c   1) /* BlinkenLib
Stefan Schuermans update email address of author

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c   2)    Copyright 2004-2014 Stefan Schuermans <stefan@schuermans.info>
Stefan Schuermans add IPv6 support to Blinken...

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c   3)    Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
BlinkenLib/Tools2.c   4)    a blinkenarea.org project */
BlinkenLib/Tools2.c   5) 
BlinkenLib/Tools2.c   6) #include <stdio.h>
BlinkenLib/Tools2.c   7) #include <stdlib.h>
BlinkenLib/Tools2.c   8) #include <string.h>
BlinkenLib/Tools2.c   9) #ifdef WIN32
BlinkenLib/Tools2.c  10) #include <winsock2.h>
Stefan Schuermans update Windows support

Stefan Schuermans authored 7 years ago

BlinkenLib/Tools2.c  11) #include <ws2tcpip.h>
Stefan Schuermans add IPv6 support to Blinken...

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c  12) #else
BlinkenLib/Tools2.c  13) #include <arpa/inet.h>
Stefan Schuermans format

Stefan Schuermans authored 1 year ago

tools/Tools2.c       14) #include <netinet/in.h>
tools/Tools2.c       15) #include <sys/socket.h>
tools/Tools2.c       16) #include <sys/types.h>
Stefan Schuermans add IPv6 support to Blinken...

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c  17) #endif
BlinkenLib/Tools2.c  18) 
BlinkenLib/Tools2.c  19) #include "Tools2.h"
BlinkenLib/Tools2.c  20) 
Stefan Schuermans update Windows support

Stefan Schuermans authored 7 years ago

BlinkenLib/Tools2.c  21) #ifdef WIN32
Stefan Schuermans format

Stefan Schuermans authored 1 year ago

tools/Tools2.c       22) static int inet_pton(int af, const char *src, void *dst) {
Stefan Schuermans update Windows support

Stefan Schuermans authored 7 years ago

BlinkenLib/Tools2.c  23)   struct sockaddr_storage ss;
BlinkenLib/Tools2.c  24)   int size = sizeof(ss);
BlinkenLib/Tools2.c  25)   char src_copy[INET6_ADDRSTRLEN + 1];
BlinkenLib/Tools2.c  26) 
BlinkenLib/Tools2.c  27)   ZeroMemory(&ss, sizeof(ss));
Stefan Schuermans format

Stefan Schuermans authored 1 year ago

tools/Tools2.c       28)   strncpy(src_copy, src, sizeof(src_copy));
Stefan Schuermans update Windows support

Stefan Schuermans authored 7 years ago

BlinkenLib/Tools2.c  29)   src_copy[sizeof(src_copy) - 1] = 0;
BlinkenLib/Tools2.c  30) 
Stefan Schuermans format

Stefan Schuermans authored 1 year ago

tools/Tools2.c       31)   if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) ==
tools/Tools2.c       32)       0) {
tools/Tools2.c       33)     switch (af) {
tools/Tools2.c       34)     case AF_INET:
tools/Tools2.c       35)       *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
tools/Tools2.c       36)       return 1;
tools/Tools2.c       37)     case AF_INET6:
tools/Tools2.c       38)       *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
tools/Tools2.c       39)       return 1;
Stefan Schuermans update Windows support

Stefan Schuermans authored 7 years ago

BlinkenLib/Tools2.c  40)     }
BlinkenLib/Tools2.c  41)   }
BlinkenLib/Tools2.c  42)   return 0;
BlinkenLib/Tools2.c  43) }
BlinkenLib/Tools2.c  44) #endif
BlinkenLib/Tools2.c  45) 
Stefan Schuermans add IPv6 support to Blinken...

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c  46) // convert text to IPv4 address
Stefan Schuermans format

Stefan Schuermans authored 1 year ago

tools/Tools2.c       47) int txt2addr(char *text, struct sockaddr_in *addr) {
Stefan Schuermans add IPv6 support to Blinken...

Stefan Schuermans authored 10 years ago

BlinkenLib/Tools2.c  48)   char txt[48];
BlinkenLib/Tools2.c  49)   unsigned short port;
BlinkenLib/Tools2.c  50) 
BlinkenLib/Tools2.c  51)   if (sscanf(text, "%32[0-9.]:%hu", txt, &port) == 2) {
BlinkenLib/Tools2.c  52)     addr->sin_family = AF_INET;
BlinkenLib/Tools2.c  53)     addr->sin_port = htons(port);
BlinkenLib/Tools2.c  54)     addr->sin_addr.s_addr = inet_addr(txt);
BlinkenLib/Tools2.c  55)     return 1;
BlinkenLib/Tools2.c  56)   } else if (sscanf(text, "%32[0-9.]", txt) == 1) {
BlinkenLib/Tools2.c  57)     addr->sin_family = AF_INET;
BlinkenLib/Tools2.c  58)     addr->sin_port = htons(2323);
BlinkenLib/Tools2.c  59)     addr->sin_addr.s_addr = inet_addr(txt);
BlinkenLib/Tools2.c  60)     return 1;
BlinkenLib/Tools2.c  61)   } else if (sscanf(text, "%hu", &port) == 1) {
BlinkenLib/Tools2.c  62)     addr->sin_family = AF_INET;
BlinkenLib/Tools2.c  63)     addr->sin_port = htons(port);
BlinkenLib/Tools2.c  64)     addr->sin_addr.s_addr = htonl(INADDR_ANY);
BlinkenLib/Tools2.c  65)     return 1;
BlinkenLib/Tools2.c  66)   } else {
BlinkenLib/Tools2.c  67)     return 0;
BlinkenLib/Tools2.c  68)   }
BlinkenLib/Tools2.c  69) }
BlinkenLib/Tools2.c  70) 
BlinkenLib/Tools2.c  71) // convert text to IPv6 address
Stefan Schuermans format

Stefan Schuermans authored 1 year ago

tools/Tools2.c       72) int txt2addr6(char *text, struct sockaddr_in6 *addr6) {