Change in libosmocore[master]: IPA: move duplicated error handling into inline function

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Fri Nov 23 22:07:23 UTC 2018


Max has uploaded this change for review. ( https://gerrit.osmocom.org/11911


Change subject: IPA: move duplicated error handling into inline function
......................................................................

IPA: move duplicated error handling into inline function

The code is re-ordered to put error handling upfront similar to te way
we do it in oter places.

Change-Id: Ic0147bffaf04b0baf97e5cca22948bd0e116668f
---
M src/gsm/ipa.c
1 file changed, 32 insertions(+), 23 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/11/11911/1

diff --git a/src/gsm/ipa.c b/src/gsm/ipa.c
index d423c26..6ee003d 100644
--- a/src/gsm/ipa.c
+++ b/src/gsm/ipa.c
@@ -541,6 +541,33 @@
 	return rc;
 }
 
+/*! Attempt to recv() message and analyse return value
+ * \param[out] ret result of recv() will be stored in here.
+ * \param[in] fd The fd for the socket to read from.
+ * \param[in] msg Message buffer to read into.
+ * \param[in] needed Home many bytes we have to read.
+ * \returns boolean indicating whether message should be discarded.
+ */
+static inline bool recv_discard_msg(int *ret, int fd, struct msgb *msg, int needed)
+{
+	*ret = recv(fd, msg->tail, needed, 0);
+
+	if (*ret == 0) /* the socket is properly closed or we've requested to receive 0 bytes */
+		return true;
+
+	if (*ret > 0) /* some data was obtained */
+		return false;
+
+	if (errno == EAGAIN || errno == EINTR) {
+		*ret = 0;
+		return false;
+	}
+
+	/* errors indicated by -1 return value, let's save the actual error code */
+	*ret = -errno;
+	return true;
+}
+
 /*! Read one ipa message from socket fd or store part if still not fully received.
  *  \param[in] fd The fd for the socket to read from.
  *  \param[out] rmsg internally allocated msgb containing a fully received ipa message.
@@ -566,6 +593,7 @@
 	struct ipaccess_head *hh;
 	int len, ret;
 	int needed;
+	bool discard;
 
 	if (msg == NULL) {
 		msg = ipa_msg_alloc(0);
@@ -579,19 +607,10 @@
 	if (msg->l2h == NULL) {
 		/* first read our 3-byte header */
 		needed = sizeof(*hh) - msg->len;
-		ret = recv(fd, msg->tail, needed, 0);
-		if (ret == 0)
+		discard = recv_discard_msg(&ret, fd, msg, needed);
+		if (discard)
 		       goto discard_msg;
 
-		if (ret < 0) {
-			if (errno == EAGAIN || errno == EINTR)
-				ret = 0;
-			else {
-				ret = -errno;
-				goto discard_msg;
-			}
-		}
-
 		msgb_put(msg, ret);
 
 		if (ret < needed) {
@@ -629,20 +648,10 @@
 	needed = len - msgb_l2len(msg);
 
 	if (needed > 0) {
-		ret = recv(fd, msg->tail, needed, 0);
-
-		if (ret == 0)
+		discard = recv_discard_msg(&ret, fd, msg, needed);
+		if (discard)
 			goto discard_msg;
 
-		if (ret < 0) {
-			if (errno == EAGAIN || errno == EINTR)
-				ret = 0;
-			else {
-				ret = -errno;
-				goto discard_msg;
-			}
-		}
-
 		msgb_put(msg, ret);
 
 		if (ret < needed) {

-- 
To view, visit https://gerrit.osmocom.org/11911
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic0147bffaf04b0baf97e5cca22948bd0e116668f
Gerrit-Change-Number: 11911
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181123/513237f5/attachment.htm>


More information about the gerrit-log mailing list