Skip to content

Commit

Permalink
force server's ip address for outgoing connection when not bound to 0…
Browse files Browse the repository at this point in the history
….0.0.0

if the user specifies a specific ip address for the server to listen on,
then it is assumed he wants to use this ip for outgoing connections.

closes #5
  • Loading branch information
rofl0r committed Sep 12, 2017
1 parent 24eba0a commit 391dcd7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "server.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int resolve(const char *host, unsigned short port, struct addrinfo** addr) {
Expand All @@ -13,6 +14,12 @@ int resolve(const char *host, unsigned short port, struct addrinfo** addr) {
return getaddrinfo(host, port_buf, &hints, addr);
}

int server_bindtoip(const struct server *server, int fd) {
if(server->bindaddr.v4.sin_family != AF_UNSPEC)
return bind(fd, (struct sockaddr*) &server->bindaddr, server->bindaddrsz);
return 0;
}

int server_waitclient(struct server *server, struct client* client) {
socklen_t clen = sizeof client->addr;
return ((client->fd = accept(server->fd, (void*)&client->addr, &clen)) == -1)*-1;
Expand Down Expand Up @@ -42,5 +49,10 @@ int server_setup(struct server *server, const char* listenip, unsigned short por
return -3;
}
server->fd = listenfd;
if(strcmp(listenip, "0.0.0.0") && !resolve(listenip, 0, &ainfo)) {
server->bindaddrsz = ainfo->ai_addrlen;
memcpy(&server->bindaddr, ainfo->ai_addr, ainfo->ai_addrlen);
freeaddrinfo(ainfo);
} else server->bindaddr.v4.sin_family = AF_UNSPEC;
return 0;
}
3 changes: 3 additions & 0 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ struct client {
};

struct server {
union sockaddr_union bindaddr;
int fd;
socklen_t bindaddrsz;
};

int resolve(const char *host, unsigned short port, struct addrinfo** addr);
int server_bindtoip(const struct server *server, int fd);
int server_waitclient(struct server *server, struct client* client);
int server_setup(struct server *server, const char* listenip, unsigned short port);

Expand Down
4 changes: 4 additions & 0 deletions sockssrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static const char* auth_user;
static const char* auth_pass;
static sblist* auth_ips;
static pthread_mutex_t auth_ips_mutex = PTHREAD_MUTEX_INITIALIZER;
static const struct server* server;

enum socksstate {
SS_1_CONNECTED,
Expand Down Expand Up @@ -139,6 +140,8 @@ static int connect_socks_target(unsigned char *buf, size_t n, struct client *cli
return -EC_GENERAL_FAILURE;
}
}
if(server_bindtoip(server, fd) == -1)
goto eval_errno;
if(connect(fd, remote->ai_addr, remote->ai_addrlen) == -1)
goto eval_errno;

Expand Down Expand Up @@ -393,6 +396,7 @@ int main(int argc, char** argv) {
perror("server_setup");
return 1;
}
server = &s;
while(1) {
collect(threads);
struct client c;
Expand Down

0 comments on commit 391dcd7

Please sign in to comment.