89 lines
1.9 KiB
C
89 lines
1.9 KiB
C
/* Copyright (C) 2010 Tony Garnock-Jones. All rights reserved. */
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/uio.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <fcntl.h>
|
|
#include <sys/wait.h>
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
|
|
typedef unsigned char u_char;
|
|
#include <event.h>
|
|
|
|
#include "cmsg_private.h"
|
|
#include "harness.h"
|
|
#include "relay.h"
|
|
#include "net.h"
|
|
#include "ref.h"
|
|
#include "sexp.h"
|
|
#include "sexpio.h"
|
|
|
|
struct boot_args {
|
|
struct sockaddr_in peername;
|
|
int fd;
|
|
};
|
|
|
|
static void relay_main(struct boot_args *args) {
|
|
IOHandle *h = new_iohandle(args->fd);
|
|
IOHandle *out = new_iohandle(1);
|
|
|
|
{
|
|
char name[256];
|
|
endpoint_name(&args->peername, CMSG_BYTES(sizeof(name), name));
|
|
info("Accepted connection from %s on fd %d\n", name, args->fd);
|
|
}
|
|
|
|
free(args);
|
|
|
|
iohandle_write(h, cmsg_cstring_bytes("Hi\n"));
|
|
ICHECK(iohandle_flush(h), "iohandle_flush 1");
|
|
nap(1000);
|
|
iohandle_write(h, cmsg_cstring_bytes("Proceed\n"));
|
|
//iohandle_settimeout(h, 3, 0);
|
|
|
|
loop:
|
|
{
|
|
sexp_t *x = sexp_read(h);
|
|
switch (h->error_kind) {
|
|
case 0:
|
|
fflush(NULL);
|
|
sexp_write(out, x);
|
|
iohandle_write(out, cmsg_cstring_bytes("\n"));
|
|
ICHECK(iohandle_flush(out), "iohandle_flush out");
|
|
DECREF(x, sexp_destructor);
|
|
iohandle_write(h, cmsg_cstring_bytes("OK, proceed\n"));
|
|
goto loop;
|
|
|
|
case EVBUFFER_TIMEOUT:
|
|
info("Timeout\n");
|
|
iohandle_clear_error(h);
|
|
iohandle_write(h, cmsg_cstring_bytes("Timed out\n"));
|
|
ICHECK(iohandle_flush(h), "iohandle_flush 2");
|
|
break;
|
|
|
|
default:
|
|
info("Error! 0x%04X\n", h->error_kind);
|
|
break;
|
|
}
|
|
}
|
|
|
|
ICHECK(close(h->fd), "close");
|
|
delete_iohandle(h);
|
|
delete_iohandle(out);
|
|
}
|
|
|
|
void start_relay(struct sockaddr_in const *peername, int fd) {
|
|
struct boot_args *args = malloc(sizeof(*args));
|
|
args->peername = *peername;
|
|
args->fd = fd;
|
|
spawn((process_main_t) relay_main, args);
|
|
}
|