You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by sc...@apache.org on 2018/02/01 05:50:13 UTC

[trafficserver] branch master updated: Use NetVCOptions to create UDPConnection

This is an automated email from the ASF dual-hosted git repository.

scw00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 21cc11e  Use NetVCOptions to create UDPConnection
21cc11e is described below

commit 21cc11e62dd933a9405e7e6b78608929a35da8e6
Author: scw00 <sc...@apache.org>
AuthorDate: Thu Feb 1 09:56:11 2018 +0800

    Use NetVCOptions to create UDPConnection
---
 iocore/net/I_UDPConnection.h |  1 +
 iocore/net/I_UDPNet.h        |  3 +--
 iocore/net/P_UDPConnection.h | 10 ++++++++
 iocore/net/UnixUDPNet.cc     | 61 +++++++++++++++++++++++++++++++-------------
 4 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/iocore/net/I_UDPConnection.h b/iocore/net/I_UDPConnection.h
index 3f2b588..850414b 100644
--- a/iocore/net/I_UDPConnection.h
+++ b/iocore/net/I_UDPConnection.h
@@ -49,6 +49,7 @@ public:
 
   SOCKET getFd();
   void setBinding(struct sockaddr const *);
+  void setBinding(const IpAddr &, in_port_t);
   inkcoreapi int getBinding(struct sockaddr *);
 
   void destroy();
diff --git a/iocore/net/I_UDPNet.h b/iocore/net/I_UDPNet.h
index b8379eb..5e3c748 100644
--- a/iocore/net/I_UDPNet.h
+++ b/iocore/net/I_UDPNet.h
@@ -49,8 +49,7 @@ public:
 
   // this function was interanal intially.. this is required for public and
   // interface probably should change.
-  bool CreateUDPSocket(int *resfd, sockaddr const *remote_addr, sockaddr *local_addr, int *local_addr_len, Action **status,
-                       int send_bufsize = 0, int recv_bufsize = 0);
+  bool CreateUDPSocket(int *resfd, sockaddr const *remote_addr, Action **status, NetVCOptions &opt);
 
   /**
      create UDPConnection
diff --git a/iocore/net/P_UDPConnection.h b/iocore/net/P_UDPConnection.h
index 70918cf..29abbb9 100644
--- a/iocore/net/P_UDPConnection.h
+++ b/iocore/net/P_UDPConnection.h
@@ -92,6 +92,16 @@ UDPConnection::setBinding(struct sockaddr const *s)
   p->binding_valid = 1;
 }
 
+TS_INLINE void
+UDPConnection::setBinding(IpAddr const &ip, in_port_t port)
+{
+  UDPConnectionInternal *p = (UDPConnectionInternal *)this;
+  IpEndpoint addr;
+  addr.assign(ip, htons(port));
+  ats_ip_copy(&p->binding, addr);
+  p->binding_valid = 1;
+}
+
 TS_INLINE int
 UDPConnection::getBinding(struct sockaddr *s)
 {
diff --git a/iocore/net/UnixUDPNet.cc b/iocore/net/UnixUDPNet.cc
index 501b029..a1b9afb 100644
--- a/iocore/net/UnixUDPNet.cc
+++ b/iocore/net/UnixUDPNet.cc
@@ -579,41 +579,66 @@ UDPNetProcessor::sendto_re(Continuation *cont, void *token, int fd, struct socka
 }
 
 bool
-UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr const *remote_addr, sockaddr *local_addr, int *local_addr_len,
-                                 Action **status, int send_bufsize, int recv_bufsize)
+UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr const *remote_addr, Action **status, NetVCOptions &opt)
 {
   int res = 0, fd = -1;
-
-  ink_assert(ats_ip_are_compatible(remote_addr, local_addr));
+  int local_addr_len;
+  IpEndpoint local_addr;
+
+  // Need to do address calculations first, so we can determine the
+  // address family for socket creation.
+  ink_zero(local_addr);
+
+  bool is_any_address = false;
+  if (NetVCOptions::FOREIGN_ADDR == opt.addr_binding || NetVCOptions::INTF_ADDR == opt.addr_binding) {
+    // Same for now, transparency for foreign addresses must be handled
+    // *after* the socket is created, and we need to do this calculation
+    // before the socket to get the IP family correct.
+    ink_release_assert(opt.local_ip.isValid());
+    local_addr.assign(opt.local_ip, htons(opt.local_port));
+    ink_assert(ats_ip_are_compatible(remote_addr, &local_addr.sa));
+  } else {
+    // No local address specified, so use family option if possible.
+    int family = ats_is_ip(opt.ip_family) ? opt.ip_family : AF_INET;
+    local_addr.setToAnyAddr(family);
+    is_any_address    = true;
+    local_addr.port() = htons(opt.local_port);
+  }
 
   *resfd = -1;
   if ((res = socketManager.socket(remote_addr->sa_family, SOCK_DGRAM, 0)) < 0) {
     goto HardError;
   }
+
   fd = res;
   if ((res = safe_fcntl(fd, F_SETFL, O_NONBLOCK)) < 0) {
     goto HardError;
   }
-  if ((res = socketManager.ink_bind(fd, local_addr, ats_ip_size(local_addr), IPPROTO_UDP)) < 0) {
-    char buff[INET6_ADDRPORTSTRLEN];
-    Debug("udpnet", "ink bind failed on %s", ats_ip_nptop(remote_addr, buff, sizeof(buff)));
-    goto SoftError;
-  }
 
-  if (recv_bufsize) {
-    if (unlikely(socketManager.set_rcvbuf_size(fd, recv_bufsize))) {
-      Debug("udpnet", "set_dnsbuf_size(%d) failed", recv_bufsize);
+  if (opt.socket_recv_bufsize > 0) {
+    if (unlikely(socketManager.set_rcvbuf_size(fd, opt.socket_recv_bufsize))) {
+      Debug("udpnet", "set_dnsbuf_size(%d) failed", opt.socket_recv_bufsize);
     }
   }
-  if (send_bufsize) {
-    if (unlikely(socketManager.set_sndbuf_size(fd, send_bufsize))) {
-      Debug("udpnet", "set_dnsbuf_size(%d) failed", send_bufsize);
+  if (opt.socket_send_bufsize > 0) {
+    if (unlikely(socketManager.set_sndbuf_size(fd, opt.socket_send_bufsize))) {
+      Debug("udpnet", "set_dnsbuf_size(%d) failed", opt.socket_send_bufsize);
     }
   }
-  if ((res = safe_getsockname(fd, local_addr, local_addr_len)) < 0) {
-    Debug("udpnet", "CreateUdpsocket: getsockname didnt' work");
-    goto HardError;
+
+  if (local_addr.port() || !is_any_address) {
+    if (-1 == socketManager.ink_bind(fd, &local_addr.sa, ats_ip_size(&local_addr.sa))) {
+      char buff[INET6_ADDRPORTSTRLEN];
+      Debug("udpnet", "ink bind failed on %s", ats_ip_nptop(local_addr, buff, sizeof(buff)));
+      goto SoftError;
+    }
+
+    if ((res = safe_getsockname(fd, &local_addr.sa, &local_addr_len)) < 0) {
+      Debug("udpnet", "CreateUdpsocket: getsockname didnt' work");
+      goto HardError;
+    }
   }
+
   *resfd  = fd;
   *status = nullptr;
   Debug("udpnet", "creating a udp socket port = %d, %d---success", ats_ip_port_host_order(remote_addr),

-- 
To stop receiving notification emails like this one, please contact
scw00@apache.org.