1 /* ----------------------------- MNI Header -----------------------------------
2 @NAME       : open_connection.c
3 @DESCRIPTION: File containing routines to open a decnet connection.
4 @GLOBALS    :
5 @CREATED    : November 22, 1993 (Peter Neelin)
6 @MODIFIED   :
7  * $Log: open_connection.c,v $
8  * Revision 6.1  1999-10-29 17:51:56  neelin
9  * Fixed Log keyword
10  *
11  * Revision 6.0  1997/09/12 13:24:27  neelin
12  * Release of minc version 0.6
13  *
14  * Revision 5.0  1997/08/21  13:25:26  neelin
15  * Release of minc version 0.5
16  *
17  * Revision 4.0  1997/05/07  20:06:20  neelin
18  * Release of minc version 0.4
19  *
20  * Revision 1.1  1997/03/04  20:56:47  neelin
21  * Initial revision
22  *
23  * Revision 3.0  1995/05/15  19:31:44  neelin
24  * Release of minc version 0.3
25  *
26  * Revision 2.5  1995/02/14  18:12:26  neelin
27  * Added project names and defaults files (using volume name).
28  * Added process id to log file name.
29  * Moved temporary files to subdirectory.
30  *
31  * Revision 2.4  1995/02/09  13:51:26  neelin
32  * Mods for irix 5 lint.
33  *
34  * Revision 2.3  1995/02/08  19:31:47  neelin
35  * Moved ARGSUSED statements for irix 5 lint.
36  *
37  * Revision 2.2  1994/12/07  09:45:59  neelin
38  * Fixed called to ioctl to get rid of type mismatch warning messages.
39  *
40  * Revision 2.1  94/12/07  08:20:10  neelin
41  * Added support for irix 5 decnet.
42  *
43  * Revision 2.0  94/09/28  10:35:32  neelin
44  * Release of minc version 0.2
45  *
46  * Revision 1.5  94/09/28  10:34:50  neelin
47  * Pre-release
48  *
49  * Revision 1.4  94/01/18  14:23:41  neelin
50  * Changed bzero to memset.
51  *
52  * Revision 1.3  93/11/30  14:42:13  neelin
53  * Copies to minc format.
54  *
55  * Revision 1.2  93/11/25  13:26:55  neelin
56  * Working version.
57  *
58  * Revision 1.1  93/11/23  14:11:54  neelin
59  * Initial revision
60  *
61 @COPYRIGHT  :
62               Copyright 1993 Peter Neelin, McConnell Brain Imaging Centre,
63               Montreal Neurological Institute, McGill University.
64               Permission to use, copy, modify, and distribute this
65               software and its documentation for any purpose and without
66               fee is hereby granted, provided that the above copyright
67               notice appear in all copies.  The author and McGill University
68               make no representations about the suitability of this
69               software for any purpose.  It is provided "as is" without
70               express or implied warranty.
71 ---------------------------------------------------------------------------- */
72 
73 #include <stdlib.h>
74 #include <stdio.h>
75 #include <sys/types.h>
76 #include <sys/socket.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <errno.h>
80 #include <signal.h>
81 #include <dicomserver.h>
82 
83 /* ----------------------------- MNI Header -----------------------------------
84 @NAME       : connection_okay
85 @INPUT      : sockfd - input file descriptor which might be a socket
86 @OUTPUT     : (none)
87 @RETURNS    : TRUE if connection is okay, FALSE otherwise
88 @DESCRIPTION: Checks whether the connection is allowed. Looks at sockfd
89               to find out if remote host is allowed to connect. If sockfd
90               is a file and not a socket, then the connection is allowed.
91 @METHOD     :
92 @GLOBALS    :
93 @CALLS      :
94 @CREATED    : February 20, 1997 (Peter Neelin)
95 @MODIFIED   :
96 ---------------------------------------------------------------------------- */
connection_okay(int sockfd)97 private int connection_okay(int sockfd)
98 {
99    struct sockaddr_in us, them;
100    int status;
101    int namelen;
102    extern int Do_logging;
103 
104    /* Get our own id. If sockfd is a file, then its okay. Check that we
105       have an internet connection. */
106    namelen = sizeof(us);
107    if (getsockname(sockfd, &us, &namelen) != 0) {
108       if (errno == ENOTSOCK)
109          return TRUE;
110       else {
111          (void) fprintf(stderr, "Unable to get our own host address.\n");
112          return FALSE;
113       }
114    }
115    else if (us.sin_family != AF_INET) {
116       (void) fprintf(stderr, "Connection is not from network.\n");
117       return FALSE;
118    }
119 
120    /* Try to get id of host at other end of connection */
121    namelen = sizeof(us);
122    status = getpeername(sockfd, &them, &namelen);
123    if (status != 0) {
124       (void) fprintf(stderr, "Unable to check connection source.\n");
125       return FALSE;
126    }
127 
128    /* */
129    if (Do_logging >= LOW_LOGGING) {
130       (void) fprintf(stderr, "Connection from %s ", inet_ntoa(them.sin_addr));
131    }
132 
133    /* Compare the addresses. Make sure that we have the same IP domain
134       assuming class C structure. */
135    if ((us.sin_addr.s_addr & IN_CLASSC_NET) !=
136        (them.sin_addr.s_addr & IN_CLASSC_NET)) {
137       if (Do_logging >= LOW_LOGGING) {
138          (void) fprintf(stderr, "refused.\n");
139       }
140       return FALSE;
141    }
142 
143    if (Do_logging >= LOW_LOGGING) {
144       (void) fprintf(stderr, "accepted.\n");
145    }
146 
147    return TRUE;
148 }
149 
150 /* ----------------------------- MNI Header -----------------------------------
151 @NAME       : open_connection
152 @INPUT      : argc - number of command-line arguments
153               argv - array of command-line arguments
154 @OUTPUT     : afpin - Acr file pointer for input
155               afpout - Acr file pointer for output
156 @RETURNS    : (nothing)
157 @DESCRIPTION: Opens the connection for reading writing dicom messages.
158 @METHOD     :
159 @GLOBALS    :
160 @CALLS      :
161 @CREATED    : November 22, 1993 (Peter Neelin)
162 @MODIFIED   :
163 ---------------------------------------------------------------------------- */
164 /* ARGSUSED */
open_connection(int argc,char * argv[],Acr_File ** afpin,Acr_File ** afpout)165 public void open_connection(int argc, char *argv[],
166                             Acr_File **afpin, Acr_File **afpout)
167 {
168    /* Set default file pointers */
169    *afpin = *afpout = NULL;
170 
171    /* Check for a valid connection */
172    if (!connection_okay(fileno(stdin))) return;
173 
174    /* Open the connection */
175    *afpin=acr_initialize_dicom_input(stdin, 0, acr_stdio_read);
176    *afpout=acr_initialize_dicom_output(stdout, 0, acr_stdio_write);
177 
178    /* Ignore SIGPIPE errors in case connection gets closed when we are
179       doing output */
180    (void) signal(SIGPIPE, SIG_IGN);
181 }
182 
183