This is an automated email from Gerrit.

"zapb <[email protected]>" just uploaded a new patch set to Gerrit, which you can 
find at https://review.openocd.org/c/openocd/+/9656

-- gerrit

commit 8a199cedb33686861518f18764bb0a3a085186e7
Author: Marc Schink <[email protected]>
Date:   Wed May 13 02:29:16 2026 +0200

    adapter/jtag-dpi: Use getaddrinfo() to support hostnames and IPv6
    
    Connection establishment currently supports only IPv4 addresses via
    inet_addr(). Replace inet_addr() with getaddrinfo() to allow using
    hostnames and to add IPv6 support.
    
    Change-Id: Iafcb9e7f120e2c002989ec8587a8cee5c410fed4
    Signed-off-by: Marc Schink <[email protected]>

diff --git a/doc/openocd.texi b/doc/openocd.texi
index 6ecd0246c1..8c43917032 100644
--- a/doc/openocd.texi
+++ b/doc/openocd.texi
@@ -3696,7 +3696,7 @@ Specifies the TCP/IP port number of the SystemVerilog DPI 
server interface.
 @end deffn
 
 @deffn {Config Command} {jtag_dpi set_address} address
-Specifies the TCP/IP address of the SystemVerilog DPI server interface.
+Specifies the hostname or IP address of the SystemVerilog DPI server interface.
 @end deffn
 @end deffn
 
diff --git a/src/jtag/drivers/jtag_dpi.c b/src/jtag/drivers/jtag_dpi.c
index 35dd245072..e10d5b6431 100644
--- a/src/jtag/drivers/jtag_dpi.c
+++ b/src/jtag/drivers/jtag_dpi.c
@@ -16,6 +16,9 @@
 #endif
 
 #include <jtag/interface.h>
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #endif
@@ -31,7 +34,6 @@ static uint16_t server_port = SERVER_PORT;
 static char *server_address;
 
 static int sockfd;
-static struct sockaddr_in serv_addr;
 
 static uint8_t *last_ir_buf;
 static int last_ir_num_bits;
@@ -264,18 +266,6 @@ static int jtag_dpi_execute_queue(struct jtag_command 
*cmd_queue)
 
 static int jtag_dpi_init(void)
 {
-       sockfd = socket(AF_INET, SOCK_STREAM, 0);
-       if (sockfd < 0) {
-               LOG_ERROR("socket: %s, function %s, file %s, line %d",
-                       strerror(errno), __func__, __FILE__, __LINE__);
-               return ERROR_FAIL;
-       }
-
-       memset(&serv_addr, 0, sizeof(serv_addr));
-
-       serv_addr.sin_family = AF_INET;
-       serv_addr.sin_port = htons(server_port);
-
        if (!server_address) {
                server_address = strdup(SERVER_ADDRESS);
                if (!server_address) {
@@ -285,26 +275,63 @@ static int jtag_dpi_init(void)
                }
        }
 
-       serv_addr.sin_addr.s_addr = inet_addr(server_address);
+       const struct addrinfo hints = {
+               .ai_family = AF_UNSPEC,
+               .ai_socktype = SOCK_STREAM
+       };
+
+       char port_str[5 + 1];
+       snprintf(port_str, sizeof(port_str), "%" PRIu16, server_port);
 
-       if (serv_addr.sin_addr.s_addr == INADDR_NONE) {
-               LOG_ERROR("inet_addr error occurred");
+       struct addrinfo *result;
+       int ret = getaddrinfo(server_address, port_str, &hints, &result);
+
+       if (ret != 0) {
+               LOG_ERROR("getaddrinfo: %s", gai_strerror(ret));
                return ERROR_FAIL;
        }
 
-       if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 
0) {
+       struct addrinfo *rp;
+       for (rp = result; rp ; rp = rp->ai_next) {
+               sockfd = socket(rp->ai_family, rp->ai_socktype, 
rp->ai_protocol);
+               if (sockfd == -1)
+                       continue;
+
+               if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
+                       break;
+
                close(sockfd);
-               LOG_ERROR("Can't connect to %s : %" PRIu16, server_address, 
server_port);
+       }
+
+       if (!rp) {
+               LOG_ERROR("Can't connect to %s : %" PRIu16, server_address,
+                       server_port);
+               freeaddrinfo(result);
                return ERROR_FAIL;
        }
-       if (serv_addr.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
+
+       bool is_loopback = false;
+       if (rp->ai_family == AF_INET) {
+               const struct sockaddr_in *sa = (const struct sockaddr_in 
*)rp->ai_addr;
+               is_loopback = (sa->sin_addr.s_addr == htonl(INADDR_LOOPBACK));
+       } else if (rp->ai_family == AF_INET6) {
+               const struct sockaddr_in6 *sa = (const struct sockaddr_in6 
*)rp->ai_addr;
+               is_loopback = IN6_IS_ADDR_LOOPBACK(&sa->sin6_addr);
+       }
+
+       if (is_loopback) {
+               LOG_DEBUG("Enabling TCP_NODELAY to enhance the speed of local 
connections");
+
                /* This increases performance dramatically for local
                * connections, which is the most likely arrangement
                * for a DPI connection. */
                int flag = 1;
-               setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, 
sizeof(int));
+               setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
+                       sizeof(int));
        }
 
+       freeaddrinfo(result);
+
        LOG_INFO("Connection to %s : %" PRIu16 " succeed", server_address, 
server_port);
 
        return ERROR_OK;
@@ -372,7 +399,7 @@ static const struct command_registration 
jtag_dpi_subcommand_handlers[] = {
                .name = "set_address",
                .handler = &jtag_dpi_set_address,
                .mode = COMMAND_CONFIG,
-               .help = "set the address of the DPI server",
+               .help = "set the hostname or IP address of the DPI server",
                .usage = "[address]",
        },
        COMMAND_REGISTRATION_DONE

-- 

Reply via email to