* Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de> * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include <arpa/nameser.h> #ifdef HAVE_ARPA_NAMESER_COMPAT_H #include <arpa/nameser_compat.h> /* Missing from the mingw headers */ static DNS_STATUS WINAPI (*MyDnsQuery_UTF8) ( PCSTR lpstrName, WORD wType, DWORD fOptions, PIP4_ARRAY aipServers, PDNS_RECORD* ppQueryResultsSet, PVOID* pReserved) = NULL; static void WINAPI (*MyDnsRecordListFree) (PDNS_RECORD pRecordList, DNS_FREE_TYPE FreeType) = NULL; static gint responsecompare(gconstpointer ar, gconstpointer br) { GaimSrvResponse *a = (GaimSrvResponse*)ar; GaimSrvResponse *b = (GaimSrvResponse*)br; if(a->weight == b->weight) if(a->weight < b->weight) static void resolve(int in, int out) { guint16 type, dlen, pref, weight, port; if(read(in, query, 256) <= 0) { size = res_query( query, C_IN, T_SRV, (u_char*)&answer, sizeof( answer)); qdcount = ntohs(answer.hdr.qdcount); ancount = ntohs(answer.hdr.ancount); cp = (guchar*)&answer + sizeof(HEADER); end = (guchar*)&answer + size; /* skip over unwanted stuff */ while (qdcount-- > 0 && cp < end) { size = dn_expand( (unsigned char*)&answer, end, cp, name, 256); while (ancount-- > 0 && cp < end) { size = dn_expand((unsigned char*)&answer, end, cp, name, 256); /* skip ttl and class since we already know it */ size = dn_expand( (unsigned char*)&answer, end, cp, name, 256); srvres = g_new0(GaimSrvResponse, 1); strcpy(srvres->hostname, name); ret = g_list_insert_sorted(ret, srvres, responsecompare); end: size = g_list_length(ret); write(out, &size, sizeof(int)); while(g_list_first(ret)) { write(out, g_list_first(ret)->data, sizeof(GaimSrvResponse)); g_free(g_list_first(ret)->data); ret = g_list_remove(ret, g_list_first(ret)->data); /* Should the resolver be reused? * There is most likely only 1 SRV queries per prpl... static void resolved(gpointer data, gint source, GaimInputCondition cond) { struct resdata *rdata = (struct resdata*)data; GaimSRVCallback cb = rdata->cb; read(source, &size, sizeof(int)); gaim_debug_info("srv","found %d SRV entries\n", size); tmp = res = g_new0(GaimSrvResponse, size); read(source, tmp++, sizeof(GaimSrvResponse)); cb(res, size, rdata->extradata); gaim_input_remove(rdata->handle); /** The Jabber Server code was inspiration for parts of this. */ static gboolean res_main_thread_cb(gpointer data) { GaimSrvResponse *srvres = NULL; struct resdata *rdata = data; if (rdata->errmsg != NULL) { gaim_debug_error("srv", rdata->errmsg); GaimSrvResponse *srvres_tmp; GSList *lst = rdata->results; size = g_slist_length(rdata->results); srvres_tmp = srvres = g_new0(GaimSrvResponse, size); memcpy(srvres_tmp++, lst->data, sizeof(GaimSrvResponse)); lst = g_slist_remove(lst, lst->data); gaim_debug_info("srv", "found %d SRV entries\n", size); rdata->cb(srvres, size, rdata->extradata); static gpointer res_thread(gpointer data) { struct resdata *rdata = data; ds = MyDnsQuery_UTF8(rdata->query, type, DNS_QUERY_STANDARD, NULL, &dr, NULL); if (ds != ERROR_SUCCESS) { gchar *msg = g_win32_error_message(ds); rdata->errmsg = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds); for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) { /* Discard any incorrect entries. I'm not sure if this is necessary */ if (dr_tmp->wType != type || strcmp(dr_tmp->pName, rdata->query) != 0) { srv_data = &dr_tmp->Data.SRV; srvres = g_new0(GaimSrvResponse, 1); strncpy(srvres->hostname, srv_data->pNameTarget, 255); srvres->hostname[255] = '\0'; srvres->pref = srv_data->wPriority; srvres->port = srv_data->wPort; srvres->weight = srv_data->wWeight; lst = g_slist_insert_sorted(lst, srvres, responsecompare); MyDnsRecordListFree(dr, DnsFreeRecordList); /* back to main thread */ g_idle_add(res_main_thread_cb, rdata); void gaim_srv_resolve(const char *protocol, const char *transport, const char *domain, GaimSRVCallback cb, gpointer extradata) { char *query = g_strdup_printf("_%s._%s.%s",protocol, transport, domain); gaim_debug_info("srv","querying SRV record for %s\n", query); if(pipe(in) || pipe(out)) { gaim_debug_error("srv", "Could not create pipe\n"); gaim_debug_error("srv","Could not create process!\n"); if(write(in[1], query, strlen(query)+1)<0) { gaim_debug_error("srv", "Could not write to SRV resolver\n"); rdata = g_new0(struct resdata,1); rdata->extradata = extradata; rdata->handle = gaim_input_add(out[0], GAIM_INPUT_READ, resolved, rdata); static gboolean initialized = FALSE; gaim_debug_info("srv","querying SRV record for %s\n", query); MyDnsQuery_UTF8 = (void*) wgaim_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8"); MyDnsRecordListFree = (void*) wgaim_find_and_loadproc( "dnsapi.dll", "DnsRecordListFree"); if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree) { gaim_debug_error("srv", "System missing DNS API (Requires W2K+)\n"); rdata = g_new0(struct resdata, 1); rdata->extradata = extradata; if (!g_thread_create(res_thread, rdata, FALSE, &err)) { rdata->errmsg = g_strdup_printf("SRV thread create failure: %s\n", err ? err->message : ""); res_main_thread_cb(rdata);