Skip to content

Commit

Permalink
feat: 포트 번호는 인자로 받기
Browse files Browse the repository at this point in the history
  • Loading branch information
potados99 committed Oct 9, 2023
1 parent 8241975 commit 5b52e11
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
// it will use native socket.
#ifdef LOCAL_TEST
#define PROXY_SERVER_LISTENING_INTERFACE INTERFACE_NATIVE
#define PROXY_SERVER_LISTENING_INTERFACE_STR "INTERFACE_NATIVE"
#else
#define PROXY_SERVER_LISTENING_INTERFACE INTERFACE_LWIP
#define PROXY_SERVER_LISTENING_INTERFACE_STR "INTERFACE_LWIP"
#endif

// When you use INTERFACE_NATIVE,
Expand Down Expand Up @@ -71,8 +73,10 @@
// it will use native socket.
#ifdef LOCAL_TEST
#define PROXY_CLIENT_REMOTE_INTERFACE INTERFACE_NATIVE
#define PROXY_CLIENT_REMOTE_INTERFACE_STR "INTERFACE_NATIVE"
#else
#define PROXY_CLIENT_REMOTE_INTERFACE INTERFACE_LWIP
#define PROXY_CLIENT_REMOTE_INTERFACE_STR "INTERFACE_LWIP"
#endif

// Listening side settings.
Expand Down
26 changes: 24 additions & 2 deletions proxy_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const quiet_lwip_ipv4_addr gateway = PROXY_CLIENT_LWIP_GATEWAY_U32;
#endif

const char *listening_address = PROXY_CLIENT_LISTENING_ADDRESS;
const int listening_port = PROXY_CLIENT_LISTENING_PORT;
int listening_port = PROXY_CLIENT_LISTENING_PORT;

const char *remote_address = PROXY_CLIENT_REMOTE_ADDRESS;
const int remote_port = PROXY_CLIENT_REMOTE_PORT;
int remote_port = PROXY_CLIENT_REMOTE_PORT;

int open_send(const char *addr, int port) {
int socket_fd = lwip_socket(AF_INET, SOCK_STREAM, 0);
Expand Down Expand Up @@ -83,6 +83,10 @@ int recv_connection(int socket_fd, struct sockaddr_in *recv_from) {
}

int app_loop(crossbar *client_crossbar, crossbar *remote_crossbar) {
log_info("Listening on %s %s:%d, will forward connection to %s %s:%d",
"INTERFACE_NATIVE", listening_address, listening_port,
PROXY_CLIENT_REMOTE_INTERFACE_STR, remote_address, remote_port);

int recv_socket = open_recv(listening_address);
if (recv_socket < 0) {
log_message("app_loop() couldn't open socket for listening.");
Expand Down Expand Up @@ -113,10 +117,28 @@ int app_loop(crossbar *client_crossbar, crossbar *remote_crossbar) {
}
}

void handle_arguments(int argc, char **argv) {
if (argc > 1) {
int listening_portnum = atoi(argv[1]);
if (listening_portnum > 0 && listening_portnum < 65536) {
listening_port = listening_portnum;
}
}

if (argc > 2) {
int remote_portnum = atoi(argv[2]);
if (remote_portnum > 0 && remote_portnum < 65536) {
remote_port = remote_portnum;
}
}
}

int main(int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
log_output(stdout);

handle_arguments(argc, argv);

#if PROXY_CLIENT_REMOTE_INTERFACE == INTERFACE_LWIP
if (start_lwip(mac, ipaddr, netmask, gateway) < 0) {
return -1;
Expand Down
16 changes: 15 additions & 1 deletion proxy_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const quiet_lwip_ipv4_addr gateway = PROXY_SERVER_LWIP_GATEWAY_U32;
#endif

const char *listening_address = PROXY_SERVER_LISTENING_ADDRESS;
const int listening_port = PROXY_SERVER_LISTENING_PORT;
int listening_port = PROXY_SERVER_LISTENING_PORT;

int open_recv(const char *addr) {
int socket_fd = lwip_socket(AF_INET, SOCK_STREAM, 0);
Expand Down Expand Up @@ -70,6 +70,9 @@ int recv_connection(int socket_fd, struct lwip_sockaddr_in *recv_from) {
}

void app_loop(crossbar *client_crossbar, crossbar *remote_crossbar) {
log_info("Listening on %s %s:%d",
PROXY_SERVER_LISTENING_INTERFACE_STR, listening_address, listening_port);

int recv_socket = open_recv(listening_address);
if (recv_socket < 0) {
log_message("app_loop() couldn't open socket for listening.");
Expand Down Expand Up @@ -280,10 +283,21 @@ void app_loop(crossbar *client_crossbar, crossbar *remote_crossbar) {
}
}

void handle_arguments(int argc, char **argv) {
if (argc > 1) {
int portnum = atoi(argv[1]);
if (portnum > 0 && portnum < 65536) {
listening_port = portnum;
}
}
}

int main(int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
log_output(stdout);

handle_arguments(argc, argv);

#if PROXY_SERVER_LISTENING_INTERFACE == INTERFACE_LWIP
if (start_lwip(mac, ipaddr, netmask, gateway) < 0) {
return -1;
Expand Down
19 changes: 19 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ void log_output(FILE *file) {
log_file = file;
}

void log_info(const char *message, ...) {
char vbuffer[255];
va_list args;
va_start(args, message);
vsnprintf(vbuffer, ARRAY_SIZE(vbuffer), message, args);
va_end(args);

time_t now;
time(&now);
char *date = ctime(&now);
date[strlen(date) - 1] = '\0';

pthread_t self = pthread_self();

fprintf(log_file, "[%s][%lu] Info: %s\n", date, self, vbuffer);

fflush(log_file);
}

void log_message(const char *message, ...) {
char vbuffer[255];
va_list args;
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

void log_output(FILE *file);
void log_info(const char *message, ...);
void log_message(const char *message, ...);

#endif //QUIET_PRACTICE_UTIL_H

0 comments on commit 5b52e11

Please sign in to comment.