Changeset 151 for trunk

Show
Ignore:
Timestamp:
03/26/10 03:50:38 (2 years ago)
Author:
mercyful
Message:

Updated Code to match latest ddTelnetd fixes.

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/ddtelnetd/ddtelnetd.c

    r145 r151  
    7676} 
    7777 
    78 int ignore = FALSE; 
     78int send_iac(unsigned char command, int option); 
    7979 
    8080int 
    8181remove_iacs(unsigned char *in, int len, unsigned char* out, int* out_count) { 
     82         
    8283        unsigned char *end = in + len; 
    8384        unsigned char* orig_in = in; 
    84  
     85        static int ignore = FALSE; 
     86    
    8587        while (in < end) { 
    8688                if (*in != IAC) { 
     
    8991                                *out_count = *out_count + 1; 
    9092                        } 
     93                        else { 
     94                                in++; 
     95                        } 
    9196                } 
    9297                else { 
     
    95100                                return 1; 
    96101                        } 
     102                         
     103                        if(ignore && *(in+1) != SE) { 
     104                                send_iac(*(in+1), -1); 
     105                                in += 2; 
     106                                continue; 
     107                        } 
     108 
     109                        syslog(LOG_INFO, "got real IAC, %d, %d", *(in+1), *(in+2)); 
    97110                        switch(*(in+1)) { 
    98111                        case IAC: 
     
    114127                        case SB: 
    115128                                ignore = TRUE; 
     129                                syslog(LOG_INFO, "Negotiation began"); 
     130                                in += 2; 
    116131                                break; 
    117132                        case SE: 
    118133                                ignore = FALSE; 
    119134                                syslog(LOG_INFO, "Negotiation completed"); 
     135                                in += 2; 
    120136                                break; 
    121137                        case NOP: 
     
    128144                                in += 2; 
    129145                                break; 
     146                        case EC: 
     147                                in += 2; 
     148                                *out = '\b'; 
     149                                ++out; 
     150                                *out_count = *out_count + 1; 
     151                                break; 
    130152                        default: 
    131153                                syslog(LOG_INFO, "Unknown IAC arg: %d", *(in+1)); 
     154                                in += 2; 
    132155                                break; 
    133156                        } 
     
    141164int send_iac(unsigned char command, int option) 
    142165{ 
    143         char buf[4]; 
     166        char buf[3]; 
    144167        int w; 
    145         sprintf(buf, "%c%c%c", (char) IAC, (char) command, (char) option); 
    146         w = write(OUTFD, buf, 3); 
     168         
     169        buf[0] = IAC; 
     170        buf[1] = command; 
     171        buf[2] = option; 
     172         
     173        w = write(OUTFD, buf, option == -1 ? 2 : 3); 
     174         
    147175        return w; 
    148176} 
     
    197225 
    198226        /// Michael Griffin Get Host Address 
    199     /// From Remote Connection. 
     227    /// From Remote Connection, Pass Like in.telnetd does. 
    200228 
    201229        fromlen = sizeof (from); 
     
    238266          exit(1); 
    239267        } 
    240         if(send_iac(DONT, TELOPT_NAWS) < 0) { 
     268        if(send_iac(WONT, TELOPT_NAWS) < 0) { 
     269          exit(1); 
     270        } 
     271        if(send_iac(WILL, TELOPT_BINARY) < 0) { 
    241272          exit(1); 
    242273        } 
     
    258289        } 
    259290        else if(pid == -1) { 
    260           closelog(); 
    261           exit(1); 
     291            closelog(); 
     292            exit(1); 
    262293        } 
    263294        else { 
    264           shell_pid = pid; 
     295            shell_pid = pid; 
    265296        } 
    266297 
     
    274305 
    275306                selret = select(ptyfd + 1, &rdfdset, NULL, 0, 0); 
    276  
    277                 if (selret < 0) 
     307                 
     308                if (selret <= 0) 
    278309                        break; 
    279310 
    280311                if (FD_ISSET(ptyfd, &rdfdset)) { 
    281                         unsigned char* ptr1, *ptr2; 
     312                        unsigned char* ptr1, *ptr2;      
    282313                        int iacs = 0; 
    283                         int r; 
    284  
    285                         r = read(ptyfd, tmpbuf, BUFSIZE / 2); 
    286  
    287                         if ((r < 0 && errno != EINTR)) { 
     314                int r; 
     315                 
     316                r = read(ptyfd, tmpbuf, BUFSIZE / 2); 
     317                         
     318                        if (r <= 0) { 
    288319                                break; 
    289320                        } 
     
    303334                                } 
    304335                        } 
    305  
    306                         if(write(OUTFD, buf, r + iacs) < 0) { 
     336                         
     337                        if(write(OUTFD, buf, r + iacs) != (r + iacs)) { 
    307338                           break; 
    308339                        } 
     
    314345 
    315346                        r = read(INFD, buf + new_offset, BUFSIZE - new_offset); 
    316  
    317                         if (r == 0 || (r < 0 && errno != EINTR)) { 
     347                                 
     348                        if (r <= 0) { 
    318349                                break; 
    319350                        } 
    320351 
    321352                        new_offset = remove_iacs(buf, r + new_offset, tmpbuf, &out_count); 
    322                         if(write(ptyfd, tmpbuf, out_count) < 0) { 
     353                         
     354                        if(write(ptyfd, tmpbuf, out_count) != out_count) { 
    323355                           break; 
    324356                        } 
     
    327359        } while (1); 
    328360 
    329         /* We will tell daydream to terminate probably (carrier lost) */ 
    330         kill(shell_pid, SIGTERM); 
    331         wait4(shell_pid, NULL, 0, NULL); 
    332361        close(ptyfd); 
     362 
     363        syslog(LOG_INFO, "Closed connection"); 
    333364        closelog(); 
    334  
    335         syslog(LOG_INFO, "Closed connection"); 
    336  
     365         
     366        _exit(0); 
     367         
    337368        return 1; 
    338369}