/pidgin/main: fc5300cb3617: Get rid of fair amount of copy-pasta...
Tomasz Wasilczyk
twasilczyk at pidgin.im
Mon May 12 16:37:15 EDT 2014
Changeset: fc5300cb3617f19fc2222c46ccbfaeb5986b1f2c
Author: Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date: 2014-05-12 22:37 +0200
Branch: release-2.x.y
URL: https://hg.pidgin.im/pidgin/main/rev/fc5300cb3617
Description:
Get rid of fair amount of copy-pasta and coverity warnings
diffstat:
libpurple/internal.h | 10 ++++++
libpurple/network.c | 35 +++++++++++++++++----
libpurple/protocols/bonjour/bonjour_ft.c | 10 +-----
libpurple/protocols/bonjour/jabber.c | 9 +-----
libpurple/protocols/irc/dcc_send.c | 9 +----
libpurple/protocols/jabber/si.c | 9 +----
libpurple/protocols/oscar/peer.c | 8 +----
libpurple/protocols/simple/simple.c | 10 +-----
libpurple/proxy.c | 50 +++----------------------------
9 files changed, 52 insertions(+), 98 deletions(-)
diffs (truncated from 347 to 300 lines):
diff --git a/libpurple/internal.h b/libpurple/internal.h
--- a/libpurple/internal.h
+++ b/libpurple/internal.h
@@ -265,4 +265,14 @@ void _purple_connection_new_unregister(P
*/
void _purple_connection_destroy(PurpleConnection *gc);
+/**
+ * Sets most commonly used socket flags: O_NONBLOCK and FD_CLOEXEC.
+ *
+ * @param fd The file descriptor for the socket.
+ *
+ * @return TRUE if succeeded, FALSE otherwise.
+ */
+gboolean
+_purple_network_set_common_socket_flags(int fd);
+
#endif /* _PURPLE_INTERNAL_H_ */
diff --git a/libpurple/network.c b/libpurple/network.c
--- a/libpurple/network.c
+++ b/libpurple/network.c
@@ -401,7 +401,6 @@ static PurpleNetworkListenData *
purple_network_do_listen(unsigned short port, int socket_family, int socket_type, PurpleNetworkListenCallback cb, gpointer cb_data)
{
int listenfd = -1;
- int flags;
const int on = 1;
PurpleNetworkListenData *listen_data;
unsigned short actual_port;
@@ -486,12 +485,7 @@ purple_network_do_listen(unsigned short
close(listenfd);
return NULL;
}
- flags = fcntl(listenfd, F_GETFL);
- fcntl(listenfd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(listenfd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("network", "couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(listenfd);
actual_port = purple_network_get_port_from_fd(listenfd);
purple_debug_info("network", "Listening on port: %hu\n", actual_port);
@@ -1135,6 +1129,33 @@ int purple_network_convert_idn_to_ascii(
#endif
}
+gboolean
+_purple_network_set_common_socket_flags(int fd)
+{
+ int flags;
+ gboolean succ = TRUE;
+
+ g_return_val_if_fail(fd >= 0, FALSE);
+
+ flags = fcntl(fd, F_GETFL);
+
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
+ purple_debug_warning("network",
+ "Couldn't set O_NONBLOCK flag\n");
+ succ = FALSE;
+ }
+
+#ifndef _WIN32
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
+ purple_debug_warning("network",
+ "Couldn't set FD_CLOEXEC flag\n");
+ succ = FALSE;
+ }
+#endif
+
+ return succ;
+}
+
void
purple_network_init(void)
{
diff --git a/libpurple/protocols/bonjour/bonjour_ft.c b/libpurple/protocols/bonjour/bonjour_ft.c
--- a/libpurple/protocols/bonjour/bonjour_ft.c
+++ b/libpurple/protocols/bonjour/bonjour_ft.c
@@ -808,17 +808,9 @@ bonjour_sock5_request_cb(gpointer data,
purple_xfer_cancel_remote(xfer);
return;
} else {
- int flags;
-
purple_debug_info("bonjour", "Accepted SOCKS5 ft connection - fd=%d\n", acceptfd);
- flags = fcntl(acceptfd, F_GETFL);
- fcntl(acceptfd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(acceptfd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("bonjour", "couldn't set FD_CLOEXEC\n");
-#endif
-
+ _purple_network_set_common_socket_flags(acceptfd);
purple_input_remove(xfer->watcher);
close(source);
xfer->watcher = purple_input_add(acceptfd, PURPLE_INPUT_READ,
diff --git a/libpurple/protocols/bonjour/jabber.c b/libpurple/protocols/bonjour/jabber.c
--- a/libpurple/protocols/bonjour/jabber.c
+++ b/libpurple/protocols/bonjour/jabber.c
@@ -637,7 +637,6 @@ static void
common_sockaddr_t their_addr; /* connector's address information */
socklen_t sin_size = sizeof(common_sockaddr_t);
int client_socket;
- int flags;
#ifdef HAVE_INET_NTOP
char addrstr[INET6_ADDRSTRLEN];
#endif
@@ -654,13 +653,7 @@ static void
if ((client_socket = accept(server_socket, &their_addr.sa, &sin_size)) == -1)
return;
-
- flags = fcntl(client_socket, F_GETFL);
- fcntl(client_socket, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(client_socket, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("bonjour", "jabber: couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(client_socket);
/* Look for the buddy that has opened the conversation and fill information */
#ifdef HAVE_INET_NTOP
diff --git a/libpurple/protocols/irc/dcc_send.c b/libpurple/protocols/irc/dcc_send.c
--- a/libpurple/protocols/irc/dcc_send.c
+++ b/libpurple/protocols/irc/dcc_send.c
@@ -245,7 +245,7 @@ static gssize irc_dccsend_send_write(con
static void irc_dccsend_send_connected(gpointer data, int source, PurpleInputCondition cond) {
PurpleXfer *xfer = (PurpleXfer *) data;
struct irc_xfer_send_data *xd = xfer->data;
- int conn, flags;
+ int conn;
conn = accept(xd->fd, NULL, 0);
if (conn == -1) {
@@ -262,12 +262,7 @@ static void irc_dccsend_send_connected(g
close(xd->fd);
xd->fd = -1;
- flags = fcntl(conn, F_GETFL);
- fcntl(conn, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(conn, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("irc", "couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(conn);
xd->inpa = purple_input_add(conn, PURPLE_INPUT_READ, irc_dccsend_send_read, xfer);
/* Start the transfer */
diff --git a/libpurple/protocols/jabber/si.c b/libpurple/protocols/jabber/si.c
--- a/libpurple/protocols/jabber/si.c
+++ b/libpurple/protocols/jabber/si.c
@@ -681,7 +681,7 @@ jabber_si_xfer_bytestreams_send_connecte
{
PurpleXfer *xfer = data;
JabberSIXfer *jsx = xfer->data;
- int acceptfd, flags;
+ int acceptfd;
purple_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_connected_cb\n");
@@ -698,12 +698,7 @@ jabber_si_xfer_bytestreams_send_connecte
close(source);
jsx->local_streamhost_fd = -1;
- flags = fcntl(acceptfd, F_GETFL);
- fcntl(acceptfd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(acceptfd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("jabber", "si: couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(acceptfd);
xfer->watcher = purple_input_add(acceptfd, PURPLE_INPUT_READ,
jabber_si_xfer_bytestreams_send_read_cb, xfer);
diff --git a/libpurple/protocols/oscar/peer.c b/libpurple/protocols/oscar/peer.c
--- a/libpurple/protocols/oscar/peer.c
+++ b/libpurple/protocols/oscar/peer.c
@@ -605,7 +605,6 @@ peer_connection_listen_cb(gpointer data,
PeerConnection *conn;
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
- int flags;
conn = data;
@@ -630,12 +629,7 @@ peer_connection_listen_cb(gpointer data,
return;
}
- flags = fcntl(conn->fd, F_GETFL);
- fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(conn->fd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("oscar", "peer: couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(conn->fd);
purple_input_remove(conn->watcher_incoming);
diff --git a/libpurple/protocols/simple/simple.c b/libpurple/protocols/simple/simple.c
--- a/libpurple/protocols/simple/simple.c
+++ b/libpurple/protocols/simple/simple.c
@@ -1721,16 +1721,10 @@ static void simple_newconn_cb(gpointer d
PurpleConnection *gc = data;
struct simple_account_data *sip = gc->proto_data;
struct sip_connection *conn;
- int newfd, flags;
+ int newfd;
newfd = accept(source, NULL, NULL);
-
- flags = fcntl(newfd, F_GETFL);
- fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(newfd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("simple", "couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(newfd);
conn = connection_create(sip, newfd);
diff --git a/libpurple/proxy.c b/libpurple/proxy.c
--- a/libpurple/proxy.c
+++ b/libpurple/proxy.c
@@ -742,8 +742,6 @@ clean_connect(gpointer data)
static void
proxy_connect_udp_none(PurpleProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
{
- int flags;
-
purple_debug_info("proxy", "UDP Connecting to %s:%d with no proxy\n",
connect_data->host, connect_data->port);
@@ -754,13 +752,7 @@ proxy_connect_udp_none(PurpleProxyConnec
_("Unable to create socket: %s"), g_strerror(errno));
return;
}
-
- flags = fcntl(connect_data->fd, F_GETFL);
- fcntl(connect_data->fd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("proxy", "couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(connect_data->fd);
if (connect(connect_data->fd, addr, addrlen) != 0)
{
@@ -805,8 +797,6 @@ proxy_connect_udp_none(PurpleProxyConnec
static void
proxy_connect_none(PurpleProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
{
- int flags;
-
purple_debug_info("proxy", "Connecting to %s:%d with no proxy\n",
connect_data->host, connect_data->port);
@@ -817,13 +807,7 @@ proxy_connect_none(PurpleProxyConnectDat
_("Unable to create socket: %s"), g_strerror(errno));
return;
}
-
- flags = fcntl(connect_data->fd, F_GETFL);
- fcntl(connect_data->fd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("proxy", "couldn't set FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(connect_data->fd);
if (connect(connect_data->fd, addr, addrlen) != 0)
{
@@ -1257,8 +1241,6 @@ http_canwrite(gpointer data, gint source
static void
proxy_connect_http(PurpleProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
{
- int flags;
-
purple_debug_info("proxy",
"Connecting to %s:%d via %s:%d using HTTP\n",
connect_data->host, connect_data->port,
@@ -1272,13 +1254,7 @@ proxy_connect_http(PurpleProxyConnectDat
_("Unable to create socket: %s"), g_strerror(errno));
return;
}
-
- flags = fcntl(connect_data->fd, F_GETFL);
- fcntl(connect_data->fd, F_SETFL, flags | O_NONBLOCK);
-#ifndef _WIN32
- if (fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC) != 0)
- purple_debug_warning("proxy", "couldn't FD_CLOEXEC\n");
-#endif
+ _purple_network_set_common_socket_flags(connect_data->fd);
if (connect(connect_data->fd, addr, addrlen) != 0) {
if (errno == EINPROGRESS || errno == EINTR) {
@@ -1452,8 +1428,6 @@ s4_canwrite(gpointer data, gint source,
More information about the Commits
mailing list