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);
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
}
}
-
-/***
- * 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)
{
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;
}
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;
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;
}
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")
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 .. */
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;
}
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) {
dprintf("%s", "socket timeout\n");
}
perror("error receiving tcp packet");
+ *size = 0;
return NULL;
}
}
}
perror("error receiving tcp packet");
LDNS_FREE(wire);
+ *size = 0;
return NULL;
}
}
}
-
ldns_status
ldns_wire2pkt(ldns_pkt **packet_p, const uint8_t *wire, size_t max)
{