Richard Levitte | 0d0723e | 2016-09-14 20:52:03 +0200 | [diff] [blame] | 1 | /* |
Rich Salz | 624265c | 2017-06-15 12:03:40 -0400 | [diff] [blame] | 2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. |
Richard Levitte | 0d0723e | 2016-09-14 20:52:03 +0200 | [diff] [blame] | 3 | * Copyright 2016 VMS Software, Inc. All Rights Reserved. |
| 4 | * |
| 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 |
| 9 | */ |
| 10 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 11 | #ifdef __VMS |
| 12 | # define OPENSSL_SYS_VMS |
| 13 | # pragma message disable DOLLARID |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 14 | |
| 15 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 16 | # include <openssl/opensslconf.h> |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 17 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 18 | # if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS) |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 19 | /* |
| 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. |
| 22 | */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 23 | # define _POSIX_C_SOURCE 2 |
| 24 | # endif |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 25 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 26 | # include <stdio.h> |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 27 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 28 | # undef _POSIX_C_SOURCE |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 29 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 30 | # include <sys/types.h> |
| 31 | # include <sys/socket.h> |
| 32 | # include <netinet/in.h> |
| 33 | # include <inet.h> |
| 34 | # include <unistd.h> |
| 35 | # include <string.h> |
| 36 | # include <errno.h> |
| 37 | # include <starlet.h> |
| 38 | # include <iodef.h> |
| 39 | # ifdef __alpha |
| 40 | # include <iosbdef.h> |
| 41 | # else |
| 42 | typedef struct _iosb { /* Copied from IOSBDEF.H for Alpha */ |
| 43 | # pragma __nomember_alignment |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 44 | __union { |
| 45 | __struct { |
| 46 | unsigned short int iosb$w_status; /* Final I/O status */ |
| 47 | __union { |
| 48 | __struct { /* 16-bit byte count variant */ |
| 49 | unsigned short int iosb$w_bcnt; /* 16-bit byte count */ |
| 50 | __union { |
| 51 | unsigned int iosb$l_dev_depend; /* 32-bit device dependent info */ |
| 52 | unsigned int iosb$l_pid; /* 32-bit pid */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 53 | } iosb$r_l; |
| 54 | } iosb$r_bcnt_16; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 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 */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 58 | } iosb$r_bcnt_32; |
| 59 | } iosb$r_devdepend; |
| 60 | } iosb$r_io_64; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 61 | __struct { |
| 62 | __union { |
| 63 | unsigned int iosb$l_getxxi_status; /* Final GETxxI status */ |
| 64 | unsigned int iosb$l_reg_status; /* Final $Registry status */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 65 | } iosb$r_l_status; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 66 | unsigned int iosb$l_reserved; /* Reserved field */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 67 | } iosb$r_get_64; |
| 68 | } iosb$r_io_get; |
| 69 | } IOSB; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 70 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 71 | # if !defined(__VAXC) |
| 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) */ |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 82 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 83 | # endif /* End of IOSBDEF */ |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 84 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 85 | # include <efndef.h> |
| 86 | # include <stdlib.h> |
| 87 | # include <ssdef.h> |
| 88 | # include <time.h> |
| 89 | # include <stdarg.h> |
| 90 | # include <descrip.h> |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 91 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 92 | # include "vms_term_sock.h" |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 93 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 94 | # ifdef __alpha |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 95 | static struct _iosb TerminalDeviceIosb; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 96 | # else |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 97 | IOSB TerminalDeviceIosb; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 98 | # endif |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 99 | |
| 100 | static char TerminalDeviceBuff[255 + 2]; |
| 101 | static int TerminalSocketPair[2] = {0, 0}; |
| 102 | static unsigned short TerminalDeviceChan = 0; |
| 103 | |
| 104 | static int CreateSocketPair (int, int, int, int *); |
| 105 | static void SocketPairTimeoutAst (int); |
| 106 | static int TerminalDeviceAst (int); |
| 107 | static void LogMessage (char *, ...); |
| 108 | |
| 109 | /* |
| 110 | ** Socket Pair Timeout Value (must be 0-59 seconds) |
| 111 | */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 112 | # define SOCKET_PAIR_TIMEOUT_VALUE 20 |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | ** Socket Pair Timeout Block which is passed to timeout AST |
| 116 | */ |
| 117 | typedef struct _SocketPairTimeoutBlock { |
| 118 | unsigned short SockChan1; |
| 119 | unsigned short SockChan2; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 120 | } SPTB; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 121 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 122 | # ifdef TERM_SOCK_TEST |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 123 | |
| 124 | /*----------------------------------------------------------------------------*/ |
| 125 | /* */ |
| 126 | /*----------------------------------------------------------------------------*/ |
| 127 | int main (int argc, char *argv[], char *envp[]) |
| 128 | { |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 129 | char TermBuff[80]; |
| 130 | int TermSock, |
| 131 | status, |
| 132 | len; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 133 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 134 | LogMessage ("Enter 'q' or 'Q' to quit ..."); |
| 135 | while (strcasecmp (TermBuff, "Q")) { |
| 136 | /* |
| 137 | ** Create the terminal socket |
| 138 | */ |
| 139 | status = TerminalSocket (TERM_SOCK_CREATE, &TermSock); |
| 140 | if (status != TERM_SOCK_SUCCESS) |
| 141 | exit (1); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 142 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 143 | /* |
| 144 | ** Process the terminal input |
| 145 | */ |
| 146 | LogMessage ("Waiting on terminal I/O ...\n"); |
Rich Salz | cbe2964 | 2017-12-07 13:39:34 -0500 | [diff] [blame] | 147 | len = recv (TermSock, TermBuff, sizeof(TermBuff), 0) ; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 148 | TermBuff[len] = '\0'; |
| 149 | LogMessage ("Received terminal I/O [%s]", TermBuff); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 150 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 151 | /* |
| 152 | ** Delete the terminal socket |
| 153 | */ |
| 154 | status = TerminalSocket (TERM_SOCK_DELETE, &TermSock); |
| 155 | if (status != TERM_SOCK_SUCCESS) |
| 156 | exit (1); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 159 | return 1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 160 | |
| 161 | } |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 162 | # endif |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 163 | |
| 164 | /*----------------------------------------------------------------------------*/ |
| 165 | /* */ |
| 166 | /*----------------------------------------------------------------------------*/ |
| 167 | int TerminalSocket (int FunctionCode, int *ReturnSocket) |
| 168 | { |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 169 | int status; |
| 170 | $DESCRIPTOR (TerminalDeviceDesc, "SYS$COMMAND"); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 171 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 172 | /* |
| 173 | ** Process the requested function code |
| 174 | */ |
| 175 | switch (FunctionCode) { |
| 176 | case TERM_SOCK_CREATE: |
| 177 | /* |
| 178 | ** Create a socket pair |
| 179 | */ |
| 180 | status = CreateSocketPair (AF_INET, SOCK_STREAM, 0, TerminalSocketPair); |
| 181 | if (status == -1) { |
| 182 | LogMessage ("TerminalSocket: CreateSocketPair () - %08X", status); |
| 183 | if (TerminalSocketPair[0]) |
| 184 | close (TerminalSocketPair[0]); |
| 185 | if (TerminalSocketPair[1]) |
| 186 | close (TerminalSocketPair[1]); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 187 | return TERM_SOCK_FAILURE; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 188 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 189 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 190 | /* |
| 191 | ** Assign a channel to the terminal device |
| 192 | */ |
| 193 | status = sys$assign (&TerminalDeviceDesc, |
| 194 | &TerminalDeviceChan, |
| 195 | 0, 0, 0); |
| 196 | if (! (status & 1)) { |
| 197 | LogMessage ("TerminalSocket: SYS$ASSIGN () - %08X", status); |
| 198 | close (TerminalSocketPair[0]); |
| 199 | close (TerminalSocketPair[1]); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 200 | return TERM_SOCK_FAILURE; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 201 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 202 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 203 | /* |
| 204 | ** Queue an async IO to the terminal device |
| 205 | */ |
| 206 | status = sys$qio (EFN$C_ENF, |
| 207 | TerminalDeviceChan, |
| 208 | IO$_READVBLK, |
| 209 | &TerminalDeviceIosb, |
| 210 | TerminalDeviceAst, |
| 211 | 0, |
| 212 | TerminalDeviceBuff, |
Rich Salz | cbe2964 | 2017-12-07 13:39:34 -0500 | [diff] [blame] | 213 | sizeof(TerminalDeviceBuff) - 2, |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 214 | 0, 0, 0, 0); |
| 215 | if (! (status & 1)) { |
| 216 | LogMessage ("TerminalSocket: SYS$QIO () - %08X", status); |
| 217 | close (TerminalSocketPair[0]); |
| 218 | close (TerminalSocketPair[1]); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 219 | return TERM_SOCK_FAILURE; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 220 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 221 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 222 | /* |
| 223 | ** Return the input side of the socket pair |
| 224 | */ |
| 225 | *ReturnSocket = TerminalSocketPair[1]; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 226 | break; |
| 227 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 228 | case TERM_SOCK_DELETE: |
| 229 | /* |
| 230 | ** Cancel any pending IO on the terminal channel |
| 231 | */ |
| 232 | status = sys$cancel (TerminalDeviceChan); |
| 233 | if (! (status & 1)) { |
| 234 | LogMessage ("TerminalSocket: SYS$CANCEL () - %08X", status); |
| 235 | close (TerminalSocketPair[0]); |
| 236 | close (TerminalSocketPair[1]); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 237 | return TERM_SOCK_FAILURE; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 238 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 239 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 240 | /* |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 241 | ** Deassign the terminal channel |
| 242 | */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 243 | status = sys$dassgn (TerminalDeviceChan); |
| 244 | if (! (status & 1)) { |
| 245 | LogMessage ("TerminalSocket: SYS$DASSGN () - %08X", status); |
| 246 | close (TerminalSocketPair[0]); |
| 247 | close (TerminalSocketPair[1]); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 248 | return TERM_SOCK_FAILURE; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 249 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 250 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 251 | /* |
| 252 | ** Close the terminal socket pair |
| 253 | */ |
| 254 | close (TerminalSocketPair[0]); |
| 255 | close (TerminalSocketPair[1]); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 256 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 257 | /* |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 258 | ** Return the initialized socket |
| 259 | */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 260 | *ReturnSocket = 0; |
| 261 | break; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 262 | |
| 263 | default: |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 264 | /* |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 265 | ** Invalid function code |
| 266 | */ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 267 | LogMessage ("TerminalSocket: Invalid Function Code - %d", FunctionCode); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 268 | return TERM_SOCK_FAILURE; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 269 | break; |
| 270 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 271 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 272 | /* |
| 273 | ** Return success |
| 274 | */ |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 275 | return TERM_SOCK_SUCCESS; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 276 | |
| 277 | } |
| 278 | |
| 279 | /*----------------------------------------------------------------------------*/ |
| 280 | /* */ |
| 281 | /*----------------------------------------------------------------------------*/ |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 282 | static int CreateSocketPair (int SocketFamily, |
| 283 | int SocketType, |
| 284 | int SocketProtocol, |
| 285 | int *SocketPair) |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 286 | { |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 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, |
| 290 | TcpDeviceChan = 0; |
| 291 | unsigned long BinTimeBuff[2]; |
| 292 | struct sockaddr_in sin; |
| 293 | char AscTimeBuff[32]; |
| 294 | short LocalHostPort; |
| 295 | int status; |
| 296 | unsigned int slen; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 297 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 298 | # ifdef __alpha |
| 299 | struct _iosb iosb; |
| 300 | # else |
| 301 | IOSB iosb; |
| 302 | # endif |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 303 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 304 | int SockDesc1 = 0, |
| 305 | SockDesc2 = 0; |
| 306 | SPTB sptb; |
| 307 | $DESCRIPTOR (TcpDeviceDesc, "TCPIP$DEVICE"); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 308 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 309 | /* |
| 310 | ** Create a socket |
| 311 | */ |
| 312 | SockDesc1 = socket (SocketFamily, SocketType, 0); |
| 313 | if (SockDesc1 < 0) { |
| 314 | LogMessage ("CreateSocketPair: socket () - %d", errno); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 315 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 318 | /* |
| 319 | ** Initialize the socket information |
| 320 | */ |
Rich Salz | cbe2964 | 2017-12-07 13:39:34 -0500 | [diff] [blame] | 321 | slen = sizeof(sin); |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 322 | memset ((char *) &sin, 0, slen); |
| 323 | sin.sin_family = SocketFamily; |
| 324 | sin.sin_addr.s_addr = inet_addr (LocalHostAddr); |
| 325 | sin.sin_port = 0; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 326 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 327 | /* |
| 328 | ** Bind the socket to the local IP |
| 329 | */ |
| 330 | status = bind (SockDesc1, (struct sockaddr *) &sin, slen); |
| 331 | if (status < 0) { |
| 332 | LogMessage ("CreateSocketPair: bind () - %d", errno); |
| 333 | close (SockDesc1); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 334 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 337 | /* |
| 338 | ** Get the socket name so we can save the port number |
| 339 | */ |
| 340 | status = getsockname (SockDesc1, (struct sockaddr *) &sin, &slen); |
| 341 | if (status < 0) { |
| 342 | LogMessage ("CreateSocketPair: getsockname () - %d", errno); |
| 343 | close (SockDesc1); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 344 | return -1; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 345 | } else |
| 346 | LocalHostPort = sin.sin_port; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 347 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 348 | /* |
| 349 | ** Setup a listen for the socket |
| 350 | */ |
| 351 | listen (SockDesc1, 5); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 352 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 353 | /* |
| 354 | ** Get the binary (64-bit) time of the specified timeout value |
| 355 | */ |
| 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); |
| 362 | close (SockDesc1); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 363 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 366 | /* |
| 367 | ** Assign another channel to the TCP/IP device for the accept. |
| 368 | ** This is the channel that ends up being connected to. |
| 369 | */ |
| 370 | status = sys$assign (&TcpDeviceDesc, &TcpDeviceChan, 0, 0, 0); |
| 371 | if (! (status & 1)) { |
| 372 | LogMessage ("CreateSocketPair: SYS$ASSIGN () - %08X", status); |
| 373 | close (SockDesc1); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 374 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 377 | /* |
| 378 | ** Get the channel of the first socket for the accept |
| 379 | */ |
| 380 | TcpAcceptChan = decc$get_sdc (SockDesc1); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 381 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 382 | /* |
| 383 | ** Perform the accept using $QIO so we can do this asynchronously |
| 384 | */ |
| 385 | status = sys$qio (EFN$C_ENF, |
| 386 | TcpAcceptChan, |
| 387 | IO$_ACCESS | IO$M_ACCEPT, |
| 388 | &iosb, |
| 389 | 0, 0, 0, 0, 0, |
| 390 | &TcpDeviceChan, |
| 391 | 0, 0); |
| 392 | if (! (status & 1)) { |
| 393 | LogMessage ("CreateSocketPair: SYS$QIO () - %08X", status); |
| 394 | close (SockDesc1); |
| 395 | sys$dassgn (TcpDeviceChan); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 396 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 399 | /* |
| 400 | ** Create the second socket to do the connect |
| 401 | */ |
| 402 | SockDesc2 = socket (SocketFamily, SocketType, 0); |
| 403 | if (SockDesc2 < 0) { |
| 404 | LogMessage ("CreateSocketPair: socket () - %d", errno); |
| 405 | sys$cancel (TcpAcceptChan); |
| 406 | close (SockDesc1); |
| 407 | sys$dassgn (TcpDeviceChan); |
| 408 | return (-1) ; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 411 | /* |
| 412 | ** Setup the Socket Pair Timeout Block |
| 413 | */ |
| 414 | sptb.SockChan1 = TcpAcceptChan; |
| 415 | sptb.SockChan2 = decc$get_sdc (SockDesc2); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 416 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 417 | /* |
| 418 | ** Before we block on the connect, set a timer that can cancel I/O on our |
| 419 | ** two sockets if it never connects. |
| 420 | */ |
| 421 | status = sys$setimr (EFN$C_ENF, |
| 422 | BinTimeBuff, |
| 423 | SocketPairTimeoutAst, |
| 424 | &sptb, |
| 425 | 0); |
| 426 | if (! (status & 1)) { |
| 427 | LogMessage ("CreateSocketPair: SYS$SETIMR () - %08X", status); |
| 428 | sys$cancel (TcpAcceptChan); |
| 429 | close (SockDesc1); |
| 430 | close (SockDesc2); |
| 431 | sys$dassgn (TcpDeviceChan); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 432 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 435 | /* |
| 436 | ** Now issue the connect |
| 437 | */ |
Rich Salz | cbe2964 | 2017-12-07 13:39:34 -0500 | [diff] [blame] | 438 | memset ((char *) &sin, 0, sizeof(sin)) ; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 439 | sin.sin_family = SocketFamily; |
| 440 | sin.sin_addr.s_addr = inet_addr (LocalHostAddr) ; |
| 441 | sin.sin_port = LocalHostPort ; |
| 442 | |
Rich Salz | cbe2964 | 2017-12-07 13:39:34 -0500 | [diff] [blame] | 443 | status = connect (SockDesc2, (struct sockaddr *) &sin, sizeof(sin)); |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 444 | if (status < 0 ) { |
| 445 | LogMessage ("CreateSocketPair: connect () - %d", errno); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 446 | sys$cantim (&sptb, 0); |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 447 | sys$cancel (TcpAcceptChan); |
| 448 | close (SockDesc1); |
| 449 | close (SockDesc2); |
| 450 | sys$dassgn (TcpDeviceChan); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 451 | return -1; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 454 | /* |
| 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 |
| 457 | ** a timeout. |
| 458 | */ |
| 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"); |
| 463 | else { |
| 464 | LogMessage ("CreateSocketPair: SYS$QIO(iosb) - %d", |
| 465 | iosb.iosb$w_status); |
| 466 | sys$cantim (&sptb, 0); |
| 467 | } |
| 468 | close (SockDesc1); |
| 469 | close (SockDesc2); |
| 470 | sys$dassgn (TcpDeviceChan); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 471 | return -1; |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 472 | } |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 473 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 474 | /* |
| 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 |
| 477 | ** connected pair. |
| 478 | */ |
| 479 | sys$cantim (&sptb, 0); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 480 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 481 | close (SockDesc1) ; |
| 482 | SocketPair[0] = SockDesc2 ; |
| 483 | SocketPair[1] = socket_fd (TcpDeviceChan); |
| 484 | |
| 485 | return (0) ; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 486 | |
| 487 | } |
| 488 | |
| 489 | /*----------------------------------------------------------------------------*/ |
| 490 | /* */ |
| 491 | /*----------------------------------------------------------------------------*/ |
| 492 | static void SocketPairTimeoutAst (int astparm) |
| 493 | { |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 494 | SPTB *sptb = (SPTB *) astparm; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 495 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 496 | sys$cancel (sptb->SockChan2); /* Cancel the connect() */ |
| 497 | sys$cancel (sptb->SockChan1); /* Cancel the accept() */ |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 498 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 499 | return; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 500 | |
| 501 | } |
| 502 | |
| 503 | /*----------------------------------------------------------------------------*/ |
| 504 | /* */ |
| 505 | /*----------------------------------------------------------------------------*/ |
| 506 | static int TerminalDeviceAst (int astparm) |
| 507 | { |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 508 | int status; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 509 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 510 | /* |
| 511 | ** Terminate the terminal buffer |
| 512 | */ |
| 513 | TerminalDeviceBuff[TerminalDeviceIosb.iosb$w_bcnt] = '\0'; |
| 514 | strcat (TerminalDeviceBuff, "\n"); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 515 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 516 | /* |
Josh Soref | 46f4e1b | 2017-11-11 19:03:10 -0500 | [diff] [blame] | 517 | ** Send the data read from the terminal device through the socket pair |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 518 | */ |
| 519 | send (TerminalSocketPair[0], TerminalDeviceBuff, |
| 520 | TerminalDeviceIosb.iosb$w_bcnt + 1, 0); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 521 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 522 | /* |
| 523 | ** Queue another async IO to the terminal device |
| 524 | */ |
| 525 | status = sys$qio (EFN$C_ENF, |
| 526 | TerminalDeviceChan, |
| 527 | IO$_READVBLK, |
| 528 | &TerminalDeviceIosb, |
| 529 | TerminalDeviceAst, |
| 530 | 0, |
| 531 | TerminalDeviceBuff, |
Rich Salz | cbe2964 | 2017-12-07 13:39:34 -0500 | [diff] [blame] | 532 | sizeof(TerminalDeviceBuff) - 1, |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 533 | 0, 0, 0, 0); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 534 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 535 | /* |
| 536 | ** Return status |
| 537 | */ |
| 538 | return status; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 539 | |
| 540 | } |
| 541 | |
| 542 | /*----------------------------------------------------------------------------*/ |
| 543 | /* */ |
| 544 | /*----------------------------------------------------------------------------*/ |
| 545 | static void LogMessage (char *msg, ...) |
| 546 | { |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 547 | char *Month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 548 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
| 549 | static unsigned int pid = 0; |
| 550 | va_list args; |
| 551 | time_t CurTime; |
| 552 | struct tm *LocTime; |
| 553 | char MsgBuff[256]; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 554 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 555 | /* |
| 556 | ** Get the process pid |
| 557 | */ |
| 558 | if (pid == 0) |
| 559 | pid = getpid (); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 560 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 561 | /* |
| 562 | ** Convert the current time into local time |
| 563 | */ |
| 564 | CurTime = time (NULL); |
| 565 | LocTime = localtime (&CurTime); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 566 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 567 | /* |
| 568 | ** Format the message buffer |
| 569 | */ |
| 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); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 574 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 575 | /* |
| 576 | ** Get any variable arguments and add them to the print of the message |
| 577 | ** buffer |
| 578 | */ |
| 579 | va_start (args, msg); |
| 580 | vfprintf (stderr, MsgBuff, args); |
| 581 | va_end (args); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 582 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 583 | /* |
| 584 | ** Flush standard error output |
| 585 | */ |
| 586 | fsync (fileno (stderr)); |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 587 | |
Richard Levitte | c7bdb6a | 2016-09-14 20:54:30 +0200 | [diff] [blame] | 588 | return; |
Richard Levitte | 0d3b658 | 2016-08-03 21:16:43 +0200 | [diff] [blame] | 589 | |
| 590 | } |
| 591 | #endif |