This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibgadu.c
1838 lines (1516 loc) · 42.6 KB
/
libgadu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: libgadu.c,v 1.148 2005/07/16 11:56:59 wojtekka Exp $ */
/*
* (C) Copyright 2001-2003 Wojtek Kaniewski <[email protected]>
* Robert J. Wo¼ny <[email protected]>
* Arkadiusz Mi¶kiewicz <[email protected]>
* Tomasz Chiliñski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version
* 2.1 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef sun
# include <sys/filio.h>
#endif
#include "libgadu-config.h"
#include <errno.h>
#include <netdb.h>
#ifdef __GG_LIBGADU_HAVE_PTHREAD
# include <pthread.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __GG_LIBGADU_HAVE_OPENSSL
# include <openssl/err.h>
# include <openssl/rand.h>
#endif
#include "compat.h"
#include "libgadu.h"
int gg_debug_level = 0;
int gg_dcc_port = 0;
unsigned long gg_dcc_ip = 0;
unsigned long gg_local_ip = 0;
/*
* zmienne opisuj±ce parametry proxy http.
*/
char *gg_proxy_host = NULL;
int gg_proxy_port = 0;
int gg_proxy_enabled = 0;
int gg_proxy_http_only = 0;
char *gg_proxy_username = NULL;
char *gg_proxy_password = NULL;
#ifndef lint
static char rcsid[]
#ifdef __GNUC__
__attribute__ ((unused))
#endif
= "$Id: libgadu.c,v 1.148 2005/07/16 11:56:59 wojtekka Exp $";
#endif
/*
* gg_libgadu_version()
*
* zwraca wersjê libgadu.
*
* - brak
*
* wersja libgadu.
*/
const char *gg_libgadu_version()
{
return GG_LIBGADU_VERSION;
}
/*
* gg_fix32()
*
* zamienia kolejno¶æ bajtów w liczbie 32-bitowej tak, by odpowiada³a
* kolejno¶ci bajtów w protokole GG. ze wzglêdu na LE-owo¶æ serwera,
* zamienia tylko na maszynach BE-wych.
*
* - x - liczba do zamiany
*
* liczba z odpowiedni± kolejno¶ci± bajtów.
*/
uint32_t gg_fix32(uint32_t x)
{
#ifndef __GG_LIBGADU_BIGENDIAN
return x;
#else
return (uint32_t)
(((x & (uint32_t) 0x000000ffU) << 24) |
((x & (uint32_t) 0x0000ff00U) << 8) |
((x & (uint32_t) 0x00ff0000U) >> 8) |
((x & (uint32_t) 0xff000000U) >> 24));
#endif
}
/*
* gg_fix16()
*
* zamienia kolejno¶æ bajtów w liczbie 16-bitowej tak, by odpowiada³a
* kolejno¶ci bajtów w protokole GG. ze wzglêdu na LE-owo¶æ serwera,
* zamienia tylko na maszynach BE-wych.
*
* - x - liczba do zamiany
*
* liczba z odpowiedni± kolejno¶ci± bajtów.
*/
uint16_t gg_fix16(uint16_t x)
{
#ifndef __GG_LIBGADU_BIGENDIAN
return x;
#else
return (uint16_t)
(((x & (uint16_t) 0x00ffU) << 8) |
((x & (uint16_t) 0xff00U) >> 8));
#endif
}
/*
* gg_login_hash() // funkcja wewnêtrzna
*
* liczy hash z has³a i danego seeda.
*
* - password - has³o do hashowania
* - seed - warto¶æ podana przez serwer
*
* hash.
*/
unsigned int gg_login_hash(const unsigned char *password, unsigned int seed)
{
unsigned int x, y, z;
y = seed;
for (x = 0; *password; password++) {
x = (x & 0xffffff00) | *password;
y ^= x;
y += x;
x <<= 8;
y ^= x;
x <<= 8;
y -= x;
x <<= 8;
y ^= x;
z = y & 0x1F;
y = (y << z) | (y >> (32 - z));
}
return y;
}
/*
* gg_resolve() // funkcja wewnêtrzna
*
* tworzy potok, forkuje siê i w drugim procesie zaczyna resolvowaæ
* podanego hosta. zapisuje w sesji deskryptor potoku. je¶li co¶ tam
* bêdzie gotowego, znaczy, ¿e mo¿na wczytaæ struct in_addr. je¶li
* nie znajdzie, zwraca INADDR_NONE.
*
* - fd - wska¼nik gdzie wrzuciæ deskryptor
* - pid - gdzie wrzuciæ pid procesu potomnego
* - hostname - nazwa hosta do zresolvowania
*
* 0, -1.
*/
int gg_resolve(int *fd, int *pid, const char *hostname)
{
int pipes[2], res;
struct in_addr a;
int errno2;
gg_debug(GG_DEBUG_FUNCTION, "** gg_resolve(%p, %p, \"%s\");\n", fd, pid, hostname);
if (!fd || !pid) {
errno = EFAULT;
return -1;
}
if (pipe(pipes) == -1)
return -1;
if ((res = fork()) == -1) {
errno2 = errno;
close(pipes[0]);
close(pipes[1]);
errno = errno2;
return -1;
}
if (!res) {
if ((a.s_addr = inet_addr(hostname)) == INADDR_NONE) {
struct in_addr *hn;
if (!(hn = gg_gethostbyname(hostname)))
a.s_addr = INADDR_NONE;
else {
a.s_addr = hn->s_addr;
free(hn);
}
}
write(pipes[1], &a, sizeof(a));
exit(0);
}
close(pipes[1]);
*fd = pipes[0];
*pid = res;
return 0;
}
#ifdef __GG_LIBGADU_HAVE_PTHREAD
struct gg_resolve_pthread_data {
char *hostname;
int fd;
};
static void *gg_resolve_pthread_thread(void *arg)
{
struct gg_resolve_pthread_data *d = arg;
struct in_addr a;
pthread_detach(pthread_self());
if ((a.s_addr = inet_addr(d->hostname)) == INADDR_NONE) {
struct in_addr *hn;
if (!(hn = gg_gethostbyname(d->hostname)))
a.s_addr = INADDR_NONE;
else {
a.s_addr = hn->s_addr;
free(hn);
}
}
write(d->fd, &a, sizeof(a));
close(d->fd);
free(d->hostname);
d->hostname = NULL;
free(d);
pthread_exit(NULL);
return NULL; /* ¿eby kompilator nie marudzi³ */
}
/*
* gg_resolve_pthread() // funkcja wewnêtrzna
*
* tworzy potok, nowy w±tek i w nim zaczyna resolvowaæ podanego hosta.
* zapisuje w sesji deskryptor potoku. je¶li co¶ tam bêdzie gotowego,
* znaczy, ¿e mo¿na wczytaæ struct in_addr. je¶li nie znajdzie, zwraca
* INADDR_NONE.
*
* - fd - wska¼nik do zmiennej przechowuj±cej desktyptor resolvera
* - resolver - wska¼nik do wska¼nika resolvera
* - hostname - nazwa hosta do zresolvowania
*
* 0, -1.
*/
int gg_resolve_pthread(int *fd, void **resolver, const char *hostname)
{
struct gg_resolve_pthread_data *d = NULL;
pthread_t *tmp;
int pipes[2], new_errno;
gg_debug(GG_DEBUG_FUNCTION, "** gg_resolve_pthread(%p, %p, \"%s\");\n", fd, resolver, hostname);
if (!resolver || !fd || !hostname) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_pthread() invalid arguments\n");
errno = EFAULT;
return -1;
}
if (!(tmp = malloc(sizeof(pthread_t)))) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_pthread() out of memory for pthread id\n");
return -1;
}
if (pipe(pipes) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_pthread() unable to create pipes (errno=%d, %s)\n", errno, strerror(errno));
free(tmp);
return -1;
}
if (!(d = malloc(sizeof(*d)))) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_pthread() out of memory\n");
new_errno = errno;
goto cleanup;
}
d->hostname = NULL;
if (!(d->hostname = strdup(hostname))) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_pthread() out of memory\n");
new_errno = errno;
goto cleanup;
}
d->fd = pipes[1];
if (pthread_create(tmp, NULL, gg_resolve_pthread_thread, d)) {
gg_debug(GG_DEBUG_MISC, "// gg_resolve_phread() unable to create thread\n");
new_errno = errno;
goto cleanup;
}
gg_debug(GG_DEBUG_MISC, "// gg_resolve_pthread() %p\n", tmp);
*resolver = tmp;
*fd = pipes[0];
return 0;
cleanup:
if (d) {
free(d->hostname);
free(d);
}
close(pipes[0]);
close(pipes[1]);
free(tmp);
errno = new_errno;
return -1;
}
#endif
/*
* gg_read() // funkcja pomocnicza
*
* czyta z gniazda okre¶lon± ilo¶æ bajtów. bierze pod uwagê, czy mamy
* po³±czenie zwyk³e czy TLS.
*
* - sess - sesja,
* - buf - bufor,
* - length - ilo¶æ bajtów,
*
* takie same warto¶ci jak read().
*/
int gg_read(struct gg_session *sess, char *buf, int length)
{
int res;
/* RL: mo¿e siê zdarzyæ, ¿e która¿ z funkcji z W32 przestawi socket na nie blokujacy.
W tym wypadku, recv bêdzie rzuca³ b³êdami na lewo i prawo... Wypada³oby siê przed tym
zabezpieczyæ...*/
#ifdef _WIN32
do {
#endif
#ifdef __GG_LIBGADU_HAVE_OPENSSL
if (sess->ssl) {
int err;
res = SSL_read(sess->ssl, buf, length);
if (res < 0) {
err = sess->ssl ? SSL_get_error(sess->ssl, res) : 0;
if (err == SSL_ERROR_WANT_READ)
errno = EAGAIN;
return -1;
}
} else
#endif
/*RL: na W32 read nie dzia³a na socketach nie wiedzieæ czemu...
Wszêdzie podmienione jest to przez #define, ale tutaj mo¿emy wstawiæ orygina³ :)
*/
res = recv(sess->fd, buf, length , 0);
#ifdef _WIN32
if (res == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
Sleep(100);
else break;
} while (1);
#endif
return res;
}
/*
* gg_write() // funkcja pomocnicza
*
* zapisuje do gniazda okre¶lon± ilo¶æ bajtów. bierze pod uwagê, czy mamy
* po³±czenie zwyk³e czy TLS.
*
* - sess - sesja,
* - buf - bufor,
* - length - ilo¶æ bajtów,
*
* takie same warto¶ci jak write().
*/
int gg_write(struct gg_session *sess, const char *buf, int length)
{
int res = 0;
#ifdef __GG_LIBGADU_HAVE_OPENSSL
if (sess->ssl) {
int err;
res = SSL_write(sess->ssl, buf, length);
if (res < 0) {
err = sess->ssl ? SSL_get_error(sess->ssl, res) : 0;
if (err == SSL_ERROR_WANT_WRITE)
errno = EAGAIN;
return -1;
}
} else
#endif
{
int written = 0;
while (written < length) {
res = write(sess->fd, buf + written, length - written);
if (res == -1) {
if (errno == EAGAIN)
continue;
else
break;
} else {
written += res;
res = written;
}
}
}
return res;
}
/*
* gg_recv_packet() // funkcja wewnêtrzna
*
* odbiera jeden pakiet i zwraca wska¼nik do niego. pamiêæ po nim
* nale¿y zwolniæ za pomoc± free().
*
* - sess - opis sesji
*
* w przypadku b³êdu NULL, kod b³êdu w errno. nale¿y zwróciæ uwagê, ¿e gdy
* po³±czenie jest nieblokuj±ce, a kod b³êdu wynosi EAGAIN, nie uda³o siê
* odczytaæ ca³ego pakietu i nie nale¿y tego traktowaæ jako b³±d.
*/
void *gg_recv_packet(struct gg_session *sess)
{
struct gg_header h;
char *buf = NULL;
int ret = 0;
unsigned int offset, size = 0;
gg_debug(GG_DEBUG_FUNCTION, "** gg_recv_packet(%p);\n", sess);
if (!sess) {
errno = EFAULT;
return NULL;
}
if (sess->recv_left < 1) {
if (sess->header_buf) {
memcpy(&h, sess->header_buf, sess->header_done);
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv: resuming last read (%d bytes left)\n", sizeof(h) - sess->header_done);
free(sess->header_buf);
sess->header_buf = NULL;
} else
sess->header_done = 0;
while (sess->header_done < sizeof(h)) {
ret = gg_read(sess, (char*) &h + sess->header_done, sizeof(h) - sess->header_done);
s_errno;
if (ret != -1 || errno != EINTR)
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv(%d,%p,%d) = %d\n", sess->fd, &h + sess->header_done, sizeof(h) - sess->header_done, ret);
if (!ret) {
errno = ECONNRESET;
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv() failed: connection broken\n");
return NULL;
}
if (ret == -1) {
if (errno == EINTR) {
/* RL - WOULDBLOCK */
//gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv() interrupted system call, resuming\n");
continue;
}
if (errno == EAGAIN) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv() incomplete header received\n");
if (!(sess->header_buf = malloc(sess->header_done))) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv() not enough memory\n");
return NULL;
}
memcpy(sess->header_buf, &h, sess->header_done);
return NULL;
}
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() header recv() failed: errno=%d, %s\n", errno, strerror(errno));
return NULL;
}
sess->header_done += ret;
}
h.type = gg_fix32(h.type);
h.length = gg_fix32(h.length);
} else
memcpy(&h, sess->recv_buf, sizeof(h));
/* jakie¶ sensowne limity na rozmiar pakietu */
if (h.length > 65535) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() invalid packet length (%d)\n", h.length);
errno = ERANGE;
return NULL;
}
if (sess->recv_left > 0) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() resuming last gg_recv_packet()\n");
size = sess->recv_left;
offset = sess->recv_done;
buf = sess->recv_buf;
} else {
if (!(buf = malloc(sizeof(h) + h.length + 1))) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() not enough memory for packet data\n");
return NULL;
}
memcpy(buf, &h, sizeof(h));
offset = 0;
size = h.length;
}
while (size > 0) {
ret = gg_read(sess, buf + sizeof(h) + offset, size);
s_errno;
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() body recv(%d,%p,%d) = %d\n", sess->fd, buf + sizeof(h) + offset, size, ret);
if (!ret) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() body recv() failed: connection broken\n");
errno = ECONNRESET;
return NULL;
}
if (ret > -1 && (unsigned)ret <= size) {
offset += ret;
size -= ret;
} else if (ret == -1) {
int errno2 = errno;
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() body recv() failed (errno=%d, %s)\n", errno, strerror(errno));
errno = errno2;
if (errno == EAGAIN) {
gg_debug(GG_DEBUG_MISC, "// gg_recv_packet() %d bytes received, %d left\n", offset, size);
sess->recv_buf = buf;
sess->recv_left = size;
sess->recv_done = offset;
return NULL;
}
if (errno != EINTR) {
free(buf);
return NULL;
}
}
}
sess->recv_left = 0;
if ((gg_debug_level & GG_DEBUG_DUMP)) {
unsigned int i;
gg_debug(GG_DEBUG_DUMP, "// gg_recv_packet(%.2x)", h.type);
for (i = 0; i < sizeof(h) + h.length; i++)
gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) buf[i]);
gg_debug(GG_DEBUG_DUMP, "\n");
}
return buf;
}
/*
* gg_send_packet() // funkcja wewnêtrzna
*
* konstruuje pakiet i wysy³a go do serwera.
*
* - sock - deskryptor gniazda
* - type - typ pakietu
* - payload_1 - pierwsza czê¶æ pakietu
* - payload_length_1 - d³ugo¶æ pierwszej czê¶ci
* - payload_2 - druga czê¶æ pakietu
* - payload_length_2 - d³ugo¶æ drugiej czê¶ci
* - ... - kolejne czê¶ci pakietu i ich d³ugo¶ci
* - NULL - koñcowym parametr (konieczny!)
*
* je¶li siê powiod³o, zwraca 0, w przypadku b³êdu -1. je¶li errno == ENOMEM,
* zabrak³o pamiêci. inaczej by³ b³±d przy wysy³aniu pakietu. dla errno == 0
* nie wys³ano ca³ego pakietu.
*/
int gg_send_packet(struct gg_session *sess, int type, ...)
{
struct gg_header *h;
char *tmp;
unsigned int tmp_length;
void *payload;
unsigned int payload_length;
va_list ap;
int res;
gg_debug(GG_DEBUG_FUNCTION, "** gg_send_packet(%p, 0x%.2x, ...)\n", sess, type);
tmp_length = sizeof(struct gg_header);
if (!(tmp = malloc(tmp_length))) {
gg_debug(GG_DEBUG_MISC, "// gg_send_packet() not enough memory for packet header\n");
return -1;
}
va_start(ap, type);
payload = va_arg(ap, void *);
while (payload) {
char *tmp2;
payload_length = va_arg(ap, unsigned int);
if (!(tmp2 = realloc(tmp, tmp_length + payload_length))) {
gg_debug(GG_DEBUG_MISC, "// gg_send_packet() not enough memory for payload\n");
free(tmp);
va_end(ap);
return -1;
}
tmp = tmp2;
memcpy(tmp + tmp_length, payload, payload_length);
tmp_length += payload_length;
payload = va_arg(ap, void *);
}
va_end(ap);
h = (struct gg_header*) tmp;
h->type = gg_fix32(type);
h->length = gg_fix32(tmp_length - sizeof(struct gg_header));
if ((gg_debug_level & GG_DEBUG_DUMP)) {
unsigned int i;
gg_debug(GG_DEBUG_DUMP, "// gg_send_packet(0x%.2x)", gg_fix32(h->type));
for (i = 0; i < tmp_length; ++i)
gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]);
gg_debug(GG_DEBUG_DUMP, "\n");
}
if ((unsigned)(res = gg_write(sess, tmp, tmp_length)) < tmp_length) {
s_errno;
gg_debug(GG_DEBUG_MISC, "// gg_send_packet() write() failed. res = %d, errno = %d (%s)\n", res, errno, strerror(errno));
free(tmp);
return -1;
}
free(tmp);
return 0;
}
/*
* gg_session_callback() // funkcja wewnêtrzna
*
* wywo³ywany z gg_session->callback, wykonuje gg_watch_fd() i pakuje
* do gg_session->event jego wynik.
*/
static int gg_session_callback(struct gg_session *s)
{
if (!s) {
errno = EFAULT;
return -1;
}
return ((s->event = gg_watch_fd(s)) != NULL) ? 0 : -1;
}
/*
* gg_login()
*
* rozpoczyna procedurê ³±czenia siê z serwerem. resztê obs³uguje siê przez
* gg_watch_fd().
*
* UWAGA! program musi obs³u¿yæ SIGCHLD, je¶li ³±czy siê asynchronicznie,
* ¿eby poprawnie zamkn±æ proces resolvera.
*
* - p - struktura opisuj±ca pocz±tkowy stan. wymagane pola: uin,
* password
*
* w przypadku b³êdu NULL, je¶li idzie dobrze (async) albo posz³o
* dobrze (sync), zwróci wska¼nik do zaalokowanej struct gg_session.
*/
struct gg_session *gg_login(/*RL: const*/ struct gg_login_params *p)
{
struct gg_session *sess = NULL;
char *hostname;
int port;
if (!p) {
gg_debug(GG_DEBUG_FUNCTION, "** gg_login(%p);\n", p);
errno = EFAULT;
return NULL;
}
gg_debug(GG_DEBUG_FUNCTION, "** gg_login(%p: [uin=%u, async=%d, ...]);\n", p, p->uin, p->async);
if (!(sess = malloc(sizeof(struct gg_session)))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() not enough memory for session data\n");
goto fail;
}
memset(sess, 0, sizeof(struct gg_session));
if (!p->password || !p->uin) {
gg_debug(GG_DEBUG_MISC, "// gg_login() invalid arguments. uin and password needed\n");
errno = EFAULT;
goto fail;
}
if (!(sess->password = strdup(p->password))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() not enough memory for password\n");
goto fail;
}
if (p->status_descr && !(sess->initial_descr = strdup(p->status_descr))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() not enough memory for status\n");
goto fail;
}
sess->uin = p->uin;
sess->state = GG_STATE_RESOLVING;
sess->check = GG_CHECK_READ;
sess->timeout = GG_DEFAULT_TIMEOUT;
sess->async = p->async;
sess->type = GG_SESSION_GG;
sess->initial_status = p->status;
sess->callback = gg_session_callback;
sess->destroy = gg_free_session;
sess->port = (p->server_port) ? p->server_port : (p->tls ? GG_HTTPS_PORT : GG_DEFAULT_PORT);
sess->server_addr = p->server_addr;
sess->external_port = p->external_port;
sess->external_addr = p->external_addr;
sess->protocol_version = (p->protocol_version) ? p->protocol_version : GG_DEFAULT_PROTOCOL_VERSION;
if (p->era_omnix)
sess->protocol_version |= GG_ERA_OMNIX_MASK;
if (p->has_audio)
sess->protocol_version |= GG_HAS_AUDIO_MASK;
sess->client_version = (p->client_version) ? strdup(p->client_version) : NULL;
sess->last_sysmsg = p->last_sysmsg;
sess->image_size = p->image_size;
sess->pid = -1;
if (p->tls == 1) {
#ifdef __GG_LIBGADU_HAVE_OPENSSL
char buf[1024];
OpenSSL_add_ssl_algorithms();
if (!RAND_status()) {
char rdata[1024];
struct {
time_t time;
void *ptr;
} rstruct;
time(&rstruct.time);
rstruct.ptr = (void *) &rstruct;
RAND_seed((void *) rdata, sizeof(rdata));
RAND_seed((void *) &rstruct, sizeof(rstruct));
}
sess->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
if (!sess->ssl_ctx) {
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
gg_debug(GG_DEBUG_MISC, "// gg_login() SSL_CTX_new() failed: %s\n", buf);
goto fail;
}
SSL_CTX_set_verify(sess->ssl_ctx, SSL_VERIFY_NONE, NULL);
sess->ssl = SSL_new(sess->ssl_ctx);
if (!sess->ssl) {
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
gg_debug(GG_DEBUG_MISC, "// gg_login() SSL_new() failed: %s\n", buf);
goto fail;
}
#else
gg_debug(GG_DEBUG_MISC, "// gg_login() client requested TLS but no support compiled in\n");
#endif
}
if (gg_proxy_enabled) {
hostname = gg_proxy_host;
sess->proxy_port = port = gg_proxy_port;
} else {
hostname = GG_APPMSG_HOST;
port = GG_APPMSG_PORT;
}
if (!p->async) {
struct in_addr a;
/* HAO | proxy_http_only*/
if ((!p->server_addr || !p->server_port) || (!gg_proxy_http_only && gg_proxy_enabled)) {
if ((a.s_addr = inet_addr(hostname)) == INADDR_NONE) {
struct in_addr *hn;
if (!(hn = gg_gethostbyname(hostname))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() host \"%s\" not found\n", hostname);
goto fail;
} else {
a.s_addr = hn->s_addr;
/* HAO | ustawiamy server_addr na pozniej */
if (gg_proxy_username && gg_proxy_http_only) {
free(hn);
if ((hn = gg_gethostbyname(GG_APPMSG_HOST))!=0) {
sess->server_addr = hn->s_addr;
}
}
free(hn);
}
}
} else {
a.s_addr = p->server_addr;
port = p->server_port;
}
sess->hub_addr = a.s_addr;
if (gg_proxy_enabled)
sess->proxy_addr = a.s_addr;
if ((sess->fd = gg_connect(&a, port, 0)) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() connection failed (errno=%d, %s)\n", errno, strerror(errno));
goto fail;
}
/* HAO | Proxy na nie synchronicznych */
if (gg_proxy_enabled) sess->proxy_addr = sess->hub_addr;
if (p->server_addr && p->server_port)
sess->state = GG_STATE_CONNECTING_GG;
else
sess->state = GG_STATE_CONNECTING_HUB;
while (sess->state != GG_STATE_CONNECTED) {
struct gg_event *e;
if (!(e = gg_watch_fd(sess))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() critical error in gg_watch_fd()\n");
goto fail;
}
if (e->type == GG_EVENT_CONN_FAILED) {
errno = EACCES;
p->failure = e->event.failure; // RL
gg_debug(GG_DEBUG_MISC, "// gg_login() could not login\n");
gg_event_free(e);
goto fail;
}
gg_event_free(e);
}
return sess;
}
if (!sess->server_addr || gg_proxy_enabled) {
#ifndef __GG_LIBGADU_HAVE_PTHREAD
if (gg_resolve(&sess->fd, &sess->pid, hostname)) {
#else
if (gg_resolve_pthread(&sess->fd, &sess->resolver, hostname)) {
#endif
gg_debug(GG_DEBUG_MISC, "// gg_login() resolving failed (errno=%d, %s)\n", errno, strerror(errno));
goto fail;
}
} else {
if ((sess->fd = gg_connect(&sess->server_addr, sess->port, sess->async)) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() direct connection failed (errno=%d, %s)\n", errno, strerror(errno));
goto fail;
}
sess->state = GG_STATE_CONNECTING_GG;
sess->check = GG_CHECK_WRITE;
}
return sess;
fail:
if (sess) {
if (sess->password)
free(sess->password);
if (sess->initial_descr)
free(sess->initial_descr);
free(sess);
}
return NULL;
}
/*
* gg_free_session()
*
* próbuje zamkn±æ po³±czenia i zwalnia pamiêæ zajmowan± przez sesjê.
*
* - sess - opis sesji
*/
void gg_free_session(struct gg_session *sess)
{
if (!sess)
return;
/* XXX dopisaæ zwalnianie i zamykanie wszystkiego, co mog³o zostaæ */
if (sess->password)
free(sess->password);
if (sess->initial_descr)
free(sess->initial_descr);
if (sess->client_version)
free(sess->client_version);
if (sess->header_buf)
free(sess->header_buf);
#ifdef __GG_LIBGADU_HAVE_OPENSSL
if (sess->ssl)
SSL_free(sess->ssl);
sess->ssl = 0; // RL
if (sess->ssl_ctx)
SSL_CTX_free(sess->ssl_ctx);
#endif
#ifdef __GG_LIBGADU_HAVE_PTHREAD
if (sess->resolver) {
pthread_cancel(*((pthread_t*) sess->resolver));
free(sess->resolver);
sess->resolver = NULL;
}
#else
// if (sess->pid != -1)
// waitpid(sess->pid, NULL, WNOHANG);
#endif
if (sess->fd != -1)
close(sess->fd);
while (sess->images)
gg_image_queue_remove(sess, sess->images, 1);
free(sess);
}
/*
* gg_change_status()
*
* zmienia status u¿ytkownika. przydatne do /away i /busy oraz /quit.
*
* - sess - opis sesji
* - status - nowy status u¿ytkownika