GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/telnet/network.c Lines: 0 36 0.0 %
Date: 2017-11-13 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: network.c,v 1.17 2014/07/22 07:30:24 jsg Exp $	*/
2
/*	$NetBSD: network.c,v 1.5 1996/02/28 21:04:06 thorpej Exp $	*/
3
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.
16
 * 3. Neither the name of the University nor the names of its contributors
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
33
#include "telnet_locl.h"
34
35
#include <sys/socket.h>
36
#include <errno.h>
37
#include <poll.h>
38
#include <unistd.h>
39
40
Ring		netoring, netiring;
41
unsigned char	netobuf[2*BUFSIZ], netibuf[BUFSIZ];
42
43
/*
44
 * Initialize internal network data structures.
45
 */
46
47
void
48
init_network(void)
49
{
50
	ring_init(&netoring, netobuf, sizeof netobuf);
51
	ring_init(&netiring, netibuf, sizeof netibuf);
52
	SetNetTrace(NULL);
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(void)
63
{
64
    struct pollfd pfd[1];
65
    int value;
66
67
    do {
68
	pfd[0].fd = net;
69
	pfd[0].events = POLLRDBAND;
70
	value = poll(pfd, 1, 0);
71
    } while ((value == -1) && (errno == EINTR));
72
73
    if (value < 0) {
74
	perror("poll");
75
	quit();
76
    }
77
    if (pfd[0].revents & POLLRDBAND)
78
	return 1;
79
    else
80
	return 0;
81
}
82
83
/*
84
 *  setneturg()
85
 *
86
 *	Sets "neturg" to the current location.
87
 */
88
89
void
90
setneturg(void)
91
{
92
    ring_mark(&netoring);
93
}
94
95
/*
96
 *  netflush
97
 *		Send as much data as possible to the network,
98
 *	handling requests for urgent data.
99
 *
100
 *		The return value indicates whether we did any
101
 *	useful work.
102
 */
103
104
int
105
netflush(void)
106
{
107
    int n, n1;
108
109
    if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
110
	if (!ring_at_mark(&netoring)) {
111
	    n = send(net, (char *)netoring.consume, n, 0); /* normal write */
112
	} else {
113
	    /*
114
	     * In 4.2 (and 4.3) systems, there is some question about
115
	     * what byte in a sendOOB operation is the "OOB" data.
116
	     * To make ourselves compatible, we only send ONE byte
117
	     * out of band, the one WE THINK should be OOB (though
118
	     * we really have more the TCP philosophy of urgent data
119
	     * rather than the Unix philosophy of OOB data).
120
	     */
121
	    n = send(net, (char *)netoring.consume, 1, MSG_OOB);/* URGENT data */
122
	}
123
    }
124
    if (n < 0) {
125
	if (errno != ENOBUFS && errno != EWOULDBLOCK) {
126
	    setcommandmode();
127
	    perror(hostname);
128
	    (void)close(net);
129
	    ring_clear_mark(&netoring);
130
	    longjmp(peerdied, -1);
131
	}
132
	n = 0;
133
    }
134
    if (netdata && n) {
135
	Dump('>', netoring.consume, n);
136
    }
137
    if (n) {
138
	ring_consumed(&netoring, n);
139
	/*
140
	 * If we sent all, and more to send, then recurse to pick
141
	 * up the other half.
142
	 */
143
	if ((n1 == n) && ring_full_consecutive(&netoring)) {
144
	    (void) netflush();
145
	}
146
	return 1;
147
    } else {
148
	return 0;
149
    }
150
}