From 9142e31a1baf77fd57bb13cb516e30c7e063a9d0 Mon Sep 17 00:00:00 2001 From: Michiel Broek Date: Sun, 1 Feb 2004 15:06:16 +0000 Subject: [PATCH] Added experimental incoming code --- mbcico/Makefile | 2 +- mbcico/session.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ mbcico/telnet.c | 9 ++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/mbcico/Makefile b/mbcico/Makefile index 270667d4..ec1e722e 100644 --- a/mbcico/Makefile +++ b/mbcico/Makefile @@ -135,7 +135,7 @@ scanout.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/reco emsi.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/dbnode.h ../lib/clcomm.h ../lib/mberrors.h ttyio.h session.h statetbl.h config.h emsi.h emsidat.h hydra.h rdoptions.h tcp.h wazoo.h inbound.h ulock.o: ../config.h ../lib/libs.h ../lib/clcomm.h callstat.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/clcomm.h ../lib/common.h callstat.h -session.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h ../lib/nodelist.h ../lib/mberrors.h ttyio.h statetbl.h emsi.h ftsc.h session.h yoohoo.h mbcico.h binkp.h callstat.h inbound.h opentcp.h +session.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h ../lib/nodelist.h ../lib/mberrors.h ttyio.h statetbl.h emsi.h ftsc.h session.h yoohoo.h mbcico.h binkp.h callstat.h inbound.h opentcp.h telnet.h call.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/dbnode.h ../lib/mberrors.h session.h callstat.h call.h config.h dial.h lutil.h portsel.h openport.h opentcp.h rdoptions.h inbound.h mbcico.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/dbcfg.h ../lib/dbnode.h ../lib/dbftn.h ../lib/mberrors.h config.h answer.h call.h lutil.h mbcico.h session.h outstat.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/dbcfg.h ../lib/dbnode.h ../lib/dbftn.h ../lib/mberrors.h scanout.h callstat.h outstat.h diff --git a/mbcico/session.c b/mbcico/session.c index e84c5089..f635b5ab 100644 --- a/mbcico/session.c +++ b/mbcico/session.c @@ -48,6 +48,7 @@ #include "callstat.h" #include "inbound.h" #include "opentcp.h" +#include "telnet.h" extern int tcp_mode; @@ -86,6 +87,10 @@ int session(faddr *a, node *nl, int role, int tp, char *dt) { int rc = MBERR_OK, addrlen = sizeof(struct sockaddr_in); fa_list *tmpl; +#ifdef USE_EXPERIMENT + int Fdo, input_pipe[2], output_pipe[2]; + pid_t fpid; +#endif session_flags = 0; type = tp; @@ -113,6 +118,77 @@ int session(faddr *a, node *nl, int role, int tp, char *dt) #ifdef USE_EXPERIMENT if ((tcp_mode == TCPMODE_ITN) && (role == 0)) { Syslog('-', "Will need to install telnet receiver thread"); + + /* + * First make sure the current input socket gets a new file descriptor + * since it's now on stadin and stdout. + */ + Fdo = dup(0); + Syslog('s', "session: new socket %s", Fdo); + + /* + * Close stdin and stdout so that when we create the pipes to + * the telnet filter they get stdin and stdout as file descriptors. + */ + fflush(stdin); + fflush(stdout); + setbuf(stdin,NULL); + setbuf(stdout, NULL); + close(0); + close(1); + + /* + * Create output pipe and start output filter. + */ + if (pipe(output_pipe) == -1) { + WriteError("$could not create output_pipe"); + die(MBERR_TTYIO_ERROR); + } + fpid = fork(); + switch (fpid) { + case -1: WriteError("fork for telout_filter failed"); + die(MBERR_TTYIO_ERROR); + case 0: if (close(output_pipe[1]) == -1) { + WriteError("$error close output_pipe[1]"); + die(MBERR_TTYIO_ERROR); + } + telout_filter(output_pipe[0], Fdo); + /* NOT REACHED */ + } + if (close(output_pipe[0] == -1)) { + WriteError("$error close output_pipe[0]"); + die(MBERR_TTYIO_ERROR); + } + Syslog('s', "telout_filter forked with pid %d", fpid); + + /* + * Create input pipe and start input filter + */ + if (pipe(input_pipe) == -1) { + WriteError("$could not create input_pipe"); + die(MBERR_TTYIO_ERROR); + } + fpid = fork(); + switch (fpid) { + case -1: WriteError("fork for telin_filter failed"); + die(MBERR_TTYIO_ERROR); + case 0: if (close(input_pipe[0]) == -1) { + WriteError("$error close input_pipe[0]"); + die(MBERR_TTYIO_ERROR); + } + telin_filter(input_pipe[1], Fdo); + /* NOT REACHED */ + } + if (close(input_pipe[1]) == -1) { + WriteError("$error close input_pipe[1]"); + die(MBERR_TTYIO_ERROR); + } + Syslog('s', "telin_filter forked with pid %d", fpid); + + Syslog('s', "stdout = %d", output_pipe[1]); + Syslog('s', "stdin = %d", input_pipe[0]); + + telnet_init(Fdo); } #endif } diff --git a/mbcico/telnet.c b/mbcico/telnet.c index 8a83c0d7..8828a038 100644 --- a/mbcico/telnet.c +++ b/mbcico/telnet.c @@ -40,11 +40,17 @@ #include "telnet.h" +/* + * Telnet I/O filters. See RFC854 for details. + */ #ifdef USE_EXPERIMENT +/* + * Send options to the remote. + */ void telnet_init(int Fd) { Syslog('s', "telnet_init(%d)", Fd); @@ -58,6 +64,9 @@ void telnet_init(int Fd) +/* + * Answer options requested by remote. + */ void telnet_answer(int tag, int opt, int Fd) { char buf[3];