File: send2dicomcopyd.c

package info (click to toggle)
lcgdm 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,532 kB
  • sloc: ansic: 226,887; sh: 13,562; perl: 11,575; python: 11,572; cpp: 5,716; sql: 1,824; makefile: 1,301; fortran: 113
file content (249 lines) | stat: -rw-r--r-- 5,718 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) 2007-2008 by CERN/IT/GD/ITR
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: send2dicomcopyd.c,v $ $Revision: 1.6 $ $Date: 2009/01/11 00:43:26 $ CERN IT-GD/ITR Jean-Philippe Baud";
#endif /* not lint */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#if defined(_WIN32)
#include <winsock2.h>
#else
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "Cnetdb.h"
#ifdef CSEC
#include "Csec_api.h"
#endif
#include "dicomcopy_api.h"
#include "dpm.h"
#include "dpm_api.h"
#include "marshall.h"
#include "net.h"
#include "serrno.h"
#if defined(_WIN32)
extern char *ws_strerr;
#endif

/* send2dicomcopyd - send a request to the DPM DICOM backend and wait for the reply */

send2dicomcopyd(host, reqp, reql, user_repbuf, user_repbuf_len)
char *host;
char *reqp;
int reql;
char *user_repbuf;
int user_repbuf_len;
{
	int actual_replen = 0;
	struct addrinfo *ai;
	struct addrinfo *aitop;
	int c;
#ifdef CSEC
	Csec_context_t ctx;
#endif
	char dicomcopydhost[CA_MAXHOSTNAMELEN+1];
	char func[16];
	int gaierrno;
	char *getconfent();
	char *getenv();
	struct addrinfo hints;
	int isconnected;
	int magic;
	int n;
	char *neterrstr = NULL;
	char *p;
	char prtbuf[PRTBUFSZ];
	int rep_type;
	char repbuf[REPBUFSZ];
	int s;
	int save_serrno;
	char strport[NI_MAXSERV];

	strcpy (func, "send2dicomcopyd");
	if ((p = getenv ("DICOMCOPYD_PORT")) || (p = getconfent ("DICOMCOPYD", "PORT", 0))) {
		strncpy (strport, p, sizeof(strport));
		strport[sizeof(strport)-1] = '\0';
	} else {
		snprintf (strport, sizeof(strport), "%u", DICOMCOPYD_PORT);
		serrno = 0;
	}
	if (host && *host)
		strcpy (dicomcopydhost, host);
	else if ((p = getenv ("DICOMCOPYD_HOST")) || (p = getconfent ("DICOMCOPYD", "HOST", 0)))
		strcpy (dicomcopydhost, p);
	else {
#if defined(DPM_HOST)
		strcpy (dicomcopydhost, DPM_HOST);
#else
		gethostname (dicomcopydhost, sizeof(dicomcopydhost));
#endif
		serrno = 0;
	}

	memset (&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
 #ifdef AI_ADDRCONFIG
	hints.ai_flags |= AI_ADDRCONFIG;
#endif
	gaierrno = Cgetaddrinfo (dicomcopydhost, strport, &hints, &aitop);

	if (gaierrno !=0 && serrno == 0)
		serrno = SENOSHOST;

	if (gaierrno == EAI_NONAME) {
		dpm_errmsg (func, DP009, "Host unknown:", dicomcopydhost);
		return (-1);
	} else if (gaierrno != 0) {
		dpm_errmsg (func, "Error during lookup of %s: %s\n",
		    dicomcopydhost, Cgai_strerror (gaierrno));
		return (-1);
	}

	isconnected = 0;
	save_serrno = 0;
	for (ai = aitop; ai && !isconnected; ai = ai->ai_next) {
		if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6)
			continue;
		if ((s = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol))<0)
			continue;

		if (connect (s, ai->ai_addr, ai->ai_addrlen) < 0) {
#if defined(_WIN32)
			if (WSAGetLastError() == WSAEAFNOSUPPORT) {
#else
			if (errno == EAFNOSUPPORT) {
#endif
				(void) netclose (s);
				continue;
			}
#if defined(_WIN32)
			if (WSAGetLastError() == WSAECONNREFUSED) {
#else
			if (errno == ECONNREFUSED) {
#endif
				save_serrno = EDPMNACT;
			} else {
				save_serrno = SECOMERR;
			}
			p = neterror ();
			if (neterrstr)
				free (neterrstr);
			neterrstr = strdup (p);
			(void) netclose (s);
		} else {
			isconnected = 1;
		}
	}
	freeaddrinfo (aitop);
	if (!isconnected) {
		serrno = (save_serrno) ? save_serrno : SECOMERR;
		switch(serrno) {
			case EDPMNACT:
				dpm_errmsg (func, DP000, dicomcopydhost);
				break;
			default:
				if (neterrstr) {
					dpm_errmsg (func, DP002, "connect", neterrstr);
				} else {
					dpm_errmsg (func, "Could not create "
					    "an outgoing connection\n");
				}
				break;
		}
		if (neterrstr)
			free (neterrstr);
		return (-1);
	}
	if (neterrstr) {
		free (neterrstr);
		neterrstr = NULL;
	}

#ifdef CSEC
	Csec_client_initContext (&ctx, CSEC_SERVICE_TYPE_HOST, NULL);
	if (Csec_client_establishContext (&ctx, s) < 0) {
		if (serrno != SECOMERR && serrno != SETIMEDOUT)
			dpm_errmsg (func, DP002, "send", Csec_getErrorMessageSummary (PRTBUFSZ-48));
		(void) netclose (s);
		Csec_clearContext (&ctx);
		return (-1);
	}
	Csec_clearContext (&ctx);
#endif

	/* send request to DPM DICOM server */

	if ((n = netwrite (s, reqp, reql)) <= 0) {
		if (n == 0)
			dpm_errmsg (func, DP002, "send", sys_serrlist[SERRNO]);
		else
			dpm_errmsg (func, DP002, "send", neterror());
		(void) netclose (s);
		serrno = SECOMERR;
		return (-1);
	}

	/* get reply */

	while (1) {
		if ((n = netread (s, repbuf, 3 * LONGSIZE)) <= 0) {
			if (n == 0)
				dpm_errmsg (func, DP002, "recv", sys_serrlist[SERRNO]);
			else
				dpm_errmsg (func, DP002, "recv", neterror());
			(void) netclose (s);
			serrno = SECOMERR;
			return (-1);
		}
		p = repbuf;
		unmarshall_LONG (p, magic) ;
		unmarshall_LONG (p, rep_type) ;
		unmarshall_LONG (p, c) ;
		if (rep_type == DPM_RC) {
			(void) netclose (s);
			if (c) {
				serrno = c;
				c = -1;
			}
			break;
		}
		if (c > REPBUFSZ) {
			dpm_errmsg (func, "reply too large\n");
			serrno = SEINTERNAL;
			return (-1);
		}
		if ((n = netread (s, repbuf, c)) <= 0) {
			if (n == 0)
				dpm_errmsg (func, DP002, "recv", sys_serrlist[SERRNO]);
			else
				dpm_errmsg (func, DP002, "recv", neterror());
			(void) netclose (s);
			serrno = SECOMERR;
			return (-1);
		}
		p = repbuf;
		if (rep_type == MSG_ERR) {
			unmarshall_STRING (p, prtbuf);
			dpm_errmsg (NULL, "%s", prtbuf);
		} else if (user_repbuf) {
			if (actual_replen + c <= user_repbuf_len)
				n = c;
			else
				n = user_repbuf_len - actual_replen;
			if (n) {
				memcpy (user_repbuf + actual_replen, repbuf, n);
				actual_replen += n;
			}
		}
	}
	return (c);
}