s3: allow setting the TCP_QUICKACK socket option
[ira/wip.git] / source3 / lib / util_sock.c
index 667dbf6dad7799e4fe6c684fe15ec77f4f0cfc8f..af64f370baf18e33e60a78a1956e642b13b2444d 100644 (file)
@@ -52,7 +52,7 @@ bool interpret_string_addr(struct sockaddr_storage *pss,
        }
 #endif
 
-       zero_addr(pss);
+       zero_sockaddr(pss);
 
        if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
                return false;
@@ -81,7 +81,7 @@ bool interpret_string_addr(struct sockaddr_storage *pss,
  Set an address to INADDR_ANY.
 ******************************************************************/
 
-void zero_addr(struct sockaddr_storage *pss)
+void zero_sockaddr(struct sockaddr_storage *pss)
 {
        memset(pss, '\0', sizeof(*pss));
        /* Ensure we're at least a valid sockaddr-storage. */
@@ -208,13 +208,11 @@ static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
        return print_sockaddr_len(addr_buf, addr_len, (struct sockaddr *)&sa, length);
 }
 
-#if 0
-/* Not currently used. JRA. */
 /****************************************************************************
  Return the port number we've bound to on a socket.
 ****************************************************************************/
 
-static int get_socket_port(int fd)
+int get_socket_port(int fd)
 {
        struct sockaddr_storage sa;
        socklen_t length = sizeof(sa);
@@ -239,19 +237,6 @@ static int get_socket_port(int fd)
        }
        return -1;
 }
-#endif
-
-void set_sockaddr_port(struct sockaddr_storage *psa, uint16 port)
-{
-#if defined(HAVE_IPV6)
-       if (psa->ss_family == AF_INET6) {
-               ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
-       }
-#endif
-       if (psa->ss_family == AF_INET) {
-               ((struct sockaddr_in *)psa)->sin_port = htons(port);
-       }
-}
 
 const char *client_name(int fd)
 {
@@ -363,6 +348,9 @@ static const smb_socket_option socket_options[] = {
 #endif
 #ifdef TCP_FASTACK
   {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
+#endif
+#ifdef TCP_QUICKACK
+  {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
 #endif
   {NULL,0,0,0,0}};
 
@@ -380,13 +368,14 @@ static void print_socket_options(int s)
         * leak in SCO Openserver 5.0 */
        /* reported on samba-technical  --jerry */
        if ( DEBUGLEVEL >= 5 ) {
+               DEBUG(5,("Socket options:\n"));
                for (; p->name != NULL; p++) {
                        if (getsockopt(s, p->level, p->option,
                                                (void *)&value, &vlen) == -1) {
-                               DEBUG(5,("Could not test socket option %s.\n",
+                               DEBUGADD(5,("\tCould not test socket option %s.\n",
                                                        p->name));
                        } else {
-                               DEBUG(5,("socket option %s = %d\n",
+                               DEBUGADD(5,("\t%s = %d\n",
                                                        p->name,value));
                        }
                }
@@ -530,7 +519,7 @@ NTSTATUS read_socket_with_timeout(int fd, char *buf,
                }
 
                while (nread < mincnt) {
-                       readret = sys_read(fd, buf + nread, maxcnt - nread);
+                       readret = sys_recv(fd, buf + nread, maxcnt - nread, 0);
 
                        if (readret == 0) {
                                DEBUG(5,("read_socket_with_timeout: "
@@ -599,7 +588,7 @@ NTSTATUS read_socket_with_timeout(int fd, char *buf,
                        return NT_STATUS_IO_TIMEOUT;
                }
 
-               readret = sys_read(fd, buf+nread, maxcnt-nread);
+               readret = sys_recv(fd, buf+nread, maxcnt-nread, 0);
 
                if (readret == 0) {
                        /* we got EOF on the file descriptor */
@@ -646,40 +635,105 @@ NTSTATUS read_data(int fd, char *buffer, size_t N)
 }
 
 /****************************************************************************
- Write data to a fd.
+ Write all data from an iov array
 ****************************************************************************/
 
-ssize_t write_data(int fd, const char *buffer, size_t N)
+ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt)
 {
-       size_t total=0;
-       ssize_t ret;
-       char addr[INET6_ADDRSTRLEN];
+       int i;
+       size_t to_send;
+       ssize_t thistime;
+       size_t sent;
+       struct iovec *iov_copy, *iov;
 
-       while (total < N) {
-               ret = sys_write(fd,buffer + total,N - total);
+       to_send = 0;
+       for (i=0; i<iovcnt; i++) {
+               to_send += orig_iov[i].iov_len;
+       }
 
-               if (ret == -1) {
-                       if (fd == get_client_fd()) {
-                               /* Try and give an error message saying
-                                * what client failed. */
-                               DEBUG(0,("write_data: write failure in "
-                                       "writing to client %s. Error %s\n",
-                                       get_peer_addr(fd,addr,sizeof(addr)),
-                                       strerror(errno) ));
-                       } else {
-                               DEBUG(0,("write_data: write failure. "
-                                       "Error = %s\n", strerror(errno) ));
+       thistime = sys_writev(fd, orig_iov, iovcnt);
+       if ((thistime <= 0) || (thistime == to_send)) {
+               return thistime;
+       }
+       sent = thistime;
+
+       /*
+        * We could not send everything in one call. Make a copy of iov that
+        * we can mess with. We keep a copy of the array start in iov_copy for
+        * the TALLOC_FREE, because we're going to modify iov later on,
+        * discarding elements.
+        */
+
+       iov_copy = (struct iovec *)TALLOC_MEMDUP(
+               talloc_tos(), orig_iov, sizeof(struct iovec) * iovcnt);
+
+       if (iov_copy == NULL) {
+               errno = ENOMEM;
+               return -1;
+       }
+       iov = iov_copy;
+
+       while (sent < to_send) {
+               /*
+                * We have to discard "thistime" bytes from the beginning
+                * iov array, "thistime" contains the number of bytes sent
+                * via writev last.
+                */
+               while (thistime > 0) {
+                       if (thistime < iov[0].iov_len) {
+                               char *new_base =
+                                       (char *)iov[0].iov_base + thistime;
+                               iov[0].iov_base = (void *)new_base;
+                               iov[0].iov_len -= thistime;
+                               break;
                        }
-                       return -1;
+                       thistime -= iov[0].iov_len;
+                       iov += 1;
+                       iovcnt -= 1;
                }
 
-               if (ret == 0) {
-                       return total;
+               thistime = sys_writev(fd, iov, iovcnt);
+               if (thistime <= 0) {
+                       break;
                }
+               sent += thistime;
+       }
+
+       TALLOC_FREE(iov_copy);
+       return sent;
+}
+
+/****************************************************************************
+ Write data to a fd.
+****************************************************************************/
+
+ssize_t write_data(int fd, const char *buffer, size_t N)
+{
+       ssize_t ret;
+       struct iovec iov;
+
+       iov.iov_base = CONST_DISCARD(void *, buffer);
+       iov.iov_len = N;
 
-               total += ret;
+       ret = write_data_iov(fd, &iov, 1);
+       if (ret >= 0) {
+               return ret;
        }
-       return (ssize_t)total;
+
+       if (fd == get_client_fd()) {
+               char addr[INET6_ADDRSTRLEN];
+               /*
+                * Try and give an error message saying what client failed.
+                */
+               DEBUG(0, ("write_data: write failure in writing to client %s. "
+                         "Error %s\n", get_peer_addr(fd,addr,sizeof(addr)),
+                         strerror(errno)));
+       } else {
+               DEBUG(0,("write_data: write failure. Error = %s\n",
+                        strerror(errno) ));
+       }
+
+       return -1;
 }
 
 /****************************************************************************
@@ -890,102 +944,306 @@ int open_socket_in(int type,
        return( res );
  }
 
+struct open_socket_out_state {
+       int fd;
+       struct event_context *ev;
+       struct sockaddr_storage ss;
+       socklen_t salen;
+       uint16_t port;
+       int wait_nsec;
+};
+
+static void open_socket_out_connected(struct tevent_req *subreq);
+
+static int open_socket_out_state_destructor(struct open_socket_out_state *s)
+{
+       if (s->fd != -1) {
+               close(s->fd);
+       }
+       return 0;
+}
+
 /****************************************************************************
  Create an outgoing socket. timeout is in milliseconds.
 **************************************************************************/
 
-int open_socket_out(int type,
-               const struct sockaddr_storage *pss,
-               uint16_t port,
-               int timeout)
+struct tevent_req *open_socket_out_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       const struct sockaddr_storage *pss,
+                                       uint16_t port,
+                                       int timeout)
 {
        char addr[INET6_ADDRSTRLEN];
-       struct sockaddr_storage sock_out = *pss;
-       int res,ret;
-       int connect_loop = 10;
-       int increment = 10;
+       struct tevent_req *result, *subreq;
+       struct open_socket_out_state *state;
+       NTSTATUS status;
 
-       /* create a socket to write to */
-       res = socket(pss->ss_family, type, 0);
-       if (res == -1) {
-                DEBUG(0,("socket error (%s)\n", strerror(errno)));
-               return -1;
+       result = tevent_req_create(mem_ctx, &state,
+                                  struct open_socket_out_state);
+       if (result == NULL) {
+               return NULL;
        }
+       state->ev = ev;
+       state->ss = *pss;
+       state->port = port;
+       state->wait_nsec = 10000;
+       state->salen = -1;
 
-       if (type != SOCK_STREAM) {
-               return res;
+       state->fd = socket(state->ss.ss_family, SOCK_STREAM, 0);
+       if (state->fd == -1) {
+               status = map_nt_error_from_unix(errno);
+               goto post_status;
+       }
+       talloc_set_destructor(state, open_socket_out_state_destructor);
+
+       if (!tevent_req_set_endtime(
+                   result, ev, timeval_current_ofs(0, timeout*1000))) {
+               goto fail;
        }
 
 #if defined(HAVE_IPV6)
        if (pss->ss_family == AF_INET6) {
-               struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)&sock_out;
+               struct sockaddr_in6 *psa6;
+               psa6 = (struct sockaddr_in6 *)&state->ss;
                psa6->sin6_port = htons(port);
-               if (psa6->sin6_scope_id == 0 &&
-                               IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
-                       setup_linklocal_scope_id((struct sockaddr *)&sock_out);
+               if (psa6->sin6_scope_id == 0
+                   && IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
+                       setup_linklocal_scope_id(
+                               (struct sockaddr *)&(state->ss));
                }
+               state->salen = sizeof(struct sockaddr_in6);
        }
 #endif
        if (pss->ss_family == AF_INET) {
-               struct sockaddr_in *psa = (struct sockaddr_in *)&sock_out;
+               struct sockaddr_in *psa;
+               psa = (struct sockaddr_in *)&state->ss;
                psa->sin_port = htons(port);
+               state->salen = sizeof(struct sockaddr_in);
+       }
+
+       if (pss->ss_family == AF_UNIX) {
+               state->salen = sizeof(struct sockaddr_un);
        }
 
-       /* set it non-blocking */
-       set_blocking(res,false);
+       print_sockaddr(addr, sizeof(addr), &state->ss);
+       DEBUG(3,("Connecting to %s at port %u\n", addr, (unsigned int)port));
 
-       print_sockaddr(addr, sizeof(addr), &sock_out);
-       DEBUG(3,("Connecting to %s at port %u\n",
-                               addr,
-                               (unsigned int)port));
+       subreq = async_connect_send(state, state->ev, state->fd,
+                                   (struct sockaddr *)&state->ss,
+                                   state->salen);
+       if ((subreq == NULL)
+           || !tevent_req_set_endtime(
+                   subreq, state->ev,
+                   timeval_current_ofs(0, state->wait_nsec))) {
+               goto fail;
+       }
+       tevent_req_set_callback(subreq, open_socket_out_connected, result);
+       return result;
 
-       /* and connect it to the destination */
-  connect_again:
+ post_status:
+       tevent_req_nterror(result, status);
+       return tevent_req_post(result, ev);
+ fail:
+       TALLOC_FREE(result);
+       return NULL;
+}
 
-       ret = sys_connect(res, (struct sockaddr *)&sock_out);
+static void open_socket_out_connected(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq, struct tevent_req);
+       struct open_socket_out_state *state =
+               tevent_req_data(req, struct open_socket_out_state);
+       int ret;
+       int sys_errno;
 
-       /* Some systems return EAGAIN when they mean EINPROGRESS */
-       if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
-                       errno == EAGAIN) && (connect_loop < timeout) ) {
-               smb_msleep(connect_loop);
-               timeout -= connect_loop;
-               connect_loop += increment;
-               if (increment < 250) {
-                       /* After 8 rounds we end up at a max of 255 msec */
-                       increment *= 1.5;
-               }
-               goto connect_again;
+       ret = async_connect_recv(subreq, &sys_errno);
+       TALLOC_FREE(subreq);
+       if (ret == 0) {
+               tevent_req_done(req);
+               return;
        }
 
-       if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
-                       errno == EAGAIN)) {
-               DEBUG(1,("timeout connecting to %s:%u\n",
-                                       addr,
-                                       (unsigned int)port));
-               close(res);
-               return -1;
+       if (
+#ifdef ETIMEDOUT
+               (sys_errno == ETIMEDOUT) ||
+#endif
+               (sys_errno == EINPROGRESS) ||
+               (sys_errno == EALREADY) ||
+               (sys_errno == EAGAIN)) {
+
+               /*
+                * retry
+                */
+
+               if (state->wait_nsec < 250000) {
+                       state->wait_nsec *= 1.5;
+               }
+
+               subreq = async_connect_send(state, state->ev, state->fd,
+                                           (struct sockaddr *)&state->ss,
+                                           state->salen);
+               if (tevent_req_nomem(subreq, req)) {
+                       return;
+               }
+               if (!tevent_req_set_endtime(
+                           subreq, state->ev,
+                           timeval_current_ofs(0, state->wait_nsec))) {
+                       tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
+                       return;
+               }
+               tevent_req_set_callback(subreq, open_socket_out_connected, req);
+               return;
        }
 
 #ifdef EISCONN
-       if (ret < 0 && errno == EISCONN) {
-               errno = 0;
-               ret = 0;
+       if (sys_errno == EISCONN) {
+               tevent_req_done(req);
+               return;
        }
 #endif
 
-       if (ret < 0) {
-               DEBUG(2,("error connecting to %s:%d (%s)\n",
-                               addr,
-                               (unsigned int)port,
-                               strerror(errno)));
-               close(res);
-               return -1;
+       /* real error */
+       tevent_req_nterror(req, map_nt_error_from_unix(sys_errno));
+}
+
+NTSTATUS open_socket_out_recv(struct tevent_req *req, int *pfd)
+{
+       struct open_socket_out_state *state =
+               tevent_req_data(req, struct open_socket_out_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
        }
+       *pfd = state->fd;
+       state->fd = -1;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS open_socket_out(const struct sockaddr_storage *pss, uint16_t port,
+                        int timeout, int *pfd)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       /* set it blocking again */
-       set_blocking(res,true);
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
 
-       return res;
+       req = open_socket_out_send(frame, ev, pss, port, timeout);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll(req, ev)) {
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto fail;
+       }
+       status = open_socket_out_recv(req, pfd);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
+struct open_socket_out_defer_state {
+       struct event_context *ev;
+       struct sockaddr_storage ss;
+       uint16_t port;
+       int timeout;
+       int fd;
+};
+
+static void open_socket_out_defer_waited(struct tevent_req *subreq);
+static void open_socket_out_defer_connected(struct tevent_req *subreq);
+
+struct tevent_req *open_socket_out_defer_send(TALLOC_CTX *mem_ctx,
+                                             struct event_context *ev,
+                                             struct timeval wait_time,
+                                             const struct sockaddr_storage *pss,
+                                             uint16_t port,
+                                             int timeout)
+{
+       struct tevent_req *req, *subreq;
+       struct open_socket_out_defer_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct open_socket_out_defer_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->ev = ev;
+       state->ss = *pss;
+       state->port = port;
+       state->timeout = timeout;
+
+       subreq = tevent_wakeup_send(
+               state, ev,
+               timeval_current_ofs(wait_time.tv_sec, wait_time.tv_usec));
+       if (subreq == NULL) {
+               goto fail;
+       }
+       tevent_req_set_callback(subreq, open_socket_out_defer_waited, req);
+       return req;
+ fail:
+       TALLOC_FREE(req);
+       return NULL;
+}
+
+static void open_socket_out_defer_waited(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct open_socket_out_defer_state *state = tevent_req_data(
+               req, struct open_socket_out_defer_state);
+       bool ret;
+
+       ret = tevent_wakeup_recv(subreq);
+       TALLOC_FREE(subreq);
+       if (!ret) {
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+               return;
+       }
+
+       subreq = open_socket_out_send(state, state->ev, &state->ss,
+                                     state->port, state->timeout);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, open_socket_out_defer_connected, req);
+}
+
+static void open_socket_out_defer_connected(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct open_socket_out_defer_state *state = tevent_req_data(
+               req, struct open_socket_out_defer_state);
+       NTSTATUS status;
+
+       status = open_socket_out_recv(subreq, &state->fd);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+NTSTATUS open_socket_out_defer_recv(struct tevent_req *req, int *pfd)
+{
+       struct open_socket_out_defer_state *state = tevent_req_data(
+               req, struct open_socket_out_defer_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *pfd = state->fd;
+       state->fd = -1;
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
@@ -1159,24 +1417,39 @@ bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
 
 int open_udp_socket(const char *host, int port)
 {
-       int type = SOCK_DGRAM;
-       struct sockaddr_in sock_out;
+       struct sockaddr_storage ss;
        int res;
-       struct in_addr addr;
 
-       addr = interpret_addr2(host);
+       if (!interpret_string_addr(&ss, host, 0)) {
+               DEBUG(10,("open_udp_socket: can't resolve name %s\n",
+                       host));
+               return -1;
+       }
 
-       res = socket(PF_INET, type, 0);
+       res = socket(ss.ss_family, SOCK_DGRAM, 0);
        if (res == -1) {
                return -1;
        }
 
-       memset((char *)&sock_out,'\0',sizeof(sock_out));
-       putip((char *)&sock_out.sin_addr,(char *)&addr);
-       sock_out.sin_port = htons(port);
-       sock_out.sin_family = PF_INET;
+#if defined(HAVE_IPV6)
+       if (ss.ss_family == AF_INET6) {
+               struct sockaddr_in6 *psa6;
+               psa6 = (struct sockaddr_in6 *)&ss;
+               psa6->sin6_port = htons(port);
+               if (psa6->sin6_scope_id == 0
+                               && IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
+                       setup_linklocal_scope_id(
+                               (struct sockaddr *)&ss);
+               }
+       }
+#endif
+        if (ss.ss_family == AF_INET) {
+                struct sockaddr_in *psa;
+                psa = (struct sockaddr_in *)&ss;
+                psa->sin_port = htons(port);
+        }
 
-       if (sys_connect(res,(struct sockaddr *)&sock_out)) {
+       if (sys_connect(res,(struct sockaddr *)&ss)) {
                close(res);
                return -1;
        }
@@ -1266,7 +1539,7 @@ static bool matchname(const char *remotehost,
                if (!res->ai_addr) {
                        continue;
                }
-               if (addr_equal((const struct sockaddr *)res->ai_addr,
+               if (sockaddr_equal((const struct sockaddr *)res->ai_addr,
                                        (struct sockaddr *)pss)) {
                        freeaddrinfo(ailist);
                        return true;
@@ -1379,7 +1652,7 @@ const char *get_peer_name(int fd, bool force_lookup)
        p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), (struct sockaddr *)&ss, &length);
 
        /* it might be the same as the last one - save some DNS work */
-       if (addr_equal((struct sockaddr *)&ss, (struct sockaddr *)&nc.ss)) {
+       if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&nc.ss)) {
                return nc.name ? nc.name : "UNKNOWN";
        }
 
@@ -1618,6 +1891,7 @@ const char *get_mydnsfullname(void)
 bool is_myname_or_ipaddr(const char *s)
 {
        TALLOC_CTX *ctx = talloc_tos();
+       char addr[INET6_ADDRSTRLEN];
        char *name = NULL;
        const char *dnsname;
        char *servername = NULL;
@@ -1669,11 +1943,11 @@ bool is_myname_or_ipaddr(const char *s)
        if (!is_ipaddress(servername)) {
                /* Use DNS to resolve the name, but only the first address */
                struct sockaddr_storage ss;
-               if (interpret_string_addr(&ss, servername,0)) {
-                       print_sockaddr(name,
-                                       sizeof(name),
+               if (interpret_string_addr(&ss, servername, 0)) {
+                       print_sockaddr(addr,
+                                       sizeof(addr),
                                        &ss);
-                       servername = name;
+                       servername = addr;
                }
        }
 
@@ -1687,19 +1961,18 @@ bool is_myname_or_ipaddr(const char *s)
                        return false;
                }
 
+               if (ismyaddr((struct sockaddr *)&ss)) {
+                       return true;
+               }
+
                if (is_zero_addr((struct sockaddr *)&ss) || 
                        is_loopback_addr((struct sockaddr *)&ss)) {
                        return false;
                }
 
-               nics = TALLOC_ARRAY(ctx, struct iface_struct,
-                                       MAX_INTERFACES);
-               if (!nics) {
-                       return false;
-               }
-               n = get_interfaces(nics, MAX_INTERFACES);
+               n = get_interfaces(talloc_tos(), &nics);
                for (i=0; i<n; i++) {
-                       if (addr_equal((struct sockaddr *)&nics[i].ip, (struct sockaddr *)&ss)) {
+                       if (sockaddr_equal((struct sockaddr *)&nics[i].ip, (struct sockaddr *)&ss)) {
                                TALLOC_FREE(nics);
                                return true;
                        }
@@ -1710,3 +1983,85 @@ bool is_myname_or_ipaddr(const char *s)
        /* No match */
        return false;
 }
+
+struct getaddrinfo_state {
+       const char *node;
+       const char *service;
+       const struct addrinfo *hints;
+       struct addrinfo *res;
+       int ret;
+};
+
+static void getaddrinfo_do(void *private_data);
+static void getaddrinfo_done(struct tevent_req *subreq);
+
+struct tevent_req *getaddrinfo_send(TALLOC_CTX *mem_ctx,
+                                   struct tevent_context *ev,
+                                   struct fncall_context *ctx,
+                                   const char *node,
+                                   const char *service,
+                                   const struct addrinfo *hints)
+{
+       struct tevent_req *req, *subreq;
+       struct getaddrinfo_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct getaddrinfo_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->node = node;
+       state->service = service;
+       state->hints = hints;
+
+       subreq = fncall_send(state, ev, ctx, getaddrinfo_do, state);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, getaddrinfo_done, req);
+       return req;
+}
+
+static void getaddrinfo_do(void *private_data)
+{
+       struct getaddrinfo_state *state =
+               (struct getaddrinfo_state *)private_data;
+
+       state->ret = getaddrinfo(state->node, state->service, state->hints,
+                                &state->res);
+}
+
+static void getaddrinfo_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       int ret, err;
+
+       ret = fncall_recv(subreq, &err);
+       TALLOC_FREE(subreq);
+       if (ret == -1) {
+               tevent_req_error(req, err);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+int getaddrinfo_recv(struct tevent_req *req, struct addrinfo **res)
+{
+       struct getaddrinfo_state *state = tevent_req_data(
+               req, struct getaddrinfo_state);
+       int err;
+
+       if (tevent_req_is_unix_error(req, &err)) {
+               switch(err) {
+               case ENOMEM:
+                       return EAI_MEMORY;
+               default:
+                       return EAI_FAIL;
+               }
+       }
+       if (state->ret == 0) {
+               *res = state->res;
+       }
+       return state->ret;
+}