From: Miek Gieben Date: Thu, 11 Aug 2005 13:43:06 +0000 (+0000) Subject: somehow somewhere,the lua stuff crashes while plain ldns does not X-Git-Tag: release-1.0.0~298 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=422749650c61401fc8063eba9dbc47ce91e51aab;p=thirdparty%2Fldns.git somehow somewhere,the lua stuff crashes while plain ldns does not Last updates for ldns networking, except for the lua-net-bug fix --- diff --git a/buffer.c b/buffer.c index a34e344a..ca97f2d3 100644 --- a/buffer.c +++ b/buffer.c @@ -47,7 +47,7 @@ ldns_buffer_new_frm_data(ldns_buffer *buffer, void *data, size_t size) buffer->_limit = buffer->_capacity = size; buffer->_data = LDNS_XMALLOC(uint8_t, size); memcpy(buffer->_data, data, size); - buffer->_fixed = 1; + buffer->_fixed = 0; buffer->_status = LDNS_STATUS_OK; ldns_buffer_invariant(buffer); diff --git a/ldns/buffer.h b/ldns/buffer.h index f2f34cd5..51a368dc 100644 --- a/ldns/buffer.h +++ b/ldns/buffer.h @@ -79,8 +79,8 @@ ldns_buffer_invariant(ldns_buffer *buffer) ldns_buffer *ldns_buffer_new(size_t capacity); /** - * creates a buffer with the specified data. The data is not copied - * and no memory allocations are done. The buffer is fixed and cannot + * creates a buffer with the specified data. The data IS copied + * and MEMORY allocations are done. The buffer is not fixed and can * be resized using buffer_reserve(). * * \param[in] buffer pointer to the buffer to put the data in diff --git a/lua/lua-rns.c b/lua/lua-rns.c index 04efdb3c..40e6f16c 100644 --- a/lua/lua-rns.c +++ b/lua/lua-rns.c @@ -305,14 +305,6 @@ l_write_wire_udp(lua_State *L) } } - -/*** - * read "something" from the wire and try to return - * a packet of it - * I rather have this as a string that lua/rns can transform - * We'll see what pops up in writting the other bits and see how - * this will be effected - */ static int l_read_wire_udp(lua_State *L) { @@ -323,13 +315,18 @@ l_read_wire_udp(lua_State *L) ldns_buffer *pktbuf; pktbuf_raw = ldns_udp_read_wire(sockfd, &size); + if (!pktbuf_raw) { + printf("[debug] nothing allright\n"); return 0; } ldns_buffer_new_frm_data(pktbuf, pktbuf_raw, size); + LDNS_FREE(pktbuf_raw); /* push our buffer onto the stack */ + printf("[debug] I've read %d bytes\n", size); + printf("[debug] buffer cap %d bytes\n", ldns_buffer_capacity(pktbuf)); lua_pushlightuserdata(L, pktbuf); return 1; } @@ -386,9 +383,15 @@ l_buf2pkt(lua_State *L) ldns_buffer *b = (ldns_buffer *)lua_touserdata(L, 1); ldns_pkt *p; + if (!b) { + return 0; + } + if (ldns_buffer2pkt_wire(&p, b) != LDNS_STATUS_OK) { + printf("[debug] conversion buf2pkt sour\n"); return 0; } + printf("[debug] conversion buf2pkt ok\n"); lua_pushlightuserdata(L, p); return 1; @@ -400,12 +403,19 @@ l_pkt2buf(lua_State *L) ldns_pkt *p = (ldns_pkt *)lua_touserdata(L, 1); ldns_buffer *b; + if (!p) { + return 0; + } + /* resize! XXX */ b = ldns_buffer_new(LDNS_MAX_PACKETLEN); - if (ldns_pkt2buffer_wire(b, p) != LDNS_STATUS_OK) { return 0; } + /* resize to current usage */ + printf("[debug] usage buffer %d\n", ldns_buffer_position(b)); + ldns_buffer_reserve(b, (size_t) + ldns_buffer_position(b)); lua_pushlightuserdata(L, b); return 1; } diff --git a/lua/rns.lua b/lua/rns.lua index 8dfe3e2a..e5f43fd8 100644 --- a/lua/rns.lua +++ b/lua/rns.lua @@ -45,18 +45,19 @@ l_pkt_print(pkt) rdf_ip = l_rdf_new_frm_str(LDNS_RDF_TYPE_A, "127.0.0.1") -- connect and bind to a server udp socket socket = l_server_socket_udp(rdf_ip, 5353) +-- -- read from the socket wirebuf = l_read_wire_udp(socket) -lua_debug("what I read") -print(wirebuf) +--lua_debug("what I read") -- close the socket l_server_socket_close_udp(socket) -- convert the packet -if not wirebuf == nil then - lua_debug("I shouldn't be here") - wirepkt = l_buf2pkt(wirebuf) - -- print the packet - l_pkt_print(wirepkt) -end -lua_debug("Then end") + +--if not wirebuf == nil then +-- lua_debug("I shouldn't be here") +-- wirepkt = l_buf2pkt(wirebuf) +-- -- print the packet +-- l_pkt_print(wirepkt) +--end +lua_debug("The end") diff --git a/net.c b/net.c index e656e90d..2c69e6e6 100644 --- a/net.c +++ b/net.c @@ -332,6 +332,10 @@ ldns_udp_read_wire(int sockfd, size_t *size) ssize_t wire_size; wire = LDNS_XMALLOC(uint8_t, LDNS_MAX_PACKETLEN); + if (!wire) { + *size = 0; + return NULL; + } wire_size = recv(sockfd, wire, LDNS_MAX_PACKETLEN, 0); /* recvfrom here .. */ @@ -340,13 +344,13 @@ ldns_udp_read_wire(int sockfd, size_t *size) if (errno == EAGAIN) { dprintf("%s", "socket timeout\n"); } + *size = 0; perror("error receiving udp packet"); return NULL; } *size = (size_t)wire_size; wire = LDNS_XREALLOC(wire, uint8_t, (size_t)wire_size); - return wire; } @@ -358,6 +362,11 @@ ldns_tcp_read_wire(int sockfd, size_t *size) ssize_t bytes = 0; wire = LDNS_XMALLOC(uint8_t, 2); + if (!wire) { + *size = 0; + return NULL; + } + while (bytes < 2) { bytes = recv(sockfd, wire, 2, 0); if (bytes == -1) { @@ -365,6 +374,7 @@ ldns_tcp_read_wire(int sockfd, size_t *size) dprintf("%s", "socket timeout\n"); } perror("error receiving tcp packet"); + *size = 0; return NULL; } } @@ -383,6 +393,7 @@ ldns_tcp_read_wire(int sockfd, size_t *size) } perror("error receiving tcp packet"); LDNS_FREE(wire); + *size = 0; return NULL; } } diff --git a/wire2host.c b/wire2host.c index 2adec1e1..ce3df4c1 100644 --- a/wire2host.c +++ b/wire2host.c @@ -344,7 +344,6 @@ ldns_buffer2pkt_wire(ldns_pkt **packet, ldns_buffer *buffer) } - ldns_status ldns_wire2pkt(ldns_pkt **packet_p, const uint8_t *wire, size_t max) {