Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Improve consistency in getgateway.h #1045

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/network_info_plus/network_info_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.5

- IOS: Fix potential issues with building native Objective C code.

## 2.1.4+1

- Add issue_tracker link.
Expand All @@ -6,7 +10,6 @@

- Android: Plugin no longer removes `"` from SSID name.


## 2.1.3

- Update nm dependency to be compatible with fresh versions of other Plus plugins
Expand Down Expand Up @@ -50,7 +53,6 @@

- migrate integration_test to flutter sdk


## 1.1.0

- Android, IOS: Adding IPv6 information
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Created by Dimitri Dessus on 08/07/2021.
//

#include <netinet/in.h>

#ifndef getgateway_h
#define getgateway_h

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// getgateway.c
// network_info_plus
//
// Created by Dimitri Dessus on 08/07/2021.
//
// directly cloned from https://stackoverflow.com/a/29440193/5078902
// thanks to Alex0072005

#include "getgateway.h"
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysctl.h>

#include "route.h"
#define TypeEN "en0"

#include <net/if.h>
#include <string.h>

#define CTL_NET 4 /* network, see socket.h */

#if defined(BSD) || defined(__APPLE__)

#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a)-1) | (sizeof(long) - 1))) : sizeof(long))

int getDefaultGateway(in_addr_t *addr) {
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY};
size_t l;
char *buf, *p;
struct rt_msghdr *rt;
struct sockaddr *sa;
struct sockaddr *sa_tab[RTAX_MAX];
int i;
int r = -1;
if (sysctl(mib, sizeof(mib) / sizeof(int), 0, &l, 0, 0) < 0) {
return -1;
}
if (l > 0) {
buf = malloc(l);
if (sysctl(mib, sizeof(mib) / sizeof(int), buf, &l, 0, 0) < 0) {
return -1;
}
for (p = buf; p < buf + l; p += rt->rtm_msglen) {
rt = (struct rt_msghdr *)p;
sa = (struct sockaddr *)(rt + 1);
for (i = 0; i < RTAX_MAX; i++) {
if (rt->rtm_addrs & (1 << i)) {
sa_tab[i] = sa;
sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
} else {
sa_tab[i] = NULL;
}
}

if (((rt->rtm_addrs & (RTA_DST | RTA_GATEWAY)) ==
(RTA_DST | RTA_GATEWAY)) &&
sa_tab[RTAX_DST]->sa_family == AF_INET &&
sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {

if (((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
char ifName[128];
if_indextoname(rt->rtm_index, ifName);
if (strcmp(TypeEN, ifName) == 0) {
*addr =
((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
r = 0;
}
}
}
}
free(buf);
}
return r;
}
#endif
2 changes: 1 addition & 1 deletion packages/network_info_plus/network_info_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: network_info_plus
description: Flutter plugin for discovering information (e.g. WiFi details) of the network.
version: 2.1.4+1
version: 2.1.5
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/network_info_plus
Expand Down