Skip to content

Commit

Permalink
Added client-side support for IPv6.
Browse files Browse the repository at this point in the history
Minor change to OFStandard::getAddressByHostname() and requestAssociationTCP()
that allow DCMTK based clients to connect to IPv6 based servers.

Thanks to Sobhita Mercy <[email protected]> for this contribution.
This closes GitHub pull request #99.
  • Loading branch information
Marco Eichelberg committed Jul 28, 2024
1 parent 5f45950 commit 4abd372
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
20 changes: 18 additions & 2 deletions dcmnet/libsrc/dulfsm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ END_EXTERN_C
#include "dcmtk/ofstd/ofstd.h"
#include <ctime>
#include <climits>
#include <cstdlib>

/* At least Solaris doesn't define this */
#ifndef INADDR_NONE
Expand Down Expand Up @@ -2247,7 +2248,7 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
PRIVATE_ASSOCIATIONKEY ** association)
{
char node[128];
int port;
int port = 0;
OFSockAddr server;
#ifdef _WIN32
SOCKET s;
Expand All @@ -2256,7 +2257,22 @@ requestAssociationTCP(PRIVATE_NETWORKKEY ** network,
#endif
struct linger sockarg;

if (sscanf(params->calledPresentationAddress, "%[^:]:%d", node, &port) != 2)
// Split the presentation address into hostname/IP address and port.
OFString addressString(params->calledPresentationAddress);
// find the last colon in the string to split the node and port
size_t lastColonPos = addressString.find_last_of(':');
if (lastColonPos != OFString_npos) {
// extract node and port from the string
OFString nodeStr = addressString.substr(0, lastColonPos);
OFString portStr = addressString.substr(lastColonPos + 1);

// copy node string into node char array
OFStandard::strlcpy(node, nodeStr.c_str(), sizeof(node));

// convert port string to integer
port = STD_NAMESPACE atoi(portStr.c_str());
}
else
{
char buf[1024];
OFStandard::snprintf(buf, sizeof(buf), "Illegal service parameter: %s", params->calledPresentationAddress);
Expand Down
5 changes: 2 additions & 3 deletions ofstd/libsrc/ofstd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3169,10 +3169,9 @@ void OFStandard::getAddressByHostname(const char *name, OFSockAddr& result)
int err = EAI_AGAIN;
int rep = DCMTK_MAX_EAI_AGAIN_REPETITIONS;

// filter for the DNS lookup. Since DCMTK does not yet fully support IPv6,
// we only look for IPv4 addresses.
// filter for the DNS lookup. AF_UNSPEC allows us to support IPv4 and IPv6.
::addrinfo hint = {};
hint.ai_family = AF_INET;
hint.ai_family = AF_UNSPEC;

// perform DNS lookup. Repeat while we receive temporary failures.
while ((EAI_AGAIN == err) && (rep-- > 0)) err = getaddrinfo(name, NULL, &hint, &result_list);
Expand Down

0 comments on commit 4abd372

Please sign in to comment.