From d389ba8b5bf9f99c4e4effcd33b10ec6f7f9523f Mon Sep 17 00:00:00 2001 From: ArnoStiefvater Date: Wed, 27 May 2020 07:03:06 +0200 Subject: [PATCH] Make requested changes --- base/networking.c | 61 +++++++++++++++++++++++------------------------ base/networking.h | 3 +-- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/base/networking.c b/base/networking.c index 09a19f1af..226ce6829 100644 --- a/base/networking.c +++ b/base/networking.c @@ -774,16 +774,17 @@ gboolean ip_islocalhost (struct sockaddr_storage *storage) { struct in_addr addr; - struct in_addr *addr_p = &addr; + struct in_addr *addr_p; struct in6_addr addr6; - struct in6_addr *addr6_p = &addr6; + struct in6_addr *addr6_p; struct sockaddr_in *sin_p; struct sockaddr_in6 *sin6_p; - struct ifaddrs *ifaddr = NULL; - struct ifaddrs *ifa = NULL; + struct ifaddrs *ifaddr, *ifa; int family; family = storage->ss_family; + addr6_p = &addr6; + addr_p = &addr; if (family == AF_INET) { @@ -828,6 +829,7 @@ ip_islocalhost (struct sockaddr_storage *storage) { struct sockaddr_in *sin; struct sockaddr_in6 *sin6; + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) @@ -853,6 +855,8 @@ ip_islocalhost (struct sockaddr_storage *storage) return FALSE; } +typedef struct route_entry route_entry_t; + /** Entry of routing table /proc/net/route */ struct route_entry { @@ -875,7 +879,7 @@ get_routes (void) gchar *line; gchar **items_in_line; int status; - struct route_entry *entry; + route_entry_t *entry; /* Open "/proc/net/route". */ file_channel = g_io_channel_new_file ("/proc/net/route", "r", &err); @@ -900,13 +904,11 @@ get_routes (void) } /* Until EOF or err we go through lines of file and extract Iface, Mask and - * Destination and but it into the to be returned list of routes.*/ + * Destination and put it into the to be returned list of routes.*/ while (1) { - gchar *interface; - unsigned long mask; - unsigned long dest; - gchar *char_p; + gchar *interface, *char_p; + unsigned long mask, dest; int count = 0; /* Get new line. */ @@ -945,7 +947,7 @@ get_routes (void) mask = strtoul (items_in_line[7], NULL, 16); /* Fill GSList entry. */ - entry = g_malloc0 (sizeof (struct route_entry)); + entry = g_malloc0 (sizeof (route_entry_t)); entry->interface = interface; entry->dest = dest; entry->mask = mask; @@ -983,13 +985,12 @@ gchar * gvm_routethrough (struct sockaddr_storage *storage_dest, struct sockaddr_storage *storage_source) { + struct ifaddrs *ifaddr, *ifa; + gchar *interface_out = NULL; + if (!storage_dest) return NULL; - struct ifaddrs *ifaddr = NULL; - struct ifaddrs *ifa = NULL; - gchar *interface_out = NULL; - if (getifaddrs (&ifaddr) == -1) { g_debug ("%s: getifaddr failed: %s", __func__, strerror (errno)); @@ -999,8 +1000,9 @@ gvm_routethrough (struct sockaddr_storage *storage_dest, /* IPv4. */ if (storage_dest->ss_family == AF_INET) { - GSList *routes = NULL; - GSList *routes_p = NULL; + GSList *routes; + GSList *routes_p; + routes = get_routes (); /* Set storage_source to localhost if storage_source was supplied and @@ -1026,10 +1028,9 @@ gvm_routethrough (struct sockaddr_storage *storage_dest, } else { - struct sockaddr_in *sin_dest_p = NULL; - struct sockaddr_in *sin_src_p = NULL; - unsigned long best_match = 0; + struct sockaddr_in *sin_dest_p, *sin_src_p; struct in_addr global_src; + unsigned long best_match = 0; /* Check if global_source_addr in use. */ gvm_source_addr (&global_src); @@ -1041,17 +1042,15 @@ gvm_routethrough (struct sockaddr_storage *storage_dest, for (routes_p = routes; routes_p; routes_p = routes_p->next) { if (((sin_dest_p->sin_addr.s_addr - & ((struct route_entry *) (routes_p->data))->mask) - == ((struct route_entry *) (routes_p->data))->dest) - && (((struct route_entry *) (routes_p->data))->mask - >= best_match)) + & ((route_entry_t *) (routes_p->data))->mask) + == ((route_entry_t *) (routes_p->data))->dest) + && (((route_entry_t *) (routes_p->data))->mask >= best_match)) { /* Interface of matching route.*/ - if (interface_out) - g_free (interface_out); - interface_out = g_strdup ( - ((struct route_entry *) (routes_p->data))->interface); - best_match = ((struct route_entry *) (routes_p->data))->mask; + g_free (interface_out); + interface_out = + g_strdup (((route_entry_t *) (routes_p->data))->interface); + best_match = ((route_entry_t *) (routes_p->data))->mask; if (!storage_source) continue; @@ -1085,8 +1084,8 @@ gvm_routethrough (struct sockaddr_storage *storage_dest, { for (routes_p = routes; routes_p; routes_p = routes_p->next) { - if (((struct route_entry *) (routes_p->data))->interface) - g_free (((struct route_entry *) (routes_p->data))->interface); + if (((route_entry_t *) (routes_p->data))->interface) + g_free (((route_entry_t *) (routes_p->data))->interface); } g_slist_free (routes); } diff --git a/base/networking.h b/base/networking.h index 6cdb42fe9..e02c86c03 100644 --- a/base/networking.h +++ b/base/networking.h @@ -114,7 +114,6 @@ int ipv6_is_enabled (); gchar * -gvm_routethrough (struct sockaddr_storage *storage_dest, - struct sockaddr_storage *storage_source); +gvm_routethrough (struct sockaddr_storage *, struct sockaddr_storage *); #endif /* not _GVM_NETWORKING_H */