BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
860e91b
Branches
Tags
master
v0.1
v0.2
v0.3
v0.3.1
v0.4
v0.4.1
v0.5
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.7.0
v0.7.1
v0.7.10
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.8.0
v0.8.1
BlinkenLib
tools
Tools2.c
format
Stefan Schuermans
commited
860e91b
at 2023-08-18 09:55:23
Tools2.c
Blame
History
Raw
/* BlinkenLib Copyright 2004-2014 Stefan Schuermans <stefan@schuermans.info> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #include <winsock2.h> #include <ws2tcpip.h> #else #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> #endif #include "Tools2.h" #ifdef WIN32 static int inet_pton(int af, const char *src, void *dst) { struct sockaddr_storage ss; int size = sizeof(ss); char src_copy[INET6_ADDRSTRLEN + 1]; ZeroMemory(&ss, sizeof(ss)); strncpy(src_copy, src, sizeof(src_copy)); src_copy[sizeof(src_copy) - 1] = 0; if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) { switch (af) { case AF_INET: *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; return 1; case AF_INET6: *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; return 1; } } return 0; } #endif // convert text to IPv4 address int txt2addr(char *text, struct sockaddr_in *addr) { char txt[48]; unsigned short port; if (sscanf(text, "%32[0-9.]:%hu", txt, &port) == 2) { addr->sin_family = AF_INET; addr->sin_port = htons(port); addr->sin_addr.s_addr = inet_addr(txt); return 1; } else if (sscanf(text, "%32[0-9.]", txt) == 1) { addr->sin_family = AF_INET; addr->sin_port = htons(2323); addr->sin_addr.s_addr = inet_addr(txt); return 1; } else if (sscanf(text, "%hu", &port) == 1) { addr->sin_family = AF_INET; addr->sin_port = htons(port); addr->sin_addr.s_addr = htonl(INADDR_ANY); return 1; } else { return 0; } } // convert text to IPv6 address int txt2addr6(char *text, struct sockaddr_in6 *addr6) { char txt[128]; unsigned short port; if (sscanf(text, "[%96[0-9a-fA-F:]]:%hu", txt, &port) == 2) { addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(port); if (inet_pton(AF_INET6, txt, &addr6->sin6_addr)) return 1; else return 0; } else if (sscanf(text, "[%96[0-9a-fA-F:]]", txt) == 1) { addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(2323); if (inet_pton(AF_INET6, txt, &addr6->sin6_addr)) return 1; else return 0; } else if (sscanf(text, "%hu", &port) == 1) { addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(port); if (inet_pton(AF_INET6, "::", &addr6->sin6_addr)) return 1; else return 0; } else { return 0; } }