Skip to content

Commit

Permalink
refactor: remove wdctl.h and streamline socket connection handling in…
Browse files Browse the repository at this point in the history
… wd_connect function

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Nov 20, 2024
1 parent 8edefb4 commit 7cdca1d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 284 deletions.
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ target_link_libraries(wifidogx
${CURL_LIBRARIES}
)

target_link_libraries(wdctlx
event
)

# Installation
install(TARGETS wifidogx wdctlx
RUNTIME DESTINATION bin
Expand Down
45 changes: 1 addition & 44 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,50 +266,7 @@ is_valid_mac(const char *mac)
return (i == 12 && (s == 5 || s == 0));
}

// when sockfd is block, set timeout for connect
int
wd_connect(int sockfd, const struct sockaddr *their_addr, socklen_t addrlen, int timeout)
{
// Set non-blocking
long arg = fcntl(sockfd, F_GETFL, NULL);
arg |= O_NONBLOCK;
fcntl(sockfd, F_SETFL, arg);

int res = connect(sockfd, their_addr, addrlen);
if ((res == -1) && (errno != EINPROGRESS)) {
goto ERROR;
} else if (res == 0) {
goto SUCCESS;
} else {
int so_error = 0;
unsigned len = sizeof(so_error);

struct pollfd fds;
memset(&fds, 0, sizeof(fds));
fds.fd = sockfd;
fds.events = POLLOUT;
res = poll(&fds, 1, timeout*1000);
switch(res) {
case 1: // data to read
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
if (so_error == 0) {
goto SUCCESS;
}
break;
default:
break;
}
}

ERROR:
return -1;
SUCCESS:
// Set to blocking mode again...
arg = fcntl(sockfd, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(sockfd, F_SETFL, arg);
return 0;
}


#define BUF_MAX 1024

Expand Down
2 changes: 0 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ int is_valid_ip6(const char *);

int is_valid_mac(const char *);

int wd_connect(int, const struct sockaddr *, socklen_t, int);

float get_cpu_usage();

#endif /* _UTIL_H_ */
2 changes: 1 addition & 1 deletion src/wd_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2115,4 +2115,4 @@ __get_client_name(t_client *client)
pclose(f_dhcp);
debug(LOG_INFO, "__get_client_name [%s]", name);
}
}
}
Loading

0 comments on commit 7cdca1d

Please sign in to comment.