while(true) {
if(read_packet(&packet)) {
+ mutex_lock(&mutex);
route(myself, &packet);
+ mutex_unlock(&mutex);
} else {
if(errno == EAGAIN || errno == EINTR) {
errno = 0;
if(c->tcplen > *len)
break;
+ mutex_lock(&mutex);
receive_tcppacket(c, reqbuf, c->tcplen);
+ mutex_unlock(&mutex);
memmove(reqbuf, reqbuf, *len - c->tcplen);
*len -= c->tcplen;
else
*end++ = 0;
- if(!receive_request(c, reqbuf))
+ mutex_lock(&mutex);
+ bool success = receive_request(c, reqbuf);
+ mutex_unlock(&mutex);
+
+ if(!success)
return false;
memmove(reqbuf, end, *len - (end - reqbuf));
getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
- if(!result)
+ if(!result) {
+ mutex_lock(&mutex);
finish_connecting(c);
- else {
+ mutex_unlock(&mutex);
+ } else {
ifdebug(CONNECTIONS) logger(LOG_DEBUG,
"Error while connecting to %s (%s): %s",
c->name, c->hostname, sockstrerror(result));
closesocket(c->socket);
+ mutex_lock(&mutex);
do_outgoing_connection(c);
+ mutex_unlock(&mutex);
return;
}
}
while(true) {
if (!receive_meta(c)) {
terminate_connection(c, c->status.active);
- return;
+ break;
}
}
}
#endif
while(true) {
- usleep(1000);
+ mutex_unlock(&mutex);
+ usleep(1000000);
+ mutex_lock(&mutex);
+
struct event *event;
while((event = get_expired_event())) {
event->handler(event->data);
#ifndef HAVE_MINGW
#define closesocket(s) close(s)
-#else
-extern CRITICAL_SECTION mutex;
#endif
+extern mutex_t mutex;
+
#endif /* __TINC_NET_H__ */
sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
+ mutex_lock(&mutex);
n = lookup_node_udp(&from);
if(!n) {
n = try_harder(&from, &pkt);
if(n)
update_node_udp(n, &from);
- else ifdebug(PROTOCOL) {
+ }
+
+ if(n) {
+ receive_udppacket(n, &pkt);
+ } else {
+ ifdebug(PROTOCOL) {
hostname = sockaddr2hostname(&from);
logger(LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
free(hostname);
- continue;
}
- else
- continue;
}
- receive_udppacket(n, &pkt);
+ mutex_unlock(&mutex);
}
}
configure_tcp(c);
+ mutex_lock(&mutex);
connection_add(c);
c->allow_request = ID;
logger(LOG_ERR, "create_thread() failed: %s", strerror(errno));
abort();
}
+ mutex_unlock(&mutex);
}
}
static inline void mutex_create(mutex_t *mutex) {
pthread_mutex_init(mutex, NULL);
}
+#if 1
+#define mutex_lock(m) logger(LOG_DEBUG, "mutex_lock() at " __FILE__ " line %d", __LINE__); pthread_mutex_lock(m)
+#define mutex_unlock(m) logger(LOG_DEBUG, "mutex_unlock() at " __FILE__ " line %d", __LINE__); pthread_mutex_unlock(m)
+#else
static inline void mutex_lock(mutex_t *mutex) {
pthread_mutex_lock(mutex);
}
pthread_mutex_unlock(mutex);
}
#endif
+#endif
#endif
{NULL, 0, NULL, 0}
};
+mutex_t mutex;
+
#ifdef HAVE_MINGW
static struct WSAData wsa_state;
CRITICAL_SECTION mutex;
g_argv = argv;
+ mutex_create(&mutex);
+ mutex_lock(&mutex);
init_events();
init_configuration(&config_tree);