net.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * net.h
  3. *
  4. * DNS Resolver definitions
  5. *
  6. * a Net::DNS like library for C
  7. *
  8. * (c) NLnet Labs, 2005-2006
  9. *
  10. * See the file LICENSE for the license
  11. */
  12. #ifndef LDNS_NET_H
  13. #define LDNS_NET_H
  14. #include <ldns/ldns.h>
  15. #ifdef _MSC_VER
  16. #include <winsock2.h>
  17. #define socklen_t int
  18. #define in_port_t USHORT
  19. #define in_addr_t UINT
  20. #else
  21. #include <sys/socket.h>
  22. #include <netdb.h>
  23. #endif
  24. #define LDNS_DEFAULT_TIMEOUT_SEC 2
  25. #define LDNS_DEFAULT_TIMEOUT_USEC 0
  26. /**
  27. * \file
  28. *
  29. * Contains functions to send and receive packets over a network.
  30. */
  31. /**
  32. * Sends a buffer to an ip using udp and return the respons as a ldns_pkt
  33. * \param[in] qbin the ldns_buffer to be send
  34. * \param[in] to the ip addr to send to
  35. * \param[in] tolen length of the ip addr
  36. * \param[in] timeout the timeout value for the network
  37. * \param[out] answersize size of the packet
  38. * \param[out] result packet with the answer
  39. * \return status
  40. */
  41. ldns_status ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answersize);
  42. /**
  43. * Send an udp query and don't wait for an answer but return
  44. * the socket
  45. * \param[in] qbin the ldns_buffer to be send
  46. * \param[in] to the ip addr to send to
  47. * \param[in] tolen length of the ip addr
  48. * \param[in] timeout the timeout value for the network
  49. * \return the socket used
  50. */
  51. int ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
  52. /**
  53. * Send an tcp query and don't wait for an answer but return
  54. * the socket
  55. * \param[in] qbin the ldns_buffer to be send
  56. * \param[in] to the ip addr to send to
  57. * \param[in] tolen length of the ip addr
  58. * \param[in] timeout the timeout value for the network
  59. * \return the socket used
  60. */
  61. int ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
  62. /**
  63. * Sends a buffer to an ip using tcp and return the respons as a ldns_pkt
  64. * \param[in] qbin the ldns_buffer to be send
  65. * \param[in] qbin the ldns_buffer to be send
  66. * \param[in] to the ip addr to send to
  67. * \param[in] tolen length of the ip addr
  68. * \param[in] timeout the timeout value for the network
  69. * \param[out] answersize size of the packet
  70. * \param[out] result packet with the answer
  71. * \return status
  72. */
  73. ldns_status ldns_tcp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answersize);
  74. /**
  75. * Sends ptk to the nameserver at the resolver object. Returns the data
  76. * as a ldns_pkt
  77. *
  78. * \param[out] pkt packet received from the nameserver
  79. * \param[in] r the resolver to use
  80. * \param[in] query_pkt the query to send
  81. * \return status
  82. */
  83. ldns_status ldns_send(ldns_pkt **pkt, ldns_resolver *r, const ldns_pkt *query_pkt);
  84. /**
  85. * Sends and ldns_buffer (presumably containing a packet to the nameserver at the resolver object. Returns the data
  86. * as a ldns_pkt
  87. *
  88. * \param[out] pkt packet received from the nameserver
  89. * \param[in] r the resolver to use
  90. * \param[in] qb the buffer to send
  91. * \param[in] tsig_mac the tsig MAC to authenticate the response with (NULL to do no TSIG authentication)
  92. * \return status
  93. */
  94. ldns_status ldns_send_buffer(ldns_pkt **pkt, ldns_resolver *r, ldns_buffer *qb, ldns_rdf *tsig_mac);
  95. /**
  96. * Create a tcp socket to the specified address
  97. * \param[in] to ip and family
  98. * \param[in] tolen length of to
  99. * \param[in] timeout timeout for the socket
  100. * \return a socket descriptor
  101. */
  102. int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
  103. /**
  104. * Create a udp socket to the specified address
  105. * \param[in] to ip and family
  106. * \param[in] timeout timeout for the socket
  107. * \return a socket descriptor
  108. */
  109. int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout);
  110. /**
  111. * send a query via tcp to a server. Don't want for the answer
  112. *
  113. * \param[in] qbin the buffer to send
  114. * \param[in] sockfd the socket to use
  115. * \param[in] to which ip to send it
  116. * \param[in] tolen socketlen
  117. * \return number of bytes sent
  118. */
  119. ssize_t ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen);
  120. /**
  121. * send a query via udp to a server. Don;t want for the answer
  122. *
  123. * \param[in] qbin the buffer to send
  124. * \param[in] sockfd the socket to use
  125. * \param[in] to which ip to send it
  126. * \param[in] tolen socketlen
  127. * \return number of bytes sent
  128. */
  129. ssize_t ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen);
  130. /**
  131. * Gives back a raw packet from the wire and reads the header data from the given
  132. * socket. Allocates the data (of size size) itself, so don't forget to free
  133. *
  134. * \param[in] sockfd the socket to read from
  135. * \param[out] size the number of bytes that are read
  136. * \return the data read
  137. */
  138. uint8_t *ldns_tcp_read_wire(int sockfd, size_t *size);
  139. /**
  140. * Gives back a raw packet from the wire and reads the header data from the given
  141. * socket. Allocates the data (of size size) itself, so don't forget to free
  142. *
  143. * \param[in] sockfd the socket to read from
  144. * \param[in] fr the address of the client (if applicable)
  145. * \param[in] *frlen the lenght of the client's addr (if applicable)
  146. * \param[out] size the number of bytes that are read
  147. * \return the data read
  148. */
  149. uint8_t *ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *fr, socklen_t *frlen);
  150. /**
  151. * returns the native sockaddr representation from the rdf.
  152. * \param[in] rd the ldns_rdf to operate on
  153. * \param[in] port what port to use. 0 means; use default (53)
  154. * \param[out] size what is the size of the sockaddr_storage
  155. * \return struct sockaddr* the address in the format so other
  156. * functions can use it (sendto)
  157. */
  158. struct sockaddr_storage * ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size);
  159. /**
  160. * returns an rdf with the sockaddr info. works for ip4 and ip6
  161. * \param[in] sock the struct sockaddr_storage to convert
  162. * \param[in] port what port was used. When NULL this is not set
  163. * \return ldns_rdf* wth the address
  164. */
  165. ldns_rdf * ldns_sockaddr_storage2rdf(struct sockaddr_storage *sock, uint16_t *port);
  166. /**
  167. * Prepares the resolver for an axfr query
  168. * The query is sent and the answers can be read with ldns_axfr_next
  169. * \param[in] resolver the resolver to use
  170. * \param[in] domain the domain to exfr
  171. * \param[in] c the class to use
  172. * \return ldns_status the status of the transfer
  173. */
  174. ldns_status ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class c);
  175. #endif /* LDNS_NET_H */