osmo_cbsp_recv_buffered() is now part of libosmocore

Change-Id: I93995e6c611701ca8688de8904adf8ea57292e69
This commit is contained in:
Harald Welte 2019-06-15 17:36:42 +02:00
parent 0560768ad2
commit 5a17cf671b
1 changed files with 0 additions and 105 deletions

View File

@ -42,111 +42,6 @@ struct osmo_cbsp_bsc {
#endif
/*! Read one CBSP 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 CBSP message.
* \param[inout] tmp_msg internally allocated msgb caching data for not yet fully received message.
*
* Function is designed just like ipa_msg_recv_buffered()
*/
int osmo_cbsp_recv_buffered(int fd, struct msgb **rmsg, struct msgb **tmp_msg)
{
struct msgb *msg = tmp_msg ? *tmp_msg : NULL;
struct cbsp_header *h;
int len, rc;
int needed;
if (!msg) {
msg = osmo_cbsp_msgb_alloc(__func__);
if (!msg) {
return -ENOMEM;
goto discard_msg;
}
msg->l1h = msg->tail;
}
if (msg->l2h == NULL) {
/* first read the [missing part of the] header */
needed = sizeof(*h) - msg->len;
rc = recv(fd, msg->tail, needed, 0);
if (rc == 0)
goto discard_msg;
else if (rc < 0) {
if (errno == EAGAIN || errno == EINTR)
rc = 0;
else {
rc = -errno;
goto discard_msg;
}
}
msgb_put(msg, rc);
if (rc < needed) {
if (msg->len == 0) {
rc = -EAGAIN;
goto discard_msg;
}
if (!tmp_msg) {
rc = -EIO;
goto discard_msg;
}
*tmp_msg = msg;
return -EAGAIN;
}
msg->l2h = msg->tail;
}
h = (struct cbsp_header *) msg->data;
/* then read the length as specified in the header */
len = h->len[0] << 16 | h->len[1] << 8 | h->len[2];
needed = len - msgb_l2len(msg);
if (needed > 0) {
rc = recv(fd, msg->tail, needed, 0);
if (rc == 0)
goto discard_msg;
else if (rc < 0) {
if (errno == EAGAIN || errno == EINTR)
rc = 0;
else {
rc = -errno;
goto discard_msg;
}
}
msgb_put(msg, rc);
/* still not all of payload received? */
if (rc < needed) {
if (!tmp_msg) {
rc = -EIO;
goto discard_msg;
}
*tmp_msg = msg;
return -EAGAIN;
}
}
/* else: complete message received */
rc = msgb_l2len(msg);
if (rc == 0) {
/* drop empty message */
rc = -EAGAIN;
goto discard_msg;
}
if (tmp_msg)
*tmp_msg = NULL;
*rmsg = msg;
return rc;
discard_msg:
if (tmp_msg)
*tmp_msg = NULL;
msgb_free(msg);
return rc;
}
const char *cbsp_cbc_client_name(const struct osmo_cbsp_cbc_client *client)
{
struct osmo_fd *ofd = osmo_stream_srv_get_ofd(client->conn);