📄 ppldns.c
字号:
} } } if (res == NULL) { ppl_dns_unlock_result_access (); return -1; } if (res->dns_ips != NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "time to live: %li, date: %i\n", res->dns_ips->ttl, res->date)); if (now - res->date > res->dns_ips->ttl) /* expired entry! */ { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "ENTRY REMOVED2\n")); ppl_dns_remove_entry (res); res = NULL; ppl_dns_unlock_result_access (); return -1; } } /* REMOVE_ELEMENT(dns_results, res); We could keep it for a while? */ ppl_dns_unlock_result_access (); *dns = res; return 0;}PPL_DECLARE (ppl_status_t)ppl_dns_get_addrinfo (struct addrinfo **addrinfo, char *hostname, int service){ unsigned long int one_inet_addr; struct addrinfo hints; int error; char portbuf[10]; if (service!=0) _snprintf(portbuf, sizeof(portbuf), "%d", service); memset (&hints, 0, sizeof (hints)); if ((int) (one_inet_addr = inet_addr (hostname)) == -1) hints.ai_flags = AI_CANONNAME; else {#if 0 struct addrinfo *_addrinfo; _addrinfo = (struct addrinfo *)osip_malloc(sizeof(struct addrinfo) + sizeof (struct sockaddr_in) + 0); /* no cannonname */ memset(_addrinfo, 0, sizeof(struct addrinfo) + sizeof (struct sockaddr_in) + 0); _addrinfo->ai_flags = AI_NUMERICHOST; _addrinfo->ai_family = AF_INET; _addrinfo->ai_socktype = SOCK_DGRAM; _addrinfo->ai_protocol = IPPROTO_UDP; _addrinfo->ai_addrlen = sizeof(struct sockaddr_in); _addrinfo->ai_addr = (void *) ((_addrinfo) + sizeof (struct addrinfo)); ((struct sockaddr_in*)_addrinfo->ai_addr)->sin_family = AF_INET; ((struct sockaddr_in*)_addrinfo->ai_addr)->sin_addr.s_addr = one_inet_addr; if (service==0) ((struct sockaddr_in*)_addrinfo->ai_addr)->sin_port = htons (5060); else ((struct sockaddr_in*)_addrinfo->ai_addr)->sin_port = htons ((unsigned short)service); _addrinfo->ai_canonname = NULL; _addrinfo->ai_next = NULL; OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "No DNS resolution needed for %s:%i\n", hostname, service)); *addrinfo = _addrinfo; return 0;#else hints.ai_flags = AI_NUMERICHOST;#endif } hints.ai_family = PF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; if (service==0) { error = getaddrinfo (hostname, "sip", &hints, addrinfo); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "SRV resolution with udp-sip-%s\n", hostname)); } else { error = getaddrinfo (hostname, portbuf, &hints, addrinfo); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "DNS resolution with %s:%i\n", hostname, service)); } if (error || *addrinfo == NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "getaddrinfo failure. %s:%i\n", hostname, service)); return -1; } /* fprintf (stdout, "canonnical name is: %s\n", (*addrinfo)->ai_canonname); fprintf (stdout, "ai_addrlen is: %i\n", (*addrinfo)->ai_addrlen); */ /* memcpy (sin, res0->ai_addr, res0->ai_addrlen); */ /* freeaddrinfo (res0); */ return PPL_SUCCESS;}PPL_DECLARE (int)ppl_dns_query_host (ppl_dns_entry_t ** dest, char *hostname, int port){ struct addrinfo *addr; ppl_dns_ip_t *dns_ip; ppl_dns_entry_t *dns; int my_error; char portbuf[10]; *dest = NULL; _snprintf(portbuf, sizeof(portbuf), "%d", port); my_error = ppl_dns_get_addrinfo (&addr, hostname, port); if (my_error) return my_error; dns_ip = (ppl_dns_ip_t *) osip_malloc (sizeof (ppl_dns_ip_t)); dns_ip->srv_ns_flag = PSP_NS_LOOKUP; dns_ip->ttl = 60; dns_ip->pref = 10; dns_ip->weight = 0; dns_ip->rweight = 0; dns_ip->port = port; dns_ip->next = NULL; dns_ip->parent = NULL; /* dns_ip->sin.sin_family = addr.sin_family; */ /* dns_ip->sin.sin_addr = addr.sin_addr; */ /* dns_ip->sin.sin_port = htons (port); */ dns_ip->addrinfo = addr; /* dns_ip->name = osip_strdup(addr->ai_canonname); */ dns_ip->name = ppl_inet_ntop ((struct sockaddr *)(addr->ai_addr)); if (dns_ip->name==NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "getaddrinfo failure. could not get printable version\n")); return -1; } dns = (ppl_dns_entry_t *) osip_malloc (sizeof (ppl_dns_entry_t)); if (dns == NULL) { if (dns_ip->addrinfo!=NULL) { freeaddrinfo(dns_ip->addrinfo); } osip_free (dns_ip->name); osip_free (dns_ip); return -1; } dns->name = hostname; dns->protocol = NULL; dns->date = ppl_time (); dns->dns_ips = dns_ip; dns->ref = 0; dns->next = NULL; dns->parent = NULL; *dest = dns; return 0;}static int ppl_dns_default_gateway_ipv4 (char *address, int size);static int ppl_dns_default_gateway_ipv6 (char *address, int size);PPL_DECLARE (int)ppl_dns_default_gateway (int familiy, char *address, int size){ if (familiy==AF_INET6) { return ppl_dns_default_gateway_ipv6 (address, size); } else { return ppl_dns_default_gateway_ipv4 (address, size); }}intppl_dns_default_gateway_ipv4 (char *address, int size){ /* w2000 and W95/98 */ unsigned long best_interface_index; DWORD hr; /* NT4 (sp4 only?) */ PMIB_IPFORWARDTABLE ipfwdt; DWORD siz_ipfwd_table = 0; unsigned int ipf_cnt; memset(address, '\0', size); best_interface_index = -1; /* w2000 and W95/98 only */ hr = GetBestInterface(inet_addr("217.12.3.11"),&best_interface_index); if (hr) { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &lpMsgBuf, 0, NULL); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "GetBestInterface: %s\r\n", lpMsgBuf)); best_interface_index = -1; } if (best_interface_index != -1) { /* probably W2000 or W95/W98 */ char *servername; char *serverip; char *netmask; OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "Default Interface found %i\r\n", best_interface_index)); if (0 == ppl_dns_get_local_fqdn(&servername, &serverip, &netmask, NULL, best_interface_index, AF_INET)) { osip_strncpy(address, serverip, size-1); osip_free(servername); osip_free(serverip); osip_free(netmask); return 0; } return -1; } if (!GetIpForwardTable(NULL, &siz_ipfwd_table, FALSE) == ERROR_INSUFFICIENT_BUFFER || !(ipfwdt = (PMIB_IPFORWARDTABLE) alloca (siz_ipfwd_table))) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "Allocation error\r\n")); return -1; } /* NT4 (sp4 support only?) */ if (!GetIpForwardTable(ipfwdt, &siz_ipfwd_table, FALSE)) { for (ipf_cnt = 0; ipf_cnt < ipfwdt->dwNumEntries; ++ipf_cnt) { if (ipfwdt->table[ipf_cnt].dwForwardDest == 0) { /* default gateway found */ char *servername; char *serverip; char *netmask; OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "Default Interface found %i\r\n", ipfwdt->table[ipf_cnt].dwForwardIfIndex)); if (0 == ppl_dns_get_local_fqdn(&servername, &serverip, &netmask, NULL, ipfwdt->table[ipf_cnt].dwForwardIfIndex, AF_INET)) { osip_strncpy(address, serverip, size-1); osip_free(servername); osip_free(serverip); osip_free(netmask); return 0; } return -1; } } } /* no default gateway interface found */ return -1;}intppl_dns_default_gateway_ipv6 (char *address, int size){ memset(address, '\0', size); return -1;}static int ppl_dns_get_local_fqdn_ipv6 (char **servername, char **serverip, char **netmask, char *_interface, unsigned int pos_interface);static int ppl_dns_get_local_fqdn_ipv4 (char **servername, char **serverip, char **netmask, char *_interface, unsigned int pos_interface);PPL_DECLARE (int)ppl_dns_get_local_fqdn (char **servername, char **serverip, char **netmask, char *_interface, unsigned int pos_interface, int family){ if (family==AF_INET6) { return ppl_dns_get_local_fqdn_ipv6(servername, serverip, netmask, _interface, pos_interface); } else { return ppl_dns_get_local_fqdn_ipv4(servername, serverip, netmask, _interface, pos_interface); }}static intppl_dns_get_local_fqdn_ipv4 (char **servername, char **serverip, char **netmask, char *UNIX_interface, unsigned int WIN32_interface){ unsigned int pos; *servername = NULL; /* no name on win32? */ *serverip = NULL; *netmask = NULL; /* First, try to get the interface where we should listen */ { DWORD size_of_iptable = 0; PMIB_IPADDRTABLE ipt; PMIB_IFROW ifrow; if (GetIpAddrTable(NULL, &size_of_iptable, TRUE) == ERROR_INSUFFICIENT_BUFFER) { ifrow = (PMIB_IFROW) _alloca (sizeof(MIB_IFROW)); ipt = (PMIB_IPADDRTABLE) _alloca (size_of_iptable); if (ifrow==NULL || ipt==NULL) { /* not very usefull to continue */ OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "ERROR alloca failed\r\n")); return -1; } if (!GetIpAddrTable(ipt, &size_of_iptable, TRUE)) { /* look for the best public interface */ for (pos=0; pos < ipt->dwNumEntries && *netmask==NULL ; ++pos) { /* index is */ struct in_addr addr; struct in_addr mask; ifrow->dwIndex = ipt->table[pos].dwIndex; if (GetIfEntry(ifrow) == NO_ERROR) { switch(ifrow->dwType) { case MIB_IF_TYPE_LOOPBACK: // break; case MIB_IF_TYPE_ETHERNET: default: addr.s_addr = ipt->table[pos].dwAddr; mask.s_addr = ipt->table[pos].dwMask; if (ipt->table[pos].dwIndex == WIN32_interface) { *servername = NULL; /* no name on win32? */ *serverip = osip_strdup(inet_ntoa(addr)); *netmask = osip_strdup(inet_ntoa(mask)); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "Interface ethernet: %s/%s\r\n", *serverip, *netmask)); break; } } } } } } } if (*serverip==NULL || *netmask==NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "ERROR No network interface found\r\n")); return -1; } return 0;}static intppl_dns_get_local_fqdn_ipv6 (char **servername, char **serverip, char **netmask, char *UNIX_interface, unsigned int WIN32_interface){ *servername = NULL; *serverip = NULL; *netmask = NULL; return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -