vcl: add api to retrieve num bytes for tx
[vpp.git] / src / vppinfra / socket.c
index e61c369..2abf2b2 100644 (file)
@@ -226,7 +226,7 @@ static clib_error_t *
 default_socket_recvmsg (clib_socket_t * s, void *msg, int msglen,
                        int fds[], int num_fds)
 {
-#ifdef CLIB_LINUX
+#if CLIB_LINUX
   char ctl[CMSG_SPACE (sizeof (int) * num_fds) +
           CMSG_SPACE (sizeof (struct ucred))];
   struct ucred *cr = 0;
@@ -261,7 +261,7 @@ default_socket_recvmsg (clib_socket_t * s, void *msg, int msglen,
     {
       if (cmsg->cmsg_level == SOL_SOCKET)
        {
-#ifdef CLIB_LINUX
+#if CLIB_LINUX
          if (cmsg->cmsg_type == SCM_CREDENTIALS)
            {
              cr = (struct ucred *) CMSG_DATA (cmsg);
@@ -314,11 +314,13 @@ static const struct
     .family = AF_INET,
     .type = CLIB_SOCKET_TYPE_INET,
     .skip_prefix = 1 },
+#if CLIB_LINUX
   { .prefix = "abstract:",
     .family = AF_UNIX,
     .type = CLIB_SOCKET_TYPE_LINUX_ABSTRACT,
     .skip_prefix = 1,
     .is_local = 1 },
+#endif /* CLIB_LINUX */
   { .prefix = "/",
     .family = AF_UNIX,
     .type = CLIB_SOCKET_TYPE_UNIX,
@@ -374,6 +376,16 @@ clib_socket_prefix_is_valid (char *s)
   return 0;
 }
 
+__clib_export int
+clib_socket_prefix_get_type (char *s)
+{
+  for (typeof (clib_socket_type_data[0]) *d = clib_socket_type_data;
+       d - clib_socket_type_data < ARRAY_LEN (clib_socket_type_data); d++)
+    if (strncmp (s, d->prefix, strlen (d->prefix)) == 0)
+      return d->type;
+  return 0;
+}
+
 __clib_export clib_error_t *
 clib_socket_init (clib_socket_t *s)
 {
@@ -450,7 +462,11 @@ clib_socket_init (clib_socket_t *s)
        {
          p += 11;
          u8 *str = _clib_socket_get_string (&p, 0);
-         u8 *pathname = format (0, "/var/run/netns/%v%c", str, 0);
+         u8 *pathname = 0;
+         if (str[0] == '/')
+           pathname = format (0, "%v%c", str, 0);
+         else
+           pathname = format (0, "/var/run/netns/%v%c", str, 0);
          if ((netns_fd = open ((char *) pathname, O_RDONLY)) < 0)
            err = clib_error_return_unix (0, "open('%s')", pathname);
          vec_free (str);
@@ -657,11 +673,24 @@ clib_socket_init (clib_socket_t *s)
        }
 #endif
 
-      if (need_bind && bind (s->fd, sa, addr_len) < 0)
+      if (need_bind)
        {
-         err =
-           clib_error_return_unix (0, "bind (fd %d, '%s')", s->fd, s->config);
-         goto done;
+         int bind_ret;
+         if (sa->sa_family == AF_UNIX && s->allow_group_write)
+           {
+             mode_t def_restrictions = umask (S_IWOTH);
+             bind_ret = bind (s->fd, sa, addr_len);
+             umask (def_restrictions);
+           }
+         else
+           bind_ret = bind (s->fd, sa, addr_len);
+
+         if (bind_ret < 0)
+           {
+             err = clib_error_return_unix (0, "bind (fd %d, '%s')", s->fd,
+                                           s->config);
+             goto done;
+           }
        }
 
       if (listen (s->fd, 5) < 0)
@@ -670,16 +699,6 @@ clib_socket_init (clib_socket_t *s)
                                        s->config);
          goto done;
        }
-
-      if (s->local_only && s->allow_group_write)
-       {
-         if (fchmod (s->fd, S_IWGRP) < 0)
-           {
-             err = clib_error_return_unix (
-               0, "fchmod (fd %d, '%s', mode S_IWGRP)", s->fd, s->config);
-             goto done;
-           }
-       }
     }
   else
     {
@@ -718,7 +737,7 @@ done:
 #if CLIB_LINUX
   if (netns_fd != -1)
     {
-      setns (CLONE_NEWNET, netns_fd);
+      setns (netns_fd, CLONE_NEWNET);
       close (netns_fd);
     }
 #endif
@@ -726,45 +745,6 @@ done:
   return err;
 }
 
-__clib_export clib_error_t *
-clib_socket_init_netns (clib_socket_t *s, u8 *namespace)
-{
-  if (namespace == NULL || namespace[0] == 0)
-    return clib_socket_init (s);
-
-  clib_error_t *error;
-  int old_netns_fd, nfd = -1;
-
-  old_netns_fd = clib_netns_open (NULL /* self */);
-  if (old_netns_fd < 0)
-    return clib_error_return_unix (0, "get current netns failed");
-
-  if ((nfd = clib_netns_open (namespace)) == -1)
-    {
-      error = clib_error_return_unix (0, "clib_netns_open '%s'", namespace);
-      goto done;
-    }
-
-  if (clib_setns (nfd) == -1)
-    {
-      error = clib_error_return_unix (0, "setns '%s'", namespace);
-      goto done;
-    }
-
-  error = clib_socket_init (s);
-
-done:
-  if (clib_setns (old_netns_fd) == -1)
-    clib_warning ("Cannot set old ns");
-
-  close (old_netns_fd);
-
-  if (-1 != nfd)
-    close (nfd);
-
-  return error;
-}
-
 __clib_export clib_error_t *
 clib_socket_accept (clib_socket_t * server, clib_socket_t * client)
 {