* \param[in] to the ip addr to send to
* \param[in] tolen length of the ip addr
* \param[in] timeout *unused*, was the timeout value for the network
- * \return the socket used
+ * \return the socket used or -1 on failure
+ */
+int ldns_udp_bgsend2(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
+
+/**
+ * Send an udp query and don't wait for an answer but return
+ * the socket
+ * This function has the flaw that it returns 0 on failure, but 0 could be a
+ * valid socket. Please use ldns_udp_bgsend2 instead of this function.
+ * \param[in] qbin the ldns_buffer to be send
+ * \param[in] to the ip addr to send to
+ * \param[in] tolen length of the ip addr
+ * \param[in] timeout *unused*, was the timeout value for the network
+ * \return the socket used or 0 on failure
*/
int ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
* \param[in] to the ip addr to send to
* \param[in] tolen length of the ip addr
* \param[in] timeout the timeout value for the connect attempt
- * \return the socket used
+ * \return the socket used or -1 on failure
+ */
+int ldns_tcp_bgsend2(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
+
+/**
+ * Send an tcp query and don't wait for an answer but return
+ * the socket
+ * This function has the flaw that it returns 0 on failure, but 0 could be a
+ * valid socket. Please use ldns_tcp_bgsend2 instead of this function.
+ * \param[in] qbin the ldns_buffer to be send
+ * \param[in] to the ip addr to send to
+ * \param[in] tolen length of the ip addr
+ * \param[in] timeout the timeout value for the connect attempt
+ * \return the socket used or 0 on failure
*/
int ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
* \param[in] to ip and family
* \param[in] tolen length of to
* \param[in] timeout timeout for the connect attempt
- * \return a socket descriptor
+ * \return a socket descriptor or -1 on failure
+ */
+int ldns_tcp_connect2(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
+
+/**
+ * Create a tcp socket to the specified address
+ * This function has the flaw that it returns 0 on failure, but 0 could be a
+ * valid socket. Please use ldns_tcp_connect2 instead of this function.
+ * \param[in] to ip and family
+ * \param[in] tolen length of to
+ * \param[in] timeout timeout for the connect attempt
+ * \return a socket descriptor or 0 on failure
*/
int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
* Create a udp socket to the specified address
* \param[in] to ip and family
* \param[in] timeout *unused*, was timeout for the socket
- * \return a socket descriptor
+ * \return a socket descriptor or -1 on failure
+ */
+int ldns_udp_connect2(const struct sockaddr_storage *to, struct timeval timeout);
+
+/**
+ * Create a udp socket to the specified address
+ * This function has the flaw that it returns 0 on failure, but 0 could be a
+ * valid socket. Please use ldns_udp_connect2 instead of this function.
+ * \param[in] to ip and family
+ * \param[in] timeout *unused*, was timeout for the socket
+ * \return a socket descriptor or 0 on failure
*/
int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout);
#ifndef S_SPLINT_S
if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM,
IPPROTO_TCP)) == SOCK_INVALID) {
- return 0;
+ return -1;
}
#endif
if (from && bind(sockfd, (const struct sockaddr*)from, fromlen) == SOCK_INVALID){
close_socket(sockfd);
- return 0;
+ return -1;
}
/* perform nonblocking connect, to be able to wait with select() */
if(1) {
#endif
close_socket(sockfd);
- return 0;
+ return -1;
}
#else /* USE_WINSOCK */
if(WSAGetLastError() != WSAEINPROGRESS &&
WSAGetLastError() != WSAEWOULDBLOCK) {
close_socket(sockfd);
- return 0;
+ return -1;
}
#endif
/* error was only telling us that it would block */
if(!ldns_sock_wait(sockfd, timeout, 1)) {
close_socket(sockfd);
- return 0;
+ return -1;
}
/* check if there is a pending error for nonblocking connect */
close_socket(sockfd);
/* error in errno for our user */
errno = error;
- return 0;
+ return -1;
}
#else /* USE_WINSOCK */
if(error == WSAEINPROGRESS)
else if(error != 0) {
close_socket(sockfd);
errno = error;
- return 0;
+ return -1;
}
#endif /* USE_WINSOCK */
/* connected */
int
ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen,
struct timeval timeout)
+{
+ int s = ldns_tcp_connect_from(to, tolen, NULL, 0, timeout);
+ return s > 0 ? s : 0;
+}
+
+int
+ldns_tcp_connect2(const struct sockaddr_storage *to, socklen_t tolen,
+ struct timeval timeout)
{
return ldns_tcp_connect_from(to, tolen, NULL, 0, timeout);
}
sockfd = ldns_tcp_connect_from(to, tolen, from, fromlen, timeout);
- if (sockfd == 0) {
- return 0;
- }
-
- if (ldns_tcp_send_query(qbin, sockfd, to, tolen) == 0) {
+ if (sockfd >= 0 && ldns_tcp_send_query(qbin, sockfd, to, tolen) == 0) {
close_socket(sockfd);
- return 0;
+ return -1;
}
return sockfd;
const struct sockaddr_storage *to, socklen_t tolen,
struct timeval timeout)
{
- return ldns_tcp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
+ int s = ldns_tcp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
+ return s > 0 ? s : 0;
}
+int
+ldns_tcp_bgsend2(ldns_buffer *qbin,
+ const struct sockaddr_storage *to, socklen_t tolen,
+ struct timeval timeout)
+{
+ return ldns_tcp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
+}
/* keep in mind that in DNS tcp messages the first 2 bytes signal the
* amount data to expect
sockfd = ldns_tcp_bgsend_from(qbin, to, tolen, from, fromlen, timeout);
- if (sockfd == 0) {
+ if (sockfd == -1) {
return LDNS_STATUS_ERR;
}
#ifndef S_SPLINT_S
if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM,
IPPROTO_UDP))
- == -1) {
+ == SOCK_INVALID) {
return 0;
}
#endif
return sockfd;
}
+int
+ldns_udp_connect2(const struct sockaddr_storage *to, struct timeval ATTR_UNUSED(timeout))
+{
+ int sockfd;
+
+#ifndef S_SPLINT_S
+ if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM,
+ IPPROTO_UDP))
+ == SOCK_INVALID) {
+ return -1;
+ }
+#endif
+ return sockfd;
+}
+
static int
ldns_udp_bgsend_from(ldns_buffer *qbin,
const struct sockaddr_storage *to , socklen_t tolen,
{
int sockfd;
- sockfd = ldns_udp_connect(to, timeout);
+ sockfd = ldns_udp_connect2(to, timeout);
- if (sockfd == 0) {
- return 0;
+ if (sockfd == -1) {
+ return -1;
}
if (from && bind(sockfd, (const struct sockaddr*)from, fromlen) == -1){
close_socket(sockfd);
- return 0;
+ return -1;
}
if (ldns_udp_send_query(qbin, sockfd, to, tolen) == 0) {
close_socket(sockfd);
- return 0;
+ return -1;
}
return sockfd;
}
ldns_udp_bgsend(ldns_buffer *qbin,
const struct sockaddr_storage *to , socklen_t tolen,
struct timeval timeout)
+{
+ s = ldns_udp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
+ return s > 0 ? s : 0;
+}
+
+int
+ldns_udp_bgsend2(ldns_buffer *qbin,
+ const struct sockaddr_storage *to , socklen_t tolen,
+ struct timeval timeout)
{
return ldns_udp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
}
sockfd = ldns_udp_bgsend_from(qbin, to, tolen, from, fromlen, timeout);
- if (sockfd == 0) {
+ if (sockfd == -1) {
return LDNS_STATUS_SOCKET_ERROR;
}