core cleanup: move code for "copy to" menu to purple backend
[siplcs.git] / src / miranda / miranda-dnsquery.c
blob7560b3798363a07671faadeb4856a7cfa3aa112b
1 /**
2 * @file miranda-dnsquery.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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
23 #include <windows.h>
24 #include <win2k.h>
25 #include <stdio.h>
26 #include <windns.h>
28 #include <glib.h>
30 #include "newpluginapi.h"
31 #include "m_protosvc.h"
32 #include "m_protoint.h"
33 #include "m_utils.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 {
42 char *host;
43 int port;
44 } srv_reply;
46 static srv_reply* srv_lookup(WORD wType,
47 const gchar* service,
48 const gchar* protocol,
49 const gchar* domain )
51 srv_reply *res = NULL;
52 HINSTANCE hDnsapi = LoadLibraryA( "dnsapi.dll" );
53 DNSQUERYA pDnsQuery;
54 DNSFREELIST pDnsRecordListFree;
55 gchar temp[256];
56 DNS_RECORD *results = NULL;
57 DNS_STATUS status;
59 if ( hDnsapi == NULL )
60 return res;
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 );
67 return res;
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) {
74 FreeLibrary(hDnsapi);
75 return res;
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);
83 FreeLibrary(hDnsapi);
84 return res;
88 struct sipe_dns_query *sipe_backend_dns_query_a(const gchar *hostname,
89 int port,
90 sipe_dns_resolved_cb callback,
91 gpointer data)
93 srv_reply* sr = srv_lookup( DNS_TYPE_A, "protocol", "transport", "domain" );
94 SIPE_DEBUG_INFO("Type A lookup for host <%s> port <%d>", hostname, port);
96 if (sr) {
97 callback( data, sr->host, sr->port);
99 g_free(sr->host);
100 g_free(sr);
101 } else {
102 callback( data, NULL, 0);
105 return NULL;
108 struct sipe_dns_query *sipe_backend_dns_query_srv(const gchar *protocol,
109 const gchar *transport,
110 const gchar *domain,
111 sipe_dns_resolved_cb callback,
112 gpointer data)
114 srv_reply* sr = srv_lookup( DNS_TYPE_SRV, protocol, transport, domain );
116 SIPE_DEBUG_INFO("Type SRV lookup for proto <%s> transport <%s> domain <%s>",
117 protocol, transport, domain);
119 if (sr) {
120 callback( data, sr->host, sr->port);
122 g_free(sr->host);
123 g_free(sr);
124 } else {
125 callback( data, NULL, 0);
128 return NULL;
131 void sipe_backend_dns_query_cancel(struct sipe_dns_query *query)
133 _NIF();
137 Local Variables:
138 mode: c
139 c-file-style: "bsd"
140 indent-tabs-mode: t
141 tab-width: 8
142 End: