From: Miek Gieben Date: Thu, 11 Aug 2005 08:33:11 +0000 (+0000) Subject: some network updates X-Git-Tag: release-1.0.0~310 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aebee4d9e516653607754b23d87efdcf313b2845;p=thirdparty%2Fldns.git some network updates --- diff --git a/ldns/net.h b/ldns/net.h index 2b9d1a6c..12f52dad 100644 --- a/ldns/net.h +++ b/ldns/net.h @@ -75,6 +75,15 @@ int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct */ int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout); +/** + * Create a udp socket to the specified address and bind it too (making it + * a server socket) + * \param[in] to ip and family + * \param[in] timeout timeout for the socket + * \return a socket descriptor + */ +int ldns_udp_server_connect(const struct sockaddr_storage *to, struct timeval timeout); + /** * send a query via tcp to a server. Don;t want for the answer * diff --git a/lua/lua-rns.c b/lua/lua-rns.c index 0850e98a..07ef5583 100644 --- a/lua/lua-rns.c +++ b/lua/lua-rns.c @@ -235,20 +235,19 @@ l_pkt_print(lua_State *L) 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 */ @@ -262,10 +261,42 @@ l_pkt_read_wire_udp(lua_State *L) } /* 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); @@ -273,8 +304,6 @@ l_pkt_read_wire_udp(lua_State *L) 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 */ @@ -287,7 +316,6 @@ l_pkt_read_wire_udp(lua_State *L) return 1; } - /* header bits */ /* read section counters */ @@ -361,24 +389,6 @@ l_pkt2string(lua_State *L) 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 @@ -435,11 +445,16 @@ register_ldns_functions(void) 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 diff --git a/net.c b/net.c index 02cb33de..13e54866 100644 --- a/net.c +++ b/net.c @@ -264,7 +264,7 @@ ldns_send_udp(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage /* 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; @@ -286,6 +286,25 @@ ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout) 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) {