return 0;
}
-/***
- * read "something" from the wire and try to return
- * a packet of it
+/*
+===========
+ NETWORKIBG
+===========
*/
+
static int
-l_pkt_read_wire_udp(lua_State *L)
+l_server_socket_udp(lua_State *L)
{
ldns_rdf *ip = (ldns_rdf*)lua_touserdata(L, 1); /* get the ip */
uint16_t port = (uint16_t)lua_tonumber(L, 2); /* port number */
struct timeval timeout;
struct sockaddr_storage *to;
- size_t size;
- uint8_t *pktbuf;
- ldns_pkt *pkt;
int sockfd;
/* use default timeout - maybe this gets to be configureable */
}
/* get the socket */
- sockfd = ldns_udp_connect(to, timeout);
+ sockfd = ldns_udp_server_connect(to, timeout);
if (sockfd == 0) {
return 0;
}
+ lua_pushnumber(L, sockfd);
+}
+
+static int
+l_server_socket_udp_close(lua_State *L)
+{
+ int sockfd = (int)lua_tonumber(L, 1);
+
+ close(sockfd);
+}
+
+static int
+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)
+{
+ int sockfd = (int)lua_tonumber(L,1);
+ size_t size;
+ uint8_t *pktbuf;
+ ldns_pkt *pkt;
pktbuf = ldns_udp_read_wire(sockfd, &size);
printf("read %d\n", size);
close(sockfd);
return 0;
}
- close(sockfd); /* return the socket also... I think */
-
/* if we got to this point, we got some data (pkt) with a certain
* size. Let's see if it can be made into a real ldsn pkt
*/
return 1;
}
-
/* header bits */
/* read section counters */
return 1;
}
-/* not sure we need this still! XXX */
-static int
-l_rdf2sockaddr_storage(lua_State *L)
-{
- ldns_rdf *rd = (ldns_rdf*)lua_touserdata(L, 1);
- uint16_t port = (uint16_t)lua_tonumber(L, 2);
- struct sockaddr_storage *s;
-
- s = ldns_rdf2native_sockaddr_storage(rd, port);
-
- if (s) {
- lua_pushlightuserdata(L, s);
- return 1;
- } else {
- return 0;
- }
-}
-
/*
============
EXAMPLES
lua_register(L, "l_pkt_ancount", l_pkt_ancount);
lua_register(L, "l_pkt_nscount", l_pkt_nscount);
lua_register(L, "l_pkt_nscount", l_pkt_nscount);
- lua_register(L, "l_pkt_read_wire_udp", l_pkt_read_wire_udp);
/* CONVERSIONs */
lua_register(L, "l_pkt2string", l_pkt2string);
- lua_register(L, "l_rdf2sockaddr_storage", l_rdf2sockaddr_storage);
+
+ /* NETWORKING */
+ lua_register(L, "l_write_wire_udp", l_write_wire_udp);
+ lua_register(L, "l_read_wire_udp", l_read_wire_udp);
+ lua_register(L, "l_server_socket_udp", l_server_socket_udp);
+ lua_register(L, "l_server_socket_udp_close", l_server_socket_udp_close);
+
}
int
/* hack hack, this is now a server socket!! XXX need to change or rename */
/* SERVER SERVER XXX */
int
-ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout)
+ldns_udp_server_connect(const struct sockaddr_storage *to, struct timeval timeout)
{
int sockfd;
return sockfd;
}
+int
+ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout)
+{
+ int sockfd;
+
+ if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP))
+ == -1) {
+ return 0;
+ }
+ if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ (socklen_t) sizeof(timeout))) {
+ perror("setsockopt");
+ close(sockfd);
+ return 0;
+ }
+
+ return sockfd;
+}
+
int
ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
{