Annotation of 43BSDReno/usr.bin/telnet/network.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1988 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted provided
                      6:  * that: (1) source distributions retain this entire copyright notice and
                      7:  * comment, and (2) distributions including binaries display the following
                      8:  * acknowledgement:  ``This product includes software developed by the
                      9:  * University of California, Berkeley and its contributors'' in the
                     10:  * documentation or other materials provided with the distribution and in
                     11:  * all advertising materials mentioning features or use of this software.
                     12:  * Neither the name of the University nor the names of its contributors may
                     13:  * be used to endorse or promote products derived from this software without
                     14:  * specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)network.c  1.15 (Berkeley) 6/28/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include <sys/types.h>
                     25: #include <sys/socket.h>
                     26: #include <sys/time.h>
                     27: 
                     28: #include <errno.h>
                     29: 
                     30: #include <arpa/telnet.h>
                     31: 
                     32: #include "ring.h"
                     33: 
                     34: #include "defines.h"
                     35: #include "externs.h"
                     36: #include "fdset.h"
                     37: 
                     38: Ring   netoring, netiring;
                     39: char   netobuf[2*BUFSIZ], netibuf[BUFSIZ];
                     40: 
                     41: /*
                     42:  * Initialize internal network data structures.
                     43:  */
                     44: 
                     45: init_network()
                     46: {
                     47:     if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
                     48:        exit(1);
                     49:     }
                     50:     if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
                     51:        exit(1);
                     52:     }
                     53:     NetTrace = stdout;
                     54: }
                     55: 
                     56: 
                     57: /*
                     58:  * Check to see if any out-of-band data exists on a socket (for
                     59:  * Telnet "synch" processing).
                     60:  */
                     61: 
                     62: int
                     63: stilloob()
                     64: {
                     65:     static struct timeval timeout = { 0 };
                     66:     fd_set     excepts;
                     67:     int value;
                     68: 
                     69:     do {
                     70:        FD_ZERO(&excepts);
                     71:        FD_SET(net, &excepts);
                     72:        value = select(net+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
                     73:     } while ((value == -1) && (errno == EINTR));
                     74: 
                     75:     if (value < 0) {
                     76:        perror("select");
                     77:        (void) quit();
                     78:        /* NOTREACHED */
                     79:     }
                     80:     if (FD_ISSET(net, &excepts)) {
                     81:        return 1;
                     82:     } else {
                     83:        return 0;
                     84:     }
                     85: }
                     86: 
                     87: 
                     88: /*
                     89:  *  setneturg()
                     90:  *
                     91:  *     Sets "neturg" to the current location.
                     92:  */
                     93: 
                     94: void
                     95: setneturg()
                     96: {
                     97:     ring_mark(&netoring);
                     98: }
                     99: 
                    100: 
                    101: /*
                    102:  *  netflush
                    103:  *             Send as much data as possible to the network,
                    104:  *     handling requests for urgent data.
                    105:  *
                    106:  *             The return value indicates whether we did any
                    107:  *     useful work.
                    108:  */
                    109: 
                    110: 
                    111: int
                    112: netflush()
                    113: {
                    114:     register int n, n1;
                    115: 
                    116:     if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
                    117:        if (!ring_at_mark(&netoring)) {
                    118:            n = send(net, netoring.consume, n, 0);      /* normal write */
                    119:        } else {
                    120:            /*
                    121:             * In 4.2 (and 4.3) systems, there is some question about
                    122:             * what byte in a sendOOB operation is the "OOB" data.
                    123:             * To make ourselves compatible, we only send ONE byte
                    124:             * out of band, the one WE THINK should be OOB (though
                    125:             * we really have more the TCP philosophy of urgent data
                    126:             * rather than the Unix philosophy of OOB data).
                    127:             */
                    128:            n = send(net, netoring.consume, 1, MSG_OOB);/* URGENT data */
                    129:        }
                    130:     }
                    131:     if (n < 0) {
                    132:        if (errno != ENOBUFS && errno != EWOULDBLOCK) {
                    133:            setcommandmode();
                    134:            perror(hostname);
                    135:            (void)NetClose(net);
                    136:            ring_clear_mark(&netoring);
                    137:            longjmp(peerdied, -1);
                    138:            /*NOTREACHED*/
                    139:        }
                    140:        n = 0;
                    141:     }
                    142:     if (netdata && n) {
                    143:        Dump('>', netoring.consume, n);
                    144:     }
                    145:     if (n) {
                    146:        ring_consumed(&netoring, n);
                    147:        /*
                    148:         * If we sent all, and more to send, then recurse to pick
                    149:         * up the other half.
                    150:         */
                    151:        if ((n1 == n) && ring_full_consecutive(&netoring)) {
                    152:            (void) netflush();
                    153:        }
                    154:        return 1;
                    155:     } else {
                    156:        return 0;
                    157:     }
                    158: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.