2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2016 VMS Software, Inc. All Rights Reserved.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 # define OPENSSL_SYS_VMS
13 # pragma message disable DOLLARID
16 # include <openssl/opensslconf.h>
18 # if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
20 * On VMS, you need to define this to get the declaration of fileno(). The
21 * value 2 is to make sure no function defined in POSIX-2 is left undefined.
23 # define _POSIX_C_SOURCE 2
28 # undef _POSIX_C_SOURCE
30 # include <sys/types.h>
31 # include <sys/socket.h>
32 # include <netinet/in.h>
42 typedef struct _iosb { /* Copied from IOSBDEF.H for Alpha */
43 # pragma __nomember_alignment
46 unsigned short int iosb$w_status; /* Final I/O status */
48 __struct { /* 16-bit byte count variant */
49 unsigned short int iosb$w_bcnt; /* 16-bit byte count */
51 unsigned int iosb$l_dev_depend; /* 32-bit device dependent info */
52 unsigned int iosb$l_pid; /* 32-bit pid */
55 __struct { /* 32-bit byte count variant */
56 unsigned int iosb$l_bcnt; /* 32-bit byte count (unaligned) */
57 unsigned short int iosb$w_dev_depend_high; /* 16-bit device dependent info */
63 unsigned int iosb$l_getxxi_status; /* Final GETxxI status */
64 unsigned int iosb$l_reg_status; /* Final $Registry status */
66 unsigned int iosb$l_reserved; /* Reserved field */
72 # define iosb$w_status iosb$r_io_get.iosb$r_io_64.iosb$w_status
73 # define iosb$w_bcnt iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_16.iosb$w_bcnt
74 # define iosb$r_l iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_16.iosb$r_l
75 # define iosb$l_dev_depend iosb$r_l.iosb$l_dev_depend
76 # define iosb$l_pid iosb$r_l.iosb$l_pid
77 # define iosb$l_bcnt iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_32.iosb$l_bcnt
78 # define iosb$w_dev_depend_high iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_32.iosb$w_dev_depend_high
79 # define iosb$l_getxxi_status iosb$r_io_get.iosb$r_get_64.iosb$r_l_status.iosb$l_getxxi_status
80 # define iosb$l_reg_status iosb$r_io_get.iosb$r_get_64.iosb$r_l_status.iosb$l_reg_status
81 # endif /* #if !defined(__VAXC) */
83 # endif /* End of IOSBDEF */
92 # include "vms_term_sock.h"
95 static struct _iosb TerminalDeviceIosb;
97 IOSB TerminalDeviceIosb;
100 static char TerminalDeviceBuff[255 + 2];
101 static int TerminalSocketPair[2] = {0, 0};
102 static unsigned short TerminalDeviceChan = 0;
104 static int CreateSocketPair (int, int, int, int *);
105 static void SocketPairTimeoutAst (int);
106 static int TerminalDeviceAst (int);
107 static void LogMessage (char *, ...);
110 ** Socket Pair Timeout Value (must be 0-59 seconds)
112 # define SOCKET_PAIR_TIMEOUT_VALUE 20
115 ** Socket Pair Timeout Block which is passed to timeout AST
117 typedef struct _SocketPairTimeoutBlock {
118 unsigned short SockChan1;
119 unsigned short SockChan2;
122 # ifdef TERM_SOCK_TEST
124 /*----------------------------------------------------------------------------*/
126 /*----------------------------------------------------------------------------*/
127 int main (int argc, char *argv[], char *envp[])
134 LogMessage ("Enter 'q' or 'Q' to quit ...");
135 while (strcasecmp (TermBuff, "Q")) {
137 ** Create the terminal socket
139 status = TerminalSocket (TERM_SOCK_CREATE, &TermSock);
140 if (status != TERM_SOCK_SUCCESS)
144 ** Process the terminal input
146 LogMessage ("Waiting on terminal I/O ...\n");
147 len = recv (TermSock, TermBuff, sizeof(TermBuff), 0) ;
148 TermBuff[len] = '\0';
149 LogMessage ("Received terminal I/O [%s]", TermBuff);
152 ** Delete the terminal socket
154 status = TerminalSocket (TERM_SOCK_DELETE, &TermSock);
155 if (status != TERM_SOCK_SUCCESS)
164 /*----------------------------------------------------------------------------*/
166 /*----------------------------------------------------------------------------*/
167 int TerminalSocket (int FunctionCode, int *ReturnSocket)
170 $DESCRIPTOR (TerminalDeviceDesc, "SYS$COMMAND");
173 ** Process the requested function code
175 switch (FunctionCode) {
176 case TERM_SOCK_CREATE:
178 ** Create a socket pair
180 status = CreateSocketPair (AF_INET, SOCK_STREAM, 0, TerminalSocketPair);
182 LogMessage ("TerminalSocket: CreateSocketPair () - %08X", status);
183 if (TerminalSocketPair[0])
184 close (TerminalSocketPair[0]);
185 if (TerminalSocketPair[1])
186 close (TerminalSocketPair[1]);
187 return TERM_SOCK_FAILURE;
191 ** Assign a channel to the terminal device
193 status = sys$assign (&TerminalDeviceDesc,
196 if (! (status & 1)) {
197 LogMessage ("TerminalSocket: SYS$ASSIGN () - %08X", status);
198 close (TerminalSocketPair[0]);
199 close (TerminalSocketPair[1]);
200 return TERM_SOCK_FAILURE;
204 ** Queue an async IO to the terminal device
206 status = sys$qio (EFN$C_ENF,
213 sizeof(TerminalDeviceBuff) - 2,
215 if (! (status & 1)) {
216 LogMessage ("TerminalSocket: SYS$QIO () - %08X", status);
217 close (TerminalSocketPair[0]);
218 close (TerminalSocketPair[1]);
219 return TERM_SOCK_FAILURE;
223 ** Return the input side of the socket pair
225 *ReturnSocket = TerminalSocketPair[1];
228 case TERM_SOCK_DELETE:
230 ** Cancel any pending IO on the terminal channel
232 status = sys$cancel (TerminalDeviceChan);
233 if (! (status & 1)) {
234 LogMessage ("TerminalSocket: SYS$CANCEL () - %08X", status);
235 close (TerminalSocketPair[0]);
236 close (TerminalSocketPair[1]);
237 return TERM_SOCK_FAILURE;
241 ** Deassign the terminal channel
243 status = sys$dassgn (TerminalDeviceChan);
244 if (! (status & 1)) {
245 LogMessage ("TerminalSocket: SYS$DASSGN () - %08X", status);
246 close (TerminalSocketPair[0]);
247 close (TerminalSocketPair[1]);
248 return TERM_SOCK_FAILURE;
252 ** Close the terminal socket pair
254 close (TerminalSocketPair[0]);
255 close (TerminalSocketPair[1]);
258 ** Return the initialized socket
265 ** Invalid function code
267 LogMessage ("TerminalSocket: Invalid Function Code - %d", FunctionCode);
268 return TERM_SOCK_FAILURE;
275 return TERM_SOCK_SUCCESS;
279 /*----------------------------------------------------------------------------*/
281 /*----------------------------------------------------------------------------*/
282 static int CreateSocketPair (int SocketFamily,
287 struct dsc$descriptor AscTimeDesc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, NULL};
288 static const char* LocalHostAddr = {"127.0.0.1"};
289 unsigned short TcpAcceptChan = 0,
291 unsigned long BinTimeBuff[2];
292 struct sockaddr_in sin;
293 char AscTimeBuff[32];
307 $DESCRIPTOR (TcpDeviceDesc, "TCPIP$DEVICE");
312 SockDesc1 = socket (SocketFamily, SocketType, 0);
314 LogMessage ("CreateSocketPair: socket () - %d", errno);
319 ** Initialize the socket information
322 memset ((char *) &sin, 0, slen);
323 sin.sin_family = SocketFamily;
324 sin.sin_addr.s_addr = inet_addr (LocalHostAddr);
328 ** Bind the socket to the local IP
330 status = bind (SockDesc1, (struct sockaddr *) &sin, slen);
332 LogMessage ("CreateSocketPair: bind () - %d", errno);
338 ** Get the socket name so we can save the port number
340 status = getsockname (SockDesc1, (struct sockaddr *) &sin, &slen);
342 LogMessage ("CreateSocketPair: getsockname () - %d", errno);
346 LocalHostPort = sin.sin_port;
349 ** Setup a listen for the socket
351 listen (SockDesc1, 5);
354 ** Get the binary (64-bit) time of the specified timeout value
356 sprintf (AscTimeBuff, "0 0:0:%02d.00", SOCKET_PAIR_TIMEOUT_VALUE);
357 AscTimeDesc.dsc$w_length = strlen (AscTimeBuff);
358 AscTimeDesc.dsc$a_pointer = AscTimeBuff;
359 status = sys$bintim (&AscTimeDesc, BinTimeBuff);
360 if (! (status & 1)) {
361 LogMessage ("CreateSocketPair: SYS$BINTIM () - %08X", status);
367 ** Assign another channel to the TCP/IP device for the accept.
368 ** This is the channel that ends up being connected to.
370 status = sys$assign (&TcpDeviceDesc, &TcpDeviceChan, 0, 0, 0);
371 if (! (status & 1)) {
372 LogMessage ("CreateSocketPair: SYS$ASSIGN () - %08X", status);
378 ** Get the channel of the first socket for the accept
380 TcpAcceptChan = decc$get_sdc (SockDesc1);
383 ** Perform the accept using $QIO so we can do this asynchronously
385 status = sys$qio (EFN$C_ENF,
387 IO$_ACCESS | IO$M_ACCEPT,
392 if (! (status & 1)) {
393 LogMessage ("CreateSocketPair: SYS$QIO () - %08X", status);
395 sys$dassgn (TcpDeviceChan);
400 ** Create the second socket to do the connect
402 SockDesc2 = socket (SocketFamily, SocketType, 0);
404 LogMessage ("CreateSocketPair: socket () - %d", errno);
405 sys$cancel (TcpAcceptChan);
407 sys$dassgn (TcpDeviceChan);
412 ** Setup the Socket Pair Timeout Block
414 sptb.SockChan1 = TcpAcceptChan;
415 sptb.SockChan2 = decc$get_sdc (SockDesc2);
418 ** Before we block on the connect, set a timer that can cancel I/O on our
419 ** two sockets if it never connects.
421 status = sys$setimr (EFN$C_ENF,
423 SocketPairTimeoutAst,
426 if (! (status & 1)) {
427 LogMessage ("CreateSocketPair: SYS$SETIMR () - %08X", status);
428 sys$cancel (TcpAcceptChan);
431 sys$dassgn (TcpDeviceChan);
436 ** Now issue the connect
438 memset ((char *) &sin, 0, sizeof(sin)) ;
439 sin.sin_family = SocketFamily;
440 sin.sin_addr.s_addr = inet_addr (LocalHostAddr) ;
441 sin.sin_port = LocalHostPort ;
443 status = connect (SockDesc2, (struct sockaddr *) &sin, sizeof(sin));
445 LogMessage ("CreateSocketPair: connect () - %d", errno);
446 sys$cantim (&sptb, 0);
447 sys$cancel (TcpAcceptChan);
450 sys$dassgn (TcpDeviceChan);
455 ** Wait for the asynch $QIO to finish. Note that if the I/O was aborted
456 ** (SS$_ABORT), then we probably canceled it from the AST routine - so log
459 status = sys$synch (EFN$C_ENF, &iosb);
460 if (! (iosb.iosb$w_status & 1)) {
461 if (iosb.iosb$w_status == SS$_ABORT)
462 LogMessage ("CreateSocketPair: SYS$QIO(iosb) timeout");
464 LogMessage ("CreateSocketPair: SYS$QIO(iosb) - %d",
466 sys$cantim (&sptb, 0);
470 sys$dassgn (TcpDeviceChan);
475 ** Here we're successfully connected, so cancel the timer, convert the
476 ** I/O channel to a socket fd, close the listener socket and return the
479 sys$cantim (&sptb, 0);
482 SocketPair[0] = SockDesc2 ;
483 SocketPair[1] = socket_fd (TcpDeviceChan);
489 /*----------------------------------------------------------------------------*/
491 /*----------------------------------------------------------------------------*/
492 static void SocketPairTimeoutAst (int astparm)
494 SPTB *sptb = (SPTB *) astparm;
496 sys$cancel (sptb->SockChan2); /* Cancel the connect() */
497 sys$cancel (sptb->SockChan1); /* Cancel the accept() */
503 /*----------------------------------------------------------------------------*/
505 /*----------------------------------------------------------------------------*/
506 static int TerminalDeviceAst (int astparm)
511 ** Terminate the terminal buffer
513 TerminalDeviceBuff[TerminalDeviceIosb.iosb$w_bcnt] = '\0';
514 strcat (TerminalDeviceBuff, "\n");
517 ** Send the data read from the terminal device through the socket pair
519 send (TerminalSocketPair[0], TerminalDeviceBuff,
520 TerminalDeviceIosb.iosb$w_bcnt + 1, 0);
523 ** Queue another async IO to the terminal device
525 status = sys$qio (EFN$C_ENF,
532 sizeof(TerminalDeviceBuff) - 1,
542 /*----------------------------------------------------------------------------*/
544 /*----------------------------------------------------------------------------*/
545 static void LogMessage (char *msg, ...)
547 char *Month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
548 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
549 static unsigned int pid = 0;
556 ** Get the process pid
562 ** Convert the current time into local time
564 CurTime = time (NULL);
565 LocTime = localtime (&CurTime);
568 ** Format the message buffer
570 sprintf (MsgBuff, "%02d-%s-%04d %02d:%02d:%02d [%08X] %s\n",
571 LocTime->tm_mday, Month[LocTime->tm_mon],
572 (LocTime->tm_year + 1900), LocTime->tm_hour, LocTime->tm_min,
573 LocTime->tm_sec, pid, msg);
576 ** Get any variable arguments and add them to the print of the message
579 va_start (args, msg);
580 vfprintf (stderr, MsgBuff, args);
584 ** Flush standard error output
586 fsync (fileno (stderr));