2 * Copyright 2016 VMS Software, Inc. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #define OPENSSL_SYS_VMS
12 #pragma message disable DOLLARID
15 #include <openssl/OPENSSLCONF.H>
17 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
19 * On VMS, you need to define this to get the declaration of fileno(). The
20 * value 2 is to make sure no function defined in POSIX-2 is left undefined.
22 # define _POSIX_C_SOURCE 2
27 #undef _POSIX_C_SOURCE
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
41 typedef struct _iosb { /* Copied from IOSBDEF.H for Alpha */
42 #pragma __nomember_alignment
45 unsigned short int iosb$w_status; /* Final I/O status */
47 __struct { /* 16-bit byte count variant */
48 unsigned short int iosb$w_bcnt; /* 16-bit byte count */
50 unsigned int iosb$l_dev_depend; /* 32-bit device dependent info */
51 unsigned int iosb$l_pid; /* 32-bit pid */
54 __struct { /* 32-bit byte count variant */
55 unsigned int iosb$l_bcnt; /* 32-bit byte count (unaligned) */
56 unsigned short int iosb$w_dev_depend_high; /* 16-bit device dependent info */
62 unsigned int iosb$l_getxxi_status; /* Final GETxxI status */
63 unsigned int iosb$l_reg_status; /* Final $Registry status */
65 unsigned int iosb$l_reserved; /* Reserved field */
71 #define iosb$w_status iosb$r_io_get.iosb$r_io_64.iosb$w_status
72 #define iosb$w_bcnt iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_16.iosb$w_bcnt
73 #define iosb$r_l iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_16.iosb$r_l
74 #define iosb$l_dev_depend iosb$r_l.iosb$l_dev_depend
75 #define iosb$l_pid iosb$r_l.iosb$l_pid
76 #define iosb$l_bcnt iosb$r_io_get.iosb$r_io_64.iosb$r_devdepend.iosb$r_bcnt_32.iosb$l_bcnt
77 #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
78 #define iosb$l_getxxi_status iosb$r_io_get.iosb$r_get_64.iosb$r_l_status.iosb$l_getxxi_status
79 #define iosb$l_reg_status iosb$r_io_get.iosb$r_get_64.iosb$r_l_status.iosb$l_reg_status
80 #endif /* #if !defined(__VAXC) */
82 #endif /* End of IOSBDEF */
91 #include "vms_term_sock.h"
94 static struct _iosb TerminalDeviceIosb;
96 IOSB TerminalDeviceIosb;
99 static char TerminalDeviceBuff[255 + 2];
100 static int TerminalSocketPair[2] = {0, 0};
101 static unsigned short TerminalDeviceChan = 0;
103 static int CreateSocketPair (int, int, int, int *);
104 static void SocketPairTimeoutAst (int);
105 static int TerminalDeviceAst (int);
106 static void LogMessage (char *, ...);
109 ** Socket Pair Timeout Value (must be 0-59 seconds)
111 #define SOCKET_PAIR_TIMEOUT_VALUE 20
114 ** Socket Pair Timeout Block which is passed to timeout AST
116 typedef struct _SocketPairTimeoutBlock {
117 unsigned short SockChan1;
118 unsigned short SockChan2;
121 #ifdef TERM_SOCK_TEST
123 /*----------------------------------------------------------------------------*/
125 /*----------------------------------------------------------------------------*/
126 int main (int argc, char *argv[], char *envp[])
133 LogMessage ("Enter 'q' or 'Q' to quit ...");
134 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)
177 case TERM_SOCK_CREATE:
179 ** Create a socket pair
181 status = CreateSocketPair (AF_INET, SOCK_STREAM, 0, TerminalSocketPair);
184 LogMessage ("TerminalSocket: CreateSocketPair () - %08X", status);
185 if (TerminalSocketPair[0])
186 close (TerminalSocketPair[0]);
187 if (TerminalSocketPair[1])
188 close (TerminalSocketPair[1]);
189 return (TERM_SOCK_FAILURE);
193 ** Assign a channel to the terminal device
195 status = sys$assign (&TerminalDeviceDesc,
200 LogMessage ("TerminalSocket: SYS$ASSIGN () - %08X", status);
201 close (TerminalSocketPair[0]);
202 close (TerminalSocketPair[1]);
203 return (TERM_SOCK_FAILURE);
207 ** Queue an async IO to the terminal device
209 status = sys$qio (EFN$C_ENF,
216 sizeof (TerminalDeviceBuff) - 2,
220 LogMessage ("TerminalSocket: SYS$QIO () - %08X", status);
221 close (TerminalSocketPair[0]);
222 close (TerminalSocketPair[1]);
223 return (TERM_SOCK_FAILURE);
227 ** Return the input side of the socket pair
229 *ReturnSocket = TerminalSocketPair[1];
232 case TERM_SOCK_DELETE:
234 ** Cancel any pending IO on the terminal channel
236 status = sys$cancel (TerminalDeviceChan);
239 LogMessage ("TerminalSocket: SYS$CANCEL () - %08X", status);
240 close (TerminalSocketPair[0]);
241 close (TerminalSocketPair[1]);
242 return (TERM_SOCK_FAILURE);
246 ** Deassign the terminal channel
248 status = sys$dassgn (TerminalDeviceChan);
251 LogMessage ("TerminalSocket: SYS$DASSGN () - %08X", status);
252 close (TerminalSocketPair[0]);
253 close (TerminalSocketPair[1]);
254 return (TERM_SOCK_FAILURE);
258 ** Close the terminal socket pair
260 close (TerminalSocketPair[0]);
261 close (TerminalSocketPair[1]);
264 ** Return the initialized socket
271 ** Invalid function code
273 LogMessage ("TerminalSocket: Invalid Function Code - %d", FunctionCode);
274 return (TERM_SOCK_FAILURE);
281 return (TERM_SOCK_SUCCESS);
285 /*----------------------------------------------------------------------------*/
287 /*----------------------------------------------------------------------------*/
288 static int CreateSocketPair (
294 struct dsc$descriptor AscTimeDesc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, NULL};
295 static const char* LocalHostAddr = {"127.0.0.1"};
296 unsigned short TcpAcceptChan = 0,
298 unsigned long BinTimeBuff[2];
299 struct sockaddr_in sin;
300 char AscTimeBuff[32];
314 $DESCRIPTOR (TcpDeviceDesc, "TCPIP$DEVICE");
319 SockDesc1 = socket (SocketFamily, SocketType, 0);
322 LogMessage ("CreateSocketPair: socket () - %d", errno);
327 ** Initialize the socket information
330 memset ((char *) &sin, 0, slen);
331 sin.sin_family = SocketFamily;
332 sin.sin_addr.s_addr = inet_addr (LocalHostAddr);
336 ** Bind the socket to the local IP
338 status = bind (SockDesc1, (struct sockaddr *) &sin, slen);
341 LogMessage ("CreateSocketPair: bind () - %d", errno);
347 ** Get the socket name so we can save the port number
349 status = getsockname (SockDesc1, (struct sockaddr *) &sin, &slen);
352 LogMessage ("CreateSocketPair: getsockname () - %d", errno);
357 LocalHostPort = sin.sin_port;
360 ** Setup a listen for the socket
362 listen (SockDesc1, 5);
365 ** Get the binary (64-bit) time of the specified timeout value
367 sprintf (AscTimeBuff, "0 0:0:%02d.00", SOCKET_PAIR_TIMEOUT_VALUE);
368 AscTimeDesc.dsc$w_length = strlen (AscTimeBuff);
369 AscTimeDesc.dsc$a_pointer = AscTimeBuff;
370 status = sys$bintim (&AscTimeDesc, BinTimeBuff);
373 LogMessage ("CreateSocketPair: SYS$BINTIM () - %08X", status);
379 ** Assign another channel to the TCP/IP device for the accept.
380 ** This is the channel that ends up being connected to.
382 status = sys$assign (&TcpDeviceDesc, &TcpDeviceChan, 0, 0, 0);
385 LogMessage ("CreateSocketPair: SYS$ASSIGN () - %08X", status);
391 ** Get the channel of the first socket for the accept
393 TcpAcceptChan = decc$get_sdc (SockDesc1);
396 ** Perform the accept using $QIO so we can do this asynchronously
398 status = sys$qio (EFN$C_ENF,
400 IO$_ACCESS | IO$M_ACCEPT,
407 LogMessage ("CreateSocketPair: SYS$QIO () - %08X", status);
409 sys$dassgn (TcpDeviceChan);
414 ** Create the second socket to do the connect
416 SockDesc2 = socket (SocketFamily, SocketType, 0);
419 LogMessage ("CreateSocketPair: socket () - %d", errno);
420 sys$cancel (TcpAcceptChan);
422 sys$dassgn (TcpDeviceChan);
427 ** Setup the Socket Pair Timeout Block
429 sptb.SockChan1 = TcpAcceptChan;
430 sptb.SockChan2 = decc$get_sdc (SockDesc2);
433 ** Before we block on the connect, set a timer that can cancel I/O on our two
434 ** sockets if it never connects.
436 status = sys$setimr (EFN$C_ENF,
438 SocketPairTimeoutAst,
443 LogMessage ("CreateSocketPair: SYS$SETIMR () - %08X", status);
444 sys$cancel (TcpAcceptChan);
447 sys$dassgn (TcpDeviceChan);
452 ** Now issue the connect
454 memset ((char *) &sin, 0, sizeof (sin)) ;
455 sin.sin_family = SocketFamily;
456 sin.sin_addr.s_addr = inet_addr (LocalHostAddr) ;
457 sin.sin_port = LocalHostPort ;
459 status = connect (SockDesc2, (struct sockaddr *) &sin, sizeof (sin));
462 LogMessage ("CreateSocketPair: connect () - %d", errno);
463 sys$cantim (&sptb, 0);
464 sys$cancel (TcpAcceptChan);
467 sys$dassgn (TcpDeviceChan);
472 ** Wait for the asynch $QIO to finish. Note that if the I/O was aborted
473 ** (SS$_ABORT), then we probably canceled it from the AST routine - so log a
476 status = sys$synch (EFN$C_ENF, &iosb);
477 if (! (iosb.iosb$w_status & 1))
479 if (iosb.iosb$w_status == SS$_ABORT)
480 LogMessage ("CreateSocketPair: SYS$QIO(iosb) timeout");
483 LogMessage ("CreateSocketPair: SYS$QIO(iosb) - %d", iosb.iosb$w_status);
484 sys$cantim (&sptb, 0);
488 sys$dassgn (TcpDeviceChan);
493 ** Here we're successfully connected, so cancel the timer, convert the I/O
494 ** channel to a socket fd, close the listener socket and return the connected
497 sys$cantim (&sptb, 0);
500 SocketPair[0] = SockDesc2 ;
501 SocketPair[1] = socket_fd (TcpDeviceChan);
507 /*----------------------------------------------------------------------------*/
509 /*----------------------------------------------------------------------------*/
510 static void SocketPairTimeoutAst (int astparm)
512 SPTB *sptb = (SPTB *) astparm;
514 sys$cancel (sptb->SockChan2); /* Cancel the connect() */
515 sys$cancel (sptb->SockChan1); /* Cancel the accept() */
521 /*----------------------------------------------------------------------------*/
523 /*----------------------------------------------------------------------------*/
524 static int TerminalDeviceAst (int astparm)
529 ** Terminate the terminal buffer
531 TerminalDeviceBuff[TerminalDeviceIosb.iosb$w_bcnt] = '\0';
532 strcat (TerminalDeviceBuff, "\n");
535 ** Send the data read from the terminal device throught the socket pair
537 send (TerminalSocketPair[0], TerminalDeviceBuff, TerminalDeviceIosb.iosb$w_bcnt + 1, 0);
540 ** Queue another async IO to the terminal device
542 status = sys$qio (EFN$C_ENF,
549 sizeof (TerminalDeviceBuff) - 1,
559 /*----------------------------------------------------------------------------*/
561 /*----------------------------------------------------------------------------*/
562 static void LogMessage (char *msg, ...)
564 char *Month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
565 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
566 static unsigned int pid = 0;
573 ** Get the process pid
579 ** Convert the current time into local time
581 CurTime = time (NULL);
582 LocTime = localtime (&CurTime);
585 ** Format the message buffer
587 sprintf (MsgBuff, "%02d-%s-%04d %02d:%02d:%02d [%08X] %s\n",
588 LocTime->tm_mday, Month[LocTime->tm_mon], (LocTime->tm_year + 1900),
589 LocTime->tm_hour, LocTime->tm_min, LocTime->tm_sec, pid, msg);
592 ** Get any variable arguments and add them to the print of the message buffer
594 va_start (args, msg);
595 vfprintf (stderr, MsgBuff, args);
599 ** Flush standard error output
601 fsync (fileno (stderr));