transports: Resize socket send buffer if needed

This was originally added in openbmc/pldm to make the socket send buffer
big enough to send a message if the message is longer than the current
send buffer size.

When you call set_sock_opt with buffer size X, given that X is within a
certain range, the send buffer is actually set to 2*X. get_sock_opt
returns the 2*X value, not X. So add in some helper functions.

Change-Id: I8ded9357db4268cd264cf0ecfb80479223106c09
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
diff --git a/src/transport/mctp-demux.c b/src/transport/mctp-demux.c
index 466d33c..2518d93 100644
--- a/src/transport/mctp-demux.c
+++ b/src/transport/mctp-demux.c
@@ -4,9 +4,11 @@
 #include "container-of.h"
 #include "libpldm/pldm.h"
 #include "libpldm/transport.h"
+#include "socket.h"
 #include "transport.h"
 
 #include <errno.h>
+#include <limits.h>
 #include <poll.h>
 #include <stdlib.h>
 #include <string.h>
@@ -24,6 +26,7 @@
 	/* In the future this probably needs to move to a tid-eid-uuid/network
 	 * id mapping for multi mctp networks */
 	pldm_tid_t tid_eid_map[MCTP_MAX_NUM_EID];
+	struct pldm_socket_sndbuf socket_send_buf;
 };
 
 #define transport_to_demux(ptr)                                                \
@@ -181,6 +184,12 @@
 	msg.msg_iov = iov;
 	msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
 
+	if (req_msg_len > INT_MAX ||
+	    pldm_socket_sndbuf_accomodate(&(demux->socket_send_buf),
+					  (int)req_msg_len)) {
+		return PLDM_REQUESTER_SEND_FAIL;
+	}
+
 	ssize_t rc = sendmsg(demux->socket, &msg, 0);
 	if (rc == -1) {
 		return PLDM_REQUESTER_SEND_FAIL;
@@ -211,6 +220,13 @@
 		free(demux);
 		return -1;
 	}
+
+	if (pldm_socket_sndbuf_init(&demux->socket_send_buf, demux->socket)) {
+		close(demux->socket);
+		free(demux);
+		return -1;
+	}
+
 	*ctx = demux;
 	return 0;
 }
@@ -249,6 +265,13 @@
 		free(demux);
 		return NULL;
 	}
+
+	if (pldm_socket_sndbuf_init(&demux->socket_send_buf, demux->socket)) {
+		close(demux->socket);
+		free(demux);
+		return NULL;
+	}
+
 	return demux;
 }