1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*
*
* Copyright (C) 2017-2021, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Marco Eichelberg
*
* Purpose: Wrapper class for struct sockaddr and related structs
*
*/
#include "dcmtk/config/osconfig.h" /* include OS specific configuration first */
#include "dcmtk/ofstd/ofsockad.h"
#include "dcmtk/ofstd/ofstream.h"
socklen_t OFSockAddr::size() const
{
switch (getFamily())
{
case AF_INET:
return sizeof(struct sockaddr_in);
case AF_INET6:
return sizeof(struct sockaddr_in6);
default:
return 0;
}
}
void OFSockAddr::setPort(unsigned short port)
{
struct sockaddr_in *si = NULL;
struct sockaddr_in6 *si6 = NULL;
switch (getFamily())
{
case AF_INET:
si = getSockaddr_in();
si->sin_port = port;
break;
case AF_INET6:
si6 = getSockaddr_in6();
si6->sin6_port = port;
break;
default:
/* unknown protocol family, do nothing */
break;
}
}
DCMTK_OFSTD_EXPORT STD_NAMESPACE ostream& operator<< (STD_NAMESPACE ostream& o, const OFSockAddr& s)
{
o << "SOCKADDR_BEGIN\n Family: ";
#ifdef _WIN32
unsigned long bufsize = 512;
#endif
char buf[512];
buf[0] = '\0';
const struct sockaddr_in *si = NULL;
const struct sockaddr_in6 *si6 = NULL;
switch (s.getFamily())
{
case 0:
o << "not set\n";
break;
case AF_INET:
si = s.getSockaddr_in_const();
o << "AF_INET";
#ifdef _WIN32
/* MinGW and some Visual Studio versions do not have inet_ntop() */
WSAAddressToStringA((struct sockaddr*) si, OFstatic_cast(DWORD, s.size()), NULL, buf, &bufsize);
o << "\n IP address: " << buf;
#else
// The typecast is necessary for older MSVC compilers, which expect a non-const void * parameter.
o << "\n IP address: " << inet_ntop(AF_INET, OFconst_cast(void *, OFreinterpret_cast(const void *, &si->sin_addr)), buf, 512);
#endif
o << "\n Port: " << ntohs(si->sin_port) << "\n";
break;
case AF_INET6:
si6 = s.getSockaddr_in6_const();
o << " AF_INET6";
#ifdef _WIN32
/* MinGW and some Visual Studio versions do not have inet_ntop() */
WSAAddressToStringA((struct sockaddr*) si6, OFstatic_cast(DWORD, s.size()), NULL, buf, &bufsize);
o << "\n IP address: " << buf;
#else
o << "\n IP address: " << inet_ntop(AF_INET6, OFconst_cast(void *, OFreinterpret_cast(const void *, &si6->sin6_addr)), buf, 512);
#endif
o << "\n Port: " << ntohs(si6->sin6_port)
<< "\n Flow info: " << si6->sin6_flowinfo
<< "\n Scope: " << si6->sin6_scope_id
<< "\n";
break;
default:
o << "unknown protocol: " << s.getFamily() << "\n";
break;
}
o << "SOCKADDR_END" << OFendl;
return o;
}
|