Use sys/xattr.h instead of attr/xattr.h
[platform/upstream/net-tools.git] / iptunnel.c
index d9c282b..e89b3c5 100644 (file)
@@ -13,6 +13,8 @@
  *
  * Rani Assaf <rani@magic.metawire.com> 980929:        resolve addresses
  * Rani Assaf <rani@magic.metawire.com> 980930:        do not allow key for ipip/sit
+ * Bernd Eckenfels 990715: add linux/types.h (not clean but solves missing __u16
+ * Arnaldo Carvalho de Melo 20010404: use setlocale
  */
 
 #include <stdio.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
-#include <linux/if.h>
-#include <linux/if_arp.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <arpa/inet.h>
+#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
+#include <net/if.h>
+#include <net/if_arp.h>
+#else
+#include <linux/if.h>
+#include <linux/if_arp.h>
+#endif
+#include <linux/types.h>
 #include <linux/if_tunnel.h>
 
 #include "config.h"
 #include "intl.h"
 #include "net-support.h"
 #include "version.h"
+#include "util.h"
 
 #undef GRE_CSUM
 #define GRE_CSUM       htons(0x8000)
@@ -60,7 +69,7 @@
 #include "util-ank.h"
 
 char *Release = RELEASE,
-     *Version = "iptunnel 1.0",
+     *Version = "iptunnel 1.01",
      *Signature = "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
 
 static void version(void)
@@ -83,7 +92,7 @@ static void usage(void)
        fprintf(stderr, _("       TOS  := { NUMBER | inherit }\n"));
        fprintf(stderr, _("       TTL  := { 1..255 | inherit }\n"));
        fprintf(stderr, _("       KEY  := { DOTTED_QUAD | NUMBER }\n"));
-       exit(-1);
+       exit(E_USAGE);
 }
 
 static int do_ioctl_get_ifindex(char *dev)
@@ -97,6 +106,7 @@ static int do_ioctl_get_ifindex(char *dev)
        err = ioctl(fd, SIOCGIFINDEX, &ifr);
        if (err) {
                perror("ioctl");
+               close(fd);
                return 0;
        }
        close(fd);
@@ -114,6 +124,7 @@ static int do_ioctl_get_iftype(char *dev)
        err = ioctl(fd, SIOCGIFHWADDR, &ifr);
        if (err) {
                perror("ioctl");
+               close(fd);
                return -1;
        }
        close(fd);
@@ -132,6 +143,7 @@ static char * do_ioctl_get_ifname(int idx)
        err = ioctl(fd, SIOCGIFNAME, &ifr);
        if (err) {
                perror("ioctl");
+               close(fd);
                return NULL;
        }
        close(fd);
@@ -279,7 +291,7 @@ static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
                                p->iph.saddr = get_addr32(*argv);
                } else if (strcmp(*argv, "dev") == 0) {
                        NEXT_ARG();
-                       strncpy(medium, *argv, IFNAMSIZ-1);
+                       safe_strncpy(medium, *argv, IFNAMSIZ-1);
                } else if (strcmp(*argv, "ttl") == 0) {
                        unsigned uval;
                        NEXT_ARG();
@@ -304,7 +316,7 @@ static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
                } else {
                        if (p->name[0])
                                usage();
-                       strncpy(p->name, *argv, IFNAMSIZ);
+                       safe_strncpy(p->name, *argv, IFNAMSIZ);
                }
                argc--; argv++;
        }
@@ -467,8 +479,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
                return -1;
        }
 
-       fgets(buf, sizeof(buf), fp);
-       fgets(buf, sizeof(buf), fp);
+       if (fgets(buf, sizeof(buf), fp))
+               /* eat line */;
+       if (fgets(buf, sizeof(buf), fp))
+               /* eat line */;
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                char *ptr;
@@ -476,6 +490,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
                if ((ptr = strchr(buf, ':')) == NULL ||
                    (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
                        fprintf(stderr, _("Wrong format of /proc/net/dev. Sorry.\n"));
+                       fclose(fp);
                        return -1;
                }
                if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
@@ -512,6 +527,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
                               tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
                }
        }
+       fclose(fp);
        return 0;
 }
 
@@ -573,6 +589,7 @@ int main(int argc, char **argv)
        char *basename;
 
 #if I18N
+       setlocale (LC_ALL, "");
        bindtextdomain("net-tools", "/usr/share/locale");
        textdomain("net-tools");
 #endif