Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giho Jang committed Apr 22, 2022
1 parent 2604947 commit 054ad9b
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions oshw/rtnet/oshw.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,36 @@

#define MAX_RT_DEVICES 8

struct rt_if_nameindex
{
unsigned int if_index; /* 1, 2, ... */
char if_name[IF_NAMESIZE]; /* null terminated name: "eth0", ... */
};

/* Return a list of all interfaces and their indices. */
__maybe_unused static struct if_nameindex* rt_if_nameindex()
__maybe_unused static void rt_if_nameindex(struct rt_if_nameindex* nameindex)
{
FILE *fh;
char buf[512];
char dummy[1000];
struct if_nameindex* nameindex = malloc(sizeof(if_nameindex) * 8);
memset(nameindex, 0, sizeof(if_nameindex) * 8);

int index;
char buf[LINE_MAX];
char name[EC_MAXLEN_ADAPTERNAME];
char dummy[LINE_MAX];
fh = fopen("/proc/rtnet/devices", "r");
if (fh) {
// eat headers.
fgets(buf, sizeof buf, fh);

while (fgets(buf, sizeof buf, fh))
{
int index;
char* name = malloc(EC_MAXLEN_ADAPTERNAME);

__maybe_unused int ret = sscanf(buf, "%d %s %s", &index, name, dummy);
index = 0;
sscanf(buf, "%d %s %s", &index, name, dummy);
nameindex[index -1].if_index = index;
nameindex[index -1].if_name = name;
strncpy(nameindex[index -1].if_name, name, IF_NAMESIZE);
}

fclose(fh);
}

return nameindex;
}

/* Free the data returned from if_nameindex. */
__maybe_unused static void rt_if_freenameindex(struct if_nameindex* ids)
{
for(int i = 0; ids[i].if_index != 0; i++)
{
free(ids[i].if_name);
}
free(ids);
ids = NULL;
}

/**
* Host to Network byte order (i.e. to big endian).
Expand Down Expand Up @@ -85,7 +75,7 @@ uint16 oshw_ntohs(uint16 network)
ec_adaptert * oshw_find_adapters(void)
{
int i;
struct if_nameindex *ids;
struct rt_if_nameindex ids[MAX_RT_DEVICES];
ec_adaptert * adapter;
ec_adaptert * prev_adapter;
ec_adaptert * ret_adapter = NULL;
Expand All @@ -95,7 +85,7 @@ ec_adaptert * oshw_find_adapters(void)
* description.
*/

ids = rt_if_nameindex ();
rt_if_nameindex (ids);
for(i = 0; ids[i].if_index != 0; i++)
{
adapter = (ec_adaptert *)malloc(sizeof(ec_adaptert));
Expand Down Expand Up @@ -131,8 +121,6 @@ ec_adaptert * oshw_find_adapters(void)
prev_adapter = adapter;
}

rt_if_freenameindex (ids);

return ret_adapter;
}

Expand Down

0 comments on commit 054ad9b

Please sign in to comment.