Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/mctp-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string.h>
#include <err.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <errno.h>
#include <limits.h>

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -183,9 +191,10 @@ static void print_usage()
{
size_t ctr;

printf("usage:\n\tmctp-client [net <net>] [eid <eid>] [iface <iface> [lladdr <lladdr>]] type <type> data <data>\n");
printf("usage:\n\tmctp-client [net <net>] [eid <eid>] [iface <iface> [lladdr <lladdr>]] type <type> [timeout <milliseconds>] data <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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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")) {
Expand All @@ -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)
Expand All @@ -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);
}
Loading