Mercurial > pidgin.yaz
diff libpurple/protocols/gg/lib/dcc.c @ 29938:6359fde67f4c
Update our internal libgadu to 1.9.0-rc2. This does not yet build on Windows.
Refs #10542. The Windows build errors are the only reason this isn't on
`im.pidgin.pidgin` already.
author | John Bailey <rekkanoryo@rekkanoryo.org> |
---|---|
date | Sun, 21 Feb 2010 16:52:42 +0000 |
parents | 259bbfb423d4 |
children | db6735e579f8 |
line wrap: on
line diff
--- a/libpurple/protocols/gg/lib/dcc.c Sun Feb 21 00:11:56 2010 +0000 +++ b/libpurple/protocols/gg/lib/dcc.c Sun Feb 21 16:52:42 2010 +0000 @@ -1,8 +1,9 @@ -/* $Id: dcc.c 16856 2006-08-19 01:13:25Z evands $ */ +/* $Id: dcc.c 711 2009-04-16 00:52:47Z darkjames $ */ /* - * (C) Copyright 2001-2002 Wojtek Kaniewski <wojtekka@irc.pl> - * Tomasz Chiliński <chilek@chilan.com> + * (C) Copyright 2001-2008 Wojtek Kaniewski <wojtekka@irc.pl> + * Tomasz ChiliĹ„ski <chilek@chilan.com> + * Adam Wysocki <gophi@ekg.chmurka.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License Version @@ -15,15 +16,18 @@ * * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, * USA. */ -#include "libgadu.h" +/** + * \file dcc.c + * + * \brief ObsĹ‚uga poĹ‚Ä…czeĹ„ bezpoĹ›rednich do wersji Gadu-Gadu 6.x + */ #include <sys/types.h> #include <sys/stat.h> -#ifndef _WIN32 #include <sys/ioctl.h> #include <sys/socket.h> #include <netinet/in.h> @@ -31,7 +35,6 @@ #ifdef sun # include <sys/filio.h> #endif -#endif #include <ctype.h> #include <errno.h> @@ -43,67 +46,72 @@ #include <unistd.h> #include "compat.h" +#include "libgadu.h" + #ifndef GG_DEBUG_DISABLE -/* - * gg_dcc_debug_data() // funkcja wewnętrzna + +/** + * \internal Przekazuje zawartość pakietu do odpluskwiania. * - * wy¶wietla zrzut pakietu w hexie. - * - * - prefix - prefiks zrzutu pakietu - * - fd - deskryptor gniazda - * - buf - bufor z danymi - * - size - rozmiar danych + * \param prefix Prefiks informacji + * \param fd Deskryptor gniazda + * \param buf Bufor z danumi + * \param size Rozmiar bufora z danymi */ static void gg_dcc_debug_data(const char *prefix, int fd, const void *buf, unsigned int size) { unsigned int i; - + gg_debug(GG_DEBUG_MISC, "++ gg_dcc %s (fd=%d,len=%d)", prefix, fd, size); - + for (i = 0; i < size; i++) gg_debug(GG_DEBUG_MISC, " %.2x", ((unsigned char*) buf)[i]); - + gg_debug(GG_DEBUG_MISC, "\n"); } #else #define gg_dcc_debug_data(a,b,c,d) do { } while (0) #endif -/* +/** + * WysyĹ‚a ĹĽÄ…danie zwrotnego poĹ‚Ä…czenia bezpoĹ›redniego. + * + * FunkcjÄ™ wykorzystuje siÄ™, jeĹ›li nie ma moĹĽliwoĹ›ci poĹ‚Ä…czenia siÄ™ z odbiorcÄ… + * pliku lub rozmowy gĹ‚osowej. Po otrzymaniu ĹĽÄ…dania druga strona sprĂłbuje + * nawiÄ…zać zwrotne poĹ‚Ä…czenie bezpoĹ›rednie z nadawcÄ…. * gg_dcc_request() * - * wysyła informację o tym, że dany klient powinien się z nami poł±czyć. - * wykorzystywane, kiedy druga strona, której chcemy co¶ wysłać jest za - * maskarad±. + * \param sess Struktura sesji + * \param uin Numer odbiorcy * - * - sess - struktura opisuj±ca sesję GG - * - uin - numerek odbiorcy + * \return Patrz \c gg_send_message_ctcp() * - * patrz gg_send_message_ctcp(). + * \ingroup dcc6 */ int gg_dcc_request(struct gg_session *sess, uin_t uin) { - return gg_send_message_ctcp(sess, GG_CLASS_CTCP, uin, (const unsigned char *)"\002", 1); + return gg_send_message_ctcp(sess, GG_CLASS_CTCP, uin, (unsigned char*) "\002", 1); } -/* - * gg_dcc_fill_filetime() // funkcja wewnętrzna +/** + * \internal Zamienia znacznik czasu w postaci uniksowej na format API WIN32. * - * zamienia czas w postaci unixowej na windowsowy. + * \note Funkcja dziaĹ‚a jedynie gdy kompilator obsĹ‚uguje typ danych + * \c long \c long. * - * - unix - czas w postaci unixowej - * - filetime - czas w postaci windowsowej + * \param ut Czas w postaci uniksowej + * \param ft Czas w postaci API WIN32 */ static void gg_dcc_fill_filetime(uint32_t ut, uint32_t *ft) { -#ifdef __GG_LIBGADU_HAVE_LONG_LONG +#ifdef GG_CONFIG_HAVE_LONG_LONG unsigned long long tmp; tmp = ut; tmp += 11644473600LL; tmp *= 10000000LL; -#ifndef __GG_LIBGADU_BIGENDIAN +#ifndef GG_CONFIG_BIGENDIAN ft[0] = (uint32_t) tmp; ft[1] = (uint32_t) (tmp >> 32); #else @@ -114,31 +122,33 @@ #endif } -/* - * gg_dcc_fill_file_info() +/** + * WypeĹ‚nia pola struktury \c gg_dcc niezbÄ™dne do wysĹ‚ania pliku. * - * wypełnia pola struct gg_dcc niezbędne do wysłania pliku. + * \note WiÄ™kszÄ… funkcjonalność zapewnia funkcja \c gg_dcc_fill_file_info2(). * - * - d - struktura opisuj±ca poł±czenie DCC - * - filename - nazwa pliku + * \param d Struktura poĹ‚Ä…czenia + * \param filename Nazwa pliku * - * 0, -1. + * \return 0 jeĹ›li siÄ™ powiodĹ‚o, -1 w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ int gg_dcc_fill_file_info(struct gg_dcc *d, const char *filename) { return gg_dcc_fill_file_info2(d, filename, filename); } -/* - * gg_dcc_fill_file_info2() - * - * wypełnia pola struct gg_dcc niezbędne do wysłania pliku. +/** + * WypeĹ‚nia pola struktury \c gg_dcc niezbÄ™dne do wysĹ‚ania pliku. * - * - d - struktura opisuj±ca poł±czenie DCC - * - filename - nazwa pliku - * - local_filename - nazwa na lokalnym systemie plików + * \param d Struktura poĹ‚Ä…czenia + * \param filename Nazwa pliku zapisywana w strukturze + * \param local_filename Nazwa pliku w lokalnym systemie plikĂłw * - * 0, -1. + * \return 0 jeĹ›li siÄ™ powiodĹ‚o, -1 w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ int gg_dcc_fill_file_info2(struct gg_dcc *d, const char *filename, const char *local_filename) { @@ -225,25 +235,23 @@ *q = 175; } } - + gg_debug(GG_DEBUG_MISC, "// gg_dcc_fill_file_info2() short name \"%s\", dos name \"%s\"\n", name, d->file_info.short_filename); - strncpy((char *)d->file_info.filename, name, sizeof(d->file_info.filename) - 1); + strncpy((char*) d->file_info.filename, name, sizeof(d->file_info.filename) - 1); return 0; } -/* - * gg_dcc_transfer() // funkcja wewnętrzna - * - * inicjuje proces wymiany pliku z danym klientem. +/** + * \internal Rozpoczyna poĹ‚Ä…czenie bezpoĹ›rednie z danym klientem. * - * - ip - adres ip odbiorcy - * - port - port odbiorcy - * - my_uin - własny numer - * - peer_uin - numer obiorcy - * - type - rodzaj wymiany (GG_SESSION_DCC_SEND lub GG_SESSION_DCC_GET) + * \param ip Adres IP odbiorcy + * \param port Port odbiorcy + * \param my_uin WĹ‚asny numer + * \param peer_uin Numer odbiorcy + * \param type Rodzaj poĹ‚Ä…czenia (\c GG_SESSION_DCC_SEND lub \c GG_SESSION_DCC_GET) * - * zaalokowana struct gg_dcc lub NULL je¶li wyst±pił bł±d. + * \return Struktura \c gg_dcc lub \c NULL w przypadku bĹ‚Ä™du */ static struct gg_dcc *gg_dcc_transfer(uint32_t ip, uint16_t port, uin_t my_uin, uin_t peer_uin, int type) { @@ -251,9 +259,9 @@ struct in_addr addr; addr.s_addr = ip; - + gg_debug(GG_DEBUG_FUNCTION, "** gg_dcc_transfer(%s, %d, %ld, %ld, %s);\n", inet_ntoa(addr), port, my_uin, peer_uin, (type == GG_SESSION_DCC_SEND) ? "SEND" : "GET"); - + if (!ip || ip == INADDR_NONE || !port || !my_uin || !peer_uin) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_transfer() invalid arguments\n"); errno = EINVAL; @@ -284,18 +292,17 @@ return d; } -/* - * gg_dcc_get_file() - * - * inicjuje proces odbierania pliku od danego klienta, gdy ten wysłał do - * nas ż±danie poł±czenia. +/** + * Rozpoczyna odbieranie pliku przez zwrotne poĹ‚Ä…czenie bezpoĹ›rednie. * - * - ip - adres ip odbiorcy - * - port - port odbiorcy - * - my_uin - własny numer - * - peer_uin - numer obiorcy + * \param ip Adres IP nadawcy + * \param port Port nadawcy + * \param my_uin WĹ‚asny numer + * \param peer_uin Numer nadawcy * - * zaalokowana struct gg_dcc lub NULL je¶li wyst±pił bł±d. + * \return Struktura \c gg_dcc lub \c NULL w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ struct gg_dcc *gg_dcc_get_file(uint32_t ip, uint16_t port, uin_t my_uin, uin_t peer_uin) { @@ -304,17 +311,17 @@ return gg_dcc_transfer(ip, port, my_uin, peer_uin, GG_SESSION_DCC_GET); } -/* - * gg_dcc_send_file() - * - * inicjuje proces wysyłania pliku do danego klienta. +/** + * Rozpoczyna wysyĹ‚anie pliku. * - * - ip - adres ip odbiorcy - * - port - port odbiorcy - * - my_uin - własny numer - * - peer_uin - numer obiorcy + * \param ip Adres IP odbiorcy + * \param port Port odbiorcy + * \param my_uin WĹ‚asny numer + * \param peer_uin Numer odbiorcy * - * zaalokowana struct gg_dcc lub NULL je¶li wyst±pił bł±d. + * \return Struktura \c gg_dcc lub \c NULL w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ struct gg_dcc *gg_dcc_send_file(uint32_t ip, uint16_t port, uin_t my_uin, uin_t peer_uin) { @@ -323,17 +330,17 @@ return gg_dcc_transfer(ip, port, my_uin, peer_uin, GG_SESSION_DCC_SEND); } -/* - * gg_dcc_voice_chat() - * - * próbuje nawi±zać poł±czenie głosowe. +/** + * Rozpoczyna poĹ‚Ä…czenie gĹ‚osowe. * - * - ip - adres ip odbiorcy - * - port - port odbiorcy - * - my_uin - własny numer - * - peer_uin - numer obiorcy + * \param ip Adres IP odbiorcy + * \param port Port odbiorcy + * \param my_uin WĹ‚asny numer + * \param peer_uin Numer odbiorcy * - * zaalokowana struct gg_dcc lub NULL je¶li wyst±pił bł±d. + * \return Struktura \c gg_dcc lub \c NULL w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ struct gg_dcc *gg_dcc_voice_chat(uint32_t ip, uint16_t port, uin_t my_uin, uin_t peer_uin) { @@ -342,30 +349,34 @@ return gg_dcc_transfer(ip, port, my_uin, peer_uin, GG_SESSION_DCC_VOICE); } -/* - * gg_dcc_set_type() +/** + * Ustawia typ przychodzÄ…cego poĹ‚Ä…czenia bezpoĹ›redniego. + * + * FunkcjÄ™ naleĹĽy wywoĹ‚ać po otrzymaniu zdarzenia \c GG_EVENT_DCC_CALLBACK. * - * po zdarzeniu GG_EVENT_DCC_CALLBACK należy ustawić typ poł±czenia za - * pomoc± tej funkcji. + * \param d Struktura poĹ‚Ä…czenia + * \param type Rodzaj poĹ‚Ä…czenia (\c GG_SESSION_DCC_SEND lub + * \c GG_SESSION_DCC_VOICE) * - * - d - struktura opisuj±ca poł±czenie - * - type - typ poł±czenia (GG_SESSION_DCC_SEND lub GG_SESSION_DCC_VOICE) + * \ingroup dcc6 */ void gg_dcc_set_type(struct gg_dcc *d, int type) { d->type = type; d->state = (type == GG_SESSION_DCC_SEND) ? GG_STATE_SENDING_FILE_INFO : GG_STATE_SENDING_VOICE_REQUEST; } - -/* - * gg_dcc_callback() // funkcja wewnętrzna + +/** + * \internal Funkcja zwrotna poĹ‚Ä…czenia bezpoĹ›redniego. * - * wywoływana z struct gg_dcc->callback, odpala gg_dcc_watch_fd i umieszcza - * rezultat w struct gg_dcc->event. + * Pole \c callback struktury \c gg_dcc zawiera wskaĹşnik do tej funkcji. + * WywoĹ‚uje ona \c gg_watch_fd() i zachowuje wynik w polu \c event. * - * - d - structura opisuj±ca poł±czenie + * \note Funkcjonalność funkcjo zwrotnej nie jest juĹĽ wspierana. * - * 0, -1. + * \param d Struktura poĹ‚Ä…czenia + * + * \return 0 jeĹ›li siÄ™ powiodĹ‚o, -1 w przypadku bĹ‚Ä™du */ static int gg_dcc_callback(struct gg_dcc *d) { @@ -376,25 +387,26 @@ return (e != NULL) ? 0 : -1; } -/* - * gg_dcc_socket_create() +/** + * Tworzy gniazdo nasĹ‚uchujÄ…ce dla poĹ‚Ä…czeĹ„ bezpoĹ›rednich. * - * tworzy gniazdo dla bezpo¶redniej komunikacji między klientami. + * Funkcja przywiÄ…zuje gniazdo do pierwszego wolnego portu TCP. * - * - uin - własny numer - * - port - preferowany port, je¶li równy 0 lub -1, próbuje domy¶lnego + * \param uin WĹ‚asny numer + * \param port Preferowany port (jeĹ›li rĂłwny 0 lub -1, prĂłbuje siÄ™ domyĹ›lnego) * - * zaalokowana struct gg_dcc, któr± poĽniej należy zwolnić funkcj± - * gg_dcc_free(), albo NULL je¶li wyst±pił bł±d. + * \return Struktura \c gg_dcc lub \c NULL w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ struct gg_dcc *gg_dcc_socket_create(uin_t uin, uint16_t port) { struct gg_dcc *c; struct sockaddr_in sin; int sock, bound = 0, errno2; - + gg_debug(GG_DEBUG_FUNCTION, "** gg_create_dcc_socket(%d, %d);\n", uin, port); - + if (!uin) { gg_debug(GG_DEBUG_MISC, "// gg_create_dcc_socket() invalid arguments\n"); errno = EINVAL; @@ -408,12 +420,12 @@ if (!port) port = GG_DEFAULT_DCC_PORT; - + while (!bound) { sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); - + gg_debug(GG_DEBUG_MISC, "// gg_create_dcc_socket() trying port %d\n", port); if (!bind(sock, (struct sockaddr*) &sin, sizeof(sin))) bound = 1; @@ -433,7 +445,7 @@ errno = errno2; return NULL; } - + gg_debug(GG_DEBUG_MISC, "// gg_create_dcc_socket() bound to port %d\n", port); if (!(c = malloc(sizeof(*c)))) { @@ -452,20 +464,20 @@ c->check = GG_CHECK_READ; c->callback = gg_dcc_callback; c->destroy = gg_dcc_free; - + return c; } -/* - * gg_dcc_voice_send() - * - * wysyła ramkę danych dla rozmowy głosowej. +/** + * WysyĹ‚a ramkÄ™ danych poĹ‚Ä…czenia gĹ‚osowego. * - * - d - struktura opisuj±ca poł±czenie dcc - * - buf - bufor z danymi - * - length - rozmiar ramki + * \param d Struktura poĹ‚Ä…czenia + * \param buf Bufor z danymi + * \param length DĹ‚ugość bufora z danymi * - * 0, -1. + * \return 0 jeĹ›li siÄ™ powiodĹ‚o, -1 w przypadku bĹ‚Ä™du + * + * \ingroup dcc6 */ int gg_dcc_voice_send(struct gg_dcc *d, char *buf, int length) { @@ -500,7 +512,14 @@ return 0; } -#define gg_read(fd, buf, size) \ +/** + * \internal Odbiera dane z poĹ‚Ä…czenia bezpoĹ›redniego z obsĹ‚ugÄ… bĹ‚Ä™dĂłw. + * + * \param fd Deskryptor gniazda + * \param buf Bufor na dane + * \param size Rozmiar bufora na dane + */ +#define gg_dcc_read(fd, buf, size) \ { \ int tmp = read(fd, buf, size); \ \ @@ -517,9 +536,16 @@ return e; \ } \ gg_dcc_debug_data("read", fd, buf, size); \ -} +} -#define gg_write(fd, buf, size) \ +/** + * \internal WysyĹ‚a dane do poĹ‚Ä…czenia bezpoĹ›redniego z obsĹ‚ugÄ… bĹ‚Ä™dĂłw. + * + * \param fd Deskryptor gniazda + * \param buf Bufor z danymi + * \param size Rozmiar bufora z danymi + */ +#define gg_dcc_write(fd, buf, size) \ { \ int tmp; \ gg_dcc_debug_data("write", fd, buf, size); \ @@ -536,14 +562,18 @@ } \ } -/* - * gg_dcc_watch_fd() +/** + * Funkcja wywoĹ‚ywana po zaobserwowaniu zmian na deskryptorze poĹ‚Ä…czenia. * - * funkcja, któr± należy wywołać, gdy co¶ się zmieni na gg_dcc->fd. + * Funkcja zwraca strukturÄ™ zdarzenia \c gg_event. JeĹ›li rodzaj zdarzenia + * to \c GG_EVENT_NONE, nie wydarzyĹ‚o siÄ™ jeszcze nic wartego odnotowania. + * StrukturÄ™ zdarzenia naleĹĽy zwolnić funkcja \c gg_event_free. * - * - h - struktura zwrócona przez gg_create_dcc_socket() + * \param h Struktura poĹ‚Ä…czenia * - * zaalokowana struct gg_event lub NULL, je¶li zabrakło pamięci na ni±. + * \return Struktura zdarzenia lub \c NULL jeĹ›li wystÄ…piĹ‚ bĹ‚Ä…d + * + * \ingroup dcc6 */ struct gg_event *gg_dcc_watch_fd(struct gg_dcc *h) { @@ -551,7 +581,7 @@ int foo; gg_debug(GG_DEBUG_FUNCTION, "** gg_dcc_watch_fd(%p);\n", h); - + if (!h || (h->type != GG_SESSION_DCC && h->type != GG_SESSION_DCC_SOCKET && h->type != GG_SESSION_DCC_SEND && h->type != GG_SESSION_DCC_GET && h->type != GG_SESSION_DCC_VOICE)) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() invalid argument\n"); errno = EINVAL; @@ -568,10 +598,9 @@ if (h->type == GG_SESSION_DCC_SOCKET) { struct sockaddr_in sin; struct gg_dcc *c; - int fd; - socklen_t sin_len = sizeof(sin); - int one = 1; - + int fd, one = 1; + unsigned int sin_len = sizeof(sin); + if ((fd = accept(h->fd, (struct sockaddr*) &sin, &sin_len)) == -1) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() can't accept() new connection (errno=%d, %s)\n", errno, strerror(errno)); return e; @@ -607,7 +636,7 @@ c->file_fd = -1; c->remote_addr = sin.sin_addr.s_addr; c->remote_port = ntohs(sin.sin_port); - + e->type = GG_EVENT_DCC_NEW; e->event.dcc_new = c; @@ -617,8 +646,7 @@ struct gg_dcc_small_packet small; struct gg_dcc_big_packet big; int size, tmp, res; - socklen_t res_size = sizeof(res); - unsigned int utmp; + unsigned int utmp, res_size = sizeof(res); char buf[1024], ack[] = "UDAG"; struct gg_dcc_file_info_packet { @@ -634,8 +662,8 @@ uin_t uin; gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_READING_UIN_%d\n", (h->state == GG_STATE_READING_UIN_1) ? 1 : 2); - - gg_read(h->fd, &uin, sizeof(uin)); + + gg_dcc_read(h->fd, &uin, sizeof(uin)); if (h->state == GG_STATE_READING_UIN_1) { h->state = GG_STATE_READING_UIN_2; @@ -656,7 +684,7 @@ case GG_STATE_SENDING_ACK: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_SENDING_ACK\n"); - gg_write(h->fd, ack, 4); + gg_dcc_write(h->fd, ack, 4); h->state = GG_STATE_READING_TYPE; h->check = GG_CHECK_READ; @@ -666,8 +694,8 @@ case GG_STATE_READING_TYPE: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_TYPE\n"); - - gg_read(h->fd, &small, sizeof(small)); + + gg_dcc_read(h->fd, &small, sizeof(small)); small.type = gg_fix32(small.type); @@ -680,7 +708,7 @@ h->timeout = GG_DEFAULT_TIMEOUT; e->type = GG_EVENT_DCC_CALLBACK; - + break; case 0x0002: /* XXX */ @@ -703,8 +731,8 @@ case GG_STATE_READING_REQUEST: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_REQUEST\n"); - - gg_read(h->fd, &small, sizeof(small)); + + gg_dcc_read(h->fd, &small, sizeof(small)); small.type = gg_fix32(small.type); @@ -715,7 +743,7 @@ h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; break; - + case 0x0003: /* XXX */ gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() voice chat request\n"); h->state = GG_STATE_SENDING_VOICE_ACK; @@ -725,22 +753,22 @@ e->type = GG_EVENT_DCC_NEED_VOICE_ACK; break; - + default: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() unknown dcc request (%.4x) from %ld\n", small.type, h->peer_uin); e->type = GG_EVENT_DCC_ERROR; e->event.dcc_error = GG_ERROR_DCC_HANDSHAKE; } - + return e; case GG_STATE_READING_FILE_INFO: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_FILE_INFO\n"); - - gg_read(h->fd, &file_info_packet, sizeof(file_info_packet)); + + gg_dcc_read(h->fd, &file_info_packet, sizeof(file_info_packet)); memcpy(&h->file_info, &file_info_packet.file_info, sizeof(h->file_info)); - + h->file_info.mode = gg_fix32(h->file_info.mode); h->file_info.size = gg_fix32(h->file_info.size); @@ -749,17 +777,17 @@ h->timeout = GG_DCC_TIMEOUT_FILE_ACK; e->type = GG_EVENT_DCC_NEED_FILE_ACK; - + return e; case GG_STATE_SENDING_FILE_ACK: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_FILE_ACK\n"); - + big.type = gg_fix32(0x0006); /* XXX */ big.dunno1 = gg_fix32(h->offset); big.dunno2 = 0; - gg_write(h->fd, &big, sizeof(big)); + gg_dcc_write(h->fd, &big, sizeof(big)); h->state = GG_STATE_READING_FILE_HEADER; h->chunk_size = sizeof(big); @@ -773,25 +801,25 @@ h->timeout = GG_DEFAULT_TIMEOUT; return e; - + case GG_STATE_SENDING_VOICE_ACK: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_VOICE_ACK\n"); - + tiny.type = 0x01; /* XXX */ - gg_write(h->fd, &tiny, sizeof(tiny)); + gg_dcc_write(h->fd, &tiny, sizeof(tiny)); h->state = GG_STATE_READING_VOICE_HEADER; h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; h->offset = 0; - + return e; - + case GG_STATE_READING_FILE_HEADER: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_FILE_HEADER\n"); - + tmp = read(h->fd, h->chunk_buf + h->chunk_offset, h->chunk_size - h->chunk_offset); if (tmp == -1) { @@ -802,7 +830,7 @@ } gg_dcc_debug_data("read", h->fd, h->chunk_buf + h->chunk_offset, h->chunk_size - h->chunk_offset); - + h->chunk_offset += tmp; if (h->chunk_offset < h->chunk_size) @@ -823,7 +851,7 @@ return e; } - if (h->chunk_size == 0) { + if (h->chunk_size == 0) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() empty chunk, EOF\n"); e->type = GG_EVENT_DCC_DONE; return e; @@ -833,13 +861,13 @@ h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; h->established = 1; - + return e; case GG_STATE_READING_VOICE_HEADER: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_VOICE_HEADER\n"); - - gg_read(h->fd, &tiny, sizeof(tiny)); + + gg_dcc_read(h->fd, &tiny, sizeof(tiny)); switch (tiny.type) { case 0x03: /* XXX */ @@ -850,19 +878,19 @@ break; case 0x04: /* XXX */ gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() peer breaking connection\n"); - /* XXX zwracać odpowiedni event */ + /* XXX zwracać odpowiedni event */ default: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() unknown request (%.2x)\n", tiny.type); e->type = GG_EVENT_DCC_ERROR; e->event.dcc_error = GG_ERROR_DCC_HANDSHAKE; } - + return e; case GG_STATE_READING_VOICE_SIZE: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_VOICE_SIZE\n"); - - gg_read(h->fd, &small, sizeof(small)); + + gg_dcc_read(h->fd, &small, sizeof(small)); small.type = gg_fix32(small.type); @@ -870,7 +898,7 @@ gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() invalid voice frame size (%d)\n", small.type); e->type = GG_EVENT_DCC_ERROR; e->event.dcc_error = GG_ERROR_DCC_NET; - + return e; } @@ -879,18 +907,19 @@ if (!(h->voice_buf = malloc(h->chunk_size))) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() out of memory for voice frame\n"); + free(e); return NULL; } h->state = GG_STATE_READING_VOICE_DATA; h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; - + return e; case GG_STATE_READING_VOICE_DATA: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_VOICE_DATA\n"); - + tmp = read(h->fd, h->voice_buf + h->chunk_offset, h->chunk_size - h->chunk_offset); if (tmp < 1) { if (tmp == -1) { @@ -909,7 +938,7 @@ if (h->chunk_offset >= h->chunk_size) { e->type = GG_EVENT_DCC_VOICE_DATA; - e->event.dcc_voice_data.data = h->voice_buf; + e->event.dcc_voice_data.data = (unsigned char*) h->voice_buf; e->event.dcc_voice_data.length = h->chunk_size; h->state = GG_STATE_READING_VOICE_HEADER; h->voice_buf = NULL; @@ -917,7 +946,7 @@ h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; - + return e; case GG_STATE_CONNECTING: @@ -925,7 +954,7 @@ uin_t uins[2]; gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_CONNECTING\n"); - + res = 0; if ((foo = getsockopt(h->fd, SOL_SOCKET, SO_ERROR, &res, &res_size)) || res) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() connection failed (fd=%d,errno=%d(%s),foo=%d,res=%d(%s))\n", h->fd, errno, strerror(errno), foo, res, strerror(res)); @@ -935,23 +964,23 @@ } gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() connected, sending uins\n"); - + uins[0] = gg_fix32(h->uin); uins[1] = gg_fix32(h->peer_uin); - gg_write(h->fd, uins, sizeof(uins)); - + gg_dcc_write(h->fd, uins, sizeof(uins)); + h->state = GG_STATE_READING_ACK; h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; - + return e; } case GG_STATE_READING_ACK: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_ACK\n"); - - gg_read(h->fd, buf, 4); + + gg_dcc_read(h->fd, buf, 4); if (strncmp(buf, ack, 4)) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() did't get ack\n"); @@ -964,29 +993,29 @@ h->check = GG_CHECK_WRITE; h->timeout = GG_DEFAULT_TIMEOUT; h->state = GG_STATE_SENDING_REQUEST; - + return e; case GG_STATE_SENDING_VOICE_REQUEST: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_VOICE_REQUEST\n"); small.type = gg_fix32(0x0003); - - gg_write(h->fd, &small, sizeof(small)); + + gg_dcc_write(h->fd, &small, sizeof(small)); h->state = GG_STATE_READING_VOICE_ACK; h->check = GG_CHECK_READ; h->timeout = GG_DEFAULT_TIMEOUT; - + return e; - + case GG_STATE_SENDING_REQUEST: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_REQUEST\n"); small.type = (h->type == GG_SESSION_DCC_GET) ? gg_fix32(0x0003) : gg_fix32(0x0002); /* XXX */ - - gg_write(h->fd, &small, sizeof(small)); - + + gg_dcc_write(h->fd, &small, sizeof(small)); + switch (h->type) { case GG_SESSION_DCC_GET: h->state = GG_STATE_READING_REQUEST; @@ -1002,7 +1031,7 @@ if (h->file_fd == -1) e->type = GG_EVENT_DCC_NEED_FILE_INFO; break; - + case GG_SESSION_DCC_VOICE: h->state = GG_STATE_SENDING_VOICE_REQUEST; h->check = GG_CHECK_WRITE; @@ -1011,7 +1040,7 @@ } return e; - + case GG_STATE_SENDING_FILE_INFO: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_FILE_INFO\n"); @@ -1021,8 +1050,8 @@ } small.type = gg_fix32(0x0001); /* XXX */ - - gg_write(h->fd, &small, sizeof(small)); + + gg_dcc_write(h->fd, &small, sizeof(small)); file_info_packet.big.type = gg_fix32(0x0003); /* XXX */ file_info_packet.big.dunno1 = 0; @@ -1030,26 +1059,26 @@ memcpy(&file_info_packet.file_info, &h->file_info, sizeof(h->file_info)); - /* zostaj± teraz u nas, więc odwracamy z powrotem */ + /* zostajÄ… teraz u nas, wiÄ™c odwracamy z powrotem */ h->file_info.size = gg_fix32(h->file_info.size); h->file_info.mode = gg_fix32(h->file_info.mode); - - gg_write(h->fd, &file_info_packet, sizeof(file_info_packet)); + + gg_dcc_write(h->fd, &file_info_packet, sizeof(file_info_packet)); h->state = GG_STATE_READING_FILE_ACK; h->check = GG_CHECK_READ; h->timeout = GG_DCC_TIMEOUT_FILE_ACK; return e; - + case GG_STATE_READING_FILE_ACK: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_FILE_ACK\n"); - - gg_read(h->fd, &big, sizeof(big)); + + gg_dcc_read(h->fd, &big, sizeof(big)); - /* XXX sprawdzać wynik */ + /* XXX sprawdzać wynik */ h->offset = gg_fix32(big.dunno1); - + h->state = GG_STATE_SENDING_FILE_HEADER; h->check = GG_CHECK_WRITE; h->timeout = GG_DEFAULT_TIMEOUT; @@ -1060,8 +1089,8 @@ case GG_STATE_READING_VOICE_ACK: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_READING_VOICE_ACK\n"); - - gg_read(h->fd, &tiny, sizeof(tiny)); + + gg_dcc_read(h->fd, &tiny, sizeof(tiny)); if (tiny.type != 0x01) { gg_debug(GG_DEBUG_MISC, "// invalid reply (%.2x), connection refused\n", tiny.type); @@ -1075,14 +1104,14 @@ h->timeout = GG_DEFAULT_TIMEOUT; e->type = GG_EVENT_DCC_ACK; - + return e; case GG_STATE_SENDING_FILE_HEADER: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_FILE_HEADER\n"); - + h->chunk_offset = 0; - + if ((h->chunk_size = h->file_info.size - h->offset) > 4096) { h->chunk_size = 4096; big.type = gg_fix32(0x0003); /* XXX */ @@ -1091,8 +1120,8 @@ big.dunno1 = gg_fix32(h->chunk_size); big.dunno2 = 0; - - gg_write(h->fd, &big, sizeof(big)); + + gg_dcc_write(h->fd, &big, sizeof(big)); h->state = GG_STATE_SENDING_FILE; h->check = GG_CHECK_WRITE; @@ -1100,13 +1129,13 @@ h->established = 1; return e; - + case GG_STATE_SENDING_FILE: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_SENDING_FILE\n"); - + if ((utmp = h->chunk_size - h->chunk_offset) > sizeof(buf)) utmp = sizeof(buf); - + gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() offset=%d, size=%d\n", h->offset, h->file_info.size); /* koniec pliku? */ @@ -1117,11 +1146,17 @@ return e; } + if (h->offset >= h->file_info.size) { + gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() offset >= size, finished\n"); + e->type = GG_EVENT_DCC_DONE; + return e; + } + lseek(h->file_fd, h->offset, SEEK_SET); size = read(h->file_fd, buf, utmp); - /* bł±d */ + /* bĹ‚Ä…d */ if (size == -1) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() read() failed. (errno=%d, %s)\n", errno, strerror(errno)); @@ -1139,8 +1174,8 @@ return e; } - - /* je¶li wczytali¶my więcej, utnijmy. */ + + /* jeĹ›li wczytaliĹ›my wiÄ™cej, utnijmy. */ if (h->offset + size > h->file_info.size) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() read() too much (read=%d, ofs=%d, size=%d)\n", size, h->offset, h->file_info.size); size = h->file_info.size - h->offset; @@ -1161,15 +1196,22 @@ return e; } - h->offset += size; - + if (tmp == 0) { + gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() write() failed (connection reset)\n"); + e->type = GG_EVENT_DCC_ERROR; + e->event.dcc_error = GG_ERROR_DCC_NET; + return e; + } + + h->offset += tmp; + if (h->offset >= h->file_info.size) { e->type = GG_EVENT_DCC_DONE; return e; } - - h->chunk_offset += size; - + + h->chunk_offset += tmp; + if (h->chunk_offset >= h->chunk_size) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() chunk finished\n"); h->state = GG_STATE_SENDING_FILE_HEADER; @@ -1178,22 +1220,28 @@ h->state = GG_STATE_SENDING_FILE; h->timeout = GG_DCC_TIMEOUT_SEND; } - + h->check = GG_CHECK_WRITE; return e; case GG_STATE_GETTING_FILE: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_GETTING_FILE\n"); - + if ((utmp = h->chunk_size - h->chunk_offset) > sizeof(buf)) utmp = sizeof(buf); - + + if (h->offset >= h->file_info.size) { + gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() offset >= size, finished\n"); + e->type = GG_EVENT_DCC_DONE; + return e; + } + size = read(h->fd, buf, utmp); gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() ofs=%d, size=%d, read()=%d\n", h->offset, h->file_info.size, size); - - /* bł±d */ + + /* bĹ‚Ä…d */ if (size == -1) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() read() failed. (errno=%d, %s)\n", errno, strerror(errno)); @@ -1211,9 +1259,9 @@ return e; } - + tmp = write(h->file_fd, buf, size); - + if (tmp == -1 || tmp < size) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() write() failed (%d:fd=%d:res=%d:%s)\n", tmp, h->file_fd, size, strerror(errno)); e->type = GG_EVENT_DCC_ERROR; @@ -1222,14 +1270,14 @@ } h->offset += size; - + if (h->offset >= h->file_info.size) { e->type = GG_EVENT_DCC_DONE; return e; } h->chunk_offset += size; - + if (h->chunk_offset >= h->chunk_size) { gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() chunk finished\n"); h->state = GG_STATE_READING_FILE_HEADER; @@ -1245,11 +1293,11 @@ h->state = GG_STATE_GETTING_FILE; h->timeout = GG_DCC_TIMEOUT_GET; } - + h->check = GG_CHECK_READ; return e; - + default: gg_debug(GG_DEBUG_MISC, "// gg_dcc_watch_fd() GG_STATE_???\n"); e->type = GG_EVENT_DCC_ERROR; @@ -1258,35 +1306,28 @@ return e; } } - + return e; } -#undef gg_read -#undef gg_write - -/* - * gg_dcc_free() +/** + * Zwalnia zasoby uĹĽywane przez poĹ‚Ä…czenie bezpoĹ›rednie. * - * zwalnia pamięć po strukturze poł±czenia dcc. + * \param d Struktura poĹ‚Ä…czenia * - * - d - zwalniana struktura + * \ingroup dcc6 */ void gg_dcc_free(struct gg_dcc *d) { gg_debug(GG_DEBUG_FUNCTION, "** gg_dcc_free(%p);\n", d); - + if (!d) return; if (d->fd != -1) close(d->fd); - if (d->chunk_buf) { - free(d->chunk_buf); - d->chunk_buf = NULL; - } - + free(d->chunk_buf); free(d); }