annotate src/protocols/simple/srvresolve.c @ 11433:0cad723a5a1b

[gaim-migrate @ 13670] from patch #1264246: Comment By: Luke Schierer (lschiere) what name should the copyright assignment go to here? Comment By: Zmitrok (zmitrok) Sony Computer Entertainment America, Inc. committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Sat, 03 Sep 2005 14:07:47 +0000
parents b073da869a55
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11181
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
1 /**
11350
b073da869a55 [gaim-migrate @ 13567]
Thomas Butter <tbutter>
parents: 11189
diff changeset
2 * @file srvresolve.c
11181
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
3 *
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
4 * gaim
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
5 *
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
7 *
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
11 * (at your option) any later version.
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
12 *
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
16 * GNU General Public License for more details.
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
17 *
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
21 */
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
22
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
23 #include "srvresolve.h"
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
24
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
25 typedef union {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
26 HEADER hdr;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
27 u_char buf[1024];
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
28 } queryans;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
29
11189
5f79dfde334c [gaim-migrate @ 13307]
Thomas Butter <tbutter>
parents: 11181
diff changeset
30 struct getserver_return *getserver(const char *domain, const char *srv) {
11181
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
31 static struct getserver_return ret;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
32 queryans answer;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
33 int size;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
34 int qdcount;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
35 int ancount;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
36 gchar *end;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
37 gchar *cp;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
38 gchar name[256];
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
39 gchar *bestname = NULL;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
40 int bestport = 5060;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
41 int bestpri=99999;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
42 int type, dlen, pref, weight, port;
11189
5f79dfde334c [gaim-migrate @ 13307]
Thomas Butter <tbutter>
parents: 11181
diff changeset
43 gchar *query = g_strdup_printf("%s.%s",srv,domain);
11181
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
44
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
45
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
46 size = res_query( query, C_IN, T_SRV, (u_char*)&answer, sizeof( answer));
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
47
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
48 g_free(query);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
49
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
50 qdcount = ntohs(answer.hdr.qdcount);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
51 ancount = ntohs(answer.hdr.ancount);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
52
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
53
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
54 cp = (char*)&answer + sizeof(HEADER);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
55 end = (char*)&answer + size;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
56
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
57 // skip over unwanted stuff
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
58 while (qdcount-- > 0 && cp < end) {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
59 size = dn_expand( (char*)&answer, end, cp, name, 256);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
60 if(size < 0) return NULL;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
61 cp += size + QFIXEDSZ;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
62 }
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
63
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
64 while (ancount-- > 0 && cp < end) {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
65 size = dn_expand((char*)&answer, end, cp, name, 256);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
66 if(size < 0)
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
67 return NULL;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
68
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
69 cp += size;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
70
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
71 NS_GET16(type,cp);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
72 cp += 6; // skip ttl and class
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
73
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
74 NS_GET16(dlen,cp);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
75
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
76 if (type == T_SRV) {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
77 NS_GET16(pref,cp);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
78
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
79 NS_GET16(weight, cp);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
80
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
81 NS_GET16(port, cp);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
82
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
83 size = dn_expand( (char*)&answer, end, cp, name, 256);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
84 if(size < 0 )
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
85 return NULL;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
86
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
87 cp += size;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
88
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
89 if(pref<bestpri) {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
90 if( bestname) g_free(bestname);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
91 bestname = g_strdup(name);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
92 bestpri = pref;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
93 bestport = port;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
94 }
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
95 } else {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
96 cp += dlen;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
97 }
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
98 }
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
99 if(bestpri < 99999) {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
100 ret.name = bestname;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
101 ret.port = bestport;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
102 } else {
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
103 ret.name = g_strdup(domain);
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
104 ret.port = 5060;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
105 }
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
106 return &ret;
e5bbe5070e04 [gaim-migrate @ 13292]
Thomas Butter <tbutter>
parents:
diff changeset
107 }