Lots of stuff.
[misc] / if-mtu.c
diff --git a/if-mtu.c b/if-mtu.c
new file mode 100644 (file)
index 0000000..b4ddbee
--- /dev/null
+++ b/if-mtu.c
@@ -0,0 +1,36 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+
+int main(int argc, char *argv[])
+{
+  int fd;
+  struct ifreq ifr;
+  if (argc != 2) {
+    fprintf(stderr, "usage: %s IFACE\n", argv[0]);
+    exit(1);
+  }
+  if (strlen(argv[1]) >= sizeof(ifr.ifr_name)) {
+    fprintf(stderr, "%s: interface name `%s' too long\n", argv[0], argv[1]);
+    exit(1);
+  }
+  if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
+    fprintf(stderr, "%s: socket: %s\n", argv[0], strerror(errno));
+    exit(1);
+  }
+  strcpy(ifr.ifr_name, argv[1]);
+  if (ioctl(fd, SIOCGIFMTU, &ifr)) {
+    fprintf(stderr, "%s: ioctl(SIOCGIFMTU): %s\n", argv[0], strerror(errno));
+    exit(1);
+  }
+  printf("%d\n", ifr.ifr_mtu);
+  return (0);
+}