Commit a6d7c580 authored by Rob Swindell's avatar Rob Swindell 💬
Browse files

Resolve the socket protocol/address family issues with IPv4 vs. IPv6

When listening, the address family cannot be PF_UNSPEC, so default to PF_INET (IPv4), when not specified.
Not sure why we're setting ai_flags to PF_UNSPEC (copied from syncterm) - that's just 0, but otherwise the PF_ and AI_ flags are not compatible.
Don't set the ai_flags to anything here since they don't seem to change the behavior.
parent d7b1f25e
Pipeline #3061 passed with stage
in 10 minutes and 7 seconds
...@@ -356,13 +356,18 @@ const char* protocol(enum mode mode) ...@@ -356,13 +356,18 @@ const char* protocol(enum mode mode)
return "Raw"; return "Raw";
} }
int address_family() int address_family(BOOL for_listen)
{ {
switch(cfg.address_family) { switch(cfg.address_family) {
case ADDRESS_FAMILY_INET: return PF_INET; case ADDRESS_FAMILY_INET: return PF_INET;
case ADDRESS_FAMILY_INET6: return PF_INET6; case ADDRESS_FAMILY_INET6: return PF_INET6;
} }
return PF_UNSPEC; return for_listen ? PF_INET : PF_UNSPEC;
}
int listen_address_family()
{
return address_family(true);
} }
int putcom(char* buf, size_t len) int putcom(char* buf, size_t len)
...@@ -582,14 +587,9 @@ char* dial(struct modem* modem, const char* number) ...@@ -582,14 +587,9 @@ char* dial(struct modem* modem, const char* number)
} }
dprintf("Connecting to port %hu at host '%s' via %s", port, host, protocol(mode)); dprintf("Connecting to port %hu at host '%s' via %s", port, host, protocol(mode));
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags=PF_UNSPEC; hints.ai_family = address_family(/* for listen: */false);
hints.ai_family=address_family();
hints.ai_socktype=SOCK_STREAM; hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP; hints.ai_protocol=IPPROTO_TCP;
hints.ai_flags=AI_NUMERICSERV;
#ifdef AI_ADDRCONFIG
hints.ai_flags|=AI_ADDRCONFIG;
#endif
dprintf("%s %d calling getaddrinfo", __FILE__, __LINE__); dprintf("%s %d calling getaddrinfo", __FILE__, __LINE__);
SAFEPRINTF(portnum, "%hu", port); SAFEPRINTF(portnum, "%hu", port);
int result = getaddrinfo(host, portnum, &hints, &res); int result = getaddrinfo(host, portnum, &hints, &res);
...@@ -1073,7 +1073,7 @@ int main(int argc, char** argv) ...@@ -1073,7 +1073,7 @@ int main(int argc, char** argv)
cfg.listen = true; cfg.listen = true;
arg++; arg++;
if(*arg != '\0') { if(*arg != '\0') {
listening_interface.addr.sa_family = address_family(); listening_interface.addr.sa_family = listen_address_family();
if(inet_ptoaddr(arg, &listening_interface, sizeof(listening_interface)) == NULL) { if(inet_ptoaddr(arg, &listening_interface, sizeof(listening_interface)) == NULL) {
fprintf(stderr, "!Error parsing network address: %s", arg); fprintf(stderr, "!Error parsing network address: %s", arg);
return EXIT_FAILURE; return EXIT_FAILURE;
...@@ -1123,13 +1123,13 @@ int main(int argc, char** argv) ...@@ -1123,13 +1123,13 @@ int main(int argc, char** argv)
if(sock != INVALID_SOCKET) if(sock != INVALID_SOCKET)
listening_sock = sock; listening_sock = sock;
else { else {
listening_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP); listening_sock = socket(listen_address_family(), SOCK_STREAM, IPPROTO_IP);
if(listening_sock == INVALID_SOCKET) { if(listening_sock == INVALID_SOCKET) {
fprintf(stderr, "Error %ld creating socket\n", WSAGetLastError()); fprintf(stderr, "Error %ld creating socket\n", WSAGetLastError());
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
listening_interface.addr.sa_family = address_family(); listening_interface.addr.sa_family = listen_address_family();
inet_setaddrport(&listening_interface, cfg.port); inet_setaddrport(&listening_interface, cfg.port);
result = bind(listening_sock, &listening_interface.addr, xp_sockaddr_len(&listening_interface)); result = bind(listening_sock, &listening_interface.addr, xp_sockaddr_len(&listening_interface));
if(result != 0) { if(result != 0) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment