#include "connection.h"
#include "node.h"
-extern void retry_outgoing(outgoing_t *);
+extern void retry_outgoing(struct meshlink_handle *mesh, outgoing_t *);
extern void handle_incoming_vpn_data(struct event_loop_t *loop, void *, int);
-extern void finish_connecting(struct connection_t *);
-extern bool do_outgoing_connection(struct outgoing_t *);
+extern void finish_connecting(struct meshlink_handle *mesh, struct connection_t *);
+extern bool do_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
extern void handle_new_meta_connection(struct event_loop_t *loop, void *, int);
extern int setup_listen_socket(const sockaddr_t *);
-extern int setup_vpn_in_socket(const sockaddr_t *);
+extern int setup_vpn_in_socket(struct meshlink_handle *mesh, const sockaddr_t *);
extern bool send_sptps_data(void *handle, uint8_t type, const char *data, size_t len);
extern bool receive_sptps_record(void *handle, uint8_t type, const char *data, uint16_t len);
extern void send_packet(struct node_t *, struct vpn_packet_t *);
extern char *get_name(struct meshlink_handle *mesh);
extern bool setup_myself_reloadable(struct meshlink_handle *mesh);
extern bool setup_network(struct meshlink_handle *mesh);
-extern void setup_outgoing_connection(struct outgoing_t *);
-extern void try_outgoing_connections(void);
+extern void setup_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
+extern void try_outgoing_connections(struct meshlink_handle * mesh);
extern void close_network_connections(void);
extern int main_loop(void);
extern void terminate_connection(struct connection_t *, bool);
#endif
}
-static bool bind_to_address(connection_t *c) {
+static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
int s = -1;
for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) {
return nfd;
}
-int setup_vpn_in_socket(const sockaddr_t *sa) {
+int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
int nfd;
char *addrstr;
int option;
} /* int setup_vpn_in_socket */
static void retry_outgoing_handler(event_loop_t *loop, void *data) {
- setup_outgoing_connection(data);
+ meshlink_handle_t *mesh = loop->data;
+ outgoing_t *outgoing = data;
+ setup_outgoing_connection(mesh, outgoing);
}
-void retry_outgoing(outgoing_t *outgoing) {
+void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
outgoing->timeout += 5;
if(outgoing->timeout > mesh->maxtimeout)
logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
}
-void finish_connecting(connection_t *c) {
+void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
logger(DEBUG_CONNECTIONS, LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
c->last_ping_time = mesh->loop.now.tv_sec;
send_id(c);
}
-static void do_outgoing_pipe(connection_t *c, char *command) {
+static void do_outgoing_pipe(meshlink_handle_t *mesh, connection_t *c, char *command) {
#ifndef HAVE_MINGW
int fd[2];
#endif
}
-static void handle_meta_write(connection_t *c) {
+static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
if(c->outbuf.len <= c->outbuf.offset)
return;
}
static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
+ meshlink_handle_t *mesh = loop->data;
connection_t *c = data;
if(c->status.connecting) {
getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
if(!result)
- finish_connecting(c);
+ finish_connecting(mesh, c);
else {
logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(result));
terminate_connection(c, false);
}
if(flags & IO_WRITE)
- handle_meta_write(c);
+ handle_meta_write(mesh, c);
else
handle_meta_connection_data(c);
}
-bool do_outgoing_connection(outgoing_t *outgoing) {
+bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
char *address, *port, *space;
struct addrinfo *proxyai = NULL;
int result;
if(!outgoing->ai) {
if(!outgoing->cfg) {
logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->name);
- retry_outgoing(outgoing);
+ retry_outgoing(mesh, outgoing);
return false;
}
c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
configure_tcp(c);
} else if(mesh->proxytype == PROXY_EXEC) {
- do_outgoing_pipe(c, mesh->proxyhost);
+ do_outgoing_pipe(mesh, c, mesh->proxyhost);
} else {
proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
if(!proxyai) {
setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
#endif
- bind_to_address(c);
+ bind_to_address(mesh, c);
}
/* Connect */
return ai;
}
-void setup_outgoing_connection(outgoing_t *outgoing) {
+void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
timeout_del(&mesh->loop, &outgoing->ev);
node_t *n = lookup_node(outgoing->name);
}
}
- do_outgoing_connection(outgoing);
+ do_outgoing_connection(mesh, outgoing);
}
/*
new connection
*/
void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
+ meshlink_handle_t *mesh = loop->data;
listen_socket_t *l = data;
connection_t *c;
sockaddr_t sa;
send_id(c);
}
-static void free_outgoing(outgoing_t *outgoing) {
+static void free_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
timeout_del(&mesh->loop, &outgoing->ev);
if(outgoing->ai)
free(outgoing);
}
-void try_outgoing_connections(void) {
+void try_outgoing_connections(meshlink_handle_t *mesh) {
/* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
if(!mesh->outgoings) {
outgoing_t *outgoing = xzalloc(sizeof *outgoing);
outgoing->name = name;
list_insert_tail(mesh->outgoings, outgoing);
- setup_outgoing_connection(outgoing);
+ setup_outgoing_connection(mesh, outgoing);
}
}