-
Notifications
You must be signed in to change notification settings - Fork 4
/
mpsock_interface.h
145 lines (116 loc) · 4.1 KB
/
mpsock_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Multipath Multi-source HTTP (mHTTP)
*
* Developers: Juhoon Kim ([email protected]), Karl Fischer
*
*/
#ifndef __MPSOCK_INTERFACE_H__
#define __MPSOCK_INTERFACE_H__
#include "libmpsocket.h"
typedef struct mpsock_interface
{
int if_id; // key, start from 0 and incremental identifier
char name[IFNAMSIZ]; // Interface name. E.g., "eth0", "wlan0"
//int in_use; // TRUE or FALSE
//struct mpsock_connection *used_by; // connection that uses this interface
int num_used;
UT_hash_handle hh;
} mpsock_interface;
// hashtable from libmpsocket.c
extern mpsock_interface *mpsock_interface_table;
/*
* get available interfaces through given socket and store them in hash_table
*/
void collect_interfaces(int sock);
/*
* insert given list of interfaces into hash_table
*/
void insert_interfaces(const char* intf_list);
/*
* reset the used flags of all interfaces
*/
void reset_interfaces();
/*
* get an interface for the given connection
*/
//mpsock_interface *get_interface(struct mpsock_connection *connection);
/*
mpsock_interface *intfs = NULL;
void collect_interfaces(int sock) {
LOG_DEBUG("collect_interfaces(%d)\n",sock);
struct ifconf ifconf;
struct ifreq ifreq[MAXINTERFACES];
int interfaces;
int i;
if (sock < 0) {
perror("Socket error");
exit(1);
}
// Point ifconf's ifc_buf to our array of interface ifreqs.
ifconf.ifc_buf = (char *) ifreq;
// Set ifconf's ifc_len to the length of our array of interface ifreqs.
ifconf.ifc_len = sizeof ifreq;
// Populate ifconf.ifc_buf (ifreq) with a list of interface names and addresses.
if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
perror("ioctl error");
exit(1);
}
// Divide the length of the interface list by the size of each entry.
// This gives us the number of interfaces on the system.
interfaces = ifconf.ifc_len / sizeof(ifreq[0]);
for (i = 0 ; i < interfaces ; i++) {
char ip[INET_ADDRSTRLEN];
struct sockaddr_in *address = (struct sockaddr_in *) &ifreq[i].ifr_addr;
// Convert the binary IP address into a readable string.
if (!inet_ntop(AF_INET, &address->sin_addr, ip, sizeof(ip))) {
perror("inet_ntop error");
exit(1);
}
mpsock_interface *intf = (mpsock_interface*) malloc(sizeof(mpsock_interface));
memset(intf, 0, sizeof(mpsock_interface));
intf->if_id = i;
strcpy(intf->name, ifreq[i].ifr_name);
// need better way to filter out unuseable interfaces such as "virbr0", "lo", or "vmnet".
if (strstr(intf->name, "wlan") != NULL || strstr(intf->name, "eth") != NULL || strstr(intf->name, "lte") != NULL) {
intf->in_use = FALSE;
intf->used_by = -1;
HASH_ADD_INT(intfs, if_id, intf);
}
}
}
void insert_interfaces(const char* intf_list) {
LOG_DEBUG("%sinsert_interfaces(%s)",FUN_EVENT,intf_list);
int i = 0;
int len = strlen(intf_list);
char tmp[len];
memset(tmp, 0, len);
strcat(tmp, intf_list);
char* token = strtok(tmp, ",");
while (token) {
mpsock_interface *intf = (mpsock_interface*) malloc(sizeof(mpsock_interface));
memset(intf, 0, sizeof(mpsock_interface));
strcpy(intf->name, token);
intf->if_id = i++;
intf->in_use = FALSE;
intf->used_by = -1;
HASH_ADD_INT(intfs, if_id, intf);
LOG_DEBUG("%sinterface#%d: %s", RESULT_EVENT, intf->if_id, intf->name);
token = strtok(NULL, ",");
}
}
mpsock_interface *get_interface(int sock) {
mpsock_interface *e;
for (e = intfs ; e != NULL ; e = e->hh.next) {
if (e->in_use == FALSE) {
LOG_DEBUG("%swill use interface#%d: %s", COND_EVENT, e->if_id, e->name);
e->in_use = TRUE;
e->used_by = sock;
return e;
}
}
// In case that all interfaces are in use. Return the first interface again.
LOG_DEBUG("\n\n[FYI] all interfaces are in use. Thus, mHTTP establishes a new connection over %s, again.\n\n", intfs->name);
return intfs;
}
*/
#endif