Mercurial > pidgin
annotate plugins/icq/icqlib.c @ 1309:0a766047b4fd
[gaim-migrate @ 1319]
Yay, new icqlib
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 19 Dec 2000 10:08:29 +0000 |
parents | 201ec77f3a60 |
children | 4c510ca3563f |
rev | line source |
---|---|
1152 | 1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
3 $Id: icqlib.c 1319 2000-12-19 10:08:29Z warmenhoven $ |
1152 | 4 $Log$ |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
5 Revision 1.2 2000/12/19 10:08:29 warmenhoven |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
6 Yay, new icqlib |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
7 |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
8 Revision 1.46 2000/12/19 06:00:07 bills |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
9 moved members from ICQLINK to ICQLINK_private struct |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
10 |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
11 Revision 1.45 2000/11/02 07:29:07 denis |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
12 Ability to disable TCP protocol has been added. |
1152 | 13 |
14 Revision 1.44 2000/07/24 03:10:08 bills | |
15 added support for real nickname during TCP transactions like file and | |
16 chat, instead of using Bill all the time (hmm, where'd I get that from? :) | |
17 | |
18 Revision 1.43 2000/07/09 22:05:11 bills | |
19 removed unnecessary functions | |
20 | |
21 Revision 1.42 2000/07/09 18:28:07 denis | |
22 Initial memset() in icq_Init() replaced by callback's clearance. | |
23 | |
24 Revision 1.41 2000/06/15 01:50:39 bills | |
25 removed *Seq functions | |
26 | |
27 Revision 1.40 2000/05/10 19:06:59 denis | |
28 UDP outgoing packet queue was implemented. | |
29 | |
30 Revision 1.39 2000/05/03 18:12:36 denis | |
31 Unfinished UDP queue was commented out. | |
32 | |
33 Revision 1.38 2000/04/10 16:36:04 denis | |
34 Some more Win32 compatibility from Guillaume Rosanis <grs@mail.com> | |
35 | |
36 Revision 1.37 2000/04/06 16:38:04 denis | |
37 icq_*Send*Seq() functions with specified sequence number were added. | |
38 | |
39 Revision 1.36 2000/04/05 14:37:02 denis | |
40 Applied patch from "Guillaume R." <grs@mail.com> for basic Win32 | |
41 compatibility. | |
42 | |
43 Revision 1.35 2000/01/16 03:59:10 bills | |
44 reworked list code so list_nodes don't need to be inside item structures, | |
45 removed strlist code and replaced with generic list calls | |
46 | |
47 Revision 1.34 1999/12/27 16:06:32 bills | |
48 cleanups | |
49 | |
50 Revision 1.33 1999/10/03 21:35:55 tim | |
51 Fixed "url" and "descr" parameters order when sending a URL via TCP. | |
52 | |
53 Revision 1.32 1999/09/29 16:49:43 denis | |
54 Host/network/icq byteorder systemized. | |
55 icq_Init() cleaned up. | |
56 | |
57 Revision 1.31 1999/07/18 20:15:55 bills | |
58 changed to use new byte-order functions & contact list functions | |
59 | |
60 Revision 1.30 1999/07/16 12:27:06 denis | |
61 Other global variables moved to ICQLINK structure. | |
62 Initialization of random number generator added in icq_Init() | |
63 Cleaned up. | |
64 | |
65 Revision 1.29 1999/07/12 15:13:31 cproch | |
66 - added definition of ICQLINK to hold session-specific global variabled | |
67 applications which have more than one connection are now possible | |
68 - changed nearly every function defintion to support ICQLINK parameter | |
69 | |
70 Revision 1.28 1999/07/03 02:26:02 bills | |
71 added new code to support thruSrv arg to SendMessage and SendURL | |
72 | |
73 Revision 1.27 1999/04/17 19:21:37 bills | |
74 modified Send* Functions to return DWORD instead of WORD | |
75 | |
76 Revision 1.26 1999/04/14 14:48:18 denis | |
77 Switched from icq_Log callback to icq_Fmt function. | |
78 Cleanups for "strict" compiling (-ansi -pedantic) | |
79 | |
80 Revision 1.25 1999/04/05 13:14:57 denis | |
81 Send messages and URLs to 'not in list' users fixed. | |
82 | |
83 Revision 1.24 1999/03/31 01:43:40 bills | |
84 added TCP support to SendURL | |
85 | |
86 Revision 1.23 1999/03/30 22:47:44 lord | |
87 list of countries now sorted. | |
88 | |
89 Revision 1.22 1999/03/28 03:18:22 bills | |
90 enable tcp messaging in icq_SendMessage, uncommented icq_OurPort and | |
91 icq_OurIp and fixed function names so icqlib compiles | |
92 | |
93 Revision 1.21 1999/03/25 22:16:43 bills | |
94 added #include "util.h" | |
95 | |
96 Revision 1.20 1999/03/24 11:37:36 denis | |
97 Underscored files with TCP stuff renamed. | |
98 TCP stuff cleaned up | |
99 Function names changed to corresponding names. | |
100 icqlib.c splitted to many small files by subject. | |
101 C++ comments changed to ANSI C comments. | |
102 | |
103 Revision 1.19 1999/03/22 20:51:28 bills | |
104 added code in icq_HandleUserOnline to set/clear new struct entries in | |
105 icq_ContactItem; added cleanup code in icq_HandleUserOffline for same | |
106 | |
107 Revision 1.18 1999/03/09 13:14:05 denis | |
108 Cyrillic recoding removed from URLs | |
109 | |
110 Revision 1.17 1999/03/05 13:57:54 denis | |
111 Some cosmetic changes... | |
112 | |
113 Revision 1.16 1998/12/08 16:00:59 denis | |
114 Cleaned up a little before releasing | |
115 | |
116 Revision 1.15 1998/11/25 19:18:16 denis | |
117 Added close icq_ProxySok in icq_Disconnect | |
118 | |
119 Revision 1.14 1998/11/25 09:48:49 denis | |
120 icq_GetProxySok and icq_HandleProxyResponse methods added | |
121 Connection terminated support added | |
122 | |
123 Revision 1.13 1998/11/19 12:22:48 denis | |
124 SOCKS support cleaned a little | |
125 icq_RecvUrl renamed to icq_RecvURL | |
126 icq_ProxyAuth added for Username/Password Authentication | |
127 URL/Description order inverted | |
128 icq_Quit splitted to icq_Logout and icq_Disconnect | |
129 icq_ProxyName and icq_ProxyPass range checking added | |
130 | |
131 Revision 1.12 1998/11/18 16:21:29 denis | |
132 Fixed SOCKS5 proxy support | |
133 | |
134 */ | |
135 | |
136 #include "icqlib.h" | |
137 | |
138 #include <stdlib.h> | |
139 | |
140 #ifndef _WIN32 | |
141 #include <unistd.h> | |
142 #endif | |
143 | |
144 #include <time.h> | |
145 | |
146 #ifdef _WIN32 | |
147 #include <winsock.h> | |
148 #else | |
149 #include <netdb.h> | |
150 #include <sys/socket.h> | |
151 #endif | |
152 | |
153 #include <sys/stat.h> | |
154 | |
155 #ifndef _WIN32 | |
156 #include <sys/time.h> | |
157 #include <netinet/in.h> | |
158 #endif | |
159 | |
160 #include "util.h" | |
161 #include "icqtypes.h" | |
162 #include "icq.h" | |
163 #include "udp.h" | |
164 #include "tcp.h" | |
165 #include "queue.h" | |
166 | |
167 int icq_Russian = FALSE; | |
168 BYTE icq_LogLevel = 0; | |
169 | |
170 DWORD icq_SendMessage(ICQLINK *link, DWORD uin, const char *text, BYTE thruSrv) | |
171 { | |
172 if(thruSrv==ICQ_SEND_THRUSERVER) | |
173 return icq_UDPSendMessage(link, uin, text); | |
174 else if(thruSrv==ICQ_SEND_DIRECT) | |
175 return icq_TCPSendMessage(link, uin, text); | |
176 else if(thruSrv==ICQ_SEND_BESTWAY) | |
177 { | |
178 icq_ContactItem *pcontact=icq_ContactFind(link, uin); | |
179 if(pcontact) | |
180 { | |
181 if(pcontact->tcp_flag == 0x04) | |
182 return icq_TCPSendMessage(link, uin, text); | |
183 else | |
184 return icq_UDPSendMessage(link, uin, text); | |
185 } | |
186 else | |
187 { | |
188 return icq_UDPSendMessage(link, uin, text); | |
189 } | |
190 } | |
191 return 0; | |
192 } | |
193 | |
194 DWORD icq_SendURL(ICQLINK *link, DWORD uin, const char *url, const char *descr, BYTE thruSrv) | |
195 { | |
196 if(thruSrv==ICQ_SEND_THRUSERVER) | |
197 return icq_UDPSendURL(link, uin, url, descr); | |
198 else if(thruSrv==ICQ_SEND_DIRECT) | |
199 return icq_TCPSendURL(link, uin, descr, url); | |
200 else if(thruSrv==ICQ_SEND_BESTWAY) | |
201 { | |
202 icq_ContactItem *pcontact=icq_ContactFind(link, uin); | |
203 if(pcontact) | |
204 { | |
205 if(pcontact->tcp_flag == 0x04) | |
206 return icq_TCPSendURL(link, uin, descr, url); | |
207 else | |
208 return icq_UDPSendURL(link, uin, url, descr); | |
209 } | |
210 else | |
211 { | |
212 return icq_UDPSendURL(link, uin, url, descr); | |
213 } | |
214 } | |
215 return 0; | |
216 } | |
217 | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
218 ICQLINK *icq_ICQLINKNew(DWORD uin, const char *password, const char *nick, |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
219 unsigned char useTCP) |
1152 | 220 { |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
221 ICQLINK *link = (ICQLINK *)malloc(sizeof(ICQLINK)); |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
222 link->d = (ICQLINK_private *)malloc(sizeof(ICQLINK_private)); |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
223 |
1152 | 224 srand(time(0L)); |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
225 |
1152 | 226 /* memset(link, 0, sizeof(ICQLINK)); */ |
227 | |
228 /* Initialize all callbacks */ | |
229 link->icq_Logged = 0L; | |
230 link->icq_Disconnected = 0L; | |
231 link->icq_RecvMessage = 0L; | |
232 link->icq_RecvURL = 0L; | |
233 link->icq_RecvWebPager = 0L; | |
234 link->icq_RecvMailExpress = 0L; | |
235 link->icq_RecvChatReq = 0L; | |
236 link->icq_RecvFileReq = 0L; | |
237 link->icq_RecvAdded = 0L; | |
238 link->icq_RecvAuthReq = 0L; | |
239 link->icq_UserFound = 0L; | |
240 link->icq_SearchDone = 0L; | |
241 link->icq_UserOnline = 0L; | |
242 link->icq_UserOffline = 0L; | |
243 link->icq_UserStatusUpdate = 0L; | |
244 link->icq_InfoReply = 0L; | |
245 link->icq_ExtInfoReply = 0L; | |
246 link->icq_WrongPassword = 0L; | |
247 link->icq_InvalidUIN = 0L; | |
248 link->icq_Log = 0L; | |
249 link->icq_SrvAck = 0L; | |
250 link->icq_RequestNotify = 0L; | |
251 link->icq_NewUIN = 0L; | |
252 link->icq_SetTimeout = 0L; | |
253 link->icq_MetaUserFound = 0L; | |
254 link->icq_MetaUserInfo = 0L; | |
255 link->icq_MetaUserWork = 0L; | |
256 link->icq_MetaUserMore = 0L; | |
257 link->icq_MetaUserAbout = 0L; | |
258 link->icq_MetaUserInterests = 0L; | |
259 link->icq_MetaUserAffiliations = 0L; | |
260 link->icq_MetaUserHomePageCategory = 0L; | |
261 | |
262 /* General stuff */ | |
263 link->icq_Uin = uin; | |
264 link->icq_Password = strdup(password); | |
265 link->icq_Nick = strdup(nick); | |
266 link->icq_OurIP = -1; | |
267 link->icq_OurPort = 0; | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
268 link->d->icq_ContactList = list_new(); |
1152 | 269 link->icq_Status = -1; |
270 | |
271 /* UDP stuff */ | |
272 link->icq_UDPSok = -1; | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
273 memset(link->d->icq_UDPServMess, FALSE, sizeof(link->d->icq_UDPServMess)); |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
274 link->d->icq_UDPSeqNum1 = 0; |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
275 link->d->icq_UDPSeqNum2 = 0; |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
276 link->d->icq_UDPSession = 0; |
1152 | 277 icq_UDPQueueNew(link); |
278 | |
279 icq_TCPInit(link); | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
280 link->icq_UseTCP = useTCP; |
1152 | 281 |
282 /* Proxy stuff */ | |
283 link->icq_UseProxy = 0; | |
284 link->icq_ProxyHost = 0L; | |
285 link->icq_ProxyIP = -1; | |
286 link->icq_ProxyPort = 0; | |
287 link->icq_ProxyAuth = 0; | |
288 link->icq_ProxyName = 0L; | |
289 link->icq_ProxyPass = 0L; | |
290 link->icq_ProxySok = -1; | |
291 link->icq_ProxyOurPort = 0; | |
292 link->icq_ProxyDestIP = -1; | |
293 link->icq_ProxyDestPort = 0; | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
294 |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
295 return link; |
1152 | 296 } |
297 | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
298 void icq_ICQLINKDelete(ICQLINK *link) |
1152 | 299 { |
300 icq_TCPDone(link); | |
301 if(link->icq_Password) | |
302 free(link->icq_Password); | |
303 if(link->icq_Nick) | |
304 free(link->icq_Nick); | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
305 if(link->d->icq_ContactList) |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
306 list_delete(link->d->icq_ContactList, icq_ContactDelete); |
1152 | 307 icq_UDPQueueDelete(link); |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
308 free(link->d); |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
309 free(link); |
1152 | 310 } |
311 | |
312 /****************************** | |
313 Main function connects gets icq_Uin | |
314 and icq_Password and logins in and sits | |
315 in a loop waiting for server responses. | |
316 *******************************/ | |
317 void icq_Main(ICQLINK *link) | |
318 { | |
319 struct timeval tv; | |
320 fd_set readfds; | |
321 | |
322 tv.tv_sec = 0; | |
323 tv.tv_usec = 0; | |
324 FD_ZERO(&readfds); | |
325 FD_SET(link->icq_UDPSok, &readfds); | |
326 select(link->icq_UDPSok+1, &readfds, 0L, 0L, &tv); | |
327 if(FD_ISSET(link->icq_UDPSok, &readfds)) | |
328 icq_HandleServerResponse(link); | |
329 icq_TCPMain(link); | |
330 } | |
331 | |
332 /********************************** | |
333 Connects to hostname on port port | |
334 hostname can be DNS or nnn.nnn.nnn.nnn | |
335 write out messages to the FD aux | |
336 ***********************************/ | |
337 int icq_Connect(ICQLINK *link, const char *hostname, int port) | |
338 { | |
339 char buf[1024]; /*, un = 1;*/ | |
340 /* char tmpbuf[256], our_host[256]*/ | |
341 int conct, res; | |
342 unsigned int length; | |
343 struct sockaddr_in sin, prsin; /* used to store inet addr stuff */ | |
344 struct hostent *host_struct; /* used in DNS llokup */ | |
345 | |
346 link->icq_UDPSok = socket(AF_INET, SOCK_DGRAM, 0);/* create the unconnected socket*/ | |
347 if(link->icq_UDPSok == -1) | |
348 { | |
349 icq_FmtLog(link, ICQ_LOG_FATAL, "Socket creation failed\n"); | |
350 return -1; | |
351 } | |
352 icq_FmtLog(link, ICQ_LOG_MESSAGE, "Socket created attempting to connect\n"); | |
353 sin.sin_addr.s_addr = INADDR_ANY; | |
354 sin.sin_family = AF_INET; /* we're using the inet not appletalk*/ | |
355 sin.sin_port = 0; | |
356 if(bind(link->icq_UDPSok, (struct sockaddr*)&sin, sizeof(struct sockaddr))<0) | |
357 { | |
358 icq_FmtLog(link, ICQ_LOG_FATAL, "Can't bind socket to free port\n"); | |
359 return -1; | |
360 } | |
361 length = sizeof(sin); | |
362 getsockname(link->icq_UDPSok, (struct sockaddr*)&sin, &length); | |
363 link->icq_ProxyOurPort = ntohs(sin.sin_port); | |
364 if(link->icq_UseProxy) | |
365 { | |
366 icq_FmtLog(link, ICQ_LOG_MESSAGE, "[SOCKS] Trying to use SOCKS5 proxy\n"); | |
367 prsin.sin_addr.s_addr = inet_addr(link->icq_ProxyHost); | |
368 if(prsin.sin_addr.s_addr == (unsigned long)-1) /* name isn't n.n.n.n so must be DNS */ | |
369 { | |
370 host_struct = gethostbyname(link->icq_ProxyHost); | |
371 if(host_struct == 0L) | |
372 { | |
373 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Can't find hostname: %s\n", link->icq_ProxyHost); | |
374 return -1; | |
375 } | |
376 prsin.sin_addr = *((struct in_addr*)host_struct->h_addr); | |
377 } | |
378 link->icq_ProxyIP = ntohl(prsin.sin_addr.s_addr); | |
379 prsin.sin_family = AF_INET; /* we're using the inet not appletalk*/ | |
380 prsin.sin_port = htons(link->icq_ProxyPort); /* port */ | |
381 link->icq_ProxySok = socket(AF_INET, SOCK_STREAM, 0);/* create the unconnected socket*/ | |
382 if(link->icq_ProxySok == -1) | |
383 { | |
384 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Socket creation failed\n"); | |
385 return -1; | |
386 } | |
387 icq_FmtLog(link, ICQ_LOG_MESSAGE, "[SOCKS] Socket created attempting to connect\n"); | |
388 conct = connect(link->icq_ProxySok, (struct sockaddr *) &prsin, sizeof(prsin)); | |
389 if(conct == -1) /* did we connect ?*/ | |
390 { | |
391 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Connection refused\n"); | |
392 return -1; | |
393 } | |
394 buf[0] = 5; /* protocol version */ | |
395 buf[1] = 1; /* number of methods */ | |
396 if(!strlen(link->icq_ProxyName) || !strlen(link->icq_ProxyPass) || !link->icq_ProxyAuth) | |
397 buf[2] = 0; /* no authorization required */ | |
398 else | |
399 buf[2] = 2; /* method username/password */ | |
400 #ifdef _WIN32 | |
401 send(link->icq_ProxySok, buf, 3, 0); | |
402 res = recv(link->icq_ProxySok, buf, 2, 0); | |
403 #else | |
404 write(link->icq_ProxySok, buf, 3); | |
405 res = read(link->icq_ProxySok, buf, 2); | |
406 #endif | |
407 if(strlen(link->icq_ProxyName) && strlen(link->icq_ProxyPass) && link->icq_ProxyAuth) | |
408 { | |
409 if(res != 2 || buf[0] != 5 || buf[1] != 2) /* username/password authentication*/ | |
410 { | |
411 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Authentication method incorrect\n"); | |
412 #ifdef _WIN32 | |
413 closesocket(link->icq_ProxySok); | |
414 #else | |
415 close(link->icq_ProxySok); | |
416 #endif | |
417 return -1; | |
418 } | |
419 buf[0] = 1; /* version of subnegotiation */ | |
420 buf[1] = strlen(link->icq_ProxyName); | |
421 memcpy(&buf[2], link->icq_ProxyName, buf[1]); | |
422 buf[2+buf[1]] = strlen(link->icq_ProxyPass); | |
423 memcpy(&buf[3+buf[1]], link->icq_ProxyPass, buf[2+buf[1]]); | |
424 #ifdef _WIN32 | |
425 send(link->icq_ProxySok, buf, buf[1]+buf[2+buf[1]]+3, 0); | |
426 res = recv(link->icq_ProxySok, buf, 2, 0); | |
427 #else | |
428 write(link->icq_ProxySok, buf, buf[1]+buf[2+buf[1]]+3); | |
429 res = read(link->icq_ProxySok, buf, 2); | |
430 #endif | |
431 if(res != 2 || buf[0] != 1 || buf[1] != 0) | |
432 { | |
433 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Authorization failure\n"); | |
434 #ifdef _WIN32 | |
435 closesocket(link->icq_ProxySok); | |
436 #else | |
437 close(link->icq_ProxySok); | |
438 #endif | |
439 return -1; | |
440 } | |
441 } | |
442 else | |
443 { | |
444 if(res != 2 || buf[0] != 5 || buf[1] != 0) /* no authentication required */ | |
445 { | |
446 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Authentication method incorrect\n"); | |
447 #ifdef _WIN32 | |
448 closesocket(link->icq_ProxySok); | |
449 #else | |
450 close(link->icq_ProxySok); | |
451 #endif | |
452 return -1; | |
453 } | |
454 } | |
455 buf[0] = 5; /* protocol version */ | |
456 buf[1] = 3; /* command UDP associate */ | |
457 buf[2] = 0; /* reserved */ | |
458 buf[3] = 1; /* address type IP v4 */ | |
459 buf[4] = (char)0; | |
460 buf[5] = (char)0; | |
461 buf[6] = (char)0; | |
462 buf[7] = (char)0; | |
463 *(unsigned short*)&buf[8] = htons(link->icq_ProxyOurPort); | |
464 /* memcpy(&buf[8], &link->icq_ProxyOurPort, 2); */ | |
465 #ifdef _WIN32 | |
466 send(link->icq_ProxySok, buf, 10, 0); | |
467 res = recv(link->icq_ProxySok, buf, 10, 0); | |
468 #else | |
469 write(link->icq_ProxySok, buf, 10); | |
470 res = read(link->icq_ProxySok, buf, 10); | |
471 #endif | |
472 if(res != 10 || buf[0] != 5 || buf[1] != 0) | |
473 { | |
474 switch(buf[1]) | |
475 { | |
476 case 1: | |
477 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] General SOCKS server failure\n"); | |
478 break; | |
479 case 2: | |
480 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Connection not allowed by ruleset\n"); | |
481 break; | |
482 case 3: | |
483 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Network unreachable\n"); | |
484 break; | |
485 case 4: | |
486 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Host unreachable\n"); | |
487 break; | |
488 case 5: | |
489 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Connection refused\n"); | |
490 break; | |
491 case 6: | |
492 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] TTL expired\n"); | |
493 break; | |
494 case 7: | |
495 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Command not supported\n"); | |
496 break; | |
497 case 8: | |
498 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Address type not supported\n"); | |
499 break; | |
500 default: | |
501 icq_FmtLog(link, ICQ_LOG_FATAL, "[SOCKS] Unknown SOCKS server failure\n"); | |
502 break; | |
503 } | |
504 #ifdef _WIN32 | |
505 closesocket(link->icq_ProxySok); | |
506 #else | |
507 close(link->icq_ProxySok); | |
508 #endif | |
509 return -1; | |
510 } | |
511 } | |
512 sin.sin_addr.s_addr = inet_addr(hostname); /* checks for n.n.n.n notation */ | |
513 if(sin.sin_addr.s_addr == (unsigned long)-1) /* name isn't n.n.n.n so must be DNS */ | |
514 { | |
515 host_struct = gethostbyname(hostname); | |
516 if(host_struct == 0L) | |
517 { | |
518 icq_FmtLog(link, ICQ_LOG_FATAL, "Can't find hostname: %s\n", hostname); | |
519 if(link->icq_UseProxy) | |
520 { | |
521 #ifdef _WIN32 | |
522 closesocket(link->icq_ProxySok); | |
523 #else | |
524 close(link->icq_ProxySok); | |
525 #endif | |
526 } | |
527 return -1; | |
528 } | |
529 sin.sin_addr = *((struct in_addr *)host_struct->h_addr); | |
530 } | |
531 if(link->icq_UseProxy) | |
532 { | |
533 link->icq_ProxyDestIP = ntohl(sin.sin_addr.s_addr); | |
534 memcpy(&sin.sin_addr.s_addr, &buf[4], 4); | |
535 } | |
536 sin.sin_family = AF_INET; /* we're using the inet not appletalk*/ | |
537 sin.sin_port = htons(port); /* port */ | |
538 if(link->icq_UseProxy) | |
539 { | |
540 link->icq_ProxyDestPort = port; | |
541 memcpy(&sin.sin_port, &buf[8], 2); | |
542 } | |
543 conct = connect(link->icq_UDPSok, (struct sockaddr*)&sin, sizeof(sin)); | |
544 if(conct == -1) /* did we connect ?*/ | |
545 { | |
546 icq_FmtLog(link, ICQ_LOG_FATAL, "Connection refused\n"); | |
547 if(link->icq_UseProxy) | |
548 { | |
549 #ifdef _WIN32 | |
550 closesocket(link->icq_ProxySok); | |
551 #else | |
552 close(link->icq_ProxySok); | |
553 #endif | |
554 } | |
555 return -1; | |
556 } | |
557 length = sizeof(sin) ; | |
558 getsockname(link->icq_UDPSok, (struct sockaddr*)&sin, &length); | |
559 link->icq_OurIP = ntohl(sin.sin_addr.s_addr); | |
560 link->icq_OurPort = ntohs(sin.sin_port); | |
561 return link->icq_UDPSok; | |
562 } | |
563 | |
564 void icq_Disconnect(ICQLINK *link) | |
565 { | |
566 #ifdef _WIN32 | |
567 closesocket(link->icq_UDPSok); | |
568 #else | |
569 close(link->icq_UDPSok); | |
570 #endif | |
571 if(link->icq_UseProxy) | |
572 { | |
573 #ifdef _WIN32 | |
574 closesocket(link->icq_ProxySok); | |
575 #else | |
576 close(link->icq_ProxySok); | |
577 #endif | |
578 } | |
579 icq_UDPQueueFree(link); | |
580 } | |
581 | |
582 /* | |
583 void icq_InitNewUser(const char *hostname, DWORD port) | |
584 { | |
585 srv_net_icq_pak pak; | |
586 int s; | |
587 struct timeval tv; | |
588 fd_set readfds; | |
589 | |
590 icq_Connect(hostname, port); | |
591 if((icq_UDPSok == -1) || (icq_UDPSok == 0)) | |
592 { | |
593 printf("Couldn't establish connection\n"); | |
594 exit(1); | |
595 } | |
596 icq_RegNewUser(icq_Password); | |
597 for(;;) | |
598 { | |
599 tv.tv_sec = 2; | |
600 tv.tv_usec = 500000; | |
601 | |
602 FD_ZERO(&readfds); | |
603 FD_SET(icq_UDPSok, &readfds); | |
604 | |
605 select(icq_UDPSok+1, &readfds, 0L, 0L, &tv); | |
606 | |
607 if(FD_ISSET(icq_UDPSok, &readfds)) | |
608 { | |
609 s = icq_UDPSockRead(icq_UDPSok, &pak.head, sizeof(pak)); | |
610 if(icqtohs(pak.head.cmd) == SRV_NEW_UIN) | |
611 { | |
612 icq_Uin = icqtohl(&pak.data[2]); | |
613 return; | |
614 } | |
615 } | |
616 } | |
617 } | |
618 */ | |
619 | |
620 /************************ | |
621 icq_UDPServMess functions | |
622 *************************/ | |
623 BOOL icq_GetServMess(ICQLINK *link, WORD num) | |
624 { | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
625 return ((link->d->icq_UDPServMess[num/8] & (1 << (num%8))) >> (num%8)); |
1152 | 626 } |
627 | |
628 void icq_SetServMess(ICQLINK *link, WORD num) | |
629 { | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
630 link->d->icq_UDPServMess[num/8] |= (1 << (num%8)); |
1152 | 631 } |
632 | |
633 int icq_GetSok(ICQLINK *link) | |
634 { | |
635 return link->icq_UDPSok; | |
636 } |