home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3 / hamradioversion3.0examsandprograms1992.iso / packet / n17jsrc / rlogin.c < prev    next >
C/C++ Source or Header  |  1991-07-29  |  4KB  |  181 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "session.h"
  6. #include "proc.h"
  7. #include "tty.h"
  8. #include "commands.h"
  9. #include "netuser.h"
  10.  
  11. static char *username = "guest";
  12. static char terminal[] = "ansi";
  13. static char *termspeed = "/38400";
  14.  
  15. extern FILE *Rawterm;
  16. int rlo_connect __ARGS((struct session *sp,char *fsocket,int len));
  17. void rlo_output __ARGS((int unused,void *p1,void *p2));
  18. void rlrecv __ARGS((struct session *sp));
  19.  
  20. /* Execute user rlogin command */
  21. int
  22. dorlogin(argc,argv,p)
  23. int argc;
  24. char *argv[];
  25. void *p;
  26. {
  27.     struct session *sp;
  28.     struct sockaddr_in fsocket;
  29.     struct sockaddr_in lsocket;
  30.  
  31.     /* Allocate a session descriptor */
  32.     if((sp = newsession(argv[1],RLOGIN,0)) == NULLSESSION){
  33.         tprintf("Too many sessions\n");
  34.         return 1;
  35.     }
  36.     fsocket.sin_family = AF_INET;
  37.     if(argc < 3)
  38.         fsocket.sin_port = IPPORT_RLOGIN;
  39.     else
  40.         fsocket.sin_port = atoi(argv[2]);
  41.  
  42.     tprintf("Resolving %s... ",sp->name);
  43.     if((fsocket.sin_addr.s_addr = resolve(sp->name)) == 0L){
  44.         tprintf(Badhost,sp->name);
  45.         keywait(NULLCHAR,1);
  46.         freesession(sp);
  47.         return 1;
  48.     }
  49.     if((sp->s = socket(AF_INET,SOCK_STREAM,0)) == -1){
  50.         tprintf("Can't create socket\n");
  51.         keywait(NULLCHAR,1);
  52.         freesession(sp);
  53.         return 1;
  54.     }
  55.     lsocket.sin_family = AF_INET;
  56.     lsocket.sin_addr.s_addr = INADDR_ANY;
  57.     lsocket.sin_port = IPPORT_RLOGIN;
  58.     bind(sp->s,(char *)&lsocket,sizeof(lsocket));
  59.     return rlo_connect(sp,(char *)&fsocket,SOCKSIZE);
  60. }
  61. /* Generic interactive connect routine */
  62. int
  63. rlo_connect(sp,fsocket,len)
  64. struct session *sp;
  65. char *fsocket;
  66. int len;
  67. {
  68.     unsigned int index;
  69.  
  70.     index = sp - Sessions;
  71.  
  72.     sockmode(sp->s,SOCK_ASCII);
  73.     tprintf("Trying %s...\n",psocket((struct sockaddr *)fsocket));
  74.     if(connect(sp->s,fsocket,len) == -1){
  75.           tprintf("%s session %u failed: %s errno %d\n",
  76.          Sestypes[sp->type], index, sockerr(sp->s),errno);
  77.  
  78.         keywait(NULLCHAR,1);
  79.         freesession(sp);
  80.         return 1;
  81.     }
  82.     tprintf("%s session ",Sestypes[sp->type]);
  83.     tprintf("%u connected to %s\n",index,sp->name);
  84.     rlrecv(sp);
  85.     return 0;
  86. }
  87.  
  88. /* Rlogin input routine, common to both rlogin and ttylink */
  89. void
  90. rlrecv(sp)
  91. struct session *sp;
  92. {
  93.     int c,s,index;
  94.     char *cp;
  95.  
  96.     s = sp->s;
  97.  
  98.     /* We run both the network and local sockets in transparent mode
  99.      * because we have to do our own eol mapping
  100.      */
  101.     seteol(s,"");
  102.     seteol(Curproc->input,"");
  103.     seteol(Curproc->output,"");
  104.  
  105.     /* Read real keystrokes from the keyboard */
  106.     sp->ttystate.crnl = 0;
  107.     /* Put tty into raw mode */
  108.     sp->ttystate.echo = 0;
  109.     sp->ttystate.edit = 0;
  110.  
  111.     setflush(s,'\n');
  112.  
  113.     index = sp - Sessions;
  114.  
  115.     /* Fork off the transmit process */
  116.     sp->proc1 = newproc("rlo_out",1024,rlo_output,0,sp,NULL,0);
  117.  
  118.     /* Process input on the connection */
  119.     while((c = recvchar(s)) != -1){
  120.         putc((char)c,Rawterm);
  121.     }
  122. quit:    /* A close was received from the remote host.
  123.      * Notify the user, kill the output task and wait for a response
  124.      * from the user before freeing the session.
  125.      */
  126.     cp = sockerr(s);
  127.     seteol(s,"\r\n");
  128.     seteol(Curproc->input,"\r\n");
  129.     seteol(Curproc->output,"\r\n");
  130.  
  131.     tprintf("%s session %u", Sestypes[sp->type],index);
  132.     tprintf(" closed: %s\n", cp != NULLCHAR ? cp : "EOF");
  133.     killproc(sp->proc1);
  134.     sp->proc1 = NULLPROC;
  135.     close_s(sp->s);
  136.     sp->s = -1;
  137.     keywait(NULLCHAR,1);
  138.     freesession(sp);
  139. }
  140.  
  141. /* User rlogin output task, started by user rlogin command */
  142. void
  143. rlo_output(unused,sp1,p)
  144. int unused;
  145. void *sp1;
  146. void *p;
  147. {
  148.     struct session *sp;
  149.     struct mbuf *bp;
  150.     char *cp;
  151.     char *logname;
  152.     sp = (struct session *)sp1;
  153.  
  154.     logname = getenv("USER");
  155.     if(logname == NULLCHAR)
  156.         logname = username;
  157.     bp = ambufw(1 + strlen(logname)+1 + strlen(logname)+1 +
  158.         strlen(terminal) + strlen(termspeed)+1);
  159.  
  160.     cp = bp->data;
  161.     *cp++ = '\0';
  162.     strcpy(cp, logname);
  163.     cp += strlen(logname) + 1;
  164.     strcpy(cp, logname);
  165.     cp += strlen(logname) + 1;
  166.     strcpy(cp, terminal);
  167.     cp += strlen(terminal);
  168.     strcpy(cp, termspeed);
  169.     cp += strlen(termspeed) + 1;
  170.     bp->cnt = cp - bp->data;
  171.     if(send_mbuf(sp->s,bp,0,NULLCHAR,0) != -1){
  172.         /* Send whatever's typed on the terminal */
  173.         while(recv_mbuf(sp->input,&bp,0,NULLCHAR,0) > 0){
  174.             if(send_mbuf(sp->s,bp,0,NULLCHAR,0) == -1)
  175.             break;
  176.         }
  177.     }
  178.     /* Make sure our parent doesn't try to kill us after we exit */
  179.     sp->proc1 = NULLPROC;
  180. }
  181.