Re: [elinks-dev] Errors in bittorent

2008-08-30 Thread Witold Filipczyk
On Sat, Aug 30, 2008 at 12:24:40PM +0200, Jonas Fonseca wrote:
> Some comments ...
> 
> Witold Filipczyk <[EMAIL PROTECTED]> wrote Sat, Aug 30, 2008:
> > commit b589f19b73c65621c0a8582199509f58dbcac09f
> > Author: Witold Filipczyk <[EMAIL PROTECTED]>
> > AuthorDate: Sat Aug 30 10:52:00 2008 +0200
> > Commit: Witold Filipczyk <[EMAIL PROTECTED]>
> > CommitDate: Sat Aug 30 10:52:00 2008 +0200
> > 
> > An issue with bittorrent.
> > 
> > It was possible that the reference to the automatic variable
> > uri of the make_bittorrent_peer_connection trashed the stack.
> > In addition done_uri crashed because uri->string was NULL.
> > 
> > Now uri is allocated and unlocked to avoid memleak.
> 
> 
> Instead of fixing this hack, why don't you add a setup_connection() that
> make_connection() can use as a wrapper and be done with this. Or if this
> is "bittorrent" URI thing actually works, why do you mess with the URI struct
> yourself? Didn't parse_uri() do all that for you already? Also don't
> unluck the URI, call done_uri(uri) after the call to make_connection().

It is not so easy, because bittorrent in ELinks uses free syntax.
I'm cheating a bit here and using http instead of bittorrent.

diff --git a/src/protocol/bittorrent/peerconnect.c 
b/src/protocol/bittorrent/peerconnect.c
index aeafbf3..7a89523 100644
--- a/src/protocol/bittorrent/peerconnect.c
+++ b/src/protocol/bittorrent/peerconnect.c
@@ -271,9 +271,11 @@ enum bittorrent_state
 make_bittorrent_peer_connection(struct bittorrent_connection *bittorrent,
struct bittorrent_peer *peer_info)
 {
-   struct uri uri;
+   struct string uri_string;
+   struct uri *uri;
struct bittorrent_peer_connection *peer;
-   unsigned char port[5];
+   unsigned char port[6];
+   int port_length;
 
peer = init_bittorrent_peer_connection(-1);
if (!peer) return BITTORRENT_STATE_OUT_OF_MEM;
@@ -296,14 +298,27 @@ make_bittorrent_peer_connection(struct 
bittorrent_connection *bittorrent,
/* FIXME: Rather change the make_connection() interface. This is an ugly
 * hack. */
/* FIXME: Set the ipv6 flag iff ... */
-   memset(&uri, 0, sizeof(uri));
-   uri.protocol = PROTOCOL_BITTORRENT;
-   uri.host = peer_info->ip;
-   uri.hostlen  = strlen(peer_info->ip);
-   uri.port = port;
-   uri.portlen  = snprintf(port, sizeof(port), "%u", peer_info->port);
-
-   make_connection(peer->socket, &uri, send_bittorrent_peer_handshake, 1);
+   if (!init_string(&uri_string)) {
+   done_bittorrent_peer_connection(peer);
+   return BITTORRENT_STATE_OUT_OF_MEM;
+   }
+   /* The bittorrent protocol uses free syntax.
+* It does not set uri->port nor uri->host */
+   add_to_string(&uri_string, "http://";);
+   add_to_string(&uri_string, peer_info->ip);
+   add_char_to_string(&uri_string, ':');
+   port_length = snprintf(port, sizeof(port), "%u", peer_info->port);
+   add_bytes_to_string(&uri_string, port, port_length);
+
+   uri = get_uri(uri_string.source, URI_BASE);
+   done_string(&uri_string);
+   if (!uri) {
+   done_bittorrent_peer_connection(peer);
+   return BITTORRENT_STATE_OUT_OF_MEM;
+   }
+   uri->protocol = PROTOCOL_BITTORRENT;
+   make_connection(peer->socket, uri, send_bittorrent_peer_handshake, 1);
+   done_uri(uri);
 
return BITTORRENT_STATE_OK;
 }
___
elinks-dev mailing list
elinks-dev@linuxfromscratch.org
http://linuxfromscratch.org/mailman/listinfo/elinks-dev


Re: [elinks-dev] Encodings in xbel

2008-08-30 Thread Witold Filipczyk
This time better patches.
Bookmarks in the xbel format are written using the System codepage.
Only the "&" is written as "&". The rest is written without
changes.

Witek
commit 9ac9ee459c4ec98c6cf253db304f6b84872eae68
Author: Witold Filipczyk <[EMAIL PROTECTED]>
Date:   Sat Aug 30 12:45:04 2008 +0200

Moved declaration of the struct codepage_desc to the header file.

diff --git a/src/intl/charsets.c b/src/intl/charsets.c
index de853b9..03f7f2b 100644
--- a/src/intl/charsets.c
+++ b/src/intl/charsets.c
@@ -29,43 +29,6 @@
 #include "util/memory.h"
 #include "util/string.h"
 
-
-/* Fix namespace clash on MacOS. */
-#define table table_elinks
-
-struct table_entry {
-   unsigned char c;
-   /* This should in principle be unicode_val_T, but because all
-* the values currently in codepage.inc fit in 16 bits, we can
-* as well use uint16_t and halve sizeof(struct table_entry)
-* from 8 bytes to 4.  Should other characters ever be needed,
-* unicode_val_T u : 24 might be a possibility, although it
-* seems a little unportable as bitfields are in principle
-* restricted to int, which may be 16-bit.  */
-   uint16_t u;
-};
-
-struct codepage_desc {
-   unsigned char *name;
-   unsigned char *const *aliases;
-
-   /* The Unicode mappings of codepage bytes 0x80...0xFF.
-* (0x00...0x7F are assumed to be ASCII in all codepages.)
-* Because all current values fit in 16 bits, we store them as
-* uint16_t rather than unicode_val_T.  If the codepage does
-* not use some byte, then @highhalf maps that byte to 0x,
-* which C code converts to UCS_REPLACEMENT_CHARACTER where
-* appropriate.  (U+ is reserved and will never be
-* assigned as a character.)  */
-   const uint16_t *highhalf;
-
-   /* If some byte in the codepage corresponds to multiple Unicode
-* characters, then the preferred character is in @highhalf
-* above, and the rest are listed here in @table.  This table
-* is not used for translating from the codepage to Unicode.  */
-   const struct table_entry *table;
-};
-
 #include "intl/codepage.inc"
 #include "intl/uni_7b.inc"
 #include "intl/entity.inc"
diff --git a/src/intl/charsets.h b/src/intl/charsets.h
index d87e2ee..5d77833 100644
--- a/src/intl/charsets.h
+++ b/src/intl/charsets.h
@@ -74,6 +74,44 @@ struct conv_table {
} u;
 };
 
+/* Fix namespace clash on MacOS. */
+#define table table_elinks
+
+struct table_entry {
+   unsigned char c;
+   /* This should in principle be unicode_val_T, but because all
+* the values currently in codepage.inc fit in 16 bits, we can
+* as well use uint16_t and halve sizeof(struct table_entry)
+* from 8 bytes to 4.  Should other characters ever be needed,
+* unicode_val_T u : 24 might be a possibility, although it
+* seems a little unportable as bitfields are in principle
+* restricted to int, which may be 16-bit.  */
+   uint16_t u;
+};
+
+struct codepage_desc {
+   unsigned char *name;
+   unsigned char *const *aliases;
+
+   /* The Unicode mappings of codepage bytes 0x80...0xFF.
+* (0x00...0x7F are assumed to be ASCII in all codepages.)
+* Because all current values fit in 16 bits, we store them as
+* uint16_t rather than unicode_val_T.  If the codepage does
+* not use some byte, then @highhalf maps that byte to 0x,
+* which C code converts to UCS_REPLACEMENT_CHARACTER where
+* appropriate.  (U+ is reserved and will never be
+* assigned as a character.)  */
+   const uint16_t *highhalf;
+
+   /* If some byte in the codepage corresponds to multiple Unicode
+* characters, then the preferred character is in @highhalf
+* above, and the rest are listed here in @table.  This table
+* is not used for translating from the codepage to Unicode.  */
+   const struct table_entry *table;
+};
+
+extern const struct codepage_desc codepages[];
+
 enum convert_string_mode {
CSM_DEFAULT, /* Convert any char. */
CSM_QUERY, /* Special handling of '&' and '=' chars. */
commit 3bcfa83ad82064f363e8e22eeb3a4ac12e415bd1
Author: Witold Filipczyk <[EMAIL PROTECTED]>
Date:   Sat Aug 30 14:22:01 2008 +0200

Support national characters in xbel. The bookmarks.xbel is written
using the system codepage.

diff --git a/src/bookmarks/backend/xbel.c b/src/bookmarks/backend/xbel.c
index 432d3ba..f3fbff3 100644
--- a/src/bookmarks/backend/xbel.c
+++ b/src/bookmarks/backend/xbel.c
@@ -55,8 +55,8 @@ static unsigned char *get_attribute_value(struct tree_node 
*node,
 
 static void read_bookmarks_xbel(FILE *f);
 static unsigned char * filename_bookmarks_xbel(int writing);
-static int xbeltree_to_bookmarks_list(struct tree_node *root,
- struct bookmark *current_parent);
+static int xbeltree_to_bookmarks_list(st

Re: [elinks-dev] Errors in bittorent

2008-08-30 Thread Jonas Fonseca
Some comments ...

Witold Filipczyk <[EMAIL PROTECTED]> wrote Sat, Aug 30, 2008:
> commit b589f19b73c65621c0a8582199509f58dbcac09f
> Author: Witold Filipczyk <[EMAIL PROTECTED]>
> AuthorDate: Sat Aug 30 10:52:00 2008 +0200
> Commit: Witold Filipczyk <[EMAIL PROTECTED]>
> CommitDate: Sat Aug 30 10:52:00 2008 +0200
> 
> An issue with bittorrent.
> 
> It was possible that the reference to the automatic variable
> uri of the make_bittorrent_peer_connection trashed the stack.
> In addition done_uri crashed because uri->string was NULL.
> 
> Now uri is allocated and unlocked to avoid memleak.
> 
> diff --git a/src/protocol/bittorrent/peerconnect.c 
> b/src/protocol/bittorrent/peerconnect.c
> index aeafbf3..5f65e68 100644
> --- a/src/protocol/bittorrent/peerconnect.c
> +++ b/src/protocol/bittorrent/peerconnect.c
> @@ -20,6 +20,7 @@
>  #include "elinks.h"
>  
>  #include "config/options.h"
> +#include "main/object.h"
>  #include "main/select.h"
>  #include "main/timer.h"
>  #include "network/connection.h"
> @@ -271,9 +272,12 @@ enum bittorrent_state
>  make_bittorrent_peer_connection(struct bittorrent_connection *bittorrent,
>   struct bittorrent_peer *peer_info)
>  {
> - struct uri uri;
> + struct string uri_string;
> + struct uri *uri;
>   struct bittorrent_peer_connection *peer;
> - unsigned char port[5];
> + unsigned char port[6];
> + int ip_start, port_start;
> + int port_length;
>  
>   peer = init_bittorrent_peer_connection(-1);
>   if (!peer) return BITTORRENT_STATE_OUT_OF_MEM;
> @@ -296,14 +300,32 @@ make_bittorrent_peer_connection(struct 
> bittorrent_connection *bittorrent,
>   /* FIXME: Rather change the make_connection() interface. This is an ugly
>* hack. */
>   /* FIXME: Set the ipv6 flag iff ... */
> - memset(&uri, 0, sizeof(uri));
> - uri.protocol = PROTOCOL_BITTORRENT;
> - uri.host = peer_info->ip;
> - uri.hostlen  = strlen(peer_info->ip);
> - uri.port = port;
> - uri.portlen  = snprintf(port, sizeof(port), "%u", peer_info->port);
> -
> - make_connection(peer->socket, &uri, send_bittorrent_peer_handshake, 1);
> + if (!init_string(&uri_string)) {
> + done_bittorrent_peer_connection(peer);
> + return BITTORRENT_STATE_OUT_OF_MEM;
> + }
> + add_to_string(&uri_string, "bittorrent:");
> + ip_start = uri_string.length;
> + add_to_string(&uri_string, peer_info->ip);
> + add_char_to_string(&uri_string, ':');
> + port_start = uri_string.length;
> +
> + port_length = snprintf(port, sizeof(port), "%u", peer_info->port);
> + add_bytes_to_string(&uri_string, port, port_length);
> + uri = get_uri(uri_string.source, URI_BASE);
> + done_string(&uri_string);
> + if (!uri) {
> + done_bittorrent_peer_connection(peer);
> + return BITTORRENT_STATE_OUT_OF_MEM;
> + }
> +
> + uri->host = uri->string + ip_start;
> + uri->hostlen = port_start - ip_start - 1;
> + uri->port = uri->string + port_start;
> + uri->portlen = port_length;
> + /* Do not lock it. */
> + object_unlock(uri);
> + make_connection(peer->socket, uri, send_bittorrent_peer_handshake, 1);
>  
>   return BITTORRENT_STATE_OK;
>  }


Instead of fixing this hack, why don't you add a setup_connection() that
make_connection() can use as a wrapper and be done with this. Or if this
is "bittorrent" URI thing actually works, why do you mess with the URI struct
yourself? Didn't parse_uri() do all that for you already? Also don't
unluck the URI, call done_uri(uri) after the call to make_connection().

Else your checking of add_..._string() calls is a bit sloppy.

-- 
Jonas Fonseca
___
elinks-dev mailing list
elinks-dev@linuxfromscratch.org
http://linuxfromscratch.org/mailman/listinfo/elinks-dev


Re: [elinks-dev] Errors in bittorent

2008-08-30 Thread Witold Filipczyk
On Fri, Aug 29, 2008 at 10:38:20PM +0200, Witold Filipczyk wrote:
> Hi,
> I noticed a bug in the bittorent protocol code while trying to get an ISO from
> http://torrents.gentoo.org/.
> 
> Here is a fix for it.
> 1) Before the uri was put on the stack and the access that uri later
> may trash the stack.
> 2) done_uri expects that uri->string is not NULL, so uri->string points to "".
> 

That patch wasn't good. I attached the second one.

Witek
commit b589f19b73c65621c0a8582199509f58dbcac09f
Author: Witold Filipczyk <[EMAIL PROTECTED]>
AuthorDate: Sat Aug 30 10:52:00 2008 +0200
Commit: Witold Filipczyk <[EMAIL PROTECTED]>
CommitDate: Sat Aug 30 10:52:00 2008 +0200

An issue with bittorrent.

It was possible that the reference to the automatic variable
uri of the make_bittorrent_peer_connection trashed the stack.
In addition done_uri crashed because uri->string was NULL.

Now uri is allocated and unlocked to avoid memleak.

diff --git a/src/protocol/bittorrent/peerconnect.c 
b/src/protocol/bittorrent/peerconnect.c
index aeafbf3..5f65e68 100644
--- a/src/protocol/bittorrent/peerconnect.c
+++ b/src/protocol/bittorrent/peerconnect.c
@@ -20,6 +20,7 @@
 #include "elinks.h"
 
 #include "config/options.h"
+#include "main/object.h"
 #include "main/select.h"
 #include "main/timer.h"
 #include "network/connection.h"
@@ -271,9 +272,12 @@ enum bittorrent_state
 make_bittorrent_peer_connection(struct bittorrent_connection *bittorrent,
struct bittorrent_peer *peer_info)
 {
-   struct uri uri;
+   struct string uri_string;
+   struct uri *uri;
struct bittorrent_peer_connection *peer;
-   unsigned char port[5];
+   unsigned char port[6];
+   int ip_start, port_start;
+   int port_length;
 
peer = init_bittorrent_peer_connection(-1);
if (!peer) return BITTORRENT_STATE_OUT_OF_MEM;
@@ -296,14 +300,32 @@ make_bittorrent_peer_connection(struct 
bittorrent_connection *bittorrent,
/* FIXME: Rather change the make_connection() interface. This is an ugly
 * hack. */
/* FIXME: Set the ipv6 flag iff ... */
-   memset(&uri, 0, sizeof(uri));
-   uri.protocol = PROTOCOL_BITTORRENT;
-   uri.host = peer_info->ip;
-   uri.hostlen  = strlen(peer_info->ip);
-   uri.port = port;
-   uri.portlen  = snprintf(port, sizeof(port), "%u", peer_info->port);
-
-   make_connection(peer->socket, &uri, send_bittorrent_peer_handshake, 1);
+   if (!init_string(&uri_string)) {
+   done_bittorrent_peer_connection(peer);
+   return BITTORRENT_STATE_OUT_OF_MEM;
+   }
+   add_to_string(&uri_string, "bittorrent:");
+   ip_start = uri_string.length;
+   add_to_string(&uri_string, peer_info->ip);
+   add_char_to_string(&uri_string, ':');
+   port_start = uri_string.length;
+
+   port_length = snprintf(port, sizeof(port), "%u", peer_info->port);
+   add_bytes_to_string(&uri_string, port, port_length);
+   uri = get_uri(uri_string.source, URI_BASE);
+   done_string(&uri_string);
+   if (!uri) {
+   done_bittorrent_peer_connection(peer);
+   return BITTORRENT_STATE_OUT_OF_MEM;
+   }
+
+   uri->host = uri->string + ip_start;
+   uri->hostlen = port_start - ip_start - 1;
+   uri->port = uri->string + port_start;
+   uri->portlen = port_length;
+   /* Do not lock it. */
+   object_unlock(uri);
+   make_connection(peer->socket, uri, send_bittorrent_peer_handshake, 1);
 
return BITTORRENT_STATE_OK;
 }
___
elinks-dev mailing list
elinks-dev@linuxfromscratch.org
http://linuxfromscratch.org/mailman/listinfo/elinks-dev