]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/inetcalc.c
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / inetcalc.c
index d08f0f3a69a820a48cf53fe2269382ecb0673299..7c072e99214b7172a1ebb422c0f52b8c8379d8be 100644 (file)
@@ -28,6 +28,8 @@
 #include <string.h>
 #include <sys/socket.h>
 
+#include <network/libnetwork.h>
+
 typedef struct ip_address {
        int family;
        struct in6_addr addr;
@@ -132,9 +134,9 @@ static int default_prefix(const int family) {
 static int ip_address_parse_simple(ip_address_t* ip, const int family, const char* address) {
        assert(family == AF_INET || family == AF_INET6);
 
-       size_t address_length = strlen(address);
-       char buffer[address_length + 1];
-       strncpy(buffer, address, sizeof(buffer));
+       // Copy input to stack
+       char buffer[512];
+       strncpy(buffer, address, sizeof(buffer) - 1);
 
        // Search for a prefix or subnet mask
        char* prefix = strchr(buffer, '/');
@@ -245,7 +247,10 @@ static void ip_address_print(const ip_address_t* ip) {
        if (r)
                return;
 
-       if (ip->prefix >= 0) {
+       int address_prefix = default_prefix(ip->family);
+
+       // Only print prefix when it is not the default one
+       if (ip->prefix != address_prefix) {
                size_t len = strlen(buffer);
                snprintf(buffer + len, sizeof(buffer) - len, "/%d", ip->prefix);
        }
@@ -510,7 +515,7 @@ int main(int argc, char** argv) {
        int family = AF_UNSPEC;
 
        while (1) {
-               int c = getopt_long(argc, argv, "46bcefgnpsv", long_options, &option_index);
+               int c = getopt_long(argc, argv, "46bcefgnpsviV", long_options, &option_index);
                if (c == -1)
                        break;
 
@@ -577,6 +582,11 @@ int main(int argc, char** argv) {
                                verbose = 1;
                                break;
 
+                       case 'V':
+                               printf("%s\n", network_version());
+                               exit(0);
+                               break;
+
                        case '?':
                                break;