From: Miek Gieben Date: Tue, 9 Aug 2005 13:42:08 +0000 (+0000) Subject: adding some udp equivs for the tcp send/receive stuff. Good for lua, good for making... X-Git-Tag: release-1.0.0~338 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dbda82357f3cc20c2ad0ba38f0895d7346d09191;p=thirdparty%2Fldns.git adding some udp equivs for the tcp send/receive stuff. Good for lua, good for making a ldns nameserver --- diff --git a/ldns/net.h b/ldns/net.h index 202ea919..4741571f 100644 --- a/ldns/net.h +++ b/ldns/net.h @@ -63,8 +63,27 @@ ldns_status ldns_send(ldns_pkt **pkt, ldns_resolver *r, ldns_pkt *query_pkt); */ int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout); +/** + * send a query via tcp to a server. Don;t want for the answer + * + * \param[in] qbin the buffer to send + * \param[in] sockfd the socket to use + * \param[in] to which ip to send it + * \param[in] tolen socketlen + * \return number of bytes sent + */ ssize_t ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen); +/** + * send a query via udp to a server. Don;t want for the answer + * + * \param[in] qbin the buffer to send + * \param[in] sockfd the socket to use + * \param[in] to which ip to send it + * \param[in] tolen socketlen + * \return number of bytes sent + */ +ssize_t ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen); /** * Gives back a raw packet from the wire and reads the header data from the given * socket. Allocates the data (of size size) itself, so don't forget to free diff --git a/lua/rns-lib.lua b/lua/rns-lib.lua index e7266efb..581f8bfc 100644 --- a/lua/rns-lib.lua +++ b/lua/rns-lib.lua @@ -6,7 +6,12 @@ LDNS_SECTION_ADDITIONAL = 3 LDNS_SECTION_ANY = 4 LDNS_SECTION_ANY_NOQUESTION = 5 --- dofile (filename) +-- read a pkt from the wire +function lua_recv_pkt() + +end + + -- transpose 2 rrs in a pkt -- function lua_transpose_rr(pkt, n1, n2) print("[info] [RR] transpose", n1, n2) diff --git a/net.c b/net.c index 3dbf7e55..f66ea12f 100644 --- a/net.c +++ b/net.c @@ -315,6 +315,36 @@ ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage return bytes; } +ssize_t +ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen) +{ + uint8_t *sendbuf; + ssize_t bytes; + + /* add length of packet */ + sendbuf = LDNS_XMALLOC(uint8_t, ldns_buffer_position(qbin) + 2); + ldns_write_uint16(sendbuf, ldns_buffer_position(qbin)); + memcpy(sendbuf+2, ldns_buffer_export(qbin), ldns_buffer_position(qbin)); + + bytes = sendto(sockfd, sendbuf, + ldns_buffer_position(qbin)+2, 0, (struct sockaddr *)to, tolen); + + LDNS_FREE(sendbuf); + + if (bytes == -1) { + dprintf("%s", "error with sending\n"); + close(sockfd); + return 0; + } + if ((size_t) bytes != ldns_buffer_position(qbin)+2) { + dprintf("%s", "amount of sent bytes mismatch\n"); + close(sockfd); + return 0; + } + + return bytes; +} + uint8_t * ldns_udp_read_wire(int sockfd, size_t *size) {