home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / RLOGIN.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  5KB  |  194 lines

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