2 * @file miranda-dnsquery.c
6 * Copyright (C) 2010-12 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "newpluginapi.h"
31 #include "m_protosvc.h"
32 #include "m_protoint.h"
35 #include "sipe-backend.h"
36 #include "miranda-private.h"
38 typedef DNS_STATUS (WINAPI
*DNSQUERYA
)(IN PCSTR pszName
, IN WORD wType
, IN DWORD Options
, IN PIP4_ARRAY aipServers OPTIONAL
, IN OUT PDNS_RECORD
*ppQueryResults OPTIONAL
, IN OUT PVOID
*pReserved OPTIONAL
);
39 typedef void (WINAPI
*DNSFREELIST
)(IN OUT PDNS_RECORD pRecordList
, IN DNS_FREE_TYPE FreeType
);
41 typedef struct srv_reply_t
{
46 static srv_reply
* srv_lookup(WORD wType
,
48 const gchar
* protocol
,
51 srv_reply
*res
= NULL
;
52 HINSTANCE hDnsapi
= LoadLibraryA( "dnsapi.dll" );
54 DNSFREELIST pDnsRecordListFree
;
56 DNS_RECORD
*results
= NULL
;
59 if ( hDnsapi
== NULL
)
62 pDnsQuery
= (DNSQUERYA
)GetProcAddress(hDnsapi
, "DnsQuery_A");
63 pDnsRecordListFree
= (DNSFREELIST
)GetProcAddress(hDnsapi
, "DnsRecordListFree");
64 if ( pDnsQuery
== NULL
) {
65 //dnsapi.dll is not the needed dnsapi ;)
66 FreeLibrary( hDnsapi
);
70 mir_snprintf( temp
, SIZEOF(temp
), "_%s._%s.%s", service
, protocol
, domain
);
72 status
= pDnsQuery(temp
, DNS_TYPE_SRV
, DNS_QUERY_STANDARD
, NULL
, &results
, NULL
);
73 if (FAILED(status
)||!results
|| results
[0].Data
.Srv
.pNameTarget
== 0||results
[0].wType
!= DNS_TYPE_SRV
) {
78 res
= g_new0(srv_reply
,1);
79 res
->host
= g_strdup((const gchar
*)results
[0].Data
.Srv
.pNameTarget
);
80 res
->port
= (int)results
[0].Data
.Srv
.wPort
;
82 pDnsRecordListFree(results
, DnsFreeRecordList
);
88 struct sipe_dns_query
*sipe_backend_dns_query_a(struct sipe_core_public
*sipe_public
,
89 const gchar
*hostname
,
91 sipe_dns_resolved_cb callback
,
94 srv_reply
* sr
= srv_lookup( DNS_TYPE_A
, "protocol", "transport", "domain" );
95 SIPE_DEBUG_INFO("Type A lookup for host <%s> port <%d>", hostname
, port
);
98 callback( data
, sr
->host
, sr
->port
);
103 callback( data
, NULL
, 0);
109 struct sipe_dns_query
*sipe_backend_dns_query_srv(struct sipe_core_public
*sipe_public
,
110 const gchar
*protocol
,
111 const gchar
*transport
,
113 sipe_dns_resolved_cb callback
,
116 srv_reply
* sr
= srv_lookup( DNS_TYPE_SRV
, protocol
, transport
, domain
);
118 SIPE_DEBUG_INFO("Type SRV lookup for proto <%s> transport <%s> domain <%s>",
119 protocol
, transport
, domain
);
122 callback( data
, sr
->host
, sr
->port
);
127 callback( data
, NULL
, 0);
133 void sipe_backend_dns_query_cancel(struct sipe_dns_query
*query
)