Mercurial > pidgin
comparison src/protocols/simple/simple.c @ 12767:53218d758ba9
[gaim-migrate @ 15114]
Make the dns lookup for udp connecting asynchronous. Thomas pointed out that it should be instantaneous anyway because the SRV lookup that has just been done, but this'll avoid blocking if the SRV lookup failed or something.
committer: Tailor Script <tailor@pidgin.im>
| author | Daniel Atallah <daniel.atallah@gmail.com> |
|---|---|
| date | Sun, 08 Jan 2006 22:09:28 +0000 |
| parents | c5acba513363 |
| children | 3f6f2d59bcb4 |
comparison
equal
deleted
inserted
replaced
| 12766:42af52cb61f7 | 12767:53218d758ba9 |
|---|---|
| 48 return g_strdup_printf("%04d%04d", rand() & 0xFFFF, rand() & 0xFFFF); | 48 return g_strdup_printf("%04d%04d", rand() & 0xFFFF, rand() & 0xFFFF); |
| 49 } | 49 } |
| 50 | 50 |
| 51 static char *genbranch() { | 51 static char *genbranch() { |
| 52 return g_strdup_printf("z9hG4bK%04X%04X%04X%04X%04X", | 52 return g_strdup_printf("z9hG4bK%04X%04X%04X%04X%04X", |
| 53 rand() & 0xFFFF, | 53 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF, |
| 54 rand() & 0xFFFF, | 54 rand() & 0xFFFF, rand() & 0xFFFF); |
| 55 rand() & 0xFFFF, | |
| 56 rand() & 0xFFFF, | |
| 57 rand() & 0xFFFF); | |
| 58 } | 55 } |
| 59 | 56 |
| 60 static char *gencallid() { | 57 static char *gencallid() { |
| 61 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx", | 58 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx", |
| 62 rand() & 0xFFFF, | 59 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF, |
| 63 rand() & 0xFFFF, | 60 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF, |
| 64 rand() & 0xFFFF, | 61 rand() & 0xFFFF, rand() & 0xFFFF); |
| 65 rand() & 0xFFFF, | |
| 66 rand() & 0xFFFF, | |
| 67 rand() & 0xFFFF, | |
| 68 rand() & 0xFFFF, | |
| 69 rand() & 0xFFFF); | |
| 70 } | 62 } |
| 71 | 63 |
| 72 static char *get_my_ip() { | 64 static char *get_my_ip() { |
| 73 static char my_ip[42]; | 65 static char my_ip[42]; |
| 74 const char *tmp = gaim_network_get_public_ip(); | 66 const char *tmp = gaim_network_get_public_ip(); |
| 548 buf = auth_header(sip, &sip->proxy, method, url); | 540 buf = auth_header(sip, &sip->proxy, method, url); |
| 549 auth = g_strdup_printf("Proxy-Authorization: %s", buf); | 541 auth = g_strdup_printf("Proxy-Authorization: %s", buf); |
| 550 g_free(buf); | 542 g_free(buf); |
| 551 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth); | 543 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth); |
| 552 } | 544 } |
| 553 | 545 |
| 554 buf = g_strdup_printf("%s %s SIP/2.0\r\n" | 546 buf = g_strdup_printf("%s %s SIP/2.0\r\n" |
| 555 "Via: SIP/2.0/%s %s:%d;branch=%s\r\n" | 547 "Via: SIP/2.0/%s %s:%d;branch=%s\r\n" |
| 556 "From: <sip:%s@%s>;tag=%s\r\n" | 548 "From: <sip:%s@%s>;tag=%s\r\n" |
| 557 "To: <%s>%s%s\r\n" | 549 "To: <%s>%s%s\r\n" |
| 558 "Max-Forwards: 10\r\n" | 550 "Max-Forwards: 10\r\n" |
| 1206 | 1198 |
| 1207 static gboolean simple_ht_equals_nick(const char *nick1, const char *nick2) { | 1199 static gboolean simple_ht_equals_nick(const char *nick1, const char *nick2) { |
| 1208 return (gaim_utf8_strcasecmp(nick1, nick2) == 0); | 1200 return (gaim_utf8_strcasecmp(nick1, nick2) == 0); |
| 1209 } | 1201 } |
| 1210 | 1202 |
| 1203 static void simple_udp_host_resolved(GSList *hosts, gpointer data, const char *error_message) { | |
| 1204 struct simple_account_data *sip = (struct simple_account_data*) data; | |
| 1205 int addr_size; | |
| 1206 | |
| 1207 if (!hosts || !hosts->data) { | |
| 1208 gaim_connection_error(sip->gc, _("Couldn't resolve host")); | |
| 1209 return; | |
| 1210 } | |
| 1211 | |
| 1212 addr_size = GPOINTER_TO_INT(hosts->data); | |
| 1213 hosts = g_slist_remove(hosts, hosts->data); | |
| 1214 memcpy(&(sip->serveraddr), hosts->data, addr_size); | |
| 1215 g_free(hosts->data); | |
| 1216 hosts = g_slist_remove(hosts, hosts->data); | |
| 1217 while(hosts) { | |
| 1218 hosts = g_slist_remove(hosts, hosts->data); | |
| 1219 g_free(hosts->data); | |
| 1220 hosts = g_slist_remove(hosts, hosts->data); | |
| 1221 } | |
| 1222 | |
| 1223 /* create socket for incoming connections */ | |
| 1224 sip->fd = gaim_network_listen_range(5060, 5160, SOCK_DGRAM); | |
| 1225 | |
| 1226 if(sip->fd == -1) { | |
| 1227 gaim_connection_error(sip->gc, _("Could not create listen socket")); | |
| 1228 return; | |
| 1229 } | |
| 1230 | |
| 1231 sip->listenport = gaim_network_get_port_from_fd(sip->fd); | |
| 1232 sip->listenfd = sip->fd; | |
| 1233 | |
| 1234 sip->listenpa = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_udp_process, sip->gc); | |
| 1235 | |
| 1236 sip->resendtimeout = gaim_timeout_add(2500, (GSourceFunc) resend_timeout, sip); | |
| 1237 do_register(sip); | |
| 1238 } | |
| 1239 | |
| 1211 static void srvresolved(GaimSrvResponse *resp, int results, gpointer data) { | 1240 static void srvresolved(GaimSrvResponse *resp, int results, gpointer data) { |
| 1212 struct simple_account_data *sip = (struct simple_account_data*) data; | 1241 struct simple_account_data *sip = (struct simple_account_data*) data; |
| 1213 | 1242 |
| 1214 gchar *hostname; | 1243 gchar *hostname; |
| 1215 int port = gaim_account_get_int(sip->account, "port", 0); | 1244 int port = gaim_account_get_int(sip->account, "port", 0); |
| 1216 | 1245 |
| 1217 int error = 0; | 1246 int error = 0; |
| 1218 struct hostent *h; | |
| 1219 | 1247 |
| 1220 /* find the host to connect to */ | 1248 /* find the host to connect to */ |
| 1221 if(results) { | 1249 if(results) { |
| 1222 hostname = g_strdup(resp->hostname); | 1250 hostname = g_strdup(resp->hostname); |
| 1223 /* TODO: Should this work more like Jabber where the SRV value will be ignored | 1251 /* TODO: Should this work more like Jabber where the SRV value will be ignored |
| 1253 } | 1281 } |
| 1254 | 1282 |
| 1255 } else { /* UDP */ | 1283 } else { /* UDP */ |
| 1256 gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port); | 1284 gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port); |
| 1257 | 1285 |
| 1258 /** TODO: this should probably be async, right? */ | 1286 gaim_gethostbyname_async(hostname, port, simple_udp_host_resolved, sip); |
| 1259 if (!(h = gethostbyname(hostname))) { | |
| 1260 gaim_connection_error(sip->gc, _("Couldn't resolve host")); | |
| 1261 return; | |
| 1262 } | |
| 1263 | |
| 1264 /* create socket for incoming connections */ | |
| 1265 sip->fd = gaim_network_listen_range(5060, 5160, SOCK_DGRAM); | |
| 1266 | |
| 1267 if(sip->fd == -1) { | |
| 1268 gaim_connection_error(sip->gc, _("Could not create listen socket")); | |
| 1269 return; | |
| 1270 } | |
| 1271 | |
| 1272 sip->listenport = gaim_network_get_port_from_fd(sip->fd); | |
| 1273 sip->listenfd = sip->fd; | |
| 1274 | |
| 1275 sip->listenpa = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_udp_process, sip->gc); | |
| 1276 sip->serveraddr.sin_family = AF_INET; | |
| 1277 sip->serveraddr.sin_port = htons(port); | |
| 1278 | |
| 1279 sip->serveraddr.sin_addr.s_addr = ((struct in_addr*)h->h_addr)->s_addr; | |
| 1280 sip->resendtimeout = gaim_timeout_add(2500, (GSourceFunc)resend_timeout, sip); | |
| 1281 do_register(sip); | |
| 1282 } | 1287 } |
| 1283 } | 1288 } |
| 1284 | 1289 |
| 1285 static void simple_login(GaimAccount *account) | 1290 static void simple_login(GaimAccount *account) |
| 1286 { | 1291 { |
