Skip to content

Commit 51661a4

Browse files
committed
Add --ip and --port flags for remote bridge connections
- Add --ip flag to specify remote bridge IP address - Add --port flag to specify custom port number - Update init_client() to use configurable IP/port with inet_pton for Windows - Add IP address validation and conversion - Update usage documentation for both spdm_requester_emu and spdm_device_validator_sample - Support both MCTP and TCP transports with custom ports - Fix Windows MSVC build by replacing deprecated inet_addr with inet_pton - Enable spdm_device_validator_sample to connect to remote bridges This enables spdm-emu tools to connect to bridges running on remote machines, useful for testing SPDM implementations across different hosts while maintaining backward compatibility with default localhost:2323 connections. Signed-off-by: Lee Ballard <lee.ballard@dell.com>
1 parent 5565c7a commit 51661a4

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

spdm_emu/spdm_device_validator_sample/spdm_device_validator_sample.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ int main(int argc, char *argv[])
9494

9595
process_args("spdm_device_validator_sample", argc, argv);
9696

97-
platform_client_routine(DEFAULT_SPDM_PLATFORM_PORT);
97+
/* Use custom port if provided, otherwise default to 2323 */
98+
uint16_t port = (m_custom_port != 0) ? m_custom_port : DEFAULT_SPDM_PLATFORM_PORT;
99+
platform_client_routine(port);
98100
printf("Client stopped\n");
99101

100102
close_pcap_packet_file();

spdm_emu/spdm_emu_common/spdm_emu.c

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ uint32_t m_exe_session =
3232

3333
#define IP_ADDRESS "127.0.0.1"
3434

35+
char m_ip_address_string[16] = "127.0.0.1";
36+
uint16_t m_custom_port = 0; /* 0 means use default */
37+
3538
#ifdef _MSC_VER
3639
struct in_addr m_ip_address = { { { 127, 0, 0, 1 } } };
3740
#else
@@ -42,6 +45,8 @@ void print_usage(const char *name)
4245
{
4346
printf("\n%s [--trans MCTP|PCI_DOE|TCP|NONE]\n", name);
4447
printf(" [--tcp_sub RI|NO_RI]\n");
48+
printf(" [--ip <ip_address>]\n");
49+
printf(" [--port <port_number>]\n");
4550
printf(" [--ver 1.0|1.1|1.2|1.3|1.4]\n");
4651
printf(" [--sec_ver 1.0|1.1|1.2]\n");
4752
printf(
@@ -609,6 +614,44 @@ void process_args(char *program_name, int argc, char *argv[])
609614
}
610615
}
611616

617+
if (strcmp(argv[0], "--ip") == 0) {
618+
if (argc >= 2) {
619+
if (strlen(argv[1]) >= sizeof(m_ip_address_string)) {
620+
printf("invalid --ip %s (too long)\n", argv[1]);
621+
print_usage(program_name);
622+
exit(0);
623+
}
624+
strcpy(m_ip_address_string, argv[1]);
625+
printf("ip - %s\n", m_ip_address_string);
626+
argc -= 2;
627+
argv += 2;
628+
continue;
629+
} else {
630+
printf("invalid --ip\n");
631+
print_usage(program_name);
632+
exit(0);
633+
}
634+
}
635+
636+
if (strcmp(argv[0], "--port") == 0) {
637+
if (argc >= 2) {
638+
m_custom_port = (uint16_t)atoi(argv[1]);
639+
if (m_custom_port == 0) {
640+
printf("invalid --port %s\n", argv[1]);
641+
print_usage(program_name);
642+
exit(0);
643+
}
644+
printf("port - %d\n", m_custom_port);
645+
argc -= 2;
646+
argv += 2;
647+
continue;
648+
} else {
649+
printf("invalid --port\n");
650+
print_usage(program_name);
651+
exit(0);
652+
}
653+
}
654+
612655
if (strcmp(argv[0], "--ver") == 0) {
613656
if (argc >= 2) {
614657
if (!get_value_from_name(
@@ -1443,10 +1486,23 @@ void process_args(char *program_name, int argc, char *argv[])
14431486
return;
14441487
}
14451488

1489+
bool convert_ip_to_addr(const char *ip_string, struct in_addr *addr)
1490+
{
1491+
#ifdef _MSC_VER
1492+
/* Use inet_pton for Windows MSVC compatibility */
1493+
return (inet_pton(AF_INET, ip_string, addr) == 1);
1494+
#else
1495+
/* Fallback to inet_addr for non-Windows platforms */
1496+
addr->s_addr = inet_addr(ip_string);
1497+
return (addr->s_addr != INADDR_NONE);
1498+
#endif
1499+
}
1500+
14461501
bool init_client(SOCKET *sock, uint16_t port)
14471502
{
14481503
SOCKET client_socket;
14491504
struct sockaddr_in server_addr;
1505+
struct in_addr ip_addr;
14501506
int32_t ret_val;
14511507

14521508
#ifdef _MSC_VER
@@ -1470,9 +1526,28 @@ bool init_client(SOCKET *sock, uint16_t port)
14701526
}
14711527

14721528
server_addr.sin_family = AF_INET;
1473-
libspdm_copy_mem(&server_addr.sin_addr.s_addr, sizeof(struct in_addr), &m_ip_address,
1474-
sizeof(struct in_addr));
1475-
server_addr.sin_port = htons(port);
1529+
1530+
/* Use custom IP if provided, otherwise use default */
1531+
if (strcmp(m_ip_address_string, "127.0.0.1") != 0) {
1532+
if (!convert_ip_to_addr(m_ip_address_string, &ip_addr)) {
1533+
printf("Invalid IP address: %s\n", m_ip_address_string);
1534+
closesocket(client_socket);
1535+
return false;
1536+
}
1537+
libspdm_copy_mem(&server_addr.sin_addr.s_addr, sizeof(struct in_addr), &ip_addr,
1538+
sizeof(struct in_addr));
1539+
} else {
1540+
libspdm_copy_mem(&server_addr.sin_addr.s_addr, sizeof(struct in_addr), &m_ip_address,
1541+
sizeof(struct in_addr));
1542+
}
1543+
1544+
/* Use custom port if provided */
1545+
if (m_custom_port != 0) {
1546+
server_addr.sin_port = htons(m_custom_port);
1547+
} else {
1548+
server_addr.sin_port = htons(port);
1549+
}
1550+
14761551
libspdm_zero_mem(server_addr.sin_zero, sizeof(server_addr.sin_zero));
14771552

14781553
ret_val = connect(client_socket, (struct sockaddr *)&server_addr,

spdm_emu/spdm_emu_common/spdm_emu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
extern uint32_t m_use_transport_layer;
2525
extern uint32_t m_use_tcp_role_inquiry;
26+
extern char m_ip_address_string[16];
27+
extern uint16_t m_custom_port;
2628
extern uint8_t m_use_version;
2729
extern uint8_t m_use_secured_message_version;
2830
extern uint32_t m_use_requester_capability_flags;

spdm_emu/spdm_requester_emu/spdm_requester_emu.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,14 @@ int main(int argc, char *argv[])
294294
process_args("spdm_requester_emu", argc, argv);
295295

296296
if (m_use_transport_layer == SOCKET_TRANSPORT_TYPE_TCP) {
297-
/* Port number 4194 for SPDM */
298-
result = platform_client_routine(TCP_SPDM_PLATFORM_PORT);
297+
/* Use custom port if specified, otherwise default to 4194 for TCP */
298+
uint16_t port = (m_custom_port != 0) ? m_custom_port : TCP_SPDM_PLATFORM_PORT;
299+
result = platform_client_routine(port);
299300
}
300301
else {
301-
result = platform_client_routine(DEFAULT_SPDM_PLATFORM_PORT);
302+
/* Use custom port if specified, otherwise default to 2323 for other transports */
303+
uint16_t port = (m_custom_port != 0) ? m_custom_port : DEFAULT_SPDM_PLATFORM_PORT;
304+
result = platform_client_routine(port);
302305
}
303306

304307
printf("Client stopped\n");

0 commit comments

Comments
 (0)