11181
|
1 /**
|
|
2 * @file simple.c
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
|
|
7 *
|
|
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.
|
|
12 *
|
|
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.
|
|
17 *
|
|
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
|
|
21 */
|
|
22
|
|
23 #include "srvresolve.h"
|
|
24
|
|
25 typedef union {
|
|
26 HEADER hdr;
|
|
27 u_char buf[1024];
|
|
28 } queryans;
|
|
29
|
11189
|
30 struct getserver_return *getserver(const char *domain, const char *srv) {
|
11181
|
31 static struct getserver_return ret;
|
|
32 queryans answer;
|
|
33 int size;
|
|
34 int qdcount;
|
|
35 int ancount;
|
|
36 gchar *end;
|
|
37 gchar *cp;
|
|
38 gchar name[256];
|
|
39 gchar *bestname = NULL;
|
|
40 int bestport = 5060;
|
|
41 int bestpri=99999;
|
|
42 int type, dlen, pref, weight, port;
|
11189
|
43 gchar *query = g_strdup_printf("%s.%s",srv,domain);
|
11181
|
44
|
|
45
|
|
46 size = res_query( query, C_IN, T_SRV, (u_char*)&answer, sizeof( answer));
|
|
47
|
|
48 g_free(query);
|
|
49
|
|
50 qdcount = ntohs(answer.hdr.qdcount);
|
|
51 ancount = ntohs(answer.hdr.ancount);
|
|
52
|
|
53
|
|
54 cp = (char*)&answer + sizeof(HEADER);
|
|
55 end = (char*)&answer + size;
|
|
56
|
|
57 // skip over unwanted stuff
|
|
58 while (qdcount-- > 0 && cp < end) {
|
|
59 size = dn_expand( (char*)&answer, end, cp, name, 256);
|
|
60 if(size < 0) return NULL;
|
|
61 cp += size + QFIXEDSZ;
|
|
62 }
|
|
63
|
|
64 while (ancount-- > 0 && cp < end) {
|
|
65 size = dn_expand((char*)&answer, end, cp, name, 256);
|
|
66 if(size < 0)
|
|
67 return NULL;
|
|
68
|
|
69 cp += size;
|
|
70
|
|
71 NS_GET16(type,cp);
|
|
72 cp += 6; // skip ttl and class
|
|
73
|
|
74 NS_GET16(dlen,cp);
|
|
75
|
|
76 if (type == T_SRV) {
|
|
77 NS_GET16(pref,cp);
|
|
78
|
|
79 NS_GET16(weight, cp);
|
|
80
|
|
81 NS_GET16(port, cp);
|
|
82
|
|
83 size = dn_expand( (char*)&answer, end, cp, name, 256);
|
|
84 if(size < 0 )
|
|
85 return NULL;
|
|
86
|
|
87 cp += size;
|
|
88
|
|
89 if(pref<bestpri) {
|
|
90 if( bestname) g_free(bestname);
|
|
91 bestname = g_strdup(name);
|
|
92 bestpri = pref;
|
|
93 bestport = port;
|
|
94 }
|
|
95 } else {
|
|
96 cp += dlen;
|
|
97 }
|
|
98 }
|
|
99 if(bestpri < 99999) {
|
|
100 ret.name = bestname;
|
|
101 ret.port = bestport;
|
|
102 } else {
|
|
103 ret.name = g_strdup(domain);
|
|
104 ret.port = 5060;
|
|
105 }
|
|
106 return &ret;
|
|
107 }
|