aboutsummaryrefslogtreecommitdiff
path: root/client_network.c
diff options
context:
space:
mode:
Diffstat (limited to 'client_network.c')
-rw-r--r--client_network.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/client_network.c b/client_network.c
index 1ae8fea..0050812 100644
--- a/client_network.c
+++ b/client_network.c
@@ -29,6 +29,7 @@
#include <gnutls/gnutls.h>
#include <netdb.h>
#include <arpa/inet.h>
+#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
@@ -535,6 +536,10 @@ int client_mode_handler(uint64_t argc, struct string *argv) {
int client_fd = -1;
int client_listen_fd;
+int client_listen_fd_ready = 0;
+struct sockaddr_in client_listen_fd_bind_addr = {
+ .sin_family = AF_INET,
+};
int initclientnetwork(void) {
client_network_commands.array = malloc(0);
@@ -555,14 +560,19 @@ int initclientnetwork(void) {
int one = 1;
setsockopt(client_listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
- struct sockaddr_in localhost = {
- .sin_family = AF_INET,
- .sin_port = htons(6667),
- };
- inet_pton(AF_INET, "127.0.0.1", &localhost.sin_addr); // this is indeed localhost for mine, and I have no intent to change this
- bind(client_listen_fd, (struct sockaddr*)&localhost, sizeof(localhost));
+ client_listen_fd_bind_addr.sin_port = htons(6667);
+
+ inet_pton(AF_INET, "127.0.0.1", &client_listen_fd_bind_addr.sin_addr); // this is indeed localhost for mine, and I have no intent to change this
- listen(client_listen_fd, 1);
+ if (bind(client_listen_fd, (struct sockaddr*)&client_listen_fd_bind_addr, sizeof(client_listen_fd_bind_addr)) == -1) {
+ if (errno != EADDRINUSE) {
+ close(client_listen_fd);
+ return 1;
+ }
+ } else {
+ listen(client_listen_fd, 1);
+ client_listen_fd_ready = 1;
+ }
return 0;
}