]> Pileus Git - ~andy/fetchmail/blobdiff - interface.c
Credit John Beck's fixes.
[~andy/fetchmail] / interface.c
index d4688094fef369ec0f2d0b1f26b03b1eb1419d17..b63e1121c75d8794fb578bd6368f123ab2e98d02 100644 (file)
@@ -63,8 +63,6 @@ struct interface_pair_s {
        struct in_addr interface_mask;
 } *interface_pair;
 
-static char *netdevfmt;
-
 /*
  * Count of packets to see on an interface before monitor considers it up.
  * Needed because when pppd shuts down the link, the packet counts go up
@@ -74,6 +72,9 @@ static char *netdevfmt;
 #define MONITOR_SLOP           5
 
 #ifdef linux
+#define have_interface_init
+
+static const char *netdevfmt;
 
 void interface_init(void)
 /* figure out which /proc/net/dev format to use */
@@ -172,13 +173,13 @@ static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
                result = FALSE;
        else
        {
-           char        *sp = strchr(ifname, '/');
-
+           char *tmp = xstrdup(ifname);
+           char *sp = strchr(tmp, '/');
+           /* hide slash and trailing info from ifname */
            if (sp)
                *sp = '\0';
-           result = _get_ifinfoGT_(socket_fd, stats_file, ifname, ifinfo);
-           if (sp)
-               *sp = '/';
+           result = _get_ifinfoGT_(socket_fd, stats_file, tmp, ifinfo);
+           free(tmp);
        }
        if (socket_fd >= 0)
            SockClose(socket_fd);
@@ -270,7 +271,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
        {
                kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
                kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
-               snprintf(tname, sizeof tname, "%s%d", tname, ifnet.if_unit);
+               snprintf(tname + strlen(tname), sizeof(tname) - strlen(tname), "%d", ifnet.if_unit);
 
                if (!strcmp(tname, iname))
                {
@@ -383,7 +384,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
     char               iname[16];
     int                        mib[6];
 
-    memset(ifinfo, 0, sizeof(ifinfo));
+    memset(ifinfo, 0, sizeof(*ifinfo));
 
     /* trim interface name */
 
@@ -417,7 +418,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
            GT_("get_ifinfo: sysctl (iflist estimate) failed"));
        exit(1);
     }
-    if ((buf = malloc(needed)) == NULL)
+    if ((buf = (char *)malloc(needed)) == NULL)
     {
        report(stderr, 
            GT_("get_ifinfo: malloc failed"));
@@ -513,7 +514,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
            }
 
            sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
-           if (!sin)
+           if (sin)
            {
                ifinfo->netmask = sin->sin_addr;
            }
@@ -523,7 +524,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
             * of non point-to-point link
             */
            sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
-           if (!sin)
+           if (sin)
            {
                ifinfo->dstaddr = sin->sin_addr;
            }
@@ -544,13 +545,8 @@ get_ifinfo_end:
 
 #endif /* __FREEBSD_USE_SYSCTL_GET_IFFINFO */
 
-#else
-
-void interface_init(void) {};
-
 #endif
 
-
 #ifndef HAVE_INET_ATON
 /*
  * Note: This is not a true replacement for inet_aton(), as it won't
@@ -583,6 +579,7 @@ void interface_parse(char *buf, struct hostdata *hp)
 /* parse 'interface' specification */
 {
        char *cp1, *cp2;
+       char mask1[] = "255.255.255.255";
 
        hp->interface = xstrdup(buf);
 
@@ -597,7 +594,7 @@ void interface_parse(char *buf, struct hostdata *hp)
 
        /* find and isolate just the netmask */
        if (!(cp2 = strchr(cp1, '/')))
-               cp2 = "255.255.255.255";
+               cp2 = mask1;
        else
                *cp2++ = '\000';
 
@@ -738,3 +735,7 @@ int interface_approve(struct hostdata *hp, flag domonitor)
        return(TRUE);
 }
 #endif /* CAN_MONITOR */
+
+#ifndef have_interface_init
+void interface_init(void) {}
+#endif