annotate src/protocols/gg/lib/common.c @ 11360:cf15c1cdcfbd

[gaim-migrate @ 13582] New Gadu-Gadu implementation. committer: Tailor Script <tailor@pidgin.im>
author Bartoz Oler <bartosz@pidgin.im>
date Sun, 28 Aug 2005 22:46:01 +0000
parents
children 3c536224f0d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11360
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
1 /* $Id: common.c 13582 2005-08-28 22:46:01Z boler $ */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
2
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
3 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
4 * (C) Copyright 2001-2002 Wojtek Kaniewski <wojtekka@irc.pl>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
5 * Robert J. Woźny <speedy@ziew.org>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
6 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
8 * it under the terms of the GNU Lesser General Public License Version
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
9 * 2.1 as published by the Free Software Foundation.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
10 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
14 * GNU Lesser General Public License for more details.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
15 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
17 * License along with this program; if not, write to the Free Software
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
19 * USA.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
20 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
21
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
22 #include <sys/types.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
23 #include <sys/ioctl.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
24 #include <sys/socket.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
25 #include <netinet/in.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
26 #include <arpa/inet.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
27 #ifdef sun
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
28 # include <sys/filio.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
29 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
30
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
31 #include <errno.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
32 #include <fcntl.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
33 #include <netdb.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
34 #include <stdarg.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
35 #include <stdio.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
36 #include <stdlib.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
37 #include <string.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
38 #include <unistd.h>
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
39
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
40 #include "libgadu.h"
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
41
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
42 FILE *gg_debug_file = NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
43
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
44 #ifndef GG_DEBUG_DISABLE
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
45
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
46 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
47 * gg_debug() // funkcja wewnętrzna
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
48 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
49 * wyświetla komunikat o danym poziomie, o ile użytkownik sobie tego życzy.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
50 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
51 * - level - poziom wiadomości
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
52 * - format... - treść wiadomości (kompatybilna z printf())
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
53 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
54 void gg_debug(int level, const char *format, ...)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
55 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
56 va_list ap;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
57 int old_errno = errno;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
58
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
59 if (gg_debug_handler) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
60 va_start(ap, format);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
61 (*gg_debug_handler)(level, format, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
62 va_end(ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
63
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
64 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
65 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
66
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
67 if ((gg_debug_level & level)) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
68 va_start(ap, format);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
69 vfprintf((gg_debug_file) ? gg_debug_file : stderr, format, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
70 va_end(ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
71 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
72
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
73 cleanup:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
74 errno = old_errno;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
75 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
76
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
77 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
78
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
79 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
80 * gg_vsaprintf() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
81 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
82 * robi dokładnie to samo, co vsprintf(), tyle że alokuje sobie wcześniej
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
83 * miejsce na dane. powinno działać na tych maszynach, które mają funkcję
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
84 * vsnprintf() zgodną z C99, jak i na wcześniejszych.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
85 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
86 * - format - opis wyświetlanego tekstu jak dla printf()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
87 * - ap - lista argumentów dla printf()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
88 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
89 * zaalokowany bufor, który należy później zwolnić, lub NULL
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
90 * jeśli nie udało się wykonać zadania.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
91 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
92 char *gg_vsaprintf(const char *format, va_list ap)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
93 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
94 int size = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
95 const char *start;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
96 char *buf = NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
97
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
98 #ifdef __GG_LIBGADU_HAVE_VA_COPY
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
99 va_list aq;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
100
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
101 va_copy(aq, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
102 #else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
103 # ifdef __GG_LIBGADU_HAVE___VA_COPY
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
104 va_list aq;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
105
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
106 __va_copy(aq, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
107 # endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
108 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
109
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
110 start = format;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
111
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
112 #ifndef __GG_LIBGADU_HAVE_C99_VSNPRINTF
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
113 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
114 int res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
115 char *tmp;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
116
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
117 size = 128;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
118 do {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
119 size *= 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
120 if (!(tmp = realloc(buf, size))) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
121 free(buf);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
122 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
123 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
124 buf = tmp;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
125 res = vsnprintf(buf, size, format, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
126 } while (res == size - 1 || res == -1);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
127 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
128 #else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
129 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
130 char tmp[2];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
131
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
132 /* libce Solarisa przy buforze NULL zawsze zwracają -1, więc
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
133 * musimy podać coś istniejącego jako cel printf()owania. */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
134 size = vsnprintf(tmp, sizeof(tmp), format, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
135 if (!(buf = malloc(size + 1)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
136 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
137 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
138 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
139
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
140 format = start;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
141
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
142 #ifdef __GG_LIBGADU_HAVE_VA_COPY
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
143 vsnprintf(buf, size + 1, format, aq);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
144 va_end(aq);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
145 #else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
146 # ifdef __GG_LIBGADU_HAVE___VA_COPY
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
147 vsnprintf(buf, size + 1, format, aq);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
148 va_end(aq);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
149 # else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
150 vsnprintf(buf, size + 1, format, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
151 # endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
152 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
153
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
154 return buf;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
155 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
156
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
157 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
158 * gg_saprintf() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
159 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
160 * robi dokładnie to samo, co sprintf(), tyle że alokuje sobie wcześniej
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
161 * miejsce na dane. powinno działać na tych maszynach, które mają funkcję
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
162 * vsnprintf() zgodną z C99, jak i na wcześniejszych.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
163 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
164 * - format... - treść taka sama jak w funkcji printf()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
165 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
166 * zaalokowany bufor, który należy później zwolnić, lub NULL
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
167 * jeśli nie udało się wykonać zadania.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
168 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
169 char *gg_saprintf(const char *format, ...)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
170 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
171 va_list ap;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
172 char *res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
173
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
174 va_start(ap, format);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
175 res = gg_vsaprintf(format, ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
176 va_end(ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
177
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
178 return res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
179 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
180
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
181 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
182 * gg_get_line() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
183 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
184 * podaje kolejną linię z bufora tekstowego. niszczy go bezpowrotnie, dzieląc
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
185 * na kolejne stringi. zdarza się, nie ma potrzeby pisania funkcji dublującej
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
186 * bufor żeby tylko mieć nieruszone dane wejściowe, skoro i tak nie będą nam
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
187 * poźniej potrzebne. obcina `\r\n'.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
188 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
189 * - ptr - wskaźnik do zmiennej, która przechowuje aktualną pozycję
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
190 * w przemiatanym buforze
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
191 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
192 * wskaźnik do kolejnej linii tekstu lub NULL, jeśli to już koniec bufora.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
193 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
194 char *gg_get_line(char **ptr)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
195 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
196 char *foo, *res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
197
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
198 if (!ptr || !*ptr || !strcmp(*ptr, ""))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
199 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
200
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
201 res = *ptr;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
202
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
203 if (!(foo = strchr(*ptr, '\n')))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
204 *ptr += strlen(*ptr);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
205 else {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
206 *ptr = foo + 1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
207 *foo = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
208 if (strlen(res) > 1 && res[strlen(res) - 1] == '\r')
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
209 res[strlen(res) - 1] = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
210 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
211
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
212 return res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
213 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
214
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
215 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
216 * gg_connect() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
217 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
218 * łączy się z serwerem. pierwszy argument jest typu (void *), żeby nie
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
219 * musieć niczego inkludować w libgadu.h i nie psuć jakiś głupich zależności
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
220 * na dziwnych systemach.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
221 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
222 * - addr - adres serwera (struct in_addr *)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
223 * - port - port serwera
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
224 * - async - asynchroniczne połączenie
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
225 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
226 * deskryptor gniazda lub -1 w przypadku błędu (kod błędu w zmiennej errno).
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
227 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
228 int gg_connect(void *addr, int port, int async)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
229 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
230 int sock, one = 1, errno2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
231 struct sockaddr_in sin;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
232 struct in_addr *a = addr;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
233 struct sockaddr_in myaddr;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
234
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
235 gg_debug(GG_DEBUG_FUNCTION, "** gg_connect(%s, %d, %d);\n", inet_ntoa(*a), port, async);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
236
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
237 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
238 gg_debug(GG_DEBUG_MISC, "// gg_connect() socket() failed (errno=%d, %s)\n", errno, strerror(errno));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
239 return -1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
240 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
241
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
242 memset(&myaddr, 0, sizeof(myaddr));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
243 myaddr.sin_family = AF_INET;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
244
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
245 myaddr.sin_addr.s_addr = gg_local_ip;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
246
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
247 if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
248 gg_debug(GG_DEBUG_MISC, "// gg_connect() bind() failed (errno=%d, %s)\n", errno, strerror(errno));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
249 return -1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
250 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
251
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
252 #ifdef ASSIGN_SOCKETS_TO_THREADS
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
253 gg_win32_thread_socket(0, sock);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
254 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
255
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
256 if (async) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
257 #ifdef FIONBIO
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
258 if (ioctl(sock, FIONBIO, &one) == -1) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
259 #else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
260 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
261 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
262 gg_debug(GG_DEBUG_MISC, "// gg_connect() ioctl() failed (errno=%d, %s)\n", errno, strerror(errno));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
263 errno2 = errno;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
264 close(sock);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
265 errno = errno2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
266 return -1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
267 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
268 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
269
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
270 sin.sin_port = htons(port);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
271 sin.sin_family = AF_INET;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
272 sin.sin_addr.s_addr = a->s_addr;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
273
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
274 if (connect(sock, (struct sockaddr*) &sin, sizeof(sin)) == -1) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
275 if (errno && (!async || errno != EINPROGRESS)) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
276 gg_debug(GG_DEBUG_MISC, "// gg_connect() connect() failed (errno=%d, %s)\n", errno, strerror(errno));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
277 errno2 = errno;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
278 close(sock);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
279 errno = errno2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
280 return -1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
281 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
282 gg_debug(GG_DEBUG_MISC, "// gg_connect() connect() in progress\n");
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
283 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
284
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
285 return sock;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
286 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
287
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
288 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
289 * gg_read_line() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
290 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
291 * czyta jedną linię tekstu z gniazda.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
292 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
293 * - sock - deskryptor gniazda
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
294 * - buf - wskaźnik do bufora
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
295 * - length - długość bufora
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
296 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
297 * jeśli trafi na błąd odczytu lub podano nieprawidłowe parametry, zwraca NULL.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
298 * inaczej zwraca buf.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
299 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
300 char *gg_read_line(int sock, char *buf, int length)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
301 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
302 int ret;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
303
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
304 if (!buf || length < 0)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
305 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
306
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
307 for (; length > 1; buf++, length--) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
308 do {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
309 if ((ret = read(sock, buf, 1)) == -1 && errno != EINTR) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
310 gg_debug(GG_DEBUG_MISC, "// gg_read_line() error on read (errno=%d, %s)\n", errno, strerror(errno));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
311 *buf = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
312 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
313 } else if (ret == 0) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
314 gg_debug(GG_DEBUG_MISC, "// gg_read_line() eof reached\n");
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
315 *buf = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
316 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
317 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
318 } while (ret == -1 && errno == EINTR);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
319
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
320 if (*buf == '\n') {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
321 buf++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
322 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
323 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
324 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
325
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
326 *buf = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
327 return buf;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
328 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
329
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
330 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
331 * gg_chomp() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
332 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
333 * ucina "\r\n" lub "\n" z końca linii.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
334 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
335 * - line - linia do przycięcia
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
336 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
337 void gg_chomp(char *line)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
338 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
339 int len;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
340
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
341 if (!line)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
342 return;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
343
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
344 len = strlen(line);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
345
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
346 if (len > 0 && line[len - 1] == '\n')
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
347 line[--len] = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
348 if (len > 0 && line[len - 1] == '\r')
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
349 line[--len] = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
350 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
351
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
352 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
353 * gg_urlencode() // funkcja wewnętrzna
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
354 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
355 * zamienia podany tekst na ciąg znaków do formularza http. przydaje się
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
356 * przy różnych usługach katalogu publicznego.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
357 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
358 * - str - ciąg znaków do zakodowania
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
359 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
360 * zaalokowany bufor, który należy później zwolnić albo NULL
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
361 * w przypadku błędu.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
362 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
363 char *gg_urlencode(const char *str)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
364 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
365 char *q, *buf, hex[] = "0123456789abcdef";
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
366 const char *p;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
367 unsigned int size = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
368
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
369 if (!str)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
370 str = "";
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
371
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
372 for (p = str; *p; p++, size++) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
373 if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == ' ') || (*p == '@') || (*p == '.') || (*p == '-'))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
374 size += 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
375 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
376
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
377 if (!(buf = malloc(size + 1)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
378 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
379
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
380 for (p = str, q = buf; *p; p++, q++) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
381 if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || (*p == '@') || (*p == '.') || (*p == '-'))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
382 *q = *p;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
383 else {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
384 if (*p == ' ')
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
385 *q = '+';
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
386 else {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
387 *q++ = '%';
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
388 *q++ = hex[*p >> 4 & 15];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
389 *q = hex[*p & 15];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
390 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
391 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
392 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
393
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
394 *q = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
395
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
396 return buf;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
397 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
398
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
399 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
400 * gg_http_hash() // funkcja wewnętrzna
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
401 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
402 * funkcja licząca hash dla adresu e-mail, hasła i paru innych.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
403 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
404 * - format... - format kolejnych parametrów ('s' jeśli dany parametr jest
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
405 * ciągiem znaków lub 'u' jeśli numerem GG)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
406 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
407 * hash wykorzystywany przy rejestracji i wszelkich manipulacjach własnego
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
408 * wpisu w katalogu publicznym.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
409 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
410 int gg_http_hash(const char *format, ...)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
411 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
412 unsigned int a, c, i, j;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
413 va_list ap;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
414 int b = -1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
415
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
416 va_start(ap, format);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
417
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
418 for (j = 0; j < strlen(format); j++) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
419 char *arg, buf[16];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
420
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
421 if (format[j] == 'u') {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
422 snprintf(buf, sizeof(buf), "%d", va_arg(ap, uin_t));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
423 arg = buf;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
424 } else {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
425 if (!(arg = va_arg(ap, char*)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
426 arg = "";
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
427 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
428
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
429 i = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
430 while ((c = (unsigned char) arg[i++]) != 0) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
431 a = (c ^ b) + (c << 8);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
432 b = (a >> 24) | (a << 8);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
433 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
434 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
435
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
436 va_end(ap);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
437
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
438 return (b < 0 ? -b : b);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
439 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
440
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
441 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
442 * gg_gethostbyname() // funkcja pomocnicza
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
443 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
444 * odpowiednik gethostbyname() troszczący się o współbieżność, gdy mamy do
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
445 * dyspozycji funkcję gethostbyname_r().
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
446 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
447 * - hostname - nazwa serwera
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
448 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
449 * zwraca wskaźnik na strukturę in_addr, którą należy zwolnić.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
450 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
451 struct in_addr *gg_gethostbyname(const char *hostname)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
452 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
453 struct in_addr *addr = NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
454
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
455 #ifdef HAVE_GETHOSTBYNAME_R
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
456 char *tmpbuf = NULL, *buf = NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
457 struct hostent *hp = NULL, *hp2 = NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
458 int h_errnop, ret;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
459 size_t buflen = 1024;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
460 int new_errno;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
461
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
462 new_errno = ENOMEM;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
463
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
464 if (!(addr = malloc(sizeof(struct in_addr))))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
465 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
466
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
467 if (!(hp = calloc(1, sizeof(*hp))))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
468 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
469
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
470 if (!(buf = malloc(buflen)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
471 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
472
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
473 tmpbuf = buf;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
474
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
475 while ((ret = gethostbyname_r(hostname, hp, buf, buflen, &hp2, &h_errnop)) == ERANGE) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
476 buflen *= 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
477
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
478 if (!(tmpbuf = realloc(buf, buflen)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
479 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
480
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
481 buf = tmpbuf;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
482 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
483
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
484 if (ret)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
485 new_errno = h_errnop;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
486
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
487 if (ret || !hp2 || !tmpbuf)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
488 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
489
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
490 memcpy(addr, hp->h_addr, sizeof(struct in_addr));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
491
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
492 free(buf);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
493 free(hp);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
494
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
495 return addr;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
496
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
497 cleanup:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
498 errno = new_errno;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
499
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
500 if (addr)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
501 free(addr);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
502 if (hp)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
503 free(hp);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
504 if (buf)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
505 free(buf);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
506
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
507 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
508 #else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
509 struct hostent *hp;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
510
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
511 if (!(addr = malloc(sizeof(struct in_addr)))) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
512 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
513 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
514
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
515 if (!(hp = gethostbyname(hostname)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
516 goto cleanup;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
517
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
518 memcpy(addr, hp->h_addr, sizeof(struct in_addr));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
519
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
520 return addr;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
521
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
522 cleanup:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
523 if (addr)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
524 free(addr);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
525
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
526 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
527 #endif
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
528 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
529
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
530 #ifdef ASSIGN_SOCKETS_TO_THREADS
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
531
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
532 typedef struct gg_win32_thread {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
533 int id;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
534 int socket;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
535 struct gg_win32_thread *next;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
536 } gg_win32_thread;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
537
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
538 struct gg_win32_thread *gg_win32_threads = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
539
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
540 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
541 * gg_win32_thread_socket() // funkcja pomocnicza, tylko dla win32
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
542 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
543 * zwraca deskryptor gniazda, które było ostatnio tworzone dla wątku
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
544 * o podanym identyfikatorze.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
545 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
546 * jeśli na win32 przy połączeniach synchronicznych zapamiętamy w jakim
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
547 * wątku uruchomiliśmy funkcję, która się z czymkolwiek łączy, to z osobnego
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
548 * wątku możemy anulować połączenie poprzez gg_win32_thread_socket(watek, -1);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
549 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
550 * - thread_id - id wątku. jeśli jest równe 0, brany jest aktualny wątek,
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
551 * jeśli równe -1, usuwa wpis o podanym sockecie.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
552 * - socket - deskryptor gniazda. jeśli równe 0, zwraca deskryptor gniazda
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
553 * dla podanego wątku, jeśli równe -1, usuwa wpis, jeśli coś
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
554 * innego, ustawia dla podanego wątku dany numer deskryptora.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
555 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
556 * jeśli socket jest równe 0, zwraca deskryptor gniazda dla podanego wątku.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
557 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
558 int gg_win32_thread_socket(int thread_id, int socket)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
559 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
560 char close = (thread_id == -1) || socket == -1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
561 gg_win32_thread *wsk = gg_win32_threads;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
562 gg_win32_thread **p_wsk = &gg_win32_threads;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
563
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
564 if (!thread_id)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
565 thread_id = GetCurrentThreadId();
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
566
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
567 while (wsk) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
568 if ((thread_id == -1 && wsk->socket == socket) || wsk->id == thread_id) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
569 if (close) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
570 /* socket zostaje usuniety */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
571 closesocket(wsk->socket);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
572 *p_wsk = wsk->next;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
573 free(wsk);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
574 return 1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
575 } else if (!socket) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
576 /* socket zostaje zwrocony */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
577 return wsk->socket;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
578 } else {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
579 /* socket zostaje ustawiony */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
580 wsk->socket = socket;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
581 return socket;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
582 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
583 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
584 p_wsk = &(wsk->next);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
585 wsk = wsk->next;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
586 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
587
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
588 if (close && socket != -1)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
589 closesocket(socket);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
590 if (close || !socket)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
591 return 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
592
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
593 /* Dodaje nowy element */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
594 wsk = malloc(sizeof(gg_win32_thread));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
595 wsk->id = thread_id;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
596 wsk->socket = socket;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
597 wsk->next = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
598 *p_wsk = wsk;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
599
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
600 return socket;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
601 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
602
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
603 #endif /* ASSIGN_SOCKETS_TO_THREADS */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
604
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
605 static char gg_base64_charset[] =
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
606 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
607
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
608 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
609 * gg_base64_encode()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
610 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
611 * zapisuje ciąg znaków w base64.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
612 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
613 * - buf - ciąg znaków.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
614 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
615 * zaalokowany bufor.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
616 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
617 char *gg_base64_encode(const char *buf)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
618 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
619 char *out, *res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
620 unsigned int i = 0, j = 0, k = 0, len = strlen(buf);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
621
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
622 res = out = malloc((len / 3 + 1) * 4 + 2);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
623
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
624 if (!res)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
625 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
626
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
627 while (j <= len) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
628 switch (i % 4) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
629 case 0:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
630 k = (buf[j] & 252) >> 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
631 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
632 case 1:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
633 if (j < len)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
634 k = ((buf[j] & 3) << 4) | ((buf[j + 1] & 240) >> 4);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
635 else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
636 k = (buf[j] & 3) << 4;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
637
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
638 j++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
639 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
640 case 2:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
641 if (j < len)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
642 k = ((buf[j] & 15) << 2) | ((buf[j + 1] & 192) >> 6);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
643 else
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
644 k = (buf[j] & 15) << 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
645
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
646 j++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
647 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
648 case 3:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
649 k = buf[j++] & 63;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
650 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
651 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
652 *out++ = gg_base64_charset[k];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
653 i++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
654 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
655
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
656 if (i % 4)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
657 for (j = 0; j < 4 - (i % 4); j++, out++)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
658 *out = '=';
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
659
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
660 *out = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
661
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
662 return res;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
663 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
664
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
665 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
666 * gg_base64_decode()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
667 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
668 * dekoduje ciąg znaków z base64.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
669 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
670 * - buf - ciąg znaków.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
671 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
672 * zaalokowany bufor.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
673 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
674 char *gg_base64_decode(const char *buf)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
675 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
676 char *res, *save, *foo, val;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
677 const char *end;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
678 unsigned int index = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
679
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
680 if (!buf)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
681 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
682
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
683 save = res = calloc(1, (strlen(buf) / 4 + 1) * 3 + 2);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
684
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
685 if (!save)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
686 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
687
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
688 end = buf + strlen(buf);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
689
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
690 while (*buf && buf < end) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
691 if (*buf == '\r' || *buf == '\n') {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
692 buf++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
693 continue;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
694 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
695 if (!(foo = strchr(gg_base64_charset, *buf)))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
696 foo = gg_base64_charset;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
697 val = (int)(foo - gg_base64_charset);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
698 buf++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
699 switch (index) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
700 case 0:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
701 *res |= val << 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
702 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
703 case 1:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
704 *res++ |= val >> 4;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
705 *res |= val << 4;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
706 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
707 case 2:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
708 *res++ |= val >> 2;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
709 *res |= val << 6;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
710 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
711 case 3:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
712 *res++ |= val;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
713 break;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
714 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
715 index++;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
716 index %= 4;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
717 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
718 *res = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
719
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
720 return save;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
721 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
722
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
723 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
724 * gg_proxy_auth() // funkcja wewnętrzna
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
725 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
726 * tworzy nagłówek autoryzacji dla proxy.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
727 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
728 * zaalokowany tekst lub NULL, jeśli proxy nie jest włączone lub nie wymaga
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
729 * autoryzacji.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
730 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
731 char *gg_proxy_auth()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
732 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
733 char *tmp, *enc, *out;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
734 unsigned int tmp_size;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
735
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
736 if (!gg_proxy_enabled || !gg_proxy_username || !gg_proxy_password)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
737 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
738
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
739 if (!(tmp = malloc((tmp_size = strlen(gg_proxy_username) + strlen(gg_proxy_password) + 2))))
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
740 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
741
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
742 snprintf(tmp, tmp_size, "%s:%s", gg_proxy_username, gg_proxy_password);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
743
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
744 if (!(enc = gg_base64_encode(tmp))) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
745 free(tmp);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
746 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
747 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
748
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
749 free(tmp);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
750
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
751 if (!(out = malloc(strlen(enc) + 40))) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
752 free(enc);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
753 return NULL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
754 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
755
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
756 snprintf(out, strlen(enc) + 40, "Proxy-Authorization: Basic %s\r\n", enc);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
757
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
758 free(enc);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
759
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
760 return out;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
761 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
762
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
763 static uint32_t gg_crc32_table[256];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
764 static int gg_crc32_initialized = 0;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
765
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
766 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
767 * gg_crc32_make_table() // funkcja wewnętrzna
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
768 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
769 static void gg_crc32_make_table()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
770 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
771 uint32_t h = 1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
772 unsigned int i, j;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
773
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
774 memset(gg_crc32_table, 0, sizeof(gg_crc32_table));
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
775
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
776 for (i = 128; i; i >>= 1) {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
777 h = (h >> 1) ^ ((h & 1) ? 0xedb88320L : 0);
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
778
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
779 for (j = 0; j < 256; j += 2 * i)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
780 gg_crc32_table[i + j] = gg_crc32_table[j] ^ h;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
781 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
782
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
783 gg_crc32_initialized = 1;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
784 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
785
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
786 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
787 * gg_crc32()
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
788 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
789 * wyznacza sumę kontrolną CRC32 danego bloku danych.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
790 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
791 * - crc - suma kontrola poprzedniego bloku danych lub 0 jeśli pierwszy
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
792 * - buf - bufor danych
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
793 * - size - ilość danych
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
794 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
795 * suma kontrolna CRC32.
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
796 */
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
797 uint32_t gg_crc32(uint32_t crc, const unsigned char *buf, int len)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
798 {
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
799 if (!gg_crc32_initialized)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
800 gg_crc32_make_table();
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
801
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
802 if (!buf || len < 0)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
803 return crc;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
804
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
805 crc ^= 0xffffffffL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
806
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
807 while (len--)
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
808 crc = (crc >> 8) ^ gg_crc32_table[(crc ^ *buf++) & 0xff];
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
809
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
810 return crc ^ 0xffffffffL;
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
811 }
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
812
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
813
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
814 /*
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
815 * Local variables:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
816 * c-indentation-style: k&r
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
817 * c-basic-offset: 8
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
818 * indent-tabs-mode: notnil
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
819 * End:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
820 *
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
821 * vim: shiftwidth=8:
cf15c1cdcfbd [gaim-migrate @ 13582]
Bartoz Oler <bartosz@pidgin.im>
parents:
diff changeset
822 */