[BACK]Return to network.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / telnet

Annotation of src/usr.bin/telnet/network.c, Revision 1.9

1.9     ! millert     1: /*     $OpenBSD: network.c,v 1.8 2003/06/03 02:56:18 millert Exp $     */
1.2       niklas      2: /*     $NetBSD: network.c,v 1.5 1996/02/28 21:04:06 thorpej Exp $      */
                      3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.8       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.5       art        33: #include "telnet_locl.h"
1.6       art        34: #include <err.h>
1.1       deraadt    35:
                     36: Ring           netoring, netiring;
                     37: unsigned char  netobuf[2*BUFSIZ], netibuf[BUFSIZ];
                     38:
                     39: /*
                     40:  * Initialize internal network data structures.
                     41:  */
                     42:
                     43:     void
                     44: init_network()
                     45: {
                     46:     if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
                     47:        exit(1);
                     48:     }
                     49:     if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
                     50:        exit(1);
                     51:     }
                     52:     NetTrace = stdout;
                     53: }
                     54:
                     55:
                     56: /*
                     57:  * Check to see if any out-of-band data exists on a socket (for
                     58:  * Telnet "synch" processing).
                     59:  */
                     60:
                     61:     int
                     62: stilloob()
                     63: {
1.9     ! millert    64:     struct pollfd pfd[1];
1.1       deraadt    65:     int value;
                     66:
                     67:     do {
1.9     ! millert    68:        pfd[0].fd = net;
        !            69:        pfd[0].events = POLLRDBAND;
        !            70:        value = poll(pfd, 1, 0);
1.1       deraadt    71:     } while ((value == -1) && (errno == EINTR));
                     72:
                     73:     if (value < 0) {
1.9     ! millert    74:        perror("poll");
1.1       deraadt    75:        (void) quit();
                     76:        /* NOTREACHED */
                     77:     }
1.9     ! millert    78:     if (pfd[0].revents & POLLRDBAND)
1.1       deraadt    79:        return 1;
1.9     ! millert    80:     else
1.1       deraadt    81:        return 0;
                     82: }
                     83:
                     84:
                     85: /*
                     86:  *  setneturg()
                     87:  *
                     88:  *     Sets "neturg" to the current location.
                     89:  */
                     90:
                     91:     void
                     92: setneturg()
                     93: {
                     94:     ring_mark(&netoring);
                     95: }
                     96:
                     97:
                     98: /*
                     99:  *  netflush
                    100:  *             Send as much data as possible to the network,
                    101:  *     handling requests for urgent data.
                    102:  *
                    103:  *             The return value indicates whether we did any
                    104:  *     useful work.
                    105:  */
                    106:
                    107:
                    108:     int
                    109: netflush()
                    110: {
1.7       mpech     111:     int n, n1;
1.1       deraadt   112:
1.5       art       113: #if    defined(ENCRYPTION)
                    114:     if (encrypt_output)
                    115:        ring_encrypt(&netoring, encrypt_output);
                    116: #endif
1.1       deraadt   117:     if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
                    118:        if (!ring_at_mark(&netoring)) {
                    119:            n = send(net, (char *)netoring.consume, n, 0); /* normal write */
                    120:        } else {
                    121:            /*
                    122:             * In 4.2 (and 4.3) systems, there is some question about
                    123:             * what byte in a sendOOB operation is the "OOB" data.
                    124:             * To make ourselves compatible, we only send ONE byte
                    125:             * out of band, the one WE THINK should be OOB (though
                    126:             * we really have more the TCP philosophy of urgent data
                    127:             * rather than the Unix philosophy of OOB data).
                    128:             */
                    129:            n = send(net, (char *)netoring.consume, 1, MSG_OOB);/* URGENT data */
                    130:        }
                    131:     }
                    132:     if (n < 0) {
                    133:        if (errno != ENOBUFS && errno != EWOULDBLOCK) {
                    134:            setcommandmode();
                    135:            perror(hostname);
                    136:            (void)NetClose(net);
                    137:            ring_clear_mark(&netoring);
                    138:            longjmp(peerdied, -1);
                    139:            /*NOTREACHED*/
                    140:        }
                    141:        n = 0;
                    142:     }
                    143:     if (netdata && n) {
                    144:        Dump('>', netoring.consume, n);
                    145:     }
                    146:     if (n) {
                    147:        ring_consumed(&netoring, n);
                    148:        /*
                    149:         * If we sent all, and more to send, then recurse to pick
                    150:         * up the other half.
                    151:         */
                    152:        if ((n1 == n) && ring_full_consecutive(&netoring)) {
                    153:            (void) netflush();
                    154:        }
                    155:        return 1;
                    156:     } else {
                    157:        return 0;
                    158:     }
                    159: }