Example #1
0
void sock_srv_lookup(const char *service, const char *proto, const char *domain, char *resulttarget, int resulttargetlength, int *resultport)
{
    int set = 0;
    char fulldomain[2048];

    snprintf(fulldomain, 2048, "_%s._%s.%s", service, proto, domain);
#ifdef _WIN32
    {
        HINSTANCE hdnsapi = NULL;
	
	DNS_STATUS (WINAPI * pDnsQuery_A)(PCSTR, WORD, DWORD, PIP4_ARRAY, PDNS_RECORD*, PVOID*);
	void (WINAPI * pDnsRecordListFree)(PDNS_RECORD, DNS_FREE_TYPE);

	if (hdnsapi = LoadLibrary("dnsapi.dll"))
	{
	    pDnsQuery_A = GetProcAddress(hdnsapi, "DnsQuery_A");
	    pDnsRecordListFree = GetProcAddress(hdnsapi, "DnsRecordListFree");

	    if (pDnsQuery_A && pDnsRecordListFree)
	    {
		PDNS_RECORD dnsrecords = NULL;

		if (pDnsQuery_A(fulldomain, DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &dnsrecords, NULL) == 0)
		{
		    PDNS_RECORD current = dnsrecords;

		    while (current)
		    {
			if (current->wType == DNS_TYPE_SRV)
			{
			    snprintf(resulttarget, resulttargetlength, current->Data.Srv.pNameTarget);
			    *resultport = current->Data.Srv.wPort;
			    set = 1;

			    current = NULL;
			}
			else
			{
			    current = current->pNext;
			}
		    }
		}

		pDnsRecordListFree(dnsrecords, DnsFreeRecordList);
	    }
	    /*UnloadLibrary(hdnsapi);*/
	}
    }
#else
#endif

    if (!set)
    {
	snprintf(resulttarget, resulttargetlength, domain);
	*resultport = 5222;
    }
}
Example #2
0
static struct mxlist_t *getmx_dnsapi(const char *domain)
{
	HINSTANCE hDnsapi;
	DNSQUERYA pDnsQuery_A;
	DNS_RECORD *pQueryResults, *pQueryRec;
	DNS_STATUS statusDns;
	char szDnsApi[] = "dnsapi.dll";
	struct mxlist_t *mx_root, *mx_top, *mx_new;

	hDnsapi = GetModuleHandle(szDnsApi);
	if (hDnsapi == NULL) {
		hDnsapi = LoadLibrary(szDnsApi);
		if (hDnsapi == NULL) return NULL;
	}
	pDnsQuery_A = (DNSQUERYA)GetProcAddress(hDnsapi, "DnsQuery_A");
	if (pDnsQuery_A == NULL) return NULL;

	statusDns = pDnsQuery_A(domain, DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &pQueryResults, NULL);
	if (statusDns != ERROR_SUCCESS) return NULL;

	mx_root = mx_top = NULL;
	for (pQueryRec=pQueryResults; pQueryRec; pQueryRec = pQueryRec->pNext) {
		if (pQueryRec->wType != DNS_TYPE_MX) continue;
		mx_new = (struct mxlist_t *)mx_alloc(sizeof(struct mxlist_t));
		if (mx_new == NULL) break;
		memset(mx_new, '\0', sizeof(struct mxlist_t));
		mx_new->pref = pQueryRec->Data.MX.wPreference;
		lstrcpyn(mx_new->mx, pQueryRec->Data.MX.pNameExchange, 255);
		if (mx_top == NULL) {
			mx_root = mx_top = mx_new;
		} else {
			mx_top->next = mx_new;
			mx_top = mx_new;
		}
	}
	return mx_root;
}