home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / network / telnet.lzh / telnet.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-22  |  7.5 KB  |  310 lines

  1. #include <sys/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <functions.h>
  5. #include "consolefun.h"
  6. #include "const.h"
  7.  
  8. extern char *mktemp();
  9. extern char *malloc();
  10.  
  11. /* functions from handlers.c */
  12. extern void handle_console_read(struct MsgPort *, char *);
  13. extern void handle_window(struct Window *, int *);
  14.  
  15. char *connect(char *, char *, char *);
  16. void print_to_screen(char *, int, int *);
  17. int send_to_host(struct MsgPort *, char *, int);
  18. void setup_console(void);
  19. void cleanup(void);
  20.  
  21.  
  22. /* external variables */
  23. struct Library    *IntuitionBase = NULL;
  24. struct Window    *window = NULL;
  25. struct IOStdReq    *writeReq = NULL;
  26. struct MsgPort    *writePort = NULL;
  27. struct IOStdReq    *readReq = NULL;
  28. struct MsgPort    *con_read_port = NULL;
  29. BOOL OpenedConsole = FALSE;
  30.  
  31. struct MsgPort *read_port = NULL;
  32. char *read_port_name = NULL;
  33.  
  34. char *write_port_name = NULL;
  35.  
  36. struct MsgPort *wait_port = NULL;
  37. int local_echo = 1;
  38.  
  39.  
  40. struct NewWindow nw = {
  41.     0,10, 640,100, -1,-1, CLOSEWINDOW, WINDOWDEPTH|WINDOWSIZING|
  42.     WINDOWDRAG|WINDOWCLOSE|SMART_REFRESH|ACTIVATE, NULL, NULL,
  43.     (UBYTE*)"Telnet", NULL, NULL, 100,45, 640,256, WBENCHSCREEN
  44. };
  45.  
  46.  
  47. /* send a string, return 0 if not succeeded */
  48. int send_to_host(waitport, string, length)
  49.     struct MsgPort *waitport;
  50.     char *string;
  51.     int length;
  52. {
  53.     /* a line was typed, send it to the mud .. */
  54.     struct data_message *msg;    
  55.     ULONG signal;
  56.     char *data;
  57.     int i;
  58.  
  59. #ifdef DEBUG
  60. printf("sending :%d :'", length);
  61. for(i=0;i<length;i++) {
  62.     if(string[i] >= 'a' && string[i] <= 'z')
  63.     printf("%c", string[i]);
  64.     else
  65.     printf("<%d>", string[i]);
  66.     }
  67. printf("'\n");
  68. #endif
  69.  
  70.     msg = (struct data_message *) malloc(sizeof(struct data_message));
  71.     if(!msg)
  72.     return 0;
  73.     msg->Msg.mn_Node.ln_Type = NT_MESSAGE;
  74.     msg->Msg.mn_Length = sizeof(struct data_message);
  75.     msg->Msg.mn_ReplyPort = waitport;
  76.  
  77.     data = malloc(length+1);
  78.     for(i=0;i<length;i++)
  79.     data[i] = string[i];
  80.     data[i] = '\0';
  81.  
  82.     msg->buffer = data;
  83.     msg->length = length;
  84.  
  85.     if(!SafePutToPort( (struct Message*)msg, write_port_name))
  86.     return 0;
  87.     return 1;
  88. }
  89.  
  90.  
  91. /* print a received string to the console, filter escape sequences */
  92. void print_to_screen(buffer, length, exit)
  93.     char *buffer;
  94.     int length;
  95.     int *exit;
  96. {
  97.     int i, new = 0;
  98.     char *new_buffer;
  99.  
  100.     new_buffer = malloc(length);
  101.  
  102.     for(i=0;i<length;i++) {
  103.     if((buffer[i] == -1) && (buffer[i+1] == -5) && (buffer[i+2]== 1)) {
  104.         local_echo = 0;
  105.         i += 2;
  106.         continue;
  107.     }
  108.     if((buffer[i] == -1) && (buffer[i+1] == -4) && (buffer[i+2]== 1)) {
  109.         local_echo = 1;
  110.         i += 2;
  111.         continue;
  112.     }
  113.     if((buffer[i] == -1) && (buffer[i+1] == -3) && (buffer[i+2]== 1)) {
  114.         *exit = 1;
  115.         i += 2;
  116.         continue;
  117.     }
  118.     new_buffer[new] = buffer[i];
  119.     new++;
  120.     }
  121.     ConWrite(writeReq, new_buffer, new);
  122.     free(new_buffer);
  123. }
  124.  
  125.  
  126. /*  try to get a port at host. Give the name of our port as an argument.
  127.  *  Succeeded    : return portname
  128.  *  Failed     : return NULL ( no name )
  129.  */
  130. char *connect(hostname, portname, our_port_name)
  131.     char *hostname, *portname;
  132.     char *our_port_name;
  133. {
  134.     struct MsgPort *reply_port;
  135.     struct connect_message *connect, *reply;
  136.     ULONG portsig, signal;
  137.  
  138.     connect = (struct connect_message *)
  139.         AllocMem(sizeof(struct connect_message), MEMF_PUBLIC);
  140.     if(!connect)
  141.     return NULL;
  142.  
  143.     reply_port = FindPort(our_port_name);
  144.     if(!reply_port)
  145.     return NULL;
  146.  
  147.     connect->Msg.mn_Node.ln_Type = NT_MESSAGE;
  148.     connect->Msg.mn_Length = sizeof(struct connect_message);
  149.     connect->Msg.mn_ReplyPort = reply_port;
  150.     connect->port_name = our_port_name;
  151.  
  152.     if(!(SafePutToPort((struct Message *)connect, portname) )) {
  153.     FreeMem(connect, sizeof(struct connect_message));
  154.     return NULL;
  155.     }
  156.  
  157.     portsig = 1L << reply_port->mp_SigBit;
  158.  
  159.     while(1) {
  160.     signal = Wait( SIGBREAKF_CTRL_E | portsig);
  161.     if(signal & portsig) {    /* got a signal at the msgport */
  162.         if(reply = (struct connect_message *) GetMsg(reply_port))
  163.         {
  164.         char *tmp;
  165.         tmp = malloc(strlen(reply->port_name)+1);
  166.         strcpy(tmp, reply->port_name);
  167.         FreeMem(connect, sizeof(struct connect_message));
  168.         return tmp;
  169.         }
  170.         }
  171.     if(signal & SIGBREAKF_CTRL_E) {
  172.         ConPuts(writeReq, "User Abort\n");
  173.         return NULL;
  174.         }
  175.     } /* einde while-loop */
  176. }
  177.  
  178.  
  179. void setup_console(void)
  180. {
  181.     if(!(IntuitionBase=OpenLibrary("intuition.library",0))) cleanup();
  182.  
  183.     if(!(window = OpenWindow(&nw))) cleanup();
  184.  
  185.     /* create reply port and io block for writing to console */
  186.     if(!(writePort = CreatePort("RKM.console.write",0))) cleanup();
  187.  
  188.     if(!(writeReq = (struct IOStdReq *) 
  189.     CreateExtIO(writePort, (long)sizeof(struct IOStdReq))))    cleanup();
  190.  
  191.     /* create reply port and io block for reading from console */
  192.     if(!(con_read_port = CreatePort("RKM.console.read",0))) cleanup();
  193.  
  194.     if(!(readReq = (struct IOStdReq *)
  195.     CreateExtIO(con_read_port, (LONG)sizeof(struct IOStdReq)))) cleanup();
  196.  
  197.     /* now, attach a console to the window */
  198.     if(OpenConsole(writeReq, readReq, window)) cleanup();
  199.  
  200.     OpenedConsole = TRUE;
  201. }
  202.  
  203.  
  204. void cleanup()
  205. {
  206.     Delay(250);
  207.  
  208.     if(OpenedConsole)    CloseConsole(writeReq);
  209.  
  210.     if(writeReq)    DeleteExtIO((struct IORequest*) writeReq);
  211.     if(writePort)    DeletePort(writePort);
  212.  
  213.     if(readReq)        DeleteExtIO((struct IORequest*) readReq);
  214.     if(con_read_port)    DeletePort(con_read_port);
  215.  
  216.     if(read_port)    DeletePort(read_port);
  217.     if(read_port_name)  free(read_port_name);
  218.  
  219.     if(write_port_name) free(write_port_name);
  220.  
  221.     if(wait_port)    DeletePort(wait_port);
  222.  
  223.     if(window)        CloseWindow(window);
  224.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  225.     exit(0);
  226. }
  227.  
  228.  
  229. int main()
  230. {
  231.     char ibuf;    /* the char we last read */
  232.     ULONG signals, conreadsig, windowsig, readsig, waitsig;
  233.     int abort = FALSE;
  234.     struct data_message *receive;
  235.     struct data_message *msg;
  236.  
  237.     setup_console();
  238.     Forbid();
  239.     read_port_name = mktemp("telnetXXXXXXXX");    /* X's will be replaced by pid */
  240.     if(!read_port_name) {
  241.     Permit();
  242.     ConPuts(writeReq, "Couldn't create read_port_name\n");
  243.     cleanup();
  244.     }
  245.  
  246.     if(!(read_port = CreatePort(read_port_name, 0))) {
  247.     Permit();
  248.     ConPuts(writeReq, "Couldn't create replyport\n");
  249.     cleanup();
  250.     }
  251.     Permit();
  252.     ConPuts(writeReq, "Trying ....\n");
  253.  
  254.     if(!(write_port_name = connect("exodus", "8888", read_port_name))) {
  255.     ConPuts(writeReq, "Connection refused by host\n");
  256.     cleanup();
  257.     }
  258.     if(!(wait_port = CreatePort(0, 0))) {
  259.     ConPuts(writeReq, "Couldn't create wait_port\n");
  260.     cleanup();
  261.     }
  262.     ConPuts(writeReq, "Connected\n");
  263.  
  264.     QueueRead(readReq, &ibuf);
  265.  
  266.     conreadsig = 1L << con_read_port->mp_SigBit;
  267.     windowsig = 1L << window->UserPort->mp_SigBit;
  268.  
  269.     readsig = 1L << read_port->mp_SigBit;
  270.     waitsig = 1L << wait_port->mp_SigBit;
  271.  
  272.     while(!abort) {
  273.     signals = Wait(SIGBREAKF_CTRL_E | windowsig | conreadsig | readsig | waitsig);
  274.  
  275.     if(signals & readsig) {    /* got a signal at the msgport */
  276.         while( (receive = (struct data_message *)
  277.         GetMsg((struct MsgPort*) read_port))) {
  278.             print_to_screen(receive->buffer, receive->length, &abort);
  279.             ReplyMsg((struct Message *) receive);
  280.         }
  281.     }
  282.  
  283.     if(signals & waitsig) {
  284.         while(msg = (struct data_message *) GetMsg(wait_port)) {
  285.         free(msg->buffer);
  286.         free((char*)msg);
  287.         }
  288.     }
  289.  
  290.     if(signals & windowsig)
  291.         handle_window(window, &abort);
  292.  
  293.     if(signals & conreadsig)
  294.         handle_console_read(con_read_port, &ibuf);
  295.  
  296.     if(signals & SIGBREAKF_CTRL_E) {
  297.         ConPuts(writeReq, "User abort\n");
  298.         abort = 1;
  299.         }
  300.     }
  301.  
  302.     ConPuts(writeReq, "\nconnection closed\n");
  303.  
  304.     if(!(CheckIO((struct IORequest*) readReq)))
  305.     AbortIO((struct IORequest*) readReq);
  306.     WaitIO((struct IORequest*) readReq);
  307.  
  308.     cleanup();
  309. }
  310.