comparison libpurple/network.c @ 15822:32c366eeeb99

sed -ie 's/gaim/purple/g'
author Sean Egan <seanegan@gmail.com>
date Mon, 19 Mar 2007 07:01:17 +0000
parents 552be3958d6a
children 9b875f0ecb86
comparison
equal deleted inserted replaced
15821:84b0f9b23ede 15822:32c366eeeb99
1 /** 1 /**
2 * @file network.c Network Implementation 2 * @file network.c Network Implementation
3 * @ingroup core 3 * @ingroup core
4 * 4 *
5 * gaim 5 * purple
6 * 6 *
7 * Gaim is the legal property of its developers, whose names are too numerous 7 * Purple is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this 8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution. 9 * source distribution.
10 * 10 *
11 * This program is free software; you can redistribute it and/or modify 11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by 12 * it under the terms of the GNU General Public License as published by
69 69
70 #elif defined _WIN32 70 #elif defined _WIN32
71 static int current_network_count; 71 static int current_network_count;
72 #endif 72 #endif
73 73
74 struct _GaimNetworkListenData { 74 struct _PurpleNetworkListenData {
75 int listenfd; 75 int listenfd;
76 int socket_type; 76 int socket_type;
77 gboolean retry; 77 gboolean retry;
78 gboolean adding; 78 gboolean adding;
79 GaimNetworkListenCallback cb; 79 PurpleNetworkListenCallback cb;
80 gpointer cb_data; 80 gpointer cb_data;
81 UPnPMappingAddRemove *mapping_data; 81 UPnPMappingAddRemove *mapping_data;
82 #ifdef ENABLE_NAT_PMP 82 #ifdef ENABLE_NAT_PMP
83 gboolean has_pmp_mapping; 83 gboolean has_pmp_mapping;
84 #endif 84 #endif
87 #ifdef HAVE_LIBNM 87 #ifdef HAVE_LIBNM
88 void nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data); 88 void nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data);
89 #endif 89 #endif
90 90
91 const unsigned char * 91 const unsigned char *
92 gaim_network_ip_atoi(const char *ip) 92 purple_network_ip_atoi(const char *ip)
93 { 93 {
94 static unsigned char ret[4]; 94 static unsigned char ret[4];
95 gchar *delimiter = "."; 95 gchar *delimiter = ".";
96 gchar **split; 96 gchar **split;
97 int i; 97 int i;
109 109
110 return ret; 110 return ret;
111 } 111 }
112 112
113 void 113 void
114 gaim_network_set_public_ip(const char *ip) 114 purple_network_set_public_ip(const char *ip)
115 { 115 {
116 g_return_if_fail(ip != NULL); 116 g_return_if_fail(ip != NULL);
117 117
118 /* XXX - Ensure the IP address is valid */ 118 /* XXX - Ensure the IP address is valid */
119 119
120 gaim_prefs_set_string("/core/network/public_ip", ip); 120 purple_prefs_set_string("/core/network/public_ip", ip);
121 } 121 }
122 122
123 const char * 123 const char *
124 gaim_network_get_public_ip(void) 124 purple_network_get_public_ip(void)
125 { 125 {
126 return gaim_prefs_get_string("/core/network/public_ip"); 126 return purple_prefs_get_string("/core/network/public_ip");
127 } 127 }
128 128
129 const char * 129 const char *
130 gaim_network_get_local_system_ip(int fd) 130 purple_network_get_local_system_ip(int fd)
131 { 131 {
132 char buffer[1024]; 132 char buffer[1024];
133 static char ip[16]; 133 static char ip[16];
134 char *tmp; 134 char *tmp;
135 struct ifconf ifc; 135 struct ifconf ifc;
174 174
175 return "0.0.0.0"; 175 return "0.0.0.0";
176 } 176 }
177 177
178 const char * 178 const char *
179 gaim_network_get_my_ip(int fd) 179 purple_network_get_my_ip(int fd)
180 { 180 {
181 const char *ip = NULL; 181 const char *ip = NULL;
182 GaimStunNatDiscovery *stun; 182 PurpleStunNatDiscovery *stun;
183 183
184 /* Check if the user specified an IP manually */ 184 /* Check if the user specified an IP manually */
185 if (!gaim_prefs_get_bool("/core/network/auto_ip")) { 185 if (!purple_prefs_get_bool("/core/network/auto_ip")) {
186 ip = gaim_network_get_public_ip(); 186 ip = purple_network_get_public_ip();
187 /* Make sure the IP address entered by the user is valid */ 187 /* Make sure the IP address entered by the user is valid */
188 if ((ip != NULL) && (gaim_network_ip_atoi(ip) != NULL)) 188 if ((ip != NULL) && (purple_network_ip_atoi(ip) != NULL))
189 return ip; 189 return ip;
190 } 190 }
191 191
192 /* Check if STUN discovery was already done */ 192 /* Check if STUN discovery was already done */
193 stun = gaim_stun_discover(NULL); 193 stun = purple_stun_discover(NULL);
194 if ((stun != NULL) && (stun->status == GAIM_STUN_STATUS_DISCOVERED)) 194 if ((stun != NULL) && (stun->status == PURPLE_STUN_STATUS_DISCOVERED))
195 return stun->publicip; 195 return stun->publicip;
196 196
197 /* Attempt to get the IP from a NAT device using UPnP */ 197 /* Attempt to get the IP from a NAT device using UPnP */
198 ip = gaim_upnp_get_public_ip(); 198 ip = purple_upnp_get_public_ip();
199 if (ip != NULL) 199 if (ip != NULL)
200 return ip; 200 return ip;
201 201
202 #ifdef ENABLE_NAT_PMP 202 #ifdef ENABLE_NAT_PMP
203 /* Attempt to ge tthe IP from a NAT device using NAT-PMP */ 203 /* Attempt to ge tthe IP from a NAT device using NAT-PMP */
204 ip = gaim_pmp_get_public_ip(); 204 ip = purple_pmp_get_public_ip();
205 if (ip != NULL) 205 if (ip != NULL)
206 return ip; 206 return ip;
207 #endif 207 #endif
208 208
209 /* Just fetch the IP of the local system */ 209 /* Just fetch the IP of the local system */
210 return gaim_network_get_local_system_ip(fd); 210 return purple_network_get_local_system_ip(fd);
211 } 211 }
212 212
213 213
214 static void 214 static void
215 gaim_network_set_upnp_port_mapping_cb(gboolean success, gpointer data) 215 purple_network_set_upnp_port_mapping_cb(gboolean success, gpointer data)
216 { 216 {
217 GaimNetworkListenData *listen_data; 217 PurpleNetworkListenData *listen_data;
218 218
219 listen_data = data; 219 listen_data = data;
220 /* TODO: Once we're keeping track of upnp requests... */ 220 /* TODO: Once we're keeping track of upnp requests... */
221 /* listen_data->pnp_data = NULL; */ 221 /* listen_data->pnp_data = NULL; */
222 222
223 if (!success) { 223 if (!success) {
224 gaim_debug_info("network", "Couldn't create UPnP mapping\n"); 224 purple_debug_info("network", "Couldn't create UPnP mapping\n");
225 if (listen_data->retry) { 225 if (listen_data->retry) {
226 listen_data->retry = FALSE; 226 listen_data->retry = FALSE;
227 listen_data->adding = FALSE; 227 listen_data->adding = FALSE;
228 listen_data->mapping_data = gaim_upnp_remove_port_mapping( 228 listen_data->mapping_data = purple_upnp_remove_port_mapping(
229 gaim_network_get_port_from_fd(listen_data->listenfd), 229 purple_network_get_port_from_fd(listen_data->listenfd),
230 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP", 230 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
231 gaim_network_set_upnp_port_mapping_cb, listen_data); 231 purple_network_set_upnp_port_mapping_cb, listen_data);
232 return; 232 return;
233 } 233 }
234 } else if (!listen_data->adding) { 234 } else if (!listen_data->adding) {
235 /* We've tried successfully to remove the port mapping. 235 /* We've tried successfully to remove the port mapping.
236 * Try to add it again */ 236 * Try to add it again */
237 listen_data->adding = TRUE; 237 listen_data->adding = TRUE;
238 listen_data->mapping_data = gaim_upnp_set_port_mapping( 238 listen_data->mapping_data = purple_upnp_set_port_mapping(
239 gaim_network_get_port_from_fd(listen_data->listenfd), 239 purple_network_get_port_from_fd(listen_data->listenfd),
240 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP", 240 (listen_data->socket_type == SOCK_STREAM) ? "TCP" : "UDP",
241 gaim_network_set_upnp_port_mapping_cb, listen_data); 241 purple_network_set_upnp_port_mapping_cb, listen_data);
242 return; 242 return;
243 } 243 }
244 244
245 if (listen_data->cb) 245 if (listen_data->cb)
246 listen_data->cb(listen_data->listenfd, listen_data->cb_data); 246 listen_data->cb(listen_data->listenfd, listen_data->cb_data);
247 247
248 /* Clear the UPnP mapping data, since it's complete and gaim_netweork_listen_cancel() will try to cancel 248 /* Clear the UPnP mapping data, since it's complete and purple_netweork_listen_cancel() will try to cancel
249 * it otherwise. */ 249 * it otherwise. */
250 listen_data->mapping_data = NULL; 250 listen_data->mapping_data = NULL;
251 gaim_network_listen_cancel(listen_data); 251 purple_network_listen_cancel(listen_data);
252 } 252 }
253 253
254 254
255 static GaimNetworkListenData * 255 static PurpleNetworkListenData *
256 gaim_network_do_listen(unsigned short port, int socket_type, GaimNetworkListenCallback cb, gpointer cb_data) 256 purple_network_do_listen(unsigned short port, int socket_type, PurpleNetworkListenCallback cb, gpointer cb_data)
257 { 257 {
258 int listenfd = -1; 258 int listenfd = -1;
259 const int on = 1; 259 const int on = 1;
260 GaimNetworkListenData *listen_data; 260 PurpleNetworkListenData *listen_data;
261 unsigned short actual_port; 261 unsigned short actual_port;
262 #ifdef HAVE_GETADDRINFO 262 #ifdef HAVE_GETADDRINFO
263 int errnum; 263 int errnum;
264 struct addrinfo hints, *res, *next; 264 struct addrinfo hints, *res, *next;
265 char serv[6]; 265 char serv[6];
273 hints.ai_family = AF_UNSPEC; 273 hints.ai_family = AF_UNSPEC;
274 hints.ai_socktype = socket_type; 274 hints.ai_socktype = socket_type;
275 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res); 275 errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res);
276 if (errnum != 0) { 276 if (errnum != 0) {
277 #ifndef _WIN32 277 #ifndef _WIN32
278 gaim_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum)); 278 purple_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum));
279 if (errnum == EAI_SYSTEM) 279 if (errnum == EAI_SYSTEM)
280 gaim_debug_warning("network", "getaddrinfo: system error: %s\n", strerror(errno)); 280 purple_debug_warning("network", "getaddrinfo: system error: %s\n", strerror(errno));
281 #else 281 #else
282 gaim_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum); 282 purple_debug_warning("network", "getaddrinfo: Error Code = %d\n", errnum);
283 #endif 283 #endif
284 return NULL; 284 return NULL;
285 } 285 }
286 286
287 /* 287 /*
292 for (next = res; next != NULL; next = next->ai_next) { 292 for (next = res; next != NULL; next = next->ai_next) {
293 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol); 293 listenfd = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
294 if (listenfd < 0) 294 if (listenfd < 0)
295 continue; 295 continue;
296 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) 296 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
297 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); 297 purple_debug_warning("network", "setsockopt: %s\n", strerror(errno));
298 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0) 298 if (bind(listenfd, next->ai_addr, next->ai_addrlen) == 0)
299 break; /* success */ 299 break; /* success */
300 /* XXX - It is unclear to me (datallah) whether we need to be 300 /* XXX - It is unclear to me (datallah) whether we need to be
301 using a new socket each time */ 301 using a new socket each time */
302 close(listenfd); 302 close(listenfd);
308 return NULL; 308 return NULL;
309 #else 309 #else
310 struct sockaddr_in sockin; 310 struct sockaddr_in sockin;
311 311
312 if ((listenfd = socket(AF_INET, socket_type, 0)) < 0) { 312 if ((listenfd = socket(AF_INET, socket_type, 0)) < 0) {
313 gaim_debug_warning("network", "socket: %s\n", strerror(errno)); 313 purple_debug_warning("network", "socket: %s\n", strerror(errno));
314 return NULL; 314 return NULL;
315 } 315 }
316 316
317 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) 317 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
318 gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno)); 318 purple_debug_warning("network", "setsockopt: %s\n", strerror(errno));
319 319
320 memset(&sockin, 0, sizeof(struct sockaddr_in)); 320 memset(&sockin, 0, sizeof(struct sockaddr_in));
321 sockin.sin_family = PF_INET; 321 sockin.sin_family = PF_INET;
322 sockin.sin_port = htons(port); 322 sockin.sin_port = htons(port);
323 323
324 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { 324 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
325 gaim_debug_warning("network", "bind: %s\n", strerror(errno)); 325 purple_debug_warning("network", "bind: %s\n", strerror(errno));
326 close(listenfd); 326 close(listenfd);
327 return NULL; 327 return NULL;
328 } 328 }
329 #endif 329 #endif
330 330
331 if (socket_type == SOCK_STREAM && listen(listenfd, 4) != 0) { 331 if (socket_type == SOCK_STREAM && listen(listenfd, 4) != 0) {
332 gaim_debug_warning("network", "listen: %s\n", strerror(errno)); 332 purple_debug_warning("network", "listen: %s\n", strerror(errno));
333 close(listenfd); 333 close(listenfd);
334 return NULL; 334 return NULL;
335 } 335 }
336 fcntl(listenfd, F_SETFL, O_NONBLOCK); 336 fcntl(listenfd, F_SETFL, O_NONBLOCK);
337 337
338 actual_port = gaim_network_get_port_from_fd(listenfd); 338 actual_port = purple_network_get_port_from_fd(listenfd);
339 339
340 gaim_debug_info("network", "Listening on port: %hu\n", actual_port); 340 purple_debug_info("network", "Listening on port: %hu\n", actual_port);
341 341
342 listen_data = g_new0(GaimNetworkListenData, 1); 342 listen_data = g_new0(PurpleNetworkListenData, 1);
343 listen_data->listenfd = listenfd; 343 listen_data->listenfd = listenfd;
344 listen_data->adding = TRUE; 344 listen_data->adding = TRUE;
345 listen_data->retry = TRUE; 345 listen_data->retry = TRUE;
346 listen_data->cb = cb; 346 listen_data->cb = cb;
347 listen_data->cb_data = cb_data; 347 listen_data->cb_data = cb_data;
348 348
349 #ifdef ENABLE_NAT_PMP 349 #ifdef ENABLE_NAT_PMP
350 /* Attempt a NAT-PMP Mapping, which will return immediately */ 350 /* Attempt a NAT-PMP Mapping, which will return immediately */
351 listen_data->has_pmp_mapping = (gaim_pmp_create_map(((socket_type == SOCK_STREAM) ? GAIM_PMP_TYPE_TCP : GAIM_PMP_TYPE_UDP), 351 listen_data->has_pmp_mapping = (purple_pmp_create_map(((socket_type == SOCK_STREAM) ? PURPLE_PMP_TYPE_TCP : PURPLE_PMP_TYPE_UDP),
352 actual_port, actual_port, GAIM_PMP_LIFETIME) != NULL); 352 actual_port, actual_port, PURPLE_PMP_LIFETIME) != NULL);
353 #endif 353 #endif
354 354
355 /* Attempt a UPnP Mapping */ 355 /* Attempt a UPnP Mapping */
356 listen_data->mapping_data = gaim_upnp_set_port_mapping( 356 listen_data->mapping_data = purple_upnp_set_port_mapping(
357 actual_port, 357 actual_port,
358 (socket_type == SOCK_STREAM) ? "TCP" : "UDP", 358 (socket_type == SOCK_STREAM) ? "TCP" : "UDP",
359 gaim_network_set_upnp_port_mapping_cb, listen_data); 359 purple_network_set_upnp_port_mapping_cb, listen_data);
360 360
361 return listen_data; 361 return listen_data;
362 } 362 }
363 363
364 GaimNetworkListenData * 364 PurpleNetworkListenData *
365 gaim_network_listen(unsigned short port, int socket_type, 365 purple_network_listen(unsigned short port, int socket_type,
366 GaimNetworkListenCallback cb, gpointer cb_data) 366 PurpleNetworkListenCallback cb, gpointer cb_data)
367 { 367 {
368 g_return_val_if_fail(port != 0, NULL); 368 g_return_val_if_fail(port != 0, NULL);
369 369
370 return gaim_network_do_listen(port, socket_type, cb, cb_data); 370 return purple_network_do_listen(port, socket_type, cb, cb_data);
371 } 371 }
372 372
373 GaimNetworkListenData * 373 PurpleNetworkListenData *
374 gaim_network_listen_range(unsigned short start, unsigned short end, 374 purple_network_listen_range(unsigned short start, unsigned short end,
375 int socket_type, GaimNetworkListenCallback cb, gpointer cb_data) 375 int socket_type, PurpleNetworkListenCallback cb, gpointer cb_data)
376 { 376 {
377 GaimNetworkListenData *ret = NULL; 377 PurpleNetworkListenData *ret = NULL;
378 378
379 if (gaim_prefs_get_bool("/core/network/ports_range_use")) { 379 if (purple_prefs_get_bool("/core/network/ports_range_use")) {
380 start = gaim_prefs_get_int("/core/network/ports_range_start"); 380 start = purple_prefs_get_int("/core/network/ports_range_start");
381 end = gaim_prefs_get_int("/core/network/ports_range_end"); 381 end = purple_prefs_get_int("/core/network/ports_range_end");
382 } else { 382 } else {
383 if (end < start) 383 if (end < start)
384 end = start; 384 end = start;
385 } 385 }
386 386
387 for (; start <= end; start++) { 387 for (; start <= end; start++) {
388 ret = gaim_network_do_listen(start, socket_type, cb, cb_data); 388 ret = purple_network_do_listen(start, socket_type, cb, cb_data);
389 if (ret != NULL) 389 if (ret != NULL)
390 break; 390 break;
391 } 391 }
392 392
393 return ret; 393 return ret;
394 } 394 }
395 395
396 void gaim_network_listen_cancel(GaimNetworkListenData *listen_data) 396 void purple_network_listen_cancel(PurpleNetworkListenData *listen_data)
397 { 397 {
398 if (listen_data->mapping_data != NULL) 398 if (listen_data->mapping_data != NULL)
399 gaim_upnp_cancel_port_mapping(listen_data->mapping_data); 399 purple_upnp_cancel_port_mapping(listen_data->mapping_data);
400 400
401 #ifdef ENABLE_NAT_PMP 401 #ifdef ENABLE_NAT_PMP
402 if (listen_data->has_pmp_mapping) 402 if (listen_data->has_pmp_mapping)
403 gaim_pmp_destroy_map(((listen_data->socket_type == SOCK_STREAM) ? GAIM_PMP_TYPE_TCP : GAIM_PMP_TYPE_UDP), 403 purple_pmp_destroy_map(((listen_data->socket_type == SOCK_STREAM) ? PURPLE_PMP_TYPE_TCP : PURPLE_PMP_TYPE_UDP),
404 gaim_network_get_port_from_fd(listen_data->listenfd)); 404 purple_network_get_port_from_fd(listen_data->listenfd));
405 #endif 405 #endif
406 406
407 g_free(listen_data); 407 g_free(listen_data);
408 } 408 }
409 409
410 unsigned short 410 unsigned short
411 gaim_network_get_port_from_fd(int fd) 411 purple_network_get_port_from_fd(int fd)
412 { 412 {
413 struct sockaddr_in addr; 413 struct sockaddr_in addr;
414 socklen_t len; 414 socklen_t len;
415 415
416 g_return_val_if_fail(fd >= 0, 0); 416 g_return_val_if_fail(fd >= 0, 0);
417 417
418 len = sizeof(addr); 418 len = sizeof(addr);
419 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) { 419 if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
420 gaim_debug_warning("network", "getsockname: %s\n", strerror(errno)); 420 purple_debug_warning("network", "getsockname: %s\n", strerror(errno));
421 return 0; 421 return 0;
422 } 422 }
423 423
424 return ntohs(addr.sin_port); 424 return ntohs(addr.sin_port);
425 } 425 }
427 #ifdef _WIN32 427 #ifdef _WIN32
428 #ifndef NS_NLA 428 #ifndef NS_NLA
429 #define NS_NLA 15 429 #define NS_NLA 15
430 #endif 430 #endif
431 static gint 431 static gint
432 wgaim_get_connected_network_count(void) 432 wpurple_get_connected_network_count(void)
433 { 433 {
434 guint net_cnt = 0; 434 guint net_cnt = 0;
435 435
436 WSAQUERYSET qs; 436 WSAQUERYSET qs;
437 HANDLE h; 437 HANDLE h;
445 retval = WSALookupServiceBegin(&qs, LUP_RETURN_ALL, &h); 445 retval = WSALookupServiceBegin(&qs, LUP_RETURN_ALL, &h);
446 if (retval != ERROR_SUCCESS) { 446 if (retval != ERROR_SUCCESS) {
447 gchar *msg; 447 gchar *msg;
448 errorid = WSAGetLastError(); 448 errorid = WSAGetLastError();
449 msg = g_win32_error_message(errorid); 449 msg = g_win32_error_message(errorid);
450 gaim_debug_warning("network", "Couldn't retrieve NLA SP lookup handle. " 450 purple_debug_warning("network", "Couldn't retrieve NLA SP lookup handle. "
451 "NLA service is probably not running. Message: %s (%d).\n", 451 "NLA service is probably not running. Message: %s (%d).\n",
452 msg, errorid); 452 msg, errorid);
453 g_free(msg); 453 g_free(msg);
454 454
455 return -1; 455 return -1;
457 char buf[4096]; 457 char buf[4096];
458 WSAQUERYSET *res = (LPWSAQUERYSET) buf; 458 WSAQUERYSET *res = (LPWSAQUERYSET) buf;
459 DWORD size = sizeof(buf); 459 DWORD size = sizeof(buf);
460 while ((retval = WSALookupServiceNext(h, 0, &size, res)) == ERROR_SUCCESS) { 460 while ((retval = WSALookupServiceNext(h, 0, &size, res)) == ERROR_SUCCESS) {
461 net_cnt++; 461 net_cnt++;
462 gaim_debug_info("network", "found network '%s'\n", 462 purple_debug_info("network", "found network '%s'\n",
463 res->lpszServiceInstanceName ? res->lpszServiceInstanceName : "(NULL)"); 463 res->lpszServiceInstanceName ? res->lpszServiceInstanceName : "(NULL)");
464 size = sizeof(buf); 464 size = sizeof(buf);
465 } 465 }
466 466
467 errorid = WSAGetLastError(); 467 errorid = WSAGetLastError();
468 if (!(errorid == WSA_E_NO_MORE || errorid == WSAENOMORE)) { 468 if (!(errorid == WSA_E_NO_MORE || errorid == WSAENOMORE)) {
469 gchar *msg = g_win32_error_message(errorid); 469 gchar *msg = g_win32_error_message(errorid);
470 gaim_debug_error("network", "got unexpected NLA response %s (%d)\n", msg, errorid); 470 purple_debug_error("network", "got unexpected NLA response %s (%d)\n", msg, errorid);
471 g_free(msg); 471 g_free(msg);
472 472
473 net_cnt = -1; 473 net_cnt = -1;
474 } 474 }
475 475
478 478
479 return net_cnt; 479 return net_cnt;
480 480
481 } 481 }
482 482
483 static gboolean wgaim_network_change_thread_cb(gpointer data) 483 static gboolean wpurple_network_change_thread_cb(gpointer data)
484 { 484 {
485 gint new_count; 485 gint new_count;
486 GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops(); 486 PurpleConnectionUiOps *ui_ops = purple_connections_get_ui_ops();
487 487
488 new_count = wgaim_get_connected_network_count(); 488 new_count = wpurple_get_connected_network_count();
489 489
490 if (new_count < 0) 490 if (new_count < 0)
491 return FALSE; 491 return FALSE;
492 492
493 gaim_debug_info("network", "Received Network Change Notification. Current network count is %d, previous count was %d.\n", new_count, current_network_count); 493 purple_debug_info("network", "Received Network Change Notification. Current network count is %d, previous count was %d.\n", new_count, current_network_count);
494 494
495 if (new_count > 0 && ui_ops != NULL && ui_ops->network_connected != NULL) { 495 if (new_count > 0 && ui_ops != NULL && ui_ops->network_connected != NULL) {
496 ui_ops->network_connected(); 496 ui_ops->network_connected();
497 } else if (new_count == 0 && current_network_count > 0 && 497 } else if (new_count == 0 && current_network_count > 0 &&
498 ui_ops != NULL && ui_ops->network_disconnected != NULL) { 498 ui_ops != NULL && ui_ops->network_disconnected != NULL) {
502 current_network_count = new_count; 502 current_network_count = new_count;
503 503
504 return FALSE; 504 return FALSE;
505 } 505 }
506 506
507 static gpointer wgaim_network_change_thread(gpointer data) 507 static gpointer wpurple_network_change_thread(gpointer data)
508 { 508 {
509 HANDLE h; 509 HANDLE h;
510 WSAQUERYSET qs; 510 WSAQUERYSET qs;
511 time_t last_trigger = time(NULL); 511 time_t last_trigger = time(NULL);
512 512
513 int WSAAPI (*MyWSANSPIoctl) ( 513 int WSAAPI (*MyWSANSPIoctl) (
514 HANDLE hLookup, DWORD dwControlCode, LPVOID lpvInBuffer, 514 HANDLE hLookup, DWORD dwControlCode, LPVOID lpvInBuffer,
515 DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer, 515 DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer,
516 LPDWORD lpcbBytesReturned, LPWSACOMPLETION lpCompletion) = NULL; 516 LPDWORD lpcbBytesReturned, LPWSACOMPLETION lpCompletion) = NULL;
517 517
518 if (!(MyWSANSPIoctl = (void*) wgaim_find_and_loadproc("ws2_32.dll", "WSANSPIoctl"))) { 518 if (!(MyWSANSPIoctl = (void*) wpurple_find_and_loadproc("ws2_32.dll", "WSANSPIoctl"))) {
519 g_thread_exit(NULL); 519 g_thread_exit(NULL);
520 return NULL; 520 return NULL;
521 } 521 }
522 522
523 while (TRUE) { 523 while (TRUE) {
528 qs.dwSize = sizeof(WSAQUERYSET); 528 qs.dwSize = sizeof(WSAQUERYSET);
529 qs.dwNameSpace = NS_NLA; 529 qs.dwNameSpace = NS_NLA;
530 if (WSALookupServiceBegin(&qs, 0, &h) == SOCKET_ERROR) { 530 if (WSALookupServiceBegin(&qs, 0, &h) == SOCKET_ERROR) {
531 int errorid = WSAGetLastError(); 531 int errorid = WSAGetLastError();
532 gchar *msg = g_win32_error_message(errorid); 532 gchar *msg = g_win32_error_message(errorid);
533 gaim_debug_warning("network", "Couldn't retrieve NLA SP lookup handle. " 533 purple_debug_warning("network", "Couldn't retrieve NLA SP lookup handle. "
534 "NLA service is probably not running. Message: %s (%d).\n", 534 "NLA service is probably not running. Message: %s (%d).\n",
535 msg, errorid); 535 msg, errorid);
536 g_free(msg); 536 g_free(msg);
537 g_thread_exit(NULL); 537 g_thread_exit(NULL);
538 return NULL; 538 return NULL;
547 547
548 /* This will block until there is a network change */ 548 /* This will block until there is a network change */
549 if (MyWSANSPIoctl(h, SIO_NSP_NOTIFY_CHANGE, NULL, 0, NULL, 0, &retLen, NULL) == SOCKET_ERROR) { 549 if (MyWSANSPIoctl(h, SIO_NSP_NOTIFY_CHANGE, NULL, 0, NULL, 0, &retLen, NULL) == SOCKET_ERROR) {
550 int errorid = WSAGetLastError(); 550 int errorid = WSAGetLastError();
551 gchar *msg = g_win32_error_message(errorid); 551 gchar *msg = g_win32_error_message(errorid);
552 gaim_debug_warning("network", "Unable to wait for changes. Message: %s (%d).\n", 552 purple_debug_warning("network", "Unable to wait for changes. Message: %s (%d).\n",
553 msg, errorid); 553 msg, errorid);
554 g_free(msg); 554 g_free(msg);
555 } 555 }
556 556
557 retval = WSALookupServiceEnd(h); 557 retval = WSALookupServiceEnd(h);
558 558
559 g_idle_add(wgaim_network_change_thread_cb, NULL); 559 g_idle_add(wpurple_network_change_thread_cb, NULL);
560 560
561 } 561 }
562 562
563 g_thread_exit(NULL); 563 g_thread_exit(NULL);
564 return NULL; 564 return NULL;
565 } 565 }
566 #endif 566 #endif
567 567
568 gboolean 568 gboolean
569 gaim_network_is_available(void) 569 purple_network_is_available(void)
570 { 570 {
571 #ifdef HAVE_LIBNM 571 #ifdef HAVE_LIBNM
572 /* Try NetworkManager first, maybe we'll get lucky */ 572 /* Try NetworkManager first, maybe we'll get lucky */
573 int libnm_retval = -1; 573 int libnm_retval = -1;
574 574
575 if (nm_context) 575 if (nm_context)
576 { 576 {
577 if ((libnm_retval = libnm_glib_get_network_state(nm_context)) == LIBNM_NO_NETWORK_CONNECTION) 577 if ((libnm_retval = libnm_glib_get_network_state(nm_context)) == LIBNM_NO_NETWORK_CONNECTION)
578 { 578 {
579 gaim_debug_warning("network", "NetworkManager not active or reports no connection (retval = %i)\n", libnm_retval); 579 purple_debug_warning("network", "NetworkManager not active or reports no connection (retval = %i)\n", libnm_retval);
580 return FALSE; 580 return FALSE;
581 } 581 }
582 if (libnm_retval == LIBNM_ACTIVE_NETWORK_CONNECTION) return TRUE; 582 if (libnm_retval == LIBNM_ACTIVE_NETWORK_CONNECTION) return TRUE;
583 } 583 }
584 #elif defined _WIN32 584 #elif defined _WIN32
590 #ifdef HAVE_LIBNM 590 #ifdef HAVE_LIBNM
591 void 591 void
592 nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data) 592 nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data)
593 { 593 {
594 GList *l; 594 GList *l;
595 GaimAccount *account; 595 PurpleAccount *account;
596 static libnm_glib_state prev = LIBNM_NO_DBUS; 596 static libnm_glib_state prev = LIBNM_NO_DBUS;
597 libnm_glib_state current; 597 libnm_glib_state current;
598 GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops(); 598 PurpleConnectionUiOps *ui_ops = purple_connections_get_ui_ops();
599 599
600 current = libnm_glib_get_network_state(ctx); 600 current = libnm_glib_get_network_state(ctx);
601 gaim_debug_info("network","Entering nm_callback_func!\n"); 601 purple_debug_info("network","Entering nm_callback_func!\n");
602 602
603 switch(current) 603 switch(current)
604 { 604 {
605 case LIBNM_ACTIVE_NETWORK_CONNECTION: 605 case LIBNM_ACTIVE_NETWORK_CONNECTION:
606 /* Call res_init in case DNS servers have changed */ 606 /* Call res_init in case DNS servers have changed */
624 } 624 }
625 } 625 }
626 #endif 626 #endif
627 627
628 void 628 void
629 gaim_network_init(void) 629 purple_network_init(void)
630 { 630 {
631 #ifdef _WIN32 631 #ifdef _WIN32
632 GError *err = NULL; 632 GError *err = NULL;
633 gint cnt = wgaim_get_connected_network_count(); 633 gint cnt = wpurple_get_connected_network_count();
634 634
635 if (cnt < 0) /* Assume there is a network */ 635 if (cnt < 0) /* Assume there is a network */
636 current_network_count = 1; 636 current_network_count = 1;
637 /* Don't listen for network changes if we can't tell anyway */ 637 /* Don't listen for network changes if we can't tell anyway */
638 else 638 else
639 { 639 {
640 current_network_count = cnt; 640 current_network_count = cnt;
641 if (!g_thread_create(wgaim_network_change_thread, NULL, FALSE, &err)) 641 if (!g_thread_create(wpurple_network_change_thread, NULL, FALSE, &err))
642 gaim_debug_error("network", "Couldn't create Network Monitor thread: %s\n", err ? err->message : ""); 642 purple_debug_error("network", "Couldn't create Network Monitor thread: %s\n", err ? err->message : "");
643 } 643 }
644 #endif 644 #endif
645 645
646 gaim_prefs_add_none ("/core/network"); 646 purple_prefs_add_none ("/core/network");
647 gaim_prefs_add_bool ("/core/network/auto_ip", TRUE); 647 purple_prefs_add_bool ("/core/network/auto_ip", TRUE);
648 gaim_prefs_add_string("/core/network/public_ip", ""); 648 purple_prefs_add_string("/core/network/public_ip", "");
649 gaim_prefs_add_bool ("/core/network/ports_range_use", FALSE); 649 purple_prefs_add_bool ("/core/network/ports_range_use", FALSE);
650 gaim_prefs_add_int ("/core/network/ports_range_start", 1024); 650 purple_prefs_add_int ("/core/network/ports_range_start", 1024);
651 gaim_prefs_add_int ("/core/network/ports_range_end", 2048); 651 purple_prefs_add_int ("/core/network/ports_range_end", 2048);
652 652
653 gaim_upnp_discover(NULL, NULL); 653 purple_upnp_discover(NULL, NULL);
654 654
655 #ifdef HAVE_LIBNM 655 #ifdef HAVE_LIBNM
656 nm_context = libnm_glib_init(); 656 nm_context = libnm_glib_init();
657 if(nm_context) 657 if(nm_context)
658 nm_callback_idx = libnm_glib_register_callback(nm_context, nm_callback_func, NULL, g_main_context_default()); 658 nm_callback_idx = libnm_glib_register_callback(nm_context, nm_callback_func, NULL, g_main_context_default());
659 #endif 659 #endif
660 } 660 }
661 661
662 void 662 void
663 gaim_network_uninit(void) 663 purple_network_uninit(void)
664 { 664 {
665 #ifdef HAVE_LIBNM 665 #ifdef HAVE_LIBNM
666 /* FIXME: If anyone can think of a more clever way to shut down libnm without 666 /* FIXME: If anyone can think of a more clever way to shut down libnm without
667 * using a global variable + this function, please do. */ 667 * using a global variable + this function, please do. */
668 if(nm_context && nm_callback_idx) 668 if(nm_context && nm_callback_idx)