1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
|
/* $Id$ */
/*
* (C) Copyright 2001-2009 Wojtek Kaniewski <wojtekka@irc.pl>
* Robert J. Woźny <speedy@ziew.org>
* Arkadiusz Miśkiewicz <arekm@pld-linux.org>
* Tomasz Chiliński <chilek@chilan.com>
* Adam Wysocki <gophi@ekg.chmurka.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version
* 2.1 as published by the Free Software Foundation.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*/
/**
* \file resolver.c
*
* \brief Funkcje rozwiązywania nazw
*/
#ifndef _WIN32
# include <sys/wait.h>
# include <netdb.h>
# include <signal.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libgadu.h"
#include "resolver.h"
#include "compat.h"
/** Sposób rozwiązywania nazw serwerów */
static gg_resolver_t gg_global_resolver_type = GG_RESOLVER_DEFAULT;
/** Funkcja rozpoczynająca rozwiązywanie nazwy */
static int (*gg_global_resolver_start)(int *fd, void **private_data, const char *hostname);
/** Funkcja zwalniająca zasoby po rozwiązaniu nazwy */
static void (*gg_global_resolver_cleanup)(void **private_data, int force);
#ifdef GG_CONFIG_HAVE_PTHREAD
#include <pthread.h>
/**
* \internal Funkcja pomocnicza zwalniająca zasoby po rozwiązywaniu nazwy
* w wątku.
*
* \param data Wskaźnik na wskaźnik bufora zaalokowanego w wątku
*/
static void gg_gethostbyname_cleaner(void *data)
{
char **buf_ptr = (char**) data;
if (buf_ptr != NULL) {
free(*buf_ptr);
*buf_ptr = NULL;
}
}
#endif /* GG_CONFIG_HAVE_PTHREAD */
/**
* \internal Odpowiednik \c gethostbyname zapewniający współbieżność.
*
* Jeśli dany system dostarcza \c gethostbyname_r, używa się tej wersji, jeśli
* nie, to zwykłej \c gethostbyname.
*
* \param hostname Nazwa serwera
* \param addr Wskaźnik na rezultat rozwiązywania nazwy
* \param pthread Flaga blokowania unicestwiania wątku podczas alokacji pamięci
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_gethostbyname_real(const char *hostname, struct in_addr *addr, int pthread)
{
#ifdef GG_CONFIG_HAVE_GETHOSTBYNAME_R
char *buf = NULL;
char *new_buf = NULL;
struct hostent he;
struct hostent *he_ptr = NULL;
size_t buf_len = 1024;
int result = -1;
int h_errnop;
int ret = 0;
#ifdef GG_CONFIG_HAVE_PTHREAD
int old_state;
#endif
#ifdef GG_CONFIG_HAVE_PTHREAD
pthread_cleanup_push(gg_gethostbyname_cleaner, &buf);
if (pthread)
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
#endif
buf = malloc(buf_len);
#ifdef GG_CONFIG_HAVE_PTHREAD
if (pthread)
pthread_setcancelstate(old_state, NULL);
#endif
if (buf != NULL) {
#ifndef sun
while ((ret = gethostbyname_r(hostname, &he, buf, buf_len, &he_ptr, &h_errnop)) == ERANGE) {
#else
while (((he_ptr = gethostbyname_r(hostname, &he, buf, buf_len, &h_errnop)) == NULL) && (errno == ERANGE)) {
#endif
buf_len *= 2;
#ifdef GG_CONFIG_HAVE_PTHREAD
if (pthread)
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
#endif
new_buf = realloc(buf, buf_len);
if (new_buf != NULL)
buf = new_buf;
#ifdef GG_CONFIG_HAVE_PTHREAD
if (pthread)
pthread_setcancelstate(old_state, NULL);
#endif
if (new_buf == NULL) {
ret = ENOMEM;
break;
}
}
if (ret == 0 && he_ptr != NULL) {
memcpy(addr, he_ptr->h_addr, sizeof(struct in_addr));
result = 0;
}
#ifdef GG_CONFIG_HAVE_PTHREAD
if (pthread)
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
#endif
free(buf);
buf = NULL;
#ifdef GG_CONFIG_HAVE_PTHREAD
if (pthread)
pthread_setcancelstate(old_state, NULL);
#endif
}
#ifdef GG_CONFIG_HAVE_PTHREAD
pthread_cleanup_pop(1);
#endif
return result;
#else
struct hostent *he;
he = gethostbyname(hostname);
if (he == NULL)
return -1;
memcpy(addr, he->h_addr, sizeof(struct in_addr));
return 0;
#endif /* GG_CONFIG_HAVE_GETHOSTBYNAME_R */
}
/**
* \internal Odpowiednik \c gethostbyname zapewniający współbieżność.
*
* Jeśli dany system dostarcza \c gethostbyname_r, używa się tej wersji, jeśli
* nie, to zwykłej \c gethostbyname.
*
* \param hostname Nazwa serwera
*
* \return Zaalokowana struktura \c in_addr lub NULL w przypadku błędu.
*/
struct in_addr *gg_gethostbyname(const char *hostname)
{
struct in_addr *addr;
if (!(addr = malloc(sizeof(struct in_addr))))
return NULL;
if (gg_gethostbyname_real(hostname, addr, 0)) {
free(addr);
return NULL;
}
return addr;
}
/**
* \internal Struktura przekazywana do wątku rozwiązującego nazwę.
*/
struct gg_resolver_fork_data {
int pid; /*< Identyfikator procesu */
};
#ifdef _WIN32
/**
* Deal with the fact that you can't select() on a win32 file fd.
* This makes it practically impossible to tie into purple's event loop.
*
* -This is thanks to Tor Lillqvist.
* XXX - Move this to where the rest of the the win32 compatiblity stuff goes when we push the changes back to libgadu.
*/
static int
socket_pipe (int *fds)
{
SOCKET temp, socket1 = -1, socket2 = -1;
struct sockaddr_in saddr;
int len;
u_long arg;
fd_set read_set, write_set;
struct timeval tv;
temp = socket(AF_INET, SOCK_STREAM, 0);
if (temp == INVALID_SOCKET) {
goto out0;
}
arg = 1;
if (ioctlsocket(temp, FIONBIO, &arg) == SOCKET_ERROR) {
goto out0;
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = 0;
saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(temp, (struct sockaddr *)&saddr, sizeof (saddr))) {
goto out0;
}
if (listen(temp, 1) == SOCKET_ERROR) {
goto out0;
}
len = sizeof(saddr);
if (getsockname(temp, (struct sockaddr *)&saddr, &len)) {
goto out0;
}
socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (socket1 == INVALID_SOCKET) {
goto out0;
}
arg = 1;
if (ioctlsocket(socket1, FIONBIO, &arg) == SOCKET_ERROR) {
goto out1;
}
if (connect(socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR ||
WSAGetLastError() != WSAEWOULDBLOCK) {
goto out1;
}
FD_ZERO(&read_set);
FD_SET(temp, &read_set);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select(0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR) {
goto out1;
}
if (!FD_ISSET(temp, &read_set)) {
goto out1;
}
socket2 = accept(temp, (struct sockaddr *) &saddr, &len);
if (socket2 == INVALID_SOCKET) {
goto out1;
}
FD_ZERO(&write_set);
FD_SET(socket1, &write_set);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select(0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR) {
goto out2;
}
if (!FD_ISSET(socket1, &write_set)) {
goto out2;
}
arg = 0;
if (ioctlsocket(socket1, FIONBIO, &arg) == SOCKET_ERROR) {
goto out2;
}
arg = 0;
if (ioctlsocket(socket2, FIONBIO, &arg) == SOCKET_ERROR) {
goto out2;
}
fds[0] = socket1;
fds[1] = socket2;
closesocket (temp);
return 0;
out2:
closesocket (socket2);
out1:
closesocket (socket1);
out0:
closesocket (temp);
errno = EIO; /* XXX */
return -1;
}
#endif
#ifdef _WIN32
struct gg_resolve_win32thread_data {
char *hostname;
int fd;
};
static DWORD WINAPI gg_resolve_win32thread_thread(LPVOID arg)
{
struct gg_resolve_win32thread_data *d = arg;
struct in_addr a;
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_thread() host: %s, fd: %i called\n", d->hostname, d->fd);
if ((a.s_addr = inet_addr(d->hostname)) == INADDR_NONE) {
/* W przypadku błędu gg_gethostbyname_real() zwróci -1
* i nie zmieni &addr. Tam jest już INADDR_NONE,
* więc nie musimy robić nic więcej. */
gg_gethostbyname_real(d->hostname, &a, 0);
}
// if ((a.s_addr = inet_addr(d->hostname)) == INADDR_NONE) {
// struct in_addr *hn;
// if (!(hn = gg_gethostbyname(d->hostname)))
// a.s_addr = INADDR_NONE;
// else {
// a.s_addr = hn->s_addr;
// free(hn);
// }
// }
write(d->fd, &a, sizeof(a));
close(d->fd);
free(d->hostname);
d->hostname = NULL;
free(d);
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_thread() done\n");
return 0;
}
static int gg_resolve_win32thread(int *fd, void **resolver, const char *hostname)
{
struct gg_resolve_win32thread_data *d = NULL;
HANDLE h;
DWORD dwTId;
int pipes[2], new_errno;
gg_debug(GG_DEBUG_FUNCTION, "** gg_resolve_win32thread(%p, %p, \"%s\");\n", fd, resolver, hostname);
if (!resolver || !fd || !hostname) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread() invalid arguments\n");
errno = EFAULT;
return -1;
}
if (socket_pipe(pipes) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread() unable to create pipes (errno=%d, %s)\n", errno, strerror(errno));
return -1;
}
if (!(d = malloc(sizeof(*d)))) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread() out of memory\n");
new_errno = errno;
goto cleanup;
}
d->hostname = NULL;
if (!(d->hostname = strdup(hostname))) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread() out of memory\n");
new_errno = errno;
goto cleanup;
}
d->fd = pipes[1];
h = CreateThread(NULL, 0, gg_resolve_win32thread_thread,
d, 0, &dwTId);
if (h == NULL) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread() unable to create thread\n");
new_errno = errno;
goto cleanup;
}
*resolver = h;
*fd = pipes[0];
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread() done\n");
return 0;
cleanup:
if (d) {
free(d->hostname);
free(d);
}
close(pipes[0]);
close(pipes[1]);
errno = new_errno;
return -1;
}
static void gg_resolve_win32thread_cleanup(void **priv_data, int force)
{
struct gg_resolve_win32thread_data *data;
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_cleanup() force: %i called\n", force);
if (priv_data == NULL || *priv_data == NULL)
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_cleanup() priv_data: NULL\n");
return;
data = (struct gg_resolve_win32thread_data*) *priv_data;
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_cleanup() data: %s called\n", data->hostname);
*priv_data = NULL;
if (force) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_cleanup() force called\n", force);
//pthread_cancel(data->thread);
//pthread_join(data->thread, NULL);
}
free(data->hostname);
data->hostname = NULL;
if (data->fd != -1) {
close(data->fd);
data->fd = -1;
}
gg_debug(GG_DEBUG_MISC, "// gg_resolve_win32thread_cleanup() done\n");
free(data);
}
#endif
#ifndef _WIN32
/**
* \internal Rozwiązuje nazwę serwera w osobnym procesie.
*
* Połączenia asynchroniczne nie mogą blokować procesu w trakcie rozwiązywania
* nazwy serwera. W tym celu tworzony jest potok, nowy proces i dopiero w nim
* przeprowadzane jest rozwiązywanie nazwy. Deskryptor strony do odczytu
* zapisuje się w strukturze sieci i czeka na dane w postaci struktury
* \c in_addr. Jeśli nie znaleziono nazwy, zwracana jest \c INADDR_NONE.
*
* \param fd Wskaźnik na zmienną, gdzie zostanie umieszczony deskryptor
* potoku
* \param priv_data Wskaźnik na zmienną, gdzie zostanie umieszczony wskaźnik
* do numeru procesu potomnego rozwiązującego nazwę
* \param hostname Nazwa serwera do rozwiązania
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
static int gg_resolver_fork_start(int *fd, void **priv_data, const char *hostname)
{
struct gg_resolver_fork_data *data = NULL;
struct in_addr addr;
int pipes[2], new_errno;
gg_debug(GG_DEBUG_FUNCTION, "** gg_resolver_fork_start(%p, %p, \"%s\");\n", fd, priv_data, hostname);
if (fd == NULL || priv_data == NULL || hostname == NULL) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_fork_start() invalid arguments\n");
errno = EFAULT;
return -1;
}
data = malloc(sizeof(struct gg_resolver_fork_data));
if (data == NULL) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_fork_start() out of memory for resolver data\n");
return -1;
}
if (pipe(pipes) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_fork_start() unable to create pipes (errno=%d, %s)\n", errno, strerror(errno));
free(data);
return -1;
}
data->pid = fork();
if (data->pid == -1) {
new_errno = errno;
goto cleanup;
}
if (data->pid == 0) {
close(pipes[0]);
if ((addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
/* W przypadku błędu gg_gethostbyname_real() zwróci -1
* i nie zmieni &addr. Tam jest już INADDR_NONE,
* więc nie musimy robić nic więcej. */
gg_gethostbyname_real(hostname, &addr, 0);
}
if (write(pipes[1], &addr, sizeof(addr)) != sizeof(addr))
_exit(1);
_exit(0);
}
close(pipes[1]);
gg_debug(GG_DEBUG_MISC, "// gg_resolver_fork_start() %p\n", data);
*fd = pipes[0];
*priv_data = data;
return 0;
cleanup:
free(data);
close(pipes[0]);
close(pipes[1]);
errno = new_errno;
return -1;
}
/**
* \internal Usuwanie zasobów po procesie rozwiązywaniu nazwy.
*
* Funkcja wywoływana po zakończeniu rozwiązanywania nazwy lub przy zwalnianiu
* zasobów sesji podczas rozwiązywania nazwy.
*
* \param priv_data Wskaźnik na zmienną przechowującą wskaźnik do prywatnych
* danych
* \param force Flaga usuwania zasobów przed zakończeniem działania
*/
static void gg_resolver_fork_cleanup(void **priv_data, int force)
{
struct gg_resolver_fork_data *data;
if (priv_data == NULL || *priv_data == NULL)
return;
data = (struct gg_resolver_fork_data*) *priv_data;
*priv_data = NULL;
if (force)
kill(data->pid, SIGKILL);
waitpid(data->pid, NULL, WNOHANG);
free(data);
}
#endif
#ifdef GG_CONFIG_HAVE_PTHREAD
/**
* \internal Struktura przekazywana do wątku rozwiązującego nazwę.
*/
struct gg_resolver_pthread_data {
pthread_t thread; /*< Identyfikator wątku */
char *hostname; /*< Nazwa serwera */
int rfd; /*< Deskryptor do odczytu */
int wfd; /*< Deskryptor do zapisu */
};
/**
* \internal Usuwanie zasobów po wątku rozwiązywaniu nazwy.
*
* Funkcja wywoływana po zakończeniu rozwiązanywania nazwy lub przy zwalnianiu
* zasobów sesji podczas rozwiązywania nazwy.
*
* \param priv_data Wskaźnik na zmienną przechowującą wskaźnik do prywatnych
* danych
* \param force Flaga usuwania zasobów przed zakończeniem działania
*/
static void gg_resolver_pthread_cleanup(void **priv_data, int force)
{
struct gg_resolver_pthread_data *data;
if (priv_data == NULL || *priv_data == NULL)
return;
data = (struct gg_resolver_pthread_data *) *priv_data;
*priv_data = NULL;
if (force) {
pthread_cancel(data->thread);
pthread_join(data->thread, NULL);
}
free(data->hostname);
data->hostname = NULL;
if (data->wfd != -1) {
close(data->wfd);
data->wfd = -1;
}
free(data);
}
/**
* \internal Wątek rozwiązujący nazwę.
*
* \param arg Wskaźnik na strukturę \c gg_resolver_pthread_data
*/
static void *gg_resolver_pthread_thread(void *arg)
{
struct gg_resolver_pthread_data *data = arg;
struct in_addr addr;
pthread_detach(pthread_self());
if ((addr.s_addr = inet_addr(data->hostname)) == INADDR_NONE) {
/* W przypadku błędu gg_gethostbyname_real() zwróci -1
* i nie zmieni &addr. Tam jest już INADDR_NONE,
* więc nie musimy robić nic więcej. */
gg_gethostbyname_real(data->hostname, &addr, 1);
}
if (write(data->wfd, &addr, sizeof(addr)) == sizeof(addr))
pthread_exit(NULL);
else
pthread_exit((void*) -1);
return NULL; /* żeby kompilator nie marudził */
}
/**
* \internal Rozwiązuje nazwę serwera w osobnym wątku.
*
* Funkcja działa analogicznie do \c gg_resolver_fork_start(), z tą różnicą,
* że działa na wątkach, nie procesach. Jest dostępna wyłącznie gdy podczas
* kompilacji włączono odpowiednią opcję.
*
* \param fd Wskaźnik na zmienną, gdzie zostanie umieszczony deskryptor
* potoku
* \param priv_data Wskaźnik na zmienną, gdzie zostanie umieszczony wskaźnik
* do prywatnych danych wątku rozwiązującego nazwę
* \param hostname Nazwa serwera do rozwiązania
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
static int gg_resolver_pthread_start(int *fd, void **priv_data, const char *hostname)
{
struct gg_resolver_pthread_data *data = NULL;
int pipes[2], new_errno;
gg_debug(GG_DEBUG_FUNCTION, "** gg_resolver_pthread_start(%p, %p, \"%s\");\n", fd, priv_data, hostname);
if (fd == NULL || priv_data == NULL || hostname == NULL) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() invalid arguments\n");
errno = EFAULT;
return -1;
}
data = malloc(sizeof(struct gg_resolver_pthread_data));
if (data == NULL) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() out of memory for resolver data\n");
return -1;
}
if (pipe(pipes) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() unable to create pipes (errno=%d, %s)\n", errno, strerror(errno));
free(data);
return -1;
}
data->hostname = strdup(hostname);
if (data->hostname == NULL) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() out of memory\n");
new_errno = errno;
goto cleanup;
}
data->rfd = pipes[0];
data->wfd = pipes[1];
if (pthread_create(&data->thread, NULL, gg_resolver_pthread_thread, data)) {
gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() unable to create thread\n");
new_errno = errno;
goto cleanup;
}
gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() %p\n", data);
*fd = pipes[0];
*priv_data = data;
return 0;
cleanup:
if (data) {
free(data->hostname);
free(data);
}
close(pipes[0]);
close(pipes[1]);
errno = new_errno;
return -1;
}
#endif /* GG_CONFIG_HAVE_PTHREAD */
/**
* Ustawia sposób rozwiązywania nazw w sesji.
*
* \param gs Struktura sesji
* \param type Sposób rozwiązywania nazw (patrz \ref build-resolver)
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_session_set_resolver(struct gg_session *gs, gg_resolver_t type)
{
if (gs == NULL) {
errno = EINVAL;
return -1;
}
if (type == GG_RESOLVER_DEFAULT) {
if (gg_global_resolver_type != GG_RESOLVER_DEFAULT) {
gs->resolver_type = gg_global_resolver_type;
gs->resolver_start = gg_global_resolver_start;
gs->resolver_cleanup = gg_global_resolver_cleanup;
return 0;
}
#ifdef _WIN32
type = GG_RESOLVER_WIN32;
#else
type = GG_RESOLVER_FORK;
#endif
#if defined(GG_CONFIG_HAVE_PTHREAD) || defined(GG_CONFIG_PTHREAD_DEFAULT)
type = GG_RESOLVER_PTHREAD;
#endif
}
switch (type) {
#ifdef _WIN32
case GG_RESOLVER_WIN32:
gs->resolver_type = type;
gs->resolver_start = gg_resolve_win32thread;
gs->resolver_cleanup = gg_resolve_win32thread_cleanup;
return 0;
#else
case GG_RESOLVER_FORK:
gs->resolver_type = type;
gs->resolver_start = gg_resolver_fork_start;
gs->resolver_cleanup = gg_resolver_fork_cleanup;
return 0;
#endif
#ifdef GG_CONFIG_HAVE_PTHREAD
case GG_RESOLVER_PTHREAD:
gs->resolver_type = type;
gs->resolver_start = gg_resolver_pthread_start;
gs->resolver_cleanup = gg_resolver_pthread_cleanup;
return 0;
#endif
default:
errno = EINVAL;
return -1;
}
}
/**
* Zwraca sposób rozwiązywania nazw w sesji.
*
* \param gs Struktura sesji
*
* \return Sposób rozwiązywania nazw
*/
gg_resolver_t gg_session_get_resolver(struct gg_session *gs)
{
if (gs == NULL) {
errno = EINVAL;
return GG_RESOLVER_INVALID;
}
return gs->resolver_type;
}
/**
* Ustawia własny sposób rozwiązywania nazw w sesji.
*
* \param gs Struktura sesji
* \param resolver_start Funkcja rozpoczynająca rozwiązywanie nazwy
* \param resolver_cleanup Funkcja zwalniająca zasoby
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_session_set_custom_resolver(struct gg_session *gs, int (*resolver_start)(int*, void**, const char*), void (*resolver_cleanup)(void**, int))
{
if (gs == NULL || resolver_start == NULL || resolver_cleanup == NULL) {
errno = EINVAL;
return -1;
}
gs->resolver_type = GG_RESOLVER_CUSTOM;
gs->resolver_start = resolver_start;
gs->resolver_cleanup = resolver_cleanup;
return 0;
}
/**
* Ustawia sposób rozwiązywania nazw połączenia HTTP.
*
* \param gh Struktura połączenia
* \param type Sposób rozwiązywania nazw (patrz \ref build-resolver)
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_http_set_resolver(struct gg_http *gh, gg_resolver_t type)
{
if (gh == NULL) {
errno = EINVAL;
return -1;
}
if (type == GG_RESOLVER_DEFAULT) {
if (gg_global_resolver_type != GG_RESOLVER_DEFAULT) {
gh->resolver_type = gg_global_resolver_type;
gh->resolver_start = gg_global_resolver_start;
gh->resolver_cleanup = gg_global_resolver_cleanup;
return 0;
}
#ifdef _WIN32
type = GG_RESOLVER_WIN32;
#else
type = GG_RESOLVER_FORK;
#endif
#if defined(GG_CONFIG_HAVE_PTHREAD) || defined(GG_CONFIG_PTHREAD_DEFAULT)
type = GG_RESOLVER_PTHREAD;
#endif
}
switch (type) {
#ifdef _WIN32
case GG_RESOLVER_WIN32:
gh->resolver_type = type;
gh->resolver_start = gg_resolve_win32thread;
gh->resolver_cleanup = gg_resolve_win32thread_cleanup;
return 0;
#else
case GG_RESOLVER_FORK:
gh->resolver_type = type;
gh->resolver_start = gg_resolver_fork_start;
gh->resolver_cleanup = gg_resolver_fork_cleanup;
return 0;
#endif
#ifdef GG_CONFIG_HAVE_PTHREAD
case GG_RESOLVER_PTHREAD:
gh->resolver_type = type;
gh->resolver_start = gg_resolver_pthread_start;
gh->resolver_cleanup = gg_resolver_pthread_cleanup;
return 0;
#endif
default:
errno = EINVAL;
return -1;
}
}
/**
* Zwraca sposób rozwiązywania nazw połączenia HTTP.
*
* \param gh Struktura połączenia
*
* \return Sposób rozwiązywania nazw
*/
gg_resolver_t gg_http_get_resolver(struct gg_http *gh)
{
if (gh == NULL) {
errno = EINVAL;
return GG_RESOLVER_INVALID;
}
return gh->resolver_type;
}
/**
* Ustawia własny sposób rozwiązywania nazw połączenia HTTP.
*
* \param gh Struktura sesji
* \param resolver_start Funkcja rozpoczynająca rozwiązywanie nazwy
* \param resolver_cleanup Funkcja zwalniająca zasoby
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_http_set_custom_resolver(struct gg_http *gh, int (*resolver_start)(int*, void**, const char*), void (*resolver_cleanup)(void**, int))
{
if (gh == NULL || resolver_start == NULL || resolver_cleanup == NULL) {
errno = EINVAL;
return -1;
}
gh->resolver_type = GG_RESOLVER_CUSTOM;
gh->resolver_start = resolver_start;
gh->resolver_cleanup = resolver_cleanup;
return 0;
}
/**
* Ustawia sposób rozwiązywania nazw globalnie dla biblioteki.
*
* \param type Sposób rozwiązywania nazw (patrz \ref build-resolver)
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_global_set_resolver(gg_resolver_t type)
{
switch (type) {
case GG_RESOLVER_DEFAULT:
gg_global_resolver_type = type;
gg_global_resolver_start = NULL;
gg_global_resolver_cleanup = NULL;
return 0;
#ifndef _WIN32
case GG_RESOLVER_FORK:
gg_global_resolver_type = type;
gg_global_resolver_start = gg_resolver_fork_start;
gg_global_resolver_cleanup = gg_resolver_fork_cleanup;
return 0;
#endif
#ifdef _WIN32
case GG_RESOLVER_WIN32:
gg_global_resolver_type = type;
gg_global_resolver_start = gg_resolve_win32thread;
gg_global_resolver_cleanup = gg_resolve_win32thread_cleanup;
return 0;
#endif
#ifdef GG_CONFIG_HAVE_PTHREAD
case GG_RESOLVER_PTHREAD:
gg_global_resolver_type = type;
gg_global_resolver_start = gg_resolver_pthread_start;
gg_global_resolver_cleanup = gg_resolver_pthread_cleanup;
return 0;
#endif
default:
errno = EINVAL;
return -1;
}
}
/**
* Zwraca sposób rozwiązywania nazw globalnie dla biblioteki.
*
* \return Sposób rozwiązywania nazw
*/
gg_resolver_t gg_global_get_resolver(void)
{
return gg_global_resolver_type;
}
/**
* Ustawia własny sposób rozwiązywania nazw globalnie dla biblioteki.
*
* \param resolver_start Funkcja rozpoczynająca rozwiązywanie nazwy
* \param resolver_cleanup Funkcja zwalniająca zasoby
*
* Parametry funkcji rozpoczynającej rozwiązywanie nazwy wyglądają następująco:
* - \c "int *fd" — wskaźnik na zmienną, gdzie zostanie umieszczony deskryptor potoku
* - \c "void **priv_data" — wskaźnik na zmienną, gdzie można umieścić wskaźnik do prywatnych danych na potrzeby rozwiązywania nazwy
* - \c "const char *name" — nazwa serwera do rozwiązania
*
* Parametry funkcji zwalniającej zasoby wyglądają następująco:
* - \c "void **priv_data" — wskaźnik na zmienną przechowującą wskaźnik do prywatnych danych, należy go ustawić na \c NULL po zakończeniu
* - \c "int force" — flaga mówiąca o tym, że zasoby są zwalniane przed zakończeniem rozwiązywania nazwy, np. z powodu zamknięcia sesji.
*
* Własny kod rozwiązywania nazwy powinien stworzyć potok, parę gniazd lub
* inny deskryptor pozwalający na co najmniej jednostronną komunikację i
* przekazać go w parametrze \c fd. Po zakończeniu rozwiązywania nazwy,
* powinien wysłać otrzymany adres IP w postaci sieciowej (big-endian) do
* deskryptora. Jeśli rozwiązywanie nazwy się nie powiedzie, należy wysłać
* \c INADDR_NONE. Następnie zostanie wywołana funkcja zwalniająca zasoby
* z parametrem \c force równym \c 0. Gdyby sesja została zakończona przed
* rozwiązaniem nazwy, np. za pomocą funkcji \c gg_logoff(), funkcja
* zwalniająca zasoby zostanie wywołana z parametrem \c force równym \c 1.
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_global_set_custom_resolver(int (*resolver_start)(int*, void**, const char*), void (*resolver_cleanup)(void**, int))
{
if (resolver_start == NULL || resolver_cleanup == NULL) {
errno = EINVAL;
return -1;
}
gg_global_resolver_type = GG_RESOLVER_CUSTOM;
gg_global_resolver_start = resolver_start;
gg_global_resolver_cleanup = resolver_cleanup;
return 0;
}
|