Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
482 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* Apache 2.0 License | ||
Copyright (c) 2015 Andrey Chilikin https://github.com/achilikin | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#include <Arduino.h> | ||
|
||
#ifdef __ARDUINO_X86__ | ||
|
||
#include <stdio.h> | ||
|
||
#include "netif.h" | ||
|
||
int get_netif_info(const char *name, netif_t *netif) | ||
{ | ||
iostat_t *pstat; | ||
char *val, str[512]; | ||
int i, ifound = 0; | ||
|
||
if (name == NULL || netif == NULL) | ||
return -1; | ||
|
||
memset(netif, 0, sizeof(netif_t)); | ||
|
||
sprintf(str, "ifconfig %s", name); | ||
FILE *pf = popen(str, "r"); | ||
if (pf == NULL) | ||
return -1; | ||
|
||
while(fgets(str, 512, pf)) { | ||
if (strstr(str, name)) { | ||
strcpy(netif->name, name); | ||
ifound = 1; | ||
} | ||
|
||
if (!ifound) | ||
continue; | ||
|
||
if ((val = strstr(str, "HWaddr ")) != NULL) { | ||
val = strchr(val, ' ') + 1; | ||
for(i = 0; val[i] != '\0' && i < 17; i++) | ||
netif->hwas[i] = val[i]; | ||
netif->hwas[i] = 0; | ||
continue; | ||
} | ||
|
||
if ((val = strstr(str, " inet addr:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
while(*val == ' ') val++; | ||
for(i = 0; val[i] != ' '; i++) | ||
netif->ip4as[i] = val[i]; | ||
netif->ip4as[i] = 0; | ||
continue; | ||
} | ||
|
||
if ((val = strstr(str, " inet6 addr:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
while(*val == ' ') val++; | ||
for(i = 0; val[i] != ' ' && val[i] != '/'; i++) | ||
netif->ip6as[i] = val[i]; | ||
netif->ip6as[i] = 0; | ||
continue; | ||
} | ||
|
||
if (strstr(str, "RX ")) | ||
pstat = &netif->rx; | ||
|
||
if (strstr(str, "TX ")) | ||
pstat = &netif->tx; | ||
|
||
if ((val = strstr(str, " packets:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
pstat->packets = strtoul(val, &val, 10); | ||
} | ||
if ((val = strstr(str, " errors:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
pstat->errors = strtoul(val, &val, 10); | ||
} | ||
if ((val = strstr(str, " dropped:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
pstat->dropped = strtoul(val, &val, 10); | ||
} | ||
|
||
if ((val = strstr(str, "RX bytes:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
netif->rx.bytes = strtoul(val, &val, 10); | ||
} | ||
if ((val = strstr(str, "TX bytes:")) != NULL) { | ||
val = strchr(val, ':') + 1; | ||
netif->tx.bytes = strtoul(val, &val, 10); | ||
break; | ||
} | ||
} | ||
|
||
fclose(pf); | ||
return ifound ? 0 : -1; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Apache 2.0 License | ||
Copyright (c) 2015 Andrey Chilikin https://github.com/achilikin | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#ifndef __YAHL_NET_IF_H__ | ||
#define __YAHL_NET_IF_H__ | ||
|
||
#ifdef __ARDUINO_X86__ | ||
|
||
#include <stdint.h> | ||
#include <net/if.h> | ||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
|
||
/* | ||
Network interface information from 'ifconfig' command | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define HW_ADDRSTRLEN 18 | ||
|
||
// network interface statistics | ||
typedef struct iostat_s | ||
{ | ||
uint32_t packets; | ||
uint32_t errors; | ||
uint32_t dropped; | ||
uint32_t bytes; // increment to uint64_t if needed | ||
} iostat_t; | ||
|
||
// network interface information | ||
typedef struct netif_s | ||
{ | ||
char name[IFNAMSIZ]; | ||
char hwas[HW_ADDRSTRLEN]; // HW address string | ||
char ip4as[INET_ADDRSTRLEN]; // IPv4 address string | ||
char ip6as[INET6_ADDRSTRLEN]; // IPv4 address string | ||
iostat_t rx; | ||
iostat_t tx; | ||
} netif_t; | ||
|
||
// interface name to get information for | ||
int get_netif_info(const char *name, netif_t *netif); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.