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
|
/* ``The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved via the world wide web at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
*
* $Id$
*/
/*
* Purpose: Std filedescriptors, break handler
*
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef __WIN32__
#include "esock_winsock.h"
#include <process.h>
#include <io.h>
#include <fcntl.h>
#else
#include <unistd.h>
#include <signal.h>
#endif
#include "esock.h"
#include "debuglog.h"
#include "esock_utils.h"
#include "esock_osio.h"
#ifdef __WIN32__
#define write _write
#define read _read
#define LOCALHOSTADDR "127.0.0.1"
#define LOCBUFSIZE 1024
#endif
#define PACKET_SIZE 4
#define EBUFSIZE 256
FD local_read_fd = 0;
static int inc_rbuf(int size);
static void free_rbuf(void);
static int read_fill(unsigned char *buf, int len);
#ifdef __WIN32__
static int create_local_thread(void);
static DWORD WINAPI local_thread(LPVOID lpvParam);
static BOOL WINAPI signal_handler(DWORD ctrl);
#endif
static unsigned char *rbuf = NULL;
static int rbuf_malloced = 0;
#ifdef __WIN32__
static unsigned long one = 1, zero = 0;
static int local_portno;
static char *local_buf;
#endif
int set_break_handler(void)
{
#ifndef __WIN32__
struct sigaction act;
/* Ignore SIGPIPE signal */
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, NULL);
return 0;
#else
SetConsoleCtrlHandler(signal_handler, TRUE);
return 0;
#endif
}
#ifdef __WIN32__
int set_binary_mode(void)
{
_setmode(0, _O_BINARY);
_setmode(1, _O_BINARY);
return 0;
}
int esock_osio_init(void)
{
return create_local_thread();
}
void esock_osio_finish(void)
{
sock_close(local_read_fd);
}
#endif
int read_ctrl(unsigned char **ebufp)
{
int tbh, cc;
unsigned char *mbuf;
if (inc_rbuf(EBUFSIZE) < 0) {
fprintf(stderr, "read_ctrl: cannot alloc rbuf\n");
return -1;
}
cc = read_fill(rbuf, PACKET_SIZE);
if (cc < 0) {
free_rbuf();
return -1;
}
if (cc == 0) {
free_rbuf();
return -1; /* XXX 0 ?? */
}
tbh = GET_INT32(rbuf);
if (tbh > rbuf_malloced - 4) {
if (inc_rbuf(tbh + 4) < 0)
return -1;
}
mbuf = rbuf + PACKET_SIZE;
cc = read_fill(mbuf, tbh);
DEBUGF(("-----------------------------------\n"));
DEBUGF(("read_ctrl: cc = %d\n", cc));
if(cc > 0) {
DEBUGMSGF(("message (hex) : [%3.*a]\n", cc, mbuf));
DEBUGMSGF(("message (char): [%3.*b]\n", cc, mbuf));
}
*ebufp = mbuf;
return cc;
}
int write_ctrl(unsigned char *buf, int len)
{
unsigned char lb[4];
PUT_INT32(len, lb);
DEBUGF(("write_ctrl: len = %d\n", len));
DEBUGMSGF(("message (hex) : [%3.*a] [%3.*a]\n", PACKET_SIZE, lb,
len, buf));
DEBUGMSGF(("message (char): [%3.*b] [%3.*b]\n", PACKET_SIZE, lb,
len, buf));
if (write(1, lb, PACKET_SIZE) != PACKET_SIZE) { /* XXX */
fprintf(stderr, "write_ctrl: Bad write \n");
return -1;
}
if (write(1, buf, len) != len) { /* XXX */
fprintf(stderr, "write_ctrl: Bad write \n");
return -1;
}
return len;
}
/*
* Local functions
*
*/
static int inc_rbuf(int size)
{
unsigned char *nbuf;
if (rbuf_malloced >= size)
return 0;
if (rbuf != NULL)
nbuf = esock_realloc(rbuf, size);
else
nbuf = esock_malloc(size);
if(nbuf != NULL) {
rbuf = nbuf;
rbuf_malloced = size;
return 0;
}
return -1;
}
static void free_rbuf(void)
{
if (rbuf != NULL) {
esock_free(rbuf);
rbuf = NULL;
rbuf_malloced = 0;
}
}
/* Fill buffer, return buffer length, 0 for EOF, < 0 for error. */
static int read_fill(unsigned char *buf, int len)
{
int i, got = 0;
do {
if ((i = sock_read(local_read_fd, buf+got, len-got)) <= 0)
return i;
got += i;
} while (got < len);
return len;
}
#ifdef __WIN32__
/*
* This routine creates a local thread, which reads from standard input
* and writes to a socket.
*/
static int create_local_thread(void)
{
struct sockaddr_in iserv_addr;
SOCKET tmpsock;
int length;
unsigned threadaddr;
local_buf = esock_malloc(LOCBUFSIZE);
if ((tmpsock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
fprintf(stderr, "create_local_thread could not create socket.\n");
return -1;
}
memset(&iserv_addr, 0, sizeof(iserv_addr));
iserv_addr.sin_family = AF_INET;
iserv_addr.sin_addr.s_addr = inet_addr(LOCALHOSTADDR);
iserv_addr.sin_port = htons(0); /* Have any port */
if (bind(tmpsock, (struct sockaddr *) &iserv_addr,
sizeof(iserv_addr)) < 0) {
fprintf(stderr, "create_local_thread could not bind.\n");
closesocket(tmpsock);
return -1;
}
listen(tmpsock, 1);
length = sizeof(iserv_addr);
if (getsockname(tmpsock, (struct sockaddr *) &iserv_addr, &length) < 0) {
fprintf(stderr, "create_local_thread could not getsockname.\n");
closesocket(tmpsock);
return -1;
}
local_portno = ntohs(iserv_addr.sin_port);
if (_beginthreadex(NULL, 0, local_thread, NULL, 0, &threadaddr) == 0) {
fprintf(stderr, "create_local_thread could not _beginthreadex().\n");
closesocket(tmpsock);
return -1;
}
local_read_fd = accept(tmpsock, (struct sockaddr *) NULL, (int *) NULL);
if (local_read_fd == INVALID_FD) {
fprintf(stderr, "create_local_thread could not accept.\n");
closesocket(tmpsock);
return -1;
}
closesocket(tmpsock);
return 0;
}
static DWORD WINAPI local_thread(LPVOID lpvParam)
{
SOCKET sock;
struct hostent *host;
char hostname[64];
struct sockaddr_in iserv_addr;
unsigned long addr;
int len;
HANDLE thread;
sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&iserv_addr, 0, sizeof(struct sockaddr_in));
iserv_addr.sin_family = AF_INET;
iserv_addr.sin_addr.s_addr = inet_addr(LOCALHOSTADDR);
iserv_addr.sin_port = htons(local_portno);
if(connect(sock, (struct sockaddr*)&iserv_addr, sizeof iserv_addr) ==
SOCKET_ERROR) {
fprintf(stderr, "local_thread thread could not connect\n");
closesocket(sock);
return 0;
}
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
/* read from 0 and write to sock */
while (1) {
if ((len = read(0, local_buf, LOCBUFSIZE)) <= 0) {
closesocket(sock);
close(0);
return 0;
}
if (send(sock, local_buf, len, 0) != len ) {
closesocket(sock);
close(0);
return 0;
}
}
return 0;
}
/* Signal handler */
static BOOL WINAPI signal_handler(DWORD ctrl)
{
switch (ctrl) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
break;
case CTRL_LOGOFF_EVENT:
if (!getenv("ERLSRV_SERVICE_NAME"))
return FALSE;
break;
default:
exit(1);
}
return TRUE;
}
#endif
|