Skip to content

Commit

Permalink
rewrite support
Browse files Browse the repository at this point in the history
  • Loading branch information
lidaobing committed May 25, 2024
1 parent f441bca commit d73ed50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
39 changes: 14 additions & 25 deletions src/iptux-core/internal/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,29 @@ using namespace std;

namespace iptux {

static void socket_enable(GSocket* sock, const char* optname, int opt) {
GError* error = NULL;
if (!g_socket_set_option(sock, SOL_SOCKET, opt, TRUE, &error)) {
LOG_WARN("g_socket_set_option for %d, %s failed: %s", g_socket_get_fd(sock),
optname, error->message);
g_error_free(error);
}
}

/**
* 让套接口支持广播.
* @param sock socket
*/
void socket_enable_broadcast(int sock) {
socklen_t len;
int optval;

optval = 1;
len = sizeof(optval);
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, len) != 0) {
LOG_WARN("setsockopt for SO_BROADCAST failed: %s", strerror(errno));
}
void socket_enable_broadcast(GSocket* sock) {
socket_enable(sock, "SO_BROADCAST", SO_BROADCAST);
}

/**
* 让套接口监听端口可重用.
* @param sock socket
*/
void socket_enable_reuse(int sock) {
socklen_t len;
int optval;

optval = 1;
len = sizeof(optval);
#ifndef __CYGWIN__
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, len) != 0) {
LOG_WARN("setsockopt for SO_REUSEPORT failed: %s", strerror(errno));
}
#else
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, len) != 0) {
LOG_WARN("setsockopt for SO_REUSEADDR failed: %s", strerror(errno));
}
#endif
void socket_enable_reuse(GSocket* sock) {
socket_enable(sock, "SO_REUSEPORT", SO_REUSEPORT);
}

/**
Expand All @@ -69,7 +58,7 @@ void socket_enable_reuse(int sock) {
* @note 链表数据不是指针而是实际的IP
*/
vector<string> get_sys_broadcast_addr(int sock) {
const uint8_t amount = 5; //支持5个IP地址
const uint8_t amount = 5; // 支持5个IP地址
uint8_t count, sum;
struct ifconf ifc;
struct ifreq* ifr;
Expand Down
7 changes: 4 additions & 3 deletions src/iptux-core/internal/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
#ifndef IPTUX_SUPPORT_H
#define IPTUX_SUPPORT_H

#include <gio/gio.h>
#include <string>
#include <vector>

namespace iptux {

void socket_enable_broadcast(int sock);
void socket_enable_reuse(int sock);
std::vector<std::string> get_sys_broadcast_addr(int sock);
void socket_enable_broadcast(GSocket* sock);
void socket_enable_reuse(GSocket* sock);
std::vector<std::string> get_sys_broadcast_addr(GSocket* sock);

} // namespace iptux

Expand Down

0 comments on commit d73ed50

Please sign in to comment.