Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Mar 5, 2015
1 parent c760f11 commit b875797
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
}
struct sockaddr_storage storage;
memset(&storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, &storage) != -1) {
if (get_sockaddr(host, port, &storage, 0) != -1) {
remote = connect_to_remote(server->listener, (struct sockaddr *)&storage);
remote->direct = 1;
}
Expand Down Expand Up @@ -1075,7 +1075,7 @@ int main(int argc, char **argv)
remote_addr[i].port;
struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage) == -1) {
if (get_sockaddr(host, port, storage, 1) == -1) {
FATAL("failed to resolve the provided hostname");
}
listen_ctx.remote_addr[i] = (struct sockaddr *)storage;
Expand Down Expand Up @@ -1205,7 +1205,7 @@ int start_ss_local_server(profile_t profile)
listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *));
struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(remote_host, remote_port_str, storage) == -1) {
if (get_sockaddr(remote_host, remote_port_str, storage, 1) == -1) {
return -1;
}
listen_ctx.remote_addr[0] = (struct sockaddr *)storage;
Expand Down
18 changes: 16 additions & 2 deletions src/netutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* <https://www.gnu.org/licenses/>.
*/

#include <math.h>
#include <unistd.h>

#include <libcork/core.h>
#include <udns.h>

Expand Down Expand Up @@ -48,7 +51,7 @@ int get_sockaddr_len(struct sockaddr *addr)
return 0;
}

int get_sockaddr(char *host, char *port, struct sockaddr_storage *storage)
int get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, int block)
{
struct cork_ip ip;
if (cork_ip_init(&ip, host) != -1) {
Expand Down Expand Up @@ -76,7 +79,18 @@ int get_sockaddr(char *host, char *port, struct sockaddr_storage *storage)
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */

int err = getaddrinfo(host, port, &hints, &result);
int err;

for (int i = 1; i < 8; i++) {
err = getaddrinfo(host, port, &hints, &result);
if (!block || !err) {
break;
} else {
sleep(pow(2, i));
LOGE("failed to resolve server name, wait %.0f seconds", pow(2, i));
}
}

if (err != 0) {
LOGE("getaddrinfo: %s", gai_strerror(err));
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/netutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
*/

int get_sockaddr_len(struct sockaddr *addr);
int get_sockaddr(char *host, char *port, struct sockaddr_storage *storage);
int get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, int block);
2 changes: 1 addition & 1 deletion src/redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ int main(int argc, char **argv)
remote_addr[i].port;
struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage) == -1) {
if (get_sockaddr(host, port, storage, 1) == -1) {
FATAL("failed to resolve the provided hostname");
}
listen_ctx.remote_addr[i] = (struct sockaddr *)storage;
Expand Down
23 changes: 12 additions & 11 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ static void free_connections(struct ev_loop *loop)
}
}

static void report_addr(int fd) {
static void report_addr(int fd)
{
struct sockaddr_storage addr;
socklen_t len = sizeof addr;
memset(&addr, 0, len);
int err = getpeername(fd, (struct sockaddr *)&addr, &len);
if (err == 0) {
char peer_name[INET6_ADDRSTRLEN] = {0};
char peer_name[INET6_ADDRSTRLEN] = { 0 };
if (addr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
dns_ntop(AF_INET, &s->sin_addr, peer_name, INET_ADDRSTRLEN);
Expand Down Expand Up @@ -172,19 +173,19 @@ int create_and_bind(const char *host, const char *port)
int s, listen_sock;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_TCP;

for (int i = 1; i < 8; i++) {
s = getaddrinfo(host, port, &hints, &result);
if (s == 0) break;
else {
if (s == 0) {
break;
} else {
sleep(pow(2, i));
LOGE("failed to resolve server name, wait %.0f seconds", pow(2, i));
}

}

if (s != 0) {
Expand Down Expand Up @@ -677,7 +678,7 @@ static void server_resolve_cb(struct sockaddr *addr, void *data)
}

if (acl) {
char host[INET6_ADDRSTRLEN] = {0};
char host[INET6_ADDRSTRLEN] = { 0 };
if (addr->sa_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)addr;
dns_ntop(AF_INET, &s->sin_addr, host, INET_ADDRSTRLEN);
Expand Down Expand Up @@ -1085,9 +1086,9 @@ int main(int argc, char **argv)
int option_index = 0;
static struct option long_options[] =
{
{ "fast-open", no_argument, 0, 0 },
{ "fast-open", no_argument, 0, 0 },
{ "acl", required_argument, 0, 0 },
{ 0, 0, 0, 0 }
{ 0, 0, 0, 0 }
};

opterr = 0;
Expand Down Expand Up @@ -1297,7 +1298,7 @@ int main(int argc, char **argv)
// Setup UDP
if (udprelay) {
init_udprelay(server_host[index], server_port, m, atoi(timeout),
iface);
iface);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ int main(int argc, char **argv)
remote_addr[i].port;
struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage) == -1) {
if (get_sockaddr(host, port, storage, 1) == -1) {
FATAL("failed to resolve the provided hostname");
}
listen_ctx.remote_addr[i] = (struct sockaddr *)storage;
Expand Down
4 changes: 2 additions & 2 deletions src/udprelay.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ int create_server_socket(const char *host, const char *port)
int s, server_sock;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_UDP;

Expand Down

0 comments on commit b875797

Please sign in to comment.