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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
*
* Copyright (C) 2000-2024 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: dcmpstat
*
* Authors: Marco Eichelberg
*
* Purpose: Sample message server for class DVPSIPCClient
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#ifdef HAVE_WINDOWS_H
#include <winsock2.h>
#include <ws2tcpip.h>
#elif defined(HAVE_WINSOCK_H)
#include <winsock.h> /* include winsock.h directly i.e. on MacOS */
#endif
BEGIN_EXTERN_C
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h> /* we have to include this before sys/time.h on Solaris */
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h> /* for struct timeval on Linux */
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
END_EXTERN_C
#include "dcmtk/dcmpstat/dvpsmsg.h" /* for class DVPSIPCMessage */
#include "dcmtk/dcmdata/cmdlnarg.h" /* for prepareCmdLineArgs */
#include "dcmtk/ofstd/ofconapp.h" /* for class OFConsoleApplication */
#include "dcmtk/dcmnet/dcmtrans.h" /* for class DcmTCPConnection */
#include "dcmtk/dcmdata/dcuid.h"
#include "dcmtk/dcmnet/dcompat.h" /* compatibility routines */
#include "dcmtk/dcmnet/dul.h"
#include "dcmtk/dcmnet/diutil.h"
#define OFFIS_CONSOLE_APPLICATION "msgserv"
static OFLogger msgservLogger = OFLog::getLogger("dcmtk.test." OFFIS_CONSOLE_APPLICATION);
static char rcsid[] = "$dcmtk: " OFFIS_CONSOLE_APPLICATION " v"
OFFIS_DCMTK_VERSION " " OFFIS_DCMTK_RELEASEDATE " $";
static const char *statusString(Uint32 i)
{
if (i==DVPSIPCMessage::statusOK) return "OK";
if (i==DVPSIPCMessage::statusWarning) return "Warning";
if (i==DVPSIPCMessage::statusError) return "Error";
return "unknown status code";
}
static const char *applicationType(Uint32 i)
{
if (i==DVPSIPCMessage::clientOther) return "unspecified";
if (i==DVPSIPCMessage::clientStoreSCP) return "Receiver (Store SCP)";
if (i==DVPSIPCMessage::clientStoreSCU) return "Sender (Store SCU)";
if (i==DVPSIPCMessage::clientPrintSCP) return "Printer (Print SCP)";
if (i==DVPSIPCMessage::clientPrintSCU) return "Print Client (Print SCU)";
if (i==DVPSIPCMessage::clientQRSCP) return "Database (Query/Retrieve SCP)";
return "unknown application type";
}
#define SHORTCOL 3
#define LONGCOL 12
int main(int argc, char *argv[])
{
OFStandard::initializeNetwork();
#ifdef WITH_TCPWRAPPER
// this code makes sure that the linker cannot optimize away
// the DUL part of the network module where the external flags
// for libwrap are defined. Needed on OpenBSD.
dcmTCPWrapperDaemonName.set(NULL);
#endif
OFCmdUnsignedInt opt_port = 0; /* listen port */
Uint32 clientID = 0; /* IDs assigned to connecting clients */
OFConsoleApplication app(OFFIS_CONSOLE_APPLICATION , "Sample message server for class DVPSIPCClient", rcsid);
OFCommandLine cmd;
cmd.setOptionColumns(LONGCOL, SHORTCOL);
cmd.setParamColumn(LONGCOL + SHORTCOL + 2);
cmd.addParam("port", "port number to listen at");
cmd.addGroup("general options:", LONGCOL, SHORTCOL);
cmd.addOption("--help", "-h", "print this help text and exit", OFCommandLine::AF_Exclusive);
OFLog::addOptions(cmd);
/* evaluate command line */
prepareCmdLineArgs(argc, argv, OFFIS_CONSOLE_APPLICATION);
if (app.parseCommandLine(cmd, argc, argv))
{
cmd.getParam(1, opt_port);
OFLog::configureFromCommandLine(cmd, app);
}
OFLOG_DEBUG(msgservLogger, rcsid << OFendl);
unsigned short networkPort = (unsigned short) opt_port;
if (networkPort==0)
{
OFLOG_FATAL(msgservLogger, "no or invalid port number");
return 10;
}
#ifndef DISABLE_PORT_PERMISSION_CHECK
#ifdef HAVE_GETEUID
/* if port is privileged we must be as well */
if ((networkPort < 1024)&&(geteuid() != 0))
{
OFLOG_FATAL(msgservLogger, "cannot listen on port " << networkPort << ", insufficient privileges");
return 10;
}
#endif
#endif
/* open listen socket */
#ifdef _WIN32
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
if (s == INVALID_SOCKET)
#else
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
#endif
{
OFLOG_FATAL(msgservLogger, "failed to create socket");
return 10;
}
int reuse = 1;
#ifdef _WIN32
if (setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) &reuse, sizeof(reuse)) < 0)
#else
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) < 0)
#endif
{
OFLOG_FATAL(msgservLogger, "failed to set socket options");
return 10;
}
/* Name socket using wildcards */
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = (unsigned short) htons(networkPort);
if (bind(s, (struct sockaddr *) & server, sizeof(server)))
{
OFLOG_FATAL(msgservLogger, "failed to bind socket to port, already in use?");
return 10;
}
listen(s, 64); // accept max 64 pending TCP connections on this socket
/* drop root privileges now and revert to the calling user id (if we are running as setuid root) */
if (OFStandard::dropPrivileges().bad())
{
OFLOG_FATAL(msgservLogger, "setuid() failed, maximum number of processes/threads for uid already running.");
return 10;
}
fd_set fdset;
struct timeval t;
int nfound;
while (1)
{
// wait for next incoming connection
FD_ZERO(&fdset);
FD_SET(s, &fdset);
t.tv_sec = 10; // 10 seconds timeout
t.tv_usec = 0;
// the typecast is safe because Windows ignores the first select() parameter anyway
nfound = select(OFstatic_cast(int, s + 1), &fdset, NULL, NULL, &t);
if (DCM_dcmnetLogger.isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
{
DU_logSelectResult(nfound);
}
if (nfound > 0)
{
// incoming connection detected
#ifdef _WIN32
SOCKET sock=0;
#else
int sock=0;
#endif
struct sockaddr from;
socklen_t len = sizeof(from);
do
{
sock = accept(s, &from, &len);
#ifdef _WIN32
} while ((sock == INVALID_SOCKET)&&(errno == WSAEINTR));
if (sock == INVALID_SOCKET)
#else
} while ((sock == -1)&&(errno == EINTR));
if (sock < 0)
#endif
{
OFLOG_FATAL(msgservLogger, "unable to accept incoming connection");
return 10;
}
reuse = 1;
#ifdef _WIN32
if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) &reuse, sizeof(reuse)) < 0)
#else
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) < 0)
#endif
{
OFLOG_FATAL(msgservLogger, "failed to set socket options");
return 10;
}
// now we can handle the incoming connection
DcmTCPConnection connection(sock);
DVPSIPCMessage msg;
Uint32 i=0;
Uint32 msgType=0;
OFString str;
OFBool finished = OFFalse;
while (!finished)
{
while (! connection.networkDataAvailable(1))
{
// waiting for network data to become available or connection to be closed
}
if (msg.receive(connection))
{
OFOStringStream oss;
// handle message
msgType = msg.getMessageType();
if (msgType == DVPSIPCMessage::OK)
{
oss << "received 'OK' (should not happen)" << OFendl;
} else if (msgType == DVPSIPCMessage::requestApplicationID) {
oss << "New client requests application ID, assigning #" << clientID+1 << OFendl
<< "Application Type: ";
if (msg.extractIntFromPayload(i)) oss << applicationType(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::assignApplicationID) {
oss << "received 'AssignApplicationID' (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::applicationTerminates) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Application Terminates, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
} else if (msgType == DVPSIPCMessage::receivedUnencryptedDICOMConnection) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Received Unencrypted DICOM Connection, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::receivedEncryptedDICOMConnection) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Received Encrypted DICOM Connection, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::connectionClosed) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Connection Closed, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
} else if (msgType == DVPSIPCMessage::connectionAborted) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Connection Aborted, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::requestedUnencryptedDICOMConnection) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Requested Unencrypted DICOM Connection, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::requestedEncryptedDICOMConnection) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Requested Encrypted DICOM Connection, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::receivedDICOMObject) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Received DICOM Object, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else if (msgType == DVPSIPCMessage::sentDICOMObject) {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "Sent DICOM Object, status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
if (msg.extractStringFromPayload(str)) oss << str << OFendl; else oss << "No description (should not happen)." << OFendl;
} else {
if (msg.extractIntFromPayload(i)) oss << "#" << i << ": "; else oss << "unknown client: ";
oss << "received unknown message type " << msg.getMessageType() << ", status: ";
if (msg.extractIntFromPayload(i)) oss << statusString(i) << OFendl; else oss << "(none)" << OFendl;
}
oss << OFStringStream_ends;
OFSTRINGSTREAM_GETSTR(oss, result);
OFLOG_INFO(msgservLogger, result);
OFSTRINGSTREAM_FREESTR(result);
msg.erasePayload();
if (msg.getMessageType() == DVPSIPCMessage::requestApplicationID)
{
msg.setMessageType(DVPSIPCMessage::assignApplicationID);
msg.addIntToPayload(++clientID);
} else {
msg.setMessageType(DVPSIPCMessage::OK);
}
if (! msg.send(connection))
{
OFLOG_WARN(msgservLogger, "unable to send response message, closing connection");
finished = OFTrue;
}
} else finished = OFTrue;
}
// connection has been closed by the client or something has gone wrong.
// clean up connection and wait for next client.
connection.close();
}
}
OFStandard::shutdownNetwork();
return 0;
}
|