END_TEST START_TEST (test_sockaddr_int_IPv4_init) { char addr_str[256]; uint32_t a = 2653302794UL; uint16_t port = 4567; struct sockaddr_storage a_addr; struct sockaddr_storage b_addr; sockaddr_initFromIPv4Int((struct sockaddr_in *)&a_addr, htonl(a), htons(port)); sockaddr_initFromIPv4String((struct sockaddr_in *)&b_addr, "158.38.48.10:4567"); sockaddr_toString( (struct sockaddr *)&a_addr, addr_str, 256, true); fail_unless( sockaddr_alike( (struct sockaddr *)&a_addr, (struct sockaddr *)&b_addr) ); fail_if(sockaddr_initFromIPv4String((struct sockaddr_in *)&b_addr, "158.38.48.10.45")); fail_if(sockaddr_initFromIPv4String((struct sockaddr_in *)&b_addr, "158.38.48.10.45.45.67.87:4556")); }
static bool StoreBindResp(STUN_TRANSACTION_DATA* trans, StunMessage* resp) { STUN_CLIENT_DATA* client = trans->client; if (resp->hasXorMappedAddress) { if (resp->xorMappedAddress.familyType == STUN_ADDR_IPv4Family) { sockaddr_initFromIPv4Int( (struct sockaddr_in*)&trans->rflxAddr, htonl(resp->xorMappedAddress.addr.v4.addr), htons(resp->xorMappedAddress.addr.v4.port) ); } else if (resp->xorMappedAddress.familyType == STUN_ADDR_IPv6Family) { sockaddr_initFromIPv6Int( (struct sockaddr_in6*)&trans->rflxAddr, resp->xorMappedAddress.addr.v6.addr, htons(resp->xorMappedAddress.addr.v6.port) ); } return true; } else { StunPrint(client->logUserData, client->Log_cb, StunInfoCategory_Error, "<STUNCLIENT:%02d> Missing XorMappedAddress BindResp", trans->inst); return false; } }
int getRemoteTurnServerIp(struct turn_info *turnInfo, char *fqdn) { struct addrinfo hints, *res, *p; int status; char ipstr[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version hints.ai_socktype = SOCK_STREAM; if ((status = getaddrinfo(fqdn, NULL, &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 2; } for(p = res;p != NULL; p = p->ai_next) { void *addr; char *ipver; // get the pointer to the address itself, // different fields in IPv4 and IPv6: if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; if ( ipv4->sin_port == 0 ) { sockaddr_initFromIPv4Int((struct sockaddr_in *)&turnInfo->remoteIp4, ipv4->sin_addr.s_addr, htons(3478)); } addr = &(ipv4->sin_addr); ipver = "IPv4"; } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; if ( ipv6->sin6_port == 0 ) { sockaddr_initFromIPv6Int((struct sockaddr_in6 *)&turnInfo->remoteIp6, ipv6->sin6_addr.s6_addr, htons(3478)); } addr = &(ipv6->sin6_addr); ipver = "IPv6"; } // convert the IP to a string and print it: //inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); //printf(" %s: %s\n", ipver, ipstr); } freeaddrinfo(res); // free the linked list return 0; }
int getSockaddrFromFqdn(struct sockaddr* addr_out, const char* fqdn, unsigned int port) { struct addrinfo hints, * res, * p; int status; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; /* AF_INET or AF_INET6 to force version */ hints.ai_socktype = SOCK_STREAM; if ( ( status = getaddrinfo(fqdn, NULL, &hints, &res) ) != 0 ) { fprintf( stderr, "getaddrinfo: %s\n", gai_strerror(status) ); return 2; } for (p = res; p != NULL; p = p->ai_next) { /* get the pointer to the address itself, */ /* different fields in IPv4 and IPv6: */ if (p->ai_family == AF_INET) /* IPv4 */ { struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr; if (ipv4->sin_port == 0) { sockaddr_initFromIPv4Int( (struct sockaddr_in*)addr_out, ipv4->sin_addr.s_addr, htons(port) ); } } else /* IPv6 */ { struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr; if (ipv6->sin6_port == 0) { sockaddr_initFromIPv6Int( (struct sockaddr_in6*)addr_out, ipv6->sin6_addr.s6_addr, htons(port) ); } } } freeaddrinfo(res); /* free the linked list */ return 0; }