File: snp_to_files.c

package info (click to toggle)
ctn 3.2.0~dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 16,924 kB
  • sloc: ansic: 179,652; makefile: 7,006; java: 1,863; csh: 1,067; yacc: 523; sh: 424; cpp: 394; sql: 389; lex: 170
file content (419 lines) | stat: -rw-r--r-- 11,189 bytes parent folder | download
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
          Copyright (C) 1993, 1994, RSNA and Washington University

          The software and supporting documentation for the Radiological
          Society of North America (RSNA) 1993, 1994 Digital Imaging and
          Communications in Medicine (DICOM) Demonstration were developed
          at the
                  Electronic Radiology Laboratory
                  Mallinckrodt Institute of Radiology
                  Washington University School of Medicine
                  510 S. Kingshighway Blvd.
                  St. Louis, MO 63110
          as part of the 1993, 1994 DICOM Central Test Node project for, and
          under contract with, the Radiological Society of North America.

          THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND NEITHER RSNA NOR
          WASHINGTON UNIVERSITY MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS
          PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
          USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY
          SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF
          THE SOFTWARE IS WITH THE USER.

          Copyright of the software and supporting documentation is
          jointly owned by RSNA and Washington University, and free access
          is hereby granted as a license to use this software, copy this
          software and prepare derivative works based upon this software.
          However, any distribution of this software source code or
          supporting documentation or derivative works (source code and
          supporting documentation) must include the three paragraphs of
          the copyright notice.
*/
/* Copyright marker.  Copyright will be inserted above.  Do not remove */

/*
**                              DICOM 94
**                   Electronic Radiology Laboratory
**                 Mallinckrodt Institute of Radiology
**              Washington University School of Medicine
**
** Module Name(s):	main
**			callbackState
**			callbackITOA
**			callbackATOI
**			usageerror
** Author, Date:	Nilesh R. Gohel, 14-Sep-1994
** Intent:		This program allows the user to snoop on one or more
**			associations
**			at the TCP/IP level on a shared media network given the
**			name of the initiator, name of the acceptor, and the port
**			number on the acceptor. Additional information required
**			includes information on the network interface to be used
**			and size of buffers for temporary stroage of data. Data
**			from the initiator to the acceptor is stored in a file
**			and data from the acceptor to the initiator stored in
**			another. Names of these files are also required.
**			The application uses the SNP facility for the snooping.
**
** Last Update:         $Author: smm $, $Date: 1998-08-03 16:27:11 $
** Source File:         $RCSfile: snp_to_files.c,v $
** Revision:            $Revision: 1.9 $
** Status:              $State: Exp $
*/

static char rcsid[] = "$Revision: 1.9 $ $RCSfile: snp_to_files.c,v $";

#include <stdio.h>
#include <stdlib.h>
#ifdef SUNOS
#include <sys/types.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "dicom.h"
#include "ctnthread.h"
#include "lst.h"
#include "condition.h"
#include "snp.h"

/*
	Prototypes for internal functions
*/
static void usageerror();
static CONDITION callbackState(int state, void *ctx);
static CONDITION callbackITOA(char *buffer, int bufsize, void *ctx);
static CONDITION callbackATOI(char *buffer, int bufsize, void *ctx);
static int write_end_assoc();

volatile int assoc;		/* Associations left to monitor */
static int current_state = NORMAL;	/* Current state reported by SNP
					 * facilities */
static int fd_atoi;		/* File descriptor for initiator -> acceptor
				 * data */
static int fd_itoa;		/* File descriptor for acceptor -> initiator
				 * data */
static TCP_BUF_HEAD buf_head;	/* Header information for each TCP data
				 * buffer */

main(int argc, char *argv[])
{
    CONDITION cond;		/* Variable to track conditions returned */

    /*
     * Check for correct usage
     */
    if (argc < 10)
	usageerror();

    /*
     * Re-iterate user entries
     */
    printf("Device = %s\n", argv[1]);
    printf("PPA = %d\n", atoi(argv[2]));
    printf("Initiator = %s\n", argv[3]);
    printf("Acceptor = %s\n", argv[4]);
    printf("Acceptor port = %d\n", atoi(argv[5]));
    printf("Buffer size (longs) = %d\n", atoi(argv[6]));
    assoc = atoi(argv[7]);
    printf("Associations = %d\n", assoc);
    printf("Initiator -> Acceptor data file = %s\n", argv[8]);
    printf("Acceptor -> Initiator data file = %s\n", argv[9]);
    printf("\n\n");

    if (assoc < 1)
	fprintf(stderr, "Need to monitor at least one association\n");

    /*
     * Initialize sequence numbering for TCP data buffers
     */
    buf_head.seq = 0;

    /*
     * Place SNP facility in debug mode - turn off for now
     */
    THR_Init();
    SNP_Debug(FALSE);

    /*
     * Initialize SNP facilities
     */
    cond = SNP_Init();
    if (cond != SNP_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    /*
     * Register callback functions
     */
    cond = SNP_RegisterCallback(callbackState, SNP_CALLBACK_STATE, NULL);
    if (cond != SNP_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    cond = SNP_RegisterCallback(callbackITOA, SNP_CALLBACK_ITOA, NULL);
    if (cond != SNP_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    cond = SNP_RegisterCallback(callbackATOI, SNP_CALLBACK_ATOI, NULL);
    if (cond != SNP_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    /*
     * Open data files
     */
    if ((fd_itoa = open(argv[8], O_WRONLY | O_CREAT | O_TRUNC, 0666)) == NULL) {
	fprintf(stderr, "Problem opening file %s\n", argv[8]);
	THR_Shutdown();
	exit(1);
    }
    if ((fd_atoi = open(argv[9], O_WRONLY | O_CREAT | O_TRUNC, 0666)) == NULL) {
	fprintf(stderr, "Problem opening file %s\n", argv[9]);
	THR_Shutdown();
	exit(1);
    }
    /*
     * Commence the snooping with given arguements
     */
    cond = SNP_Start(argv[1], atoi(argv[2]), argv[3],
		     argv[4], atoi(argv[5]), 20, 5, atoi(argv[6]));
    if (cond != SNP_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    printf("\nInitialization complete .... ready to monitor communications\n");
    /*
     * Until have monitored correct number of associations or until something
     * goes wrong keep snooping - update user with number of associations to
     * go
     */
    while (assoc > 0) {
	sleep(1);
    }

    /*
     * If finished in a bad state .... something went wrong
     */
    if (current_state != NORMAL) {
	printf("\nError: %s\n", SNP_StateMsg(current_state));
	THR_Shutdown();
	exit(1);
    } else
	printf("\nCompleted monitoring associations normally\n");

    /*
     * Discontinue snooping operations
     */
    cond = SNP_Stop();
    if (cond != SNP_NORMAL)
	COND_DumpConditions();

    /*
     * Terminate activities with SNP facility
     */
    cond = SNP_Terminate();
    if (cond != SNP_NORMAL)
	COND_DumpConditions();

    /*
     * Close file descriptors
     */
    close(fd_itoa);
    close(fd_atoi);

    THR_Shutdown();
    exit(0);
}

/* callbackState
**
** Purpose:
**	Used to report the current state of the underlying SNP
**	facilities.
**
** Parameter Dictionary:
**	state	The state number
**	ctx	Context pointer
**
** Return Values:
**	SNP_NORMAL
**	SNP_DONE
**
** Algorithm:
**	Description of the algorithm (optional) and any other notes
*/
CONDITION
callbackState(int state, void *ctx)
{

    current_state = state;
    switch (state) {
    case END_ASSOC:
	if (write_end_assoc()) {
	    fprintf(stderr, "Error writing End of Association\n");
	    return SNP_DONE;
	}
	current_state = NORMAL;
	assoc--;
	printf("\n%d associations to go\n", assoc);
	if (!assoc) {
	    return SNP_DONE;
	}
	break;
    case NORMAL:
	break;
    default:
	assoc = 0;
	return SNP_DONE;
    }
    return SNP_NORMAL;
}


/* callbackITOA
**
** Purpose:
**	Used to dump parsed data from underlying SNP facilities out to
**	appropriate file. Data handled by this function are from
**	the initiator to the acceptor.
**
** Parameter Dictionary:
**	fd_itoa (global)	File descriptor to write data to
**	buffer			Pointer to data being passed
**	bufsize 		Size of data buffer being passed
**	ctx			Context pointer
**
** Return Values:
**	SNP_NORMAL
**	SNP_CALLBACKFAIL
**
** Algorithm:
**	Description of the algorithm (optional) and any other notes.
*/
CONDITION
callbackITOA(char *buffer, int bufsize, void *ctx)
{
    /*
     * Construct header to add to each TCP data buffer placed in file. Header
     * provides direction, sequence number, and length of buffer. Write
     * buffer to file.
     */
    buf_head.type = ITOA;
    buf_head.len = bufsize;
    if ((write(fd_itoa, (void *) &buf_head, 12)) != 12)
	return SNP_CALLBACKFAIL;
    /*
     * Write data buffer to file
     */
    if ((write(fd_itoa, (void *) buffer, bufsize)) != bufsize)
	return SNP_CALLBACKFAIL;
    /*
     * Increment sequence number
     */
    buf_head.seq++;
    return SNP_NORMAL;
}


/* callbackATOI
**
** Purpose:
**	Used to dump parsed data from underlying SNP facilities out to
**	appropriate file. Data handled by this function are from
**	the acceptor to the initiator.
**
** Parameter Dictionary:
**	fd_atoi (global)	File descriptor to write data to
**	buffer			Pointer to data being passed
**	bufsize 		Size of data buffer being passed
**	ctx			Context pointer
**
** Return Values:
**	SNP_NORMAL
**	SNP_CALLBACKFAIL
**
** Algorithm:
**	Description of the algorithm (optional) and any other notes.
*/
CONDITION
callbackATOI(char *buffer, int bufsize, void *ctx)
{
    /*
     * Construct header to add to each TCP data buffer placed in file. Header
     * provides direction, sequence number, and length of buffer. Write
     * buffer to file.
     */
    buf_head.type = ATOI;
    buf_head.len = bufsize;
    if ((write(fd_atoi, (void *) &buf_head, 12)) != 12)
	return SNP_CALLBACKFAIL;
    /*
     * Write data buffer to file
     */
    if ((write(fd_atoi, (void *) buffer, bufsize)) != bufsize)
	return SNP_CALLBACKFAIL;
    /*
     * Increment sequence number
     */
    buf_head.seq++;
    return SNP_NORMAL;
}

/* write_end_assoc
**
** Purpose:
**	Writes a header to file for parsed ini->acc data indicating
**	an END OF ASSOCIATION at the TCP/IP level (socket closed).
**
** Parameter Dictionary:
**
**
** Return Values:
**	0			Success
**	1			Failure
**
** Algorithm:
**	Description of the algorithm (optional) and any other notes.
*/
static int
write_end_assoc()
{
    buf_head.type = SNP_EOA;
    buf_head.len = 0;
    if ((write(fd_itoa, (void *) &buf_head, 12)) != 12)
	return 1;
    buf_head.seq++;
    return 0;
}


/* usageerror
**
** Purpose:
**	Print the usage string for this application and exit.
**
** Parameter Dictionary:
**	None
**
** Return Values:
**	None
**
** Algorithm:
**	Description of the algorithm (optional) and any other notes.
*/

static void
usageerror()
{

    fprintf(stderr, "Usage:\n\tsnp_to_files device ppa initiator acceptor port buffersize(longs) associations ini->acc_file acc->ini_file\n\n");
    exit(1);
}