transport: Make APIs work for all types of messages
These transport APIs were written with the idea of being used by a
requester. However they don't need to be, so remove these checks so we
can send and receive both requests and responses.
Put the checks into the old API calls so we don't break anyone.
Change-Id: I194e22341025a00388fa7e031d2c6db05976874f
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
diff --git a/src/requester/pldm.c b/src/requester/pldm.c
index 45179f4..89fd188 100644
--- a/src/requester/pldm.c
+++ b/src/requester/pldm.c
@@ -53,11 +53,10 @@
/* This macro does the setup and teardown required for the old API to use the
* new API. Since the setup/teardown logic is the same for all four send/recv
* functions, it makes sense to only define it once. */
-#define PLDM_REQ_FN(eid, fd, fn, ...) \
+#define PLDM_REQ_FN(eid, fd, fn, rc, ...) \
do { \
struct pldm_transport_mctp_demux *demux; \
bool using_open_transport = false; \
- pldm_requester_rc_t rc; \
pldm_tid_t tid = 1; \
struct pldm_transport *ctx; \
/* The fd can be for a socket we opened or one the consumer \
@@ -85,15 +84,32 @@
if (!using_open_transport) { \
pldm_transport_mctp_demux_destroy(demux); \
} \
- return rc; \
+ break; \
} while (0)
LIBPLDM_ABI_STABLE
pldm_requester_rc_t pldm_recv_any(mctp_eid_t eid, int mctp_fd,
uint8_t **pldm_resp_msg, size_t *resp_msg_len)
{
- PLDM_REQ_FN(eid, mctp_fd, pldm_transport_recv_msg,
+ pldm_requester_rc_t rc = 0;
+ PLDM_REQ_FN(eid, mctp_fd, pldm_transport_recv_msg, rc,
(void **)pldm_resp_msg, resp_msg_len);
+ struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg);
+ if (rc != PLDM_REQUESTER_SUCCESS) {
+ return rc;
+ }
+ if (hdr && (hdr->request || hdr->datagram)) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_NOT_RESP_MSG;
+ }
+ uint8_t pldm_cc = 0;
+ if (*resp_msg_len < (sizeof(struct pldm_msg_hdr) + sizeof(pldm_cc))) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_RESP_MSG_TOO_SMALL;
+ }
+ return rc;
}
LIBPLDM_ABI_STABLE
@@ -119,16 +135,38 @@
size_t req_msg_len, uint8_t **pldm_resp_msg,
size_t *resp_msg_len)
{
- PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_recv_msg, pldm_req_msg,
- req_msg_len, (void **)pldm_resp_msg, resp_msg_len);
+ pldm_requester_rc_t rc = 0;
+ struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)pldm_req_msg;
+ if (hdr && !hdr->request) {
+ return PLDM_REQUESTER_NOT_REQ_MSG;
+ }
+ PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_recv_msg, rc,
+ pldm_req_msg, req_msg_len, (void **)pldm_resp_msg,
+ resp_msg_len);
+ if (rc != PLDM_REQUESTER_SUCCESS) {
+ return rc;
+ }
+ hdr = (struct pldm_msg_hdr *)pldm_resp_msg;
+ if (hdr && (hdr->request || hdr->datagram)) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_NOT_RESP_MSG;
+ }
+ return rc;
}
LIBPLDM_ABI_STABLE
pldm_requester_rc_t pldm_send(mctp_eid_t eid, int mctp_fd,
const uint8_t *pldm_req_msg, size_t req_msg_len)
{
- PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, (void *)pldm_req_msg,
- req_msg_len);
+ pldm_requester_rc_t rc = 0;
+ struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)pldm_req_msg;
+ if (!hdr->request) {
+ return PLDM_REQUESTER_NOT_REQ_MSG;
+ }
+ PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, rc,
+ (void *)pldm_req_msg, req_msg_len);
+ return rc;
}
/* Adding this here for completeness in the case we can't smoothly
diff --git a/src/transport/transport.c b/src/transport/transport.c
index 0cbef29..1219147 100644
--- a/src/transport/transport.c
+++ b/src/transport/transport.c
@@ -66,11 +66,6 @@
return PLDM_REQUESTER_NOT_REQ_MSG;
}
- const struct pldm_msg_hdr *hdr = pldm_req_msg;
- if (!hdr->request) {
- return PLDM_REQUESTER_NOT_REQ_MSG;
- }
-
return transport->send(transport, tid, pldm_req_msg, req_msg_len);
}
@@ -90,20 +85,11 @@
return rc;
}
- struct pldm_msg_hdr *hdr = *pldm_resp_msg;
- if (hdr->request || hdr->datagram) {
+ if (*resp_msg_len < sizeof(struct pldm_msg_hdr)) {
free(*pldm_resp_msg);
*pldm_resp_msg = NULL;
- return PLDM_REQUESTER_NOT_RESP_MSG;
+ return PLDM_REQUESTER_INVALID_RECV_LEN;
}
-
- uint8_t pldm_rc = 0;
- if (*resp_msg_len < (sizeof(struct pldm_msg_hdr) + sizeof(pldm_rc))) {
- free(*pldm_resp_msg);
- *pldm_resp_msg = NULL;
- return PLDM_REQUESTER_RESP_MSG_TOO_SMALL;
- }
-
return PLDM_REQUESTER_SUCCESS;
}