[exim-cvs] Debug: socket details

Startseite
Nachricht löschen
Nachricht beantworten
Autor: Exim Git Commits Mailing List
Datum:  
To: exim-cvs
Betreff: [exim-cvs] Debug: socket details
Gitweb: https://git.exim.org/exim.git/commitdiff/a8e46b3b7147d214072bc34beff184724f3f2be6
Commit:     a8e46b3b7147d214072bc34beff184724f3f2be6
Parent:     3f14427fb2444a4a6ca14c0e62f5009981b5ca4c
Author:     Jeremy Harris <jgh146exb@???>
AuthorDate: Sat May 2 14:22:31 2020 +0100
Committer:  Jeremy Harris <jgh146exb@???>
CommitDate: Sat May 2 14:22:31 2020 +0100


    Debug: socket details
---
 src/src/debug.c     | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/src/functions.h |  2 ++
 src/src/malware.c   |  8 +++++-
 3 files changed, 87 insertions(+), 1 deletion(-)


diff --git a/src/src/debug.c b/src/src/debug.c
index de97962..806573f 100644
--- a/src/src/debug.c
+++ b/src/src/debug.c
@@ -315,4 +315,82 @@ if (debug_ptr[-1] == '\n')
errno = save_errno;
}

+
+
+/* Output the details of a socket */
+
+void
+debug_print_socket(int fd)
+{
+struct stat s;
+if (fstat(fd, &s) == 0 && (s.st_mode & S_IFMT) == S_IFSOCK)
+  {
+  gstring * g = NULL;
+  int val;
+  socklen_t vlen = sizeof(val);
+  struct sockaddr a;
+  socklen_t alen = sizeof(a);
+  struct sockaddr_in * sinp = (struct sockaddr_in *)&a;
+  struct sockaddr_in6 * sin6p = (struct sockaddr_in6 *)&a;
+  struct sockaddr_un * sa_unp ; (struct sockaddr_un *)&a;
+
+  if (getsockname(fd, &a, &alen) == 0)
+    switch (sinp->sin_family)
+      {
+      case AF_INET:
+    g = string_cat(g, US" domain AF_INET");
+    g = string_fmt_append(g, " lcl [%s]:%u",
+      inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port));
+    if (getpeername(fd, &a, &alen) == 0)
+      g = string_fmt_append(g, " rmt [%s]:%u",
+        inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port));
+    break;
+      case AF_INET6:
+    {
+    uschar buf[46];
+    g = string_cat(g, US" domain AF_INET6");
+    g = string_fmt_append(g, " lcl [%s]:%u",
+      inet_ntop(AF_INET6, &sin6p->sin6_addr, CS buf, sizeof(buf)),
+      ntohs(sin6p->sin6_port));
+    if (getpeername(fd, &a, &alen) == 0)
+      g = string_fmt_append(g, " rmt [%s]:%u",
+        inet_ntop(AF_INET6, &sin6p->sin6_addr, CS buf, sizeof(buf)),
+        ntohs(sin6p->sin6_port));
+    break;
+    }
+      case AF_UNIX:
+    g = string_cat(g, US" domain AF_UNIX");
+    g = string_fmt_append(g, " lcl %s%s",
+      sa_unp->sun_path[0] ? US"" : US"@",
+      sa_unp->sun_path[0] ? sa_unp->sun_path : sa_unp->sun_path+1);
+    if (getpeername(fd, &a, &alen) == 0)
+      g = string_fmt_append(g, " rmt %s%s",
+        sa_unp->sun_path[0] ? US"" : US"@",
+        sa_unp->sun_path[0] ? sa_unp->sun_path : sa_unp->sun_path+1);
+    break;
+      default:
+    g = string_fmt_append(g, " domain %u", sinp->sin_family);
+    break;
+      }
+  if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &vlen) == 0)
+    switch (val)
+      {
+      case SOCK_STREAM:    g = string_cat(g, US" type SOCK_STREAM"); break;
+      case SOCK_DGRAM:    g = string_cat(g, US" type SOCK_DGRAM"); break;
+      default:    g = string_fmt_append(g, " type %d", val); break;
+      }
+  if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &val, &vlen) == 0)
+    {
+    struct protoent * p = getprotobynumber(val);
+    g = p
+      ? string_fmt_append(g, " proto %s\n", p->p_name)
+      : string_fmt_append(g, " proto %d", val);
+    }
+  debug_printf_indent(" socket: %s\n", string_from_gstring(g));
+  }
+else
+  debug_printf_indent(" fd st_mode 0%o\n", s.st_mode);
+}
+
+
 /* End of debug.c */
diff --git a/src/src/functions.h b/src/src/functions.h
index fbddd35..0028deb 100644
--- a/src/src/functions.h
+++ b/src/src/functions.h
@@ -184,6 +184,8 @@ extern void    debug_printf_indent(const char *, ...) PRINTF_FUNCTION(1,2);
 extern void    debug_print_string(uschar *);
 extern void    debug_print_tree(tree_node *);
 extern void    debug_vprintf(int, const char *, va_list);
+extern void    debug_print_socket(int);
+
 extern void    decode_bits(unsigned int *, size_t, int *,
                uschar *, bit_table *, int, uschar *, int);
 extern void    delete_pid_file(void);
diff --git a/src/src/malware.c b/src/src/malware.c
index 5ed469f..7ed4d35 100644
--- a/src/src/malware.c
+++ b/src/src/malware.c
@@ -262,6 +262,7 @@ static inline int
 m_panic_defer_3(struct scan * scanent, const uschar * hostport,
   const uschar * str, int fd_to_close)
 {
+DEBUG(D_acl) debug_print_socket(fd_to_close);
 (void) close(fd_to_close);
 return m_panic_defer(scanent, hostport, str);
 }
@@ -338,6 +339,7 @@ else
 return cre;
 }


+
 /*
  Simple though inefficient wrapper for reading a line.  Drop CRs and the
  trailing newline. Can return early on buffer full. Null-terminate.
@@ -369,8 +371,12 @@ while ((rcv = read(fd, p, 1)) > 0)
   }
 if (!ok)
   {
-  DEBUG(D_acl) debug_printf_indent("Malware scan: read %s (%s)\n",
+  DEBUG(D_acl)
+    {
+    debug_printf_indent("Malware scan: read %s (%s)\n",
         rcv==0 ? "EOF" : "error", strerror(errno));
+    debug_print_socket(fd);
+    }
   return rcv==0 ? -1 : -2;
   }
 *p = '\0';