]> git.saurik.com Git - apple/network_cmds.git/blame - kdumpd.tproj/kdumpsubs.c
network_cmds-176.4.1.tar.gz
[apple/network_cmds.git] / kdumpd.tproj / kdumpsubs.c
CommitLineData
ac2f15b3
A
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/* Simple minded read-ahead/write-behind subroutines for tftp user and
31 server. Written originally with multiple buffers in mind, but current
32 implementation has two buffer logic wired in.
33
34 Todo: add some sort of final error check so when the write-buffer
35 is finally flushed, the caller can detect if the disk filled up
36 (or had an i/o error) and return a nak to the other side.
37
38 Jim Guyton 10/85
39 */
40
41#ifdef HAVE_CONFIG_H
42#include <config.h>
43#endif
44
45#include <sys/types.h>
46#include <sys/socket.h>
47#include <sys/ioctl.h>
48#ifdef HAVE_SYS_FILIO_H
49#include <sys/filio.h>
50#endif
51#include <netinet/in.h>
52#include "kdump.h"
53
54#include <stdio.h>
55#include <unistd.h>
56
57#include "kdumpsubs.h"
58
59#define PKTSIZE SEGSIZE+6 /* should be moved to kdump.h */
60
61struct bf {
62 int counter; /* size of data in buffer, or flag */
63 char buf[PKTSIZE]; /* room for data packet */
64} bfs[2];
65
66 /* Values for bf.counter */
67#define BF_ALLOC -3 /* alloc'd but not yet filled */
68#define BF_FREE -2 /* free */
69/* [-1 .. SEGSIZE] = size of data in the data buffer */
70
71static int nextone; /* index of next buffer to use */
72static int current; /* index of buffer in use */
73
74 /* control flags for crlf conversions */
75int newline = 0; /* fillbuf: in middle of newline expansion */
76int prevchar = -1; /* putbuf: previous char (cr check) */
77
78static struct kdumphdr *rw_init __P ((int));
79
80struct kdumphdr *w_init() { return rw_init(0); } /* write-behind */
81struct kdumphdr *r_init() { return rw_init(1); } /* read-ahead */
82
83/* init for either read-ahead or write-behind */
84/* zero for write-behind, one for read-head */
85static struct kdumphdr *
86rw_init(int x)
87{
88 newline = 0; /* init crlf flag */
89 prevchar = -1;
90 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
91 current = 0;
92 bfs[1].counter = BF_FREE;
93 nextone = x; /* ahead or behind? */
94 return (struct kdumphdr *)bfs[0].buf;
95}
96
97
98/* Have emptied current buffer by sending to net and getting ack.
99 Free it and return next buffer filled with data.
100 */
101/* if true, convert to ascii */
102/* file opened for read */
103
104/* int */
105/* readit(FILE *file, struct kdumphdr **dpp, int convert) */
106/* { */
107/* struct bf *b; */
108
109/* bfs[current].counter = BF_FREE; /\* free old one *\/ */
110/* current = !current; /\* "incr" current *\/ */
111
112/* b = &bfs[current]; /\* look at new buffer *\/ */
113/* if (b->counter == BF_FREE) /\* if it's empty *\/ */
114/* read_ahead(file, convert); /\* fill it *\/ */
115/* /\* assert(b->counter != BF_FREE);*\//\* check *\/ */
116/* *dpp = (struct kdumphdr *)b->buf; /\* set caller's ptr *\/ */
117/* return b->counter; */
118/* } */
119
120/*
121 * fill the input buffer, doing ascii conversions if requested
122 * conversions are lf -> cr,lf and cr -> cr, nul
123 */
124/* FILE *file; file opened for read */
125/* int convert; if true, convert to ascii */
126void
127read_ahead(FILE *file, int convert)
128{
129 register int i;
130 register char *p;
131 register int c;
132 struct bf *b;
133 struct kdumphdr *dp;
134
135 b = &bfs[nextone]; /* look at "next" buffer */
136 if (b->counter != BF_FREE) /* nop if not free */
137 return;
138 nextone = !nextone; /* "incr" next buffer ptr */
139
140 dp = (struct kdumphdr *)b->buf;
141
142 if (convert == 0) {
143 b->counter = read(fileno(file), dp->th_data, SEGSIZE);
144 return;
145 }
146
147 p = dp->th_data;
148 for (i = 0 ; i < SEGSIZE; i++) {
149 if (newline) {
150 if (prevchar == '\n')
151 c = '\n'; /* lf to cr,lf */
152 else c = '\0'; /* cr to cr,nul */
153 newline = 0;
154 }
155 else {
156 c = getc(file);
157 if (c == EOF) break;
158 if (c == '\n' || c == '\r') {
159 prevchar = c;
160 c = '\r';
161 newline = 1;
162 }
163 }
164 *p++ = c;
165 }
166 b->counter = (int)(p - dp->th_data);
167}
168
169/* Update count associated with the buffer, get new buffer
170 from the queue. Calls write_behind only if next buffer not
171 available.
172 */
173int
174writeit(FILE *file, struct kdumphdr **dpp, int ct, int convert)
175{
176 bfs[current].counter = ct; /* set size of data to write */
177 current = !current; /* switch to other buffer */
178 if (bfs[current].counter != BF_FREE) /* if not free */
179 (void)write_behind(file, convert); /* flush it */
180 bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
181 *dpp = (struct kdumphdr *)bfs[current].buf;
182 return ct; /* this is a lie of course */
183}
184
185/*
186 * Output a buffer to a file, converting from netascii if requested.
187 * CR,NUL -> CR and CR,LF => LF.
188 * Note spec is undefined if we get CR as last byte of file or a
189 * CR followed by anything else. In this case we leave it alone.
190 */
191int
192write_behind(FILE *file, int convert)
193{
194 char *buf;
195 int count;
196 register int ct;
197 register char *p;
198 register int c; /* current character */
199 struct bf *b;
200 struct kdumphdr *dp;
201
202 b = &bfs[nextone];
203 if (b->counter < -1) /* anything to flush? */
204 return 0; /* just nop if nothing to do */
205
206 count = b->counter; /* remember byte count */
207 b->counter = BF_FREE; /* reset flag */
208 dp = (struct kdumphdr *)b->buf;
209 nextone = !nextone; /* incr for next time */
210 buf = dp->th_data;
211
212 if (count <= 0) return -1; /* nak logic? */
213
214 if (convert == 0)
215 return write(fileno(file), buf, count);
216
217 p = buf;
218 ct = count;
219 while (ct--) { /* loop over the buffer */
220 c = *p++; /* pick up a character */
221 if (prevchar == '\r') { /* if prev char was cr */
222 if (c == '\n') /* if have cr,lf then just */
223 fseek(file, -1, 1); /* smash lf on top of the cr */
224 else
225 if (c == '\0') /* if have cr,nul then */
226 goto skipit; /* just skip over the putc */
227 /* else just fall through and allow it */
228 }
229 putc(c, file);
230skipit:
231 prevchar = c;
232 }
233 return count;
234}
235
236
237/* When an error has occurred, it is possible that the two sides
238 * are out of synch. Ie: that what I think is the other side's
239 * response to packet N is really their response to packet N-1.
240 *
241 * So, to try to prevent that, we flush all the input queued up
242 * for us on the network connection on our host.
243 *
244 * We return the number of packets we flushed (mostly for reporting
245 * when trace is active).
246 */
247
248/*int f;socket to flush */
249int
250synchnet(int f)
251{
252 int i, j = 0;
253 char rbuf[PKTSIZE];
254 struct sockaddr_in from;
255 int fromlen;
256
257 while (1) {
258 (void) ioctl(f, FIONREAD, &i);
259 if (i) {
260 j++;
261 fromlen = sizeof from;
262 (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
263 (struct sockaddr *)&from, &fromlen);
264 } else {
265 return(j);
266 }
267 }
268}