diff --git a/src/mctp-client.c b/src/mctp-client.c index e7bce8e..3f026ad 100644 --- a/src/mctp-client.c +++ b/src/mctp-client.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -111,9 +112,13 @@ static int do_type_lookup(char *type_str) static int do_send_recv(unsigned int net, mctp_eid_t eid, uint8_t type, const struct physical_address *physical, - struct data_t *data) + struct data_t *data, uint32_t timeout_ms) { struct sockaddr_mctp_ext addr; + struct timeval timeout = { + .tv_sec = timeout_ms / 1000, + .tv_usec = (timeout_ms % 1000) * 1000, + }; ssize_t rc, recvlen, ctr; uint8_t *recv_buffer; socklen_t addrlen; @@ -123,6 +128,9 @@ static int do_send_recv(unsigned int net, mctp_eid_t eid, uint8_t type, if (sd < 0) err(EXIT_FAILURE, "socket"); + if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))) + err(EXIT_FAILURE, "setsockopt(SO_RCVTIMEO)"); + memset(&addr, 0, sizeof(addr)); addr.smctp_base.smctp_tag = MCTP_TAG_OWNER; addr.smctp_base.smctp_family = AF_MCTP; @@ -183,9 +191,10 @@ static void print_usage() { size_t ctr; - printf("usage:\n\tmctp-client [net ] [eid ] [iface [lladdr ]] type data \n"); + printf("usage:\n\tmctp-client [net ] [eid ] [iface [lladdr ]] type [timeout ] data \n"); printf("at least one of eid or iface is required. iface selects physical addressing.\n"); printf("net defaults to MCTP_NET_ANY, eid defaults to 0 with iface.\n"); + printf("timeout defaults to 500 milliseconds.\n"); printf("data is space delimited hexadecimal and must be the last parameter.\n"); printf("possible types:\n"); for (ctr = 0; ctr < ARRAY_SIZE(type_lookup); ++ctr) { @@ -277,6 +286,7 @@ int main(int argc, char **argv) struct physical_address physical = { 0 }; bool valid_eid, valid_type, valid_lladdr; unsigned int net = MCTP_NET_ANY; + uint32_t timeout_ms = 500; const char *iface = NULL; struct data_t send_data; int type, ctr, data_idx; @@ -305,6 +315,9 @@ int main(int argc, char **argv) int rc; tag = argv[ctr]; + if (ctr + 1 >= argc) + errx(EXIT_FAILURE, "missing value for argument: %s", + tag); val = argv[ctr + 1]; if (!strcmp(tag, "eid")) { @@ -316,6 +329,10 @@ int main(int argc, char **argv) rc = parse_uint32(val, &net); if (rc) errx(EXIT_FAILURE, "invalid net: %s", val); + } else if (!strcmp(tag, "timeout")) { + rc = parse_uint32(val, &timeout_ms); + if (rc || !timeout_ms || val[0] == '-') + errx(EXIT_FAILURE, "invalid timeout: %s", val); } else if (!strcmp(tag, "type")) { type = do_type_lookup(val); if (type < 0 || type > 0xff) @@ -342,5 +359,5 @@ int main(int argc, char **argv) resolve_physical_address(iface, &physical); return do_send_recv(net, eid, type, iface ? &physical : NULL, - &send_data); + &send_data, timeout_ms); }