INM_LOGE("");
struct sockaddr_nl addr;
int fd;
+ int ret = 0;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
return -1;
}
- channel = g_io_channel_unix_new(fd);
- g_io_channel_set_close_on_unref(channel, TRUE);
-
- g_io_channel_set_encoding(channel, NULL, NULL);
- g_io_channel_set_buffered(channel, FALSE);
+ ret = inm_util_create_gio_channel(fd, &channel);
+ if (ret != 0) {
+ INM_LOGI("Failed to create gio channel");
+ close(fd);
+ return -1;
+ }
channel_watch = g_io_add_watch(channel,
G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
channel_watch = 0;
}
- g_io_channel_shutdown(channel, TRUE, NULL);
- g_io_channel_unref(channel);
-
+ inm_util_destroy_gio_channel(channel, TRUE);
channel = NULL;
}
return 0;
}
-static inline GIOChannel *__set_socket_channel(gint sock)
-{
- GIOChannel *sock_io_channel;
-
- sock_io_channel = g_io_channel_unix_new(sock);
- if (!sock_io_channel) {
- INM_LOGE("Failed to create channel");
- return NULL;
- }
-
- g_io_channel_set_close_on_unref(sock_io_channel, TRUE);
-
- if (G_IO_STATUS_NORMAL !=
- g_io_channel_set_encoding(sock_io_channel,
- NULL, NULL))
- INM_LOGI("Failed to set encoding NULL on io channel");
-
- if (G_IO_STATUS_NORMAL !=
- g_io_channel_set_flags(sock_io_channel,
- G_IO_FLAG_NONBLOCK, NULL))
- INM_LOGI("Failed to set flags on io channel");
-
- return sock_io_channel;
-}
-
-static inline void __clear_socket_io_channel(GIOChannel *sock_io_channel)
-{
- GError* error = NULL;
-
- if (!sock_io_channel)
- return;
-
- if (G_IO_STATUS_NORMAL !=
- g_io_channel_shutdown(sock_io_channel,
- FALSE,
- &error)) {
- INM_LOGI("Failure received while shutdown io channel[%d]:[%s]",
- error->code, error->message);
- g_error_free(error);
- }
-
- g_io_channel_unref(sock_io_channel);
-}
-
-
static inline gboolean __check_is_udp_proto(struct addrinfo *p_addr)
{
return p_addr->ai_family == AF_INET &&
static inline int __create_sock_data()
{
inm_trace_route_data_s *p_trace_route = NULL;
+ int ret;
__INM_FUNC_ENTER__;
return -1;
}
- p_trace_route->sock_io_channel = __set_socket_channel(p_trace_route->sock);
- RET_MINUS_WITH_MSG_IF_NULL(p_trace_route->sock_io_channel, "Failed to create channel");
+ ret = inm_util_create_gio_channel(p_trace_route->sock, &p_trace_route->sock_io_channel);
+ if (ret != 0) {
+ INM_LOGI("Failed to create gio channel");
+ close(p_trace_route->sock);
+ p_trace_route->sock = -1;
+ __INM_FUNC_EXIT__;
+ return -1;
+ }
p_trace_route->sock_source_id =
g_io_add_watch(p_trace_route->sock_io_channel,
static inline void __clear_trace_route_data(inm_trace_route_data_s *p_trace_route)
{
REMOVE_G_SOURCE(p_trace_route->tr_timer_source_id);
- __clear_socket_io_channel(p_trace_route->sock_io_channel);
+ inm_util_destroy_gio_channel(p_trace_route->sock_io_channel, FALSE);
g_free(p_trace_route->target);
g_free(p_trace_route->packet);
g_free(p_trace_route->received);
static void __destroy_trace_route_data()
{
- inm_trace_route_data_s *p_trace_route = NULL;
-
__INM_FUNC_ENTER__;
if (!g_p_tr_data) {
__clear_trace_route_data(g_p_tr_data);
g_p_tr_data = NULL;
+
__INM_FUNC_EXIT__;
}
return;
}
-static inline GIOChannel *__set_socket_channel(gint sock)
+int inm_util_create_gio_channel(int sock_fd, GIOChannel **channel)
{
- GIOChannel *sock_io_channel;
+ GIOChannel *p_channel;
+ GIOFlags flags;
- sock_io_channel = g_io_channel_unix_new(sock);
- if (!sock_io_channel) {
- INM_LOGE("Failed to create channel");
- return NULL;
- }
+ if (sock_fd == 0 || !channel)
+ return -1;
- g_io_channel_set_close_on_unref(sock_io_channel, TRUE);
+ p_channel = g_io_channel_unix_new(sock_fd);
+ if (!p_channel)
+ return -1;
+ flags = g_io_channel_get_flags(p_channel);
if (G_IO_STATUS_NORMAL !=
- g_io_channel_set_encoding(sock_io_channel,
- NULL, NULL))
- INM_LOGI("Failed to set encoding NULL on io channel");
+ g_io_channel_set_flags(p_channel,
+ flags | G_IO_FLAG_NONBLOCK, NULL))
+ INM_LOGI("Failed to set flags on io channel");
if (G_IO_STATUS_NORMAL !=
- g_io_channel_set_flags(sock_io_channel,
- G_IO_FLAG_NONBLOCK, NULL))
- INM_LOGI("Failed to set flags on io channel");
+ g_io_channel_set_encoding(p_channel,
+ NULL, NULL))
+ INM_LOGI("Failed to set encoding NULL on io channel");
- return sock_io_channel;
+ *channel = p_channel;
+ return 0;
}
-static inline void __clear_socket_io_channel(GIOChannel *sock_io_channel)
+void inm_util_destroy_gio_channel(GIOChannel *gio_channel, gboolean flush)
{
GError* error = NULL;
+ if (!gio_channel)
+ return;
+
if (G_IO_STATUS_NORMAL !=
- g_io_channel_shutdown(sock_io_channel,
- FALSE,
+ g_io_channel_shutdown(gio_channel,
+ flush,
&error)) {
INM_LOGI("Failure received while shutdown io channel[%d]:[%s]",
error->code, error->message);
g_error_free(error);
}
- g_io_channel_unref(sock_io_channel);
+ g_io_channel_unref(gio_channel);
}
void util_create_arp_sock(inm_util_arp_data_s *arp_data)
{
__INM_FUNC_ENTER__;
gint sock = -1;
+ int ret;
if (!arp_data) {
INM_LOGW("NULL data");
sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
RET_IF_SOCK_FAILURE(sock);
- arp_data->sock = sock;
- arp_data->sock_io_channel = __set_socket_channel(sock);
- if (!arp_data->sock_io_channel) {
- INM_LOGE("Failed to create channel");
- arp_data->sock = -1;
+ ret = inm_util_create_gio_channel(sock, &arp_data->sock_io_channel);
+ if (ret != 0) {
+ INM_LOGI("Failed to create gio channel");
close(sock);
__INM_FUNC_EXIT__;
return;
}
+ arp_data->sock = sock;
arp_data->sock_source_id =
g_io_add_watch(arp_data->sock_io_channel, G_IO_IN,
__check_arp_receive, arp_data);
RET_IF_SOCK_FAILURE(arp_data->sock);
- __clear_socket_io_channel(arp_data->sock_io_channel);
+ inm_util_destroy_gio_channel(arp_data->sock_io_channel, FALSE);
REMOVE_G_SOURCE(arp_data->sock_source_id);
close(arp_data->sock);
arp_data->sock = -1;
{
__INM_FUNC_ENTER__;
gint sock = -1;
+ int ret;
__init_icmp_sock_data(inm_util_icmp_data);
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
RET_IF_SOCK_FAILURE(sock);
- inm_util_icmp_data->sock = sock;
- inm_util_icmp_data->sock_io_channel = __set_socket_channel(sock);
- if (!inm_util_icmp_data->sock_io_channel) {
- INM_LOGE("Failed to create channel");
- inm_util_icmp_data->sock = -1;
+ ret = inm_util_create_gio_channel(sock, &inm_util_icmp_data->sock_io_channel);
+ if (ret != 0) {
+ INM_LOGI("Failed to create gio channel");
close(sock);
__INM_FUNC_EXIT__;
return;
}
+ inm_util_icmp_data->sock = sock;
inm_util_icmp_data->sock_source_id =
g_io_add_watch(inm_util_icmp_data->sock_io_channel, G_IO_IN,
__check_icmp_receive, inm_util_icmp_data);
RET_IF_SOCK_FAILURE(inm_util_icmp_data->sock);
- __clear_socket_io_channel(inm_util_icmp_data->sock_io_channel);
+ inm_util_destroy_gio_channel(inm_util_icmp_data->sock_io_channel, FALSE);
REMOVE_G_SOURCE(inm_util_icmp_data->sock_source_id);
close(inm_util_icmp_data->sock);
inm_util_icmp_data->sock = -1;
}
fd = nl_cache_mngr_get_fd(p_nl_cm);
- nl_data->sock_io_channel = __set_socket_channel(fd);
- if (!nl_data->sock_io_channel) {
- INM_LOGE("Failed to create channel");
+ res = inm_util_create_gio_channel(fd, &nl_data->sock_io_channel);
+ if (res != 0) {
+ INM_LOGI("Failed to create gio channel");
nl_cache_mngr_free(p_nl_cm);
nl_socket_free(p_nl_sock);
__INM_FUNC_EXIT__;
return;
}
- __clear_socket_io_channel(nl_data->sock_io_channel);
+ inm_util_destroy_gio_channel(nl_data->sock_io_channel, FALSE);
REMOVE_G_SOURCE(nl_data->sock_source_id);
nl_data->sock_io_channel = NULL;