Skip to content

Commit f8cdae3

Browse files
rany2PolynomialDivision
authored andcommitted
tcpsocket: add option to bind to specific ip
Fixes #218
1 parent be63ed4 commit f8cdae3

File tree

7 files changed

+13
-5
lines changed

7 files changed

+13
-5
lines changed

CONFIGURE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ grep 'CONFIG-N:' `find . -type f -name "*.[ch]"`|sed 's/^.*CONFIG-.: *\(.*\)$/|\
227227
|network_option|Method of networking between DAWN instances|0 = Broadcast; 2 = Multicast; [2 = TCP with UMDNS discovery]; 3 = TCP w/out UMDNS discovery|
228228
|server_ip|IP address when not using UMDNS|No default|
229229
|shared_key|Unused|N/A|
230+
|tcp_ip|IP address for TCP networking|[0.0.0.0]|
230231
|tcp_port|Port for TCP networking|[1026]|
231232
|use_symm_enc|Enable encryption of network traffic|[0 = Disabled]; 1 = Enabled|
232233

dawn-config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ config local
55
config network
66
option broadcast_ip '10.0.0.255'
77
option broadcast_port '1025'
8+
option tcp_ip '0.0.0.0'
89
option tcp_port '1026'
910
option network_option '2'
1011
option shared_key 'Niiiiiiiiiiiiick'

src/include/datastorage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct network_config_s {
124124
char broadcast_ip[MAX_IP_LENGTH + 1];
125125
int broadcast_port;
126126
char server_ip[MAX_IP_LENGTH + 1];
127+
char tcp_ip[MAX_IP_LENGTH + 1];
127128
int tcp_port;
128129
int network_option; // 0:Broadcast; 1:Multicast; 2:TCP+UMDNS; 3:TCP
129130
char shared_key[MAX_KEY_LENGTH + 1];

src/include/tcpsocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int add_tcp_connection(char *ipv4, int port);
3030
* @param port
3131
* @return
3232
*/
33-
int run_server(int port);
33+
int run_server(char *ipv4, int port);
3434

3535
/**
3636
* Send message via tcp to all other hosts.

src/network/tcpsocket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ static void server_cb(struct uloop_fd *fd, unsigned int events) {
247247
dawnlog_info("New connection\n");
248248
}
249249

250-
int run_server(int port) {
250+
int run_server(char *ipv4, int port) {
251251
dawnlog_debug("Adding socket!\n");
252252
char port_str[12];
253253
sprintf(port_str, "%d", port); // TODO: Manage buffer length
254254

255255
server.cb = server_cb;
256-
server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, INADDR_ANY, port_str);
256+
server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, ipv4, port_str);
257257
if (server.fd < 0) {
258258
dawnlog_perror("usock");
259259
return 1;

src/utils/dawn_uci.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ struct network_config_s uci_get_dawn_network() {
382382
.broadcast_ip = "",
383383
.broadcast_port = 1025,
384384
.server_ip = "",
385+
.tcp_ip = "0.0.0.0",
385386
.tcp_port = 1026,
386387
.network_option = 2,
387388
.shared_key = "Niiiiiiiiiiiiiik",
@@ -424,6 +425,10 @@ struct network_config_s uci_get_dawn_network() {
424425

425426
// CONFIG-N: network_option|Method of networking between DAWN instances|0 = Broadcast; 2 = Multicast; [2 = TCP with UMDNS discovery]; 3 = TCP w/out UMDNS discovery
426427
DAWN_SET_CONFIG_INT(ret, s, network_option);
428+
// CONFIG-N: tcp_ip|IP address for TCP networking|[0.0.0.0]
429+
const char* str_tcp_ip = uci_lookup_option_string(uci_ctx, s, "tcp_ip");
430+
if (str_tcp_ip)
431+
strncpy(ret.tcp_ip, str_tcp_ip, MAX_IP_LENGTH);
427432
// CONFIG-N: tcp_port|Port for TCP networking|[1025]
428433
DAWN_SET_CONFIG_INT(ret, s, tcp_port);
429434
// CONFIG-N: use_symm_enc|Enable encryption of network traffic|[0 = Disabled]; 1 = Enabled

src/utils/ubus.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir) {
802802
|| network_config.network_option == 3)
803803
{
804804
start_tcp_con_update();
805-
if(run_server(network_config.tcp_port))
805+
if(run_server(network_config.tcp_ip, network_config.tcp_port))
806806
uloop_timeout_set(&usock_timer, 1 * 1000);
807807
}
808808

@@ -1009,7 +1009,7 @@ void update_clients(struct uloop_timeout *t) {
10091009
void run_server_update(struct uloop_timeout *t) {
10101010
dawnlog_debug_func("Entering...");
10111011

1012-
if(run_server(network_config.tcp_port))
1012+
if(run_server(network_config.tcp_ip, network_config.tcp_port))
10131013
uloop_timeout_set(&usock_timer, 1 * 1000);
10141014
}
10151015

0 commit comments

Comments
 (0)