diff mbox

Move common socket routine into separate function

Message ID 1413204865-20297-1-git-send-email-max.suraev@fairwaves.co
State Accepted
Headers show

Commit Message

Max Oct. 13, 2014, 12:54 p.m. UTC
Signed-off-by: Max <max.suraev@fairwaves.co>
---
 src/socket.c | 57 +++++++++++++++++++++++++--------------------------------
 1 file changed, 25 insertions(+), 32 deletions(-)

Comments

Max Oct. 13, 2014, 1:02 p.m. UTC | #1
Just found copy-pasted code and could not resist aesthetic urge to make it a function :)
Holger Freyther Oct. 13, 2014, 7:09 p.m. UTC | #2
On Mon, Oct 13, 2014 at 02:54:25PM +0200, Max wrote:
> Signed-off-by: Max <max.suraev@fairwaves.co>

nice clean-up. Only issue. Please use tabs and not spaces. :)
Holger Freyther Oct. 13, 2014, 7:10 p.m. UTC | #3
On Mon, Oct 13, 2014 at 03:02:13PM +0200, ☎ wrote:
> Just found copy-pasted code and could not resist aesthetic urge to make it a function :)

code clones are more than an aesthetic issue. Thanks for
finding an easy candidate to share the code.
Max Oct. 14, 2014, 1:56 p.m. UTC | #4
Doh! I was sure I've set coding style autodetect in emacs already.

13.10.2014 21:09, Holger Hans Peter Freyther пишет:
> On Mon, Oct 13, 2014 at 02:54:25PM +0200, Max wrote:
>> Signed-off-by: Max <max.suraev@fairwaves.co>
> 
> nice clean-up. Only issue. Please use tabs and not spaces. :)
>
Sylvain Munaut Oct. 14, 2014, 2:16 p.m. UTC | #5
> Doh! I was sure I've set coding style autodetect in emacs already.

                                                      ^^^^^

Well, there's your problem !

Cheers,

   Sylvain
diff mbox

Patch

diff --git a/src/socket.c b/src/socket.c
index 844ebd3..04737be 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -150,6 +150,29 @@  int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
 	return sfd;
 }
 
+/*! \brief fill \ref osmo_fd for a give sfd
+ *  \param[out] ofd file descriptor (will be filled in)
+ *  \param[in] sfd socket file descriptor
+ *
+ * This function fills the \a ofd structure.
+ */
+static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd)
+{
+    if (sfd < 0)
+	return sfd;
+
+    ofd->fd = sfd;
+    ofd->when = BSC_FD_READ;
+
+    int rc = osmo_fd_register(ofd);
+    if (rc < 0) {
+	close(sfd);
+	return rc;
+    }
+
+    return sfd;
+}
+
 /*! \brief Initialize a socket and fill \ref osmo_fd
  *  \param[out] ofd file descriptor (will be filled in)
  *  \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
@@ -165,22 +188,7 @@  int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
 int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto,
 			const char *host, uint16_t port, unsigned int flags)
 {
-	int sfd, rc;
-
-	sfd = osmo_sock_init(family, type, proto, host, port, flags);
-	if (sfd < 0)
-		return sfd;
-
-	ofd->fd = sfd;
-	ofd->when = BSC_FD_READ;
-
-	rc = osmo_fd_register(ofd);
-	if (rc < 0) {
-		close(sfd);
-		return rc;
-	}
-
-	return sfd;
+	return osmo_fd_init_ofd(ofd, osmo_sock_init(family, type, proto, host, port, flags));
 }
 
 /*! \brief Initialize a socket and fill \ref sockaddr
@@ -362,22 +370,7 @@  err:
 int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto,
 			    const char *socket_path, unsigned int flags)
 {
-	int sfd, rc;
-
-	sfd = osmo_sock_unix_init(type, proto, socket_path, flags);
-	if (sfd < 0)
-		return sfd;
-
-	ofd->fd = sfd;
-	ofd->when = BSC_FD_READ;
-
-	rc = osmo_fd_register(ofd);
-	if (rc < 0) {
-		close(sfd);
-		return rc;
-	}
-
-	return sfd;
+	return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags));
 }
 
 #endif /* HAVE_SYS_SOCKET_H */