Skip to content

Commit

Permalink
Make requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoStiefvater committed May 27, 2020
1 parent 773a996 commit d389ba8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
61 changes: 30 additions & 31 deletions base/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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
{
Expand All @@ -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);
Expand All @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions base/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit d389ba8

Please sign in to comment.