]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
some network updates
authorMiek Gieben <miekg@NLnetLabs.nl>
Thu, 11 Aug 2005 08:33:11 +0000 (08:33 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Thu, 11 Aug 2005 08:33:11 +0000 (08:33 +0000)
ldns/net.h
lua/lua-rns.c
net.c

index 2b9d1a6c3edd521a37708e6d21bf98747c572292..12f52dadd3cb461bf554a023551ebc1dd2f71b57 100644 (file)
@@ -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
  *
index 0850e98aa9191c208d64a4dcad84b21ceddcea22..07ef558360bb3b2d2a8863dda8ce7a962e367ac6 100644 (file)
@@ -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 02cb33dec47d43bfce720ec4a5236d16046aead6..13e5486623c73ccb49b10cb93ab94c88d5c145c6 100644 (file)
--- 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)
 {