2 * Copyright (C) 1996-2005 The Free Software Foundation, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 /* Code for the buffer data structure. */
23 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
28 # include <sys/socket.h>
31 /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
32 a different error into EIO strikes me as pretty dubious. */
37 /* Linked list of available buffer_data structures. */
38 static struct buffer_data *free_buffer_data;
40 /* Local functions. */
41 static void buf_default_memory_error PROTO ((struct buffer *));
42 static void allocate_buffer_datas PROTO((void));
43 static struct buffer_data *get_buffer_data PROTO((void));
45 /* Initialize a buffer structure. */
48 buf_initialize (input, output, flush, block, shutdown, memory, closure)
49 int (*input) PROTO((void *, char *, int, int, int *));
50 int (*output) PROTO((void *, const char *, int, int *));
51 int (*flush) PROTO((void *));
52 int (*block) PROTO((void *, int));
53 int (*shutdown) PROTO((struct buffer *));
54 void (*memory) PROTO((struct buffer *));
59 buf = (struct buffer *) xmalloc (sizeof (struct buffer));
67 buf->shutdown = shutdown;
68 buf->memory_error = memory ? memory : buf_default_memory_error;
69 buf->closure = closure;
73 /* Free a buffer structure. */
79 if (buf->closure != NULL)
84 if (buf->data != NULL)
86 buf->last->next = free_buffer_data;
87 free_buffer_data = buf->data;
92 /* Initialize a buffer structure which is not to be used for I/O. */
95 buf_nonio_initialize (memory)
96 void (*memory) PROTO((struct buffer *));
98 return (buf_initialize
99 ((int (*) PROTO((void *, char *, int, int, int *))) NULL,
100 (int (*) PROTO((void *, const char *, int, int *))) NULL,
101 (int (*) PROTO((void *))) NULL,
102 (int (*) PROTO((void *, int))) NULL,
103 (int (*) PROTO((struct buffer *))) NULL,
108 /* Default memory error handler. */
111 buf_default_memory_error (buf)
114 error (1, 0, "out of memory");
117 /* Allocate more buffer_data structures. */
120 allocate_buffer_datas ()
122 struct buffer_data *alc;
126 /* Allocate buffer_data structures in blocks of 16. */
127 #define ALLOC_COUNT (16)
129 alc = xmalloc (ALLOC_COUNT * sizeof (struct buffer_data));
130 space = (char *) valloc (ALLOC_COUNT * BUFFER_DATA_SIZE);
136 for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE)
138 alc->next = free_buffer_data;
139 free_buffer_data = alc;
144 /* Get a new buffer_data structure. */
146 static struct buffer_data *
149 struct buffer_data *ret;
151 if (free_buffer_data == NULL)
153 allocate_buffer_datas ();
154 if (free_buffer_data == NULL)
158 ret = free_buffer_data;
159 free_buffer_data = ret->next;
165 /* See whether a buffer and its file descriptor is empty. */
170 /* Try and read any data on the file descriptor first.
171 * We already know the descriptor is non-blocking.
173 buf_input_data (buf, NULL);
174 return buf_empty_p (buf);
179 /* See whether a buffer is empty. */
184 struct buffer_data *data;
186 for (data = buf->data; data != NULL; data = data->next)
194 #ifdef SERVER_FLOWCONTROL
196 * Count how much data is stored in the buffer..
197 * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
204 struct buffer_data *data;
207 for (data = buf->data; data != NULL; data = data->next)
208 mem += BUFFER_DATA_SIZE;
212 #endif /* SERVER_FLOWCONTROL */
214 /* Add data DATA of length LEN to BUF. */
217 buf_output (buf, data, len)
222 if (buf->data != NULL
223 && (((buf->last->text + BUFFER_DATA_SIZE)
224 - (buf->last->bufp + buf->last->size))
227 memcpy (buf->last->bufp + buf->last->size, data, len);
228 buf->last->size += len;
234 struct buffer_data *newdata;
236 newdata = get_buffer_data ();
239 (*buf->memory_error) (buf);
243 if (buf->data == NULL)
246 buf->last->next = newdata;
247 newdata->next = NULL;
250 newdata->bufp = newdata->text;
252 if (len <= BUFFER_DATA_SIZE)
255 memcpy (newdata->text, data, len);
259 newdata->size = BUFFER_DATA_SIZE;
260 memcpy (newdata->text, data, BUFFER_DATA_SIZE);
262 data += BUFFER_DATA_SIZE;
263 len -= BUFFER_DATA_SIZE;
269 /* Add a '\0' terminated string to BUF. */
272 buf_output0 (buf, string)
276 buf_output (buf, string, strlen (string));
279 /* Add a single character to BUF. */
282 buf_append_char (buf, ch)
286 if (buf->data != NULL
287 && (buf->last->text + BUFFER_DATA_SIZE
288 != buf->last->bufp + buf->last->size))
290 *(buf->last->bufp + buf->last->size) = ch;
298 buf_output (buf, &b, 1);
303 * Send all the output we've been saving up. Returns 0 for success or
304 * errno code. If the buffer has been set to be nonblocking, this
305 * will just write until the write would block.
309 buf_send_output (buf)
312 if (buf->output == NULL)
315 while (buf->data != NULL)
317 struct buffer_data *data;
325 status = (*buf->output) (buf->closure, data->bufp, data->size,
329 /* Some sort of error. Discard the data, and return. */
331 buf->last->next = free_buffer_data;
332 free_buffer_data = buf->data;
339 if (nbytes != data->size)
341 /* Not all the data was written out. This is only
342 permitted in nonblocking mode. Adjust the buffer,
345 assert (buf->nonblocking);
347 data->size -= nbytes;
348 data->bufp += nbytes;
354 buf->data = data->next;
355 data->next = free_buffer_data;
356 free_buffer_data = data;
365 * Flush any data queued up in the buffer. If BLOCK is nonzero, then
366 * if the buffer is in nonblocking mode, put it into blocking mode for
367 * the duration of the flush. This returns 0 on success, or an error
372 buf_flush (buf, block)
379 if (buf->flush == NULL)
382 nonblocking = buf->nonblocking;
383 if (nonblocking && block)
385 status = set_block (buf);
390 status = buf_send_output (buf);
392 status = (*buf->flush) (buf->closure);
394 if (nonblocking && block)
398 blockstat = set_nonblock (buf);
407 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
417 if (buf->nonblocking)
419 if (buf->block == NULL)
421 status = (*buf->block) (buf->closure, 0);
424 buf->nonblocking = 1;
429 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
439 if (! buf->nonblocking)
441 if (buf->block == NULL)
443 status = (*buf->block) (buf->closure, 1);
446 buf->nonblocking = 0;
451 * Send a character count and some output. Returns errno code or 0 for
454 * Sending the count in binary is OK since this is only used on a pipe
455 * within the same system.
459 buf_send_counted (buf)
463 struct buffer_data *data;
466 for (data = buf->data; data != NULL; data = data->next)
469 data = get_buffer_data ();
472 (*buf->memory_error) (buf);
476 data->next = buf->data;
478 if (buf->last == NULL)
481 data->bufp = data->text;
482 data->size = sizeof (int);
484 *((int *) data->text) = size;
486 return buf_send_output (buf);
490 * Send a special count. COUNT should be negative. It will be
491 * handled speciallyi by buf_copy_counted. This function returns 0 or
494 * Sending the count in binary is OK since this is only used on a pipe
495 * within the same system.
499 buf_send_special_count (buf, count)
503 struct buffer_data *data;
505 data = get_buffer_data ();
508 (*buf->memory_error) (buf);
512 data->next = buf->data;
514 if (buf->last == NULL)
517 data->bufp = data->text;
518 data->size = sizeof (int);
520 *((int *) data->text) = count;
522 return buf_send_output (buf);
525 /* Append a list of buffer_data structures to an buffer. */
528 buf_append_data (buf, data, last)
530 struct buffer_data *data;
531 struct buffer_data *last;
535 if (buf->data == NULL)
538 buf->last->next = data;
543 /* Append the data on one buffer to another. This removes the data
544 from the source buffer. */
547 buf_append_buffer (to, from)
551 buf_append_data (to, from->data, from->last);
557 * Copy the contents of file F into buffer_data structures. We can't
558 * copy directly into an buffer, because we want to handle failure and
559 * succeess differently. Returns 0 on success, or -2 if out of
560 * memory, or a status code on error. Since the caller happens to
561 * know the size of the file, it is passed in as SIZE. On success,
562 * this function sets *RETP and *LASTP, which may be passed to
567 buf_read_file (f, size, retp, lastp)
570 struct buffer_data **retp;
571 struct buffer_data **lastp;
580 struct buffer_data *data;
583 data = get_buffer_data ();
593 (*lastp)->next = data;
597 data->bufp = data->text;
600 if (size > BUFFER_DATA_SIZE)
601 get = BUFFER_DATA_SIZE;
606 if (fread (data->text, get, 1, f) != 1)
621 (*lastp)->next = free_buffer_data;
622 free_buffer_data = *retp;
628 * Copy the contents of file F into buffer_data structures. We can't
629 * copy directly into an buffer, because we want to handle failure and
630 * succeess differently. Returns 0 on success, or -2 if out of
631 * memory, or a status code on error. On success, this function sets
632 * *RETP and *LASTP, which may be passed to buf_append_data.
636 buf_read_file_to_eof (f, retp, lastp)
638 struct buffer_data **retp;
639 struct buffer_data **lastp;
648 struct buffer_data *data;
651 data = get_buffer_data ();
661 (*lastp)->next = data;
665 data->bufp = data->text;
668 get = BUFFER_DATA_SIZE;
671 nread = fread (data->text, 1, get, f);
672 if (nread == 0 && !feof (f))
686 (*lastp)->next = free_buffer_data;
687 free_buffer_data = *retp;
692 /* Return the number of bytes in a chain of buffer_data structures. */
695 buf_chain_length (buf)
696 struct buffer_data *buf;
707 /* Return the number of bytes in a buffer. */
713 return buf_chain_length (buf->data);
717 * Read an arbitrary amount of data into an input buffer. The buffer
718 * will be in nonblocking mode, and we just grab what we can. Return
719 * 0 on success, or -1 on end of file, or -2 if out of memory, or an
720 * error code. If COUNTP is not NULL, *COUNTP is set to the number of
725 buf_input_data (buf, countp)
729 if (buf->input == NULL)
740 if (buf->data == NULL
741 || (buf->last->bufp + buf->last->size
742 == buf->last->text + BUFFER_DATA_SIZE))
744 struct buffer_data *data;
746 data = get_buffer_data ();
749 (*buf->memory_error) (buf);
753 if (buf->data == NULL)
756 buf->last->next = data;
760 data->bufp = data->text;
764 get = ((buf->last->text + BUFFER_DATA_SIZE)
765 - (buf->last->bufp + buf->last->size));
767 status = (*buf->input) (buf->closure,
768 buf->last->bufp + buf->last->size,
773 buf->last->size += nbytes;
779 /* If we did not fill the buffer, then presumably we read
780 all the available data. */
789 * Read a line (characters up to a \012) from an input buffer. (We
790 * use \012 rather than \n for the benefit of non Unix clients for
791 * which \n means something else). This returns 0 on success, or -1
792 * on end of file, or -2 if out of memory, or an error code. If it
793 * succeeds, it sets *LINE to an allocated buffer holding the contents
794 * of the line. The trailing \012 is not included in the buffer. If
795 * LENP is not NULL, then *LENP is set to the number of bytes read;
796 * strlen may not work, because there may be embedded null bytes.
800 buf_read_line (buf, line, lenp)
805 if (buf->input == NULL)
812 int len, finallen = 0;
813 struct buffer_data *data;
816 /* See if there is a newline in BUF. */
818 for (data = buf->data; data != NULL; data = data->next)
820 nl = memchr (data->bufp, '\012', data->size);
823 finallen = nl - data->bufp;
830 /* If we found a newline, copy the line into a memory buffer,
831 and remove it from BUF. */
835 struct buffer_data *nldata;
837 p = xmalloc (len + 1);
844 while (data != nldata)
846 struct buffer_data *next;
848 memcpy (p, data->bufp, data->size);
851 data->next = free_buffer_data;
852 free_buffer_data = data;
856 memcpy (p, data->bufp, finallen);
859 data->size -= finallen + 1;
869 /* Read more data until we get a newline. */
872 int size, status, nbytes;
875 if (buf->data == NULL
876 || (buf->last->bufp + buf->last->size
877 == buf->last->text + BUFFER_DATA_SIZE))
879 data = get_buffer_data ();
882 (*buf->memory_error) (buf);
886 if (buf->data == NULL)
889 buf->last->next = data;
893 data->bufp = data->text;
897 mem = buf->last->bufp + buf->last->size;
898 size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
900 /* We need to read at least 1 byte. We can handle up to
901 SIZE bytes. This will only be efficient if the
902 underlying communication stream does its own buffering,
903 or is clever about getting more than 1 byte at a time. */
904 status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
908 buf->last->size += nbytes;
910 /* Optimize slightly to avoid an unnecessary call to
919 if (memchr (mem, '\012', nbytes) != NULL)
927 * Extract data from the input buffer BUF. This will read up to WANT
928 * bytes from the buffer. It will set *RETDATA to point at the bytes,
929 * and set *GOT to the number of bytes to be found there. Any buffer
930 * call which uses BUF may change the contents of the buffer at *DATA,
931 * so the data should be fully processed before any further calls are
932 * made. This returns 0 on success, or -1 on end of file, or -2 if
933 * out of memory, or an error code.
937 buf_read_data (buf, want, retdata, got)
943 if (buf->input == NULL)
946 while (buf->data != NULL && buf->data->size == 0)
948 struct buffer_data *next;
950 next = buf->data->next;
951 buf->data->next = free_buffer_data;
952 free_buffer_data = buf->data;
958 if (buf->data == NULL)
960 struct buffer_data *data;
961 int get, status, nbytes;
963 data = get_buffer_data ();
966 (*buf->memory_error) (buf);
973 data->bufp = data->text;
976 if (want < BUFFER_DATA_SIZE)
979 get = BUFFER_DATA_SIZE;
980 status = (*buf->input) (buf->closure, data->bufp, get,
981 BUFFER_DATA_SIZE, &nbytes);
988 *retdata = buf->data->bufp;
989 if (want < buf->data->size)
992 buf->data->size -= want;
993 buf->data->bufp += want;
997 *got = buf->data->size;
1005 * Copy lines from an input buffer to an output buffer. This copies
1006 * all complete lines (characters up to a newline) from INBUF to
1007 * OUTBUF. Each line in OUTBUF is preceded by the character COMMAND
1012 buf_copy_lines (outbuf, inbuf, command)
1013 struct buffer *outbuf;
1014 struct buffer *inbuf;
1019 struct buffer_data *data;
1020 struct buffer_data *nldata;
1024 /* See if there is a newline in INBUF. */
1027 for (data = inbuf->data; data != NULL; data = data->next)
1029 nl = memchr (data->bufp, '\n', data->size);
1039 /* There are no more lines in INBUF. */
1043 /* Put in the command. */
1044 buf_append_char (outbuf, command);
1045 buf_append_char (outbuf, ' ');
1047 if (inbuf->data != nldata)
1050 * Simply move over all the buffers up to the one containing
1053 for (data = inbuf->data; data->next != nldata; data = data->next)
1056 buf_append_data (outbuf, inbuf->data, data);
1057 inbuf->data = nldata;
1061 * If the newline is at the very end of the buffer, just move
1062 * the buffer onto OUTBUF. Otherwise we must copy the data.
1064 len = nl + 1 - nldata->bufp;
1065 if (len == nldata->size)
1067 inbuf->data = nldata->next;
1068 if (inbuf->data == NULL)
1071 nldata->next = NULL;
1072 buf_append_data (outbuf, nldata, nldata);
1076 buf_output (outbuf, nldata->bufp, len);
1077 nldata->bufp += len;
1078 nldata->size -= len;
1084 * Copy counted data from one buffer to another. The count is an
1085 * integer, host size, host byte order (it is only used across a
1086 * pipe). If there is enough data, it should be moved over. If there
1087 * is not enough data, it should remain on the original buffer. A
1088 * negative count is a special case. if one is seen, *SPECIAL is set
1089 * to the (negative) count value and no additional data is gathered
1090 * from the buffer; normally *SPECIAL is set to 0. This function
1091 * returns the number of bytes it needs to see in order to actually
1092 * copy something over.
1096 buf_copy_counted (outbuf, inbuf, special)
1097 struct buffer *outbuf;
1098 struct buffer *inbuf;
1105 struct buffer_data *data;
1109 char intbuf[sizeof (int)];
1114 struct buffer_data *start;
1116 struct buffer_data *stop;
1119 /* See if we have enough bytes to figure out the count. */
1120 need = sizeof (int);
1122 for (data = inbuf->data; data != NULL; data = data->next)
1124 if (data->size >= need)
1126 memcpy (intp, data->bufp, need);
1129 memcpy (intp, data->bufp, data->size);
1135 /* We don't have enough bytes to form an integer. */
1145 /* A negative COUNT is a special case meaning that we
1146 don't need any further information. */
1153 * We have an integer in COUNT. We have gotten all the
1154 * data from INBUF in all buffers before START, and we
1155 * have gotten STARTOFF bytes from START. See if we have
1156 * enough bytes remaining in INBUF.
1158 need = count - (start->size - startoff);
1166 for (data = start->next; data != NULL; data = data->next)
1168 if (need <= data->size)
1174 /* We don't have enough bytes. */
1183 * We have enough bytes. Free any buffers in INBUF before
1184 * START, and remove STARTOFF bytes from START, so that we can
1185 * forget about STARTOFF.
1187 start->bufp += startoff;
1188 start->size -= startoff;
1190 if (start->size == 0)
1191 start = start->next;
1193 if (stop->size == stopwant)
1199 while (inbuf->data != start)
1202 inbuf->data = data->next;
1203 data->next = free_buffer_data;
1204 free_buffer_data = data;
1207 /* If COUNT is negative, set *SPECIAL and get out now. */
1215 * We want to copy over the bytes from START through STOP. We
1216 * only want STOPWANT bytes from STOP.
1221 /* Attach the buffers from START through STOP to OUTBUF. */
1222 for (data = start; data->next != stop; data = data->next)
1226 buf_append_data (outbuf, start, data);
1231 buf_output (outbuf, stop->bufp, stopwant);
1232 stop->bufp += stopwant;
1233 stop->size -= stopwant;
1240 /* Shut down a buffer. This returns 0 on success, or an errno code. */
1247 return (*buf->shutdown) (buf);
1253 /* The simplest type of buffer is one built on top of a stdio FILE.
1254 For simplicity, and because it is all that is required, we do not
1255 implement setting this type of buffer into nonblocking mode. The
1256 closure field is just a FILE *. */
1258 static int stdio_buffer_input PROTO((void *, char *, int, int, int *));
1259 static int stdio_buffer_output PROTO((void *, const char *, int, int *));
1260 static int stdio_buffer_flush PROTO((void *));
1261 static int stdio_buffer_shutdown PROTO((struct buffer *buf));
1265 /* Initialize a buffer built on a stdio FILE. */
1266 struct stdio_buffer_closure
1275 stdio_buffer_initialize (fp, child_pid, input, memory)
1279 void (*memory) PROTO((struct buffer *));
1281 struct stdio_buffer_closure *bc = xmalloc (sizeof (*bc));
1284 bc->child_pid = child_pid;
1286 return buf_initialize (input ? stdio_buffer_input : NULL,
1287 input ? NULL : stdio_buffer_output,
1288 input ? NULL : stdio_buffer_flush,
1289 (int (*) PROTO((void *, int))) NULL,
1290 stdio_buffer_shutdown,
1295 /* Return the file associated with a stdio buffer. */
1297 stdio_buffer_get_file (buf)
1300 struct stdio_buffer_closure *bc;
1302 assert(buf->shutdown == stdio_buffer_shutdown);
1304 bc = (struct stdio_buffer_closure *) buf->closure;
1309 /* The buffer input function for a buffer built on a stdio FILE. */
1312 stdio_buffer_input (closure, data, need, size, got)
1319 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1322 /* Since stdio does its own buffering, we don't worry about
1323 getting more bytes than we need. */
1325 if (need == 0 || need == 1)
1335 else if (errno == 0)
1346 nbytes = fread (data, 1, need, bc->fp);
1353 else if (errno == 0)
1364 /* The buffer output function for a buffer built on a stdio FILE. */
1367 stdio_buffer_output (closure, data, have, wrote)
1373 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1381 nbytes = fwrite (data, 1, have, bc->fp);
1401 /* The buffer flush function for a buffer built on a stdio FILE. */
1403 stdio_buffer_flush (closure)
1406 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1408 if (fflush (bc->fp) != 0)
1422 stdio_buffer_shutdown (buf)
1425 struct stdio_buffer_closure *bc = buf->closure;
1427 int closefp, statted;
1429 /* Must be a pipe or a socket. What could go wrong?
1430 * Well, apparently for disconnected clients under AIX, the
1431 * fstat() will return -1 on the server if the client has gone
1434 if (fstat(fileno(bc->fp), &s) == -1) statted = 0;
1438 /* Flush the buffer if we can */
1447 /* There used to be a check here for unread data in the buffer of on
1448 * the pipe, but it was deemed unnecessary and possibly dangerous. In
1449 * some sense it could be second-guessing the caller who requested it
1453 # ifdef SHUTDOWN_SERVER
1454 if (current_parsed_root->method != server_method)
1456 # ifndef NO_SOCKET_TO_FD
1458 /* shutdown() sockets */
1459 if (statted && S_ISSOCK (s.st_mode))
1460 shutdown (fileno (bc->fp), 0);
1462 # endif /* NO_SOCKET_TO_FD */
1463 # ifdef START_RSH_WITH_POPEN_RW
1464 /* Can't be set with SHUTDOWN_SERVER defined */
1465 else if (pclose (bc->fp) == EOF)
1467 error (0, errno, "closing connection to %s",
1468 current_parsed_root->hostname);
1471 # endif /* START_RSH_WITH_POPEN_RW */
1475 else if (buf->output)
1477 # ifdef SHUTDOWN_SERVER
1478 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
1479 * SHUTDOWN_SERVER_OUTPUT
1481 if (current_parsed_root->method == server_method)
1482 SHUTDOWN_SERVER (fileno (bc->fp));
1485 # ifndef NO_SOCKET_TO_FD
1486 /* shutdown() sockets */
1487 if (statted && S_ISSOCK (s.st_mode))
1488 shutdown (fileno (bc->fp), 1);
1491 /* I'm not sure I like this empty block, but the alternative
1492 * is a another nested NO_SOCKET_TO_FD switch above.
1495 # endif /* NO_SOCKET_TO_FD */
1500 if (statted && closefp && fclose (bc->fp) == EOF)
1506 # ifdef CLIENT_SUPPORT
1507 /* We are already closing the connection.
1508 * On error, print a warning and try to
1509 * continue to avoid infinte loops.
1513 "closing down connection to %s",
1514 current_parsed_root->hostname);
1515 # endif /* CLIENT_SUPPORT */
1518 /* If we were talking to a process, make sure it exited */
1524 w = waitpid (bc->child_pid, (int *) 0, 0);
1525 while (w == -1 && errno == EINTR);
1527 /* We are already closing the connection.
1528 * On error, print a warning and try to
1529 * continue to avoid infinte loops.
1532 error (0, errno, "waiting for process %d", bc->child_pid);
1539 /* Certain types of communication input and output data in packets,
1540 where each packet is translated in some fashion. The packetizing
1541 buffer type supports that, given a buffer which handles lower level
1542 I/O and a routine to translate the data in a packet.
1544 This code uses two bytes for the size of a packet, so packets are
1545 restricted to 65536 bytes in total.
1547 The translation functions should just translate; they may not
1548 significantly increase or decrease the amount of data. The actual
1549 size of the initial data is part of the translated data. The
1550 output translation routine may add up to PACKET_SLOP additional
1551 bytes, and the input translation routine should shrink the data
1554 #define PACKET_SLOP (100)
1556 /* This structure is the closure field of a packetizing buffer. */
1558 struct packetizing_buffer
1560 /* The underlying buffer. */
1562 /* The input translation function. Exactly one of inpfn and outfn
1563 will be NULL. The input translation function should
1564 untranslate the data in INPUT, storing the result in OUTPUT.
1565 SIZE is the amount of data in INPUT, and is also the size of
1566 OUTPUT. This should return 0 on success, or an errno code. */
1567 int (*inpfn) PROTO((void *fnclosure, const char *input, char *output,
1569 /* The output translation function. This should translate the
1570 data in INPUT, storing the result in OUTPUT. The first two
1571 bytes in INPUT will be the size of the data, and so will SIZE.
1572 This should set *TRANSLATED to the amount of translated data in
1573 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP
1574 bytes. This should return 0 on success, or an errno code. */
1575 int (*outfn) PROTO((void *fnclosure, const char *input, char *output,
1576 int size, int *translated));
1577 /* A closure for the translation function. */
1579 /* For an input buffer, we may have to buffer up data here. */
1580 /* This is non-zero if the buffered data has been translated.
1581 Otherwise, the buffered data has not been translated, and starts
1582 with the two byte packet size. */
1584 /* The amount of buffered data. */
1586 /* The buffer allocated to hold the data. */
1588 /* The size of holdbuf. */
1590 /* If translated is set, we need another data pointer to track
1591 where we are in holdbuf. If translated is clear, then this
1592 pointer is not used. */
1596 static int packetizing_buffer_input PROTO((void *, char *, int, int, int *));
1597 static int packetizing_buffer_output PROTO((void *, const char *, int, int *));
1598 static int packetizing_buffer_flush PROTO((void *));
1599 static int packetizing_buffer_block PROTO((void *, int));
1600 static int packetizing_buffer_shutdown PROTO((struct buffer *));
1602 /* Create a packetizing buffer. */
1605 packetizing_buffer_initialize (buf, inpfn, outfn, fnclosure, memory)
1607 int (*inpfn) PROTO ((void *, const char *, char *, int));
1608 int (*outfn) PROTO ((void *, const char *, char *, int, int *));
1610 void (*memory) PROTO((struct buffer *));
1612 struct packetizing_buffer *pb;
1614 pb = (struct packetizing_buffer *) xmalloc (sizeof *pb);
1615 memset (pb, 0, sizeof *pb);
1620 pb->fnclosure = fnclosure;
1624 /* Add PACKET_SLOP to handle larger translated packets, and
1625 add 2 for the count. This buffer is increased if
1627 pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1628 pb->holdbuf = xmalloc (pb->holdbufsize);
1631 return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1632 inpfn != NULL ? NULL : packetizing_buffer_output,
1633 inpfn != NULL ? NULL : packetizing_buffer_flush,
1634 packetizing_buffer_block,
1635 packetizing_buffer_shutdown,
1640 /* Input data from a packetizing buffer. */
1643 packetizing_buffer_input (closure, data, need, size, got)
1650 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1654 if (pb->holdsize > 0 && pb->translated)
1658 copy = pb->holdsize;
1662 memcpy (data, pb->holddata, size);
1663 pb->holdsize -= size;
1664 pb->holddata += size;
1669 memcpy (data, pb->holddata, copy);
1679 while (need > 0 || *got == 0)
1681 int get, status, nread, count, tcount;
1683 char stackoutbuf[BUFFER_DATA_SIZE + PACKET_SLOP];
1684 char *inbuf, *outbuf;
1686 /* If we don't already have the two byte count, get it. */
1687 if (pb->holdsize < 2)
1689 get = 2 - pb->holdsize;
1690 status = buf_read_data (pb->buf, get, &bytes, &nread);
1693 /* buf_read_data can return -2, but a buffer input
1694 function is only supposed to return -1, 0, or an
1703 /* The buffer is in nonblocking mode, and we didn't
1704 manage to read anything. */
1709 pb->holdbuf[1] = bytes[0];
1712 pb->holdbuf[0] = bytes[0];
1715 /* We only got one byte, but we needed two. Stash
1716 the byte we got, and try again. */
1720 pb->holdbuf[1] = bytes[1];
1725 /* Read the packet. */
1727 count = (((pb->holdbuf[0] & 0xff) << 8)
1728 + (pb->holdbuf[1] & 0xff));
1730 if (count + 2 > pb->holdbufsize)
1734 /* We didn't allocate enough space in the initialize
1737 n = xrealloc (pb->holdbuf, count + 2);
1740 (*pb->buf->memory_error) (pb->buf);
1744 pb->holdbufsize = count + 2;
1747 get = count - (pb->holdsize - 2);
1749 status = buf_read_data (pb->buf, get, &bytes, &nread);
1752 /* buf_read_data can return -2, but a buffer input
1753 function is only supposed to return -1, 0, or an error
1762 /* We did not get any data. Presumably the buffer is in
1763 nonblocking mode. */
1769 /* We did not get all the data we need to fill the packet.
1770 buf_read_data does not promise to return all the bytes
1771 requested, so we must try again. */
1772 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1773 pb->holdsize += nread;
1777 /* We have a complete untranslated packet of COUNT bytes. */
1779 if (pb->holdsize == 2)
1781 /* We just read the entire packet (the 2 bytes in
1782 PB->HOLDBUF are the size). Save a memcpy by
1783 translating directly from BYTES. */
1788 /* We already had a partial packet in PB->HOLDBUF. We
1789 need to copy the new data over to make the input
1791 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1792 inbuf = pb->holdbuf + 2;
1795 if (count <= sizeof stackoutbuf)
1796 outbuf = stackoutbuf;
1799 outbuf = xmalloc (count);
1802 (*pb->buf->memory_error) (pb->buf);
1807 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1811 /* The first two bytes in the translated buffer are the real
1812 length of the translated data. */
1813 tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1816 error (1, 0, "Input translation failure");
1820 /* We have more data than the caller has provided space
1821 for. We need to save some of it for the next call. */
1823 memcpy (data, outbuf + 2, size);
1826 pb->holdsize = tcount - size;
1827 memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1828 pb->holddata = pb->holdbuf;
1831 if (outbuf != stackoutbuf)
1837 memcpy (data, outbuf + 2, tcount);
1839 if (outbuf != stackoutbuf)
1853 /* Output data to a packetizing buffer. */
1856 packetizing_buffer_output (closure, data, have, wrote)
1862 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1863 char inbuf[BUFFER_DATA_SIZE + 2];
1864 char stack_outbuf[BUFFER_DATA_SIZE + PACKET_SLOP + 4];
1865 struct buffer_data *outdata = NULL;
1867 int size, status, translated;
1869 if (have > BUFFER_DATA_SIZE)
1871 /* It would be easy to xmalloc a buffer, but I don't think this
1872 case can ever arise. */
1876 inbuf[0] = (have >> 8) & 0xff;
1877 inbuf[1] = have & 0xff;
1878 memcpy (inbuf + 2, data, have);
1882 /* The output function is permitted to add up to PACKET_SLOP
1883 bytes, and we need 2 bytes for the size of the translated data.
1884 If we can guarantee that the result will fit in a buffer_data,
1885 we translate directly into one to avoid a memcpy in buf_output. */
1886 if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1887 outbuf = stack_outbuf;
1890 outdata = get_buffer_data ();
1891 if (outdata == NULL)
1893 (*pb->buf->memory_error) (pb->buf);
1897 outdata->next = NULL;
1898 outdata->bufp = outdata->text;
1900 outbuf = outdata->text;
1903 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1908 /* The output function is permitted to add up to PACKET_SLOP
1910 if (translated > size + PACKET_SLOP)
1913 outbuf[0] = (translated >> 8) & 0xff;
1914 outbuf[1] = translated & 0xff;
1916 if (outbuf == stack_outbuf)
1917 buf_output (pb->buf, outbuf, translated + 2);
1920 /* if ((have + PACKET_SLOP + 4) > BUFFER_DATA_SIZE), then
1921 outdata may be NULL. */
1922 if (outdata == NULL)
1925 outdata->size = translated + 2;
1926 buf_append_data (pb->buf, outdata, outdata);
1931 /* We will only be here because buf_send_output was called on the
1932 packetizing buffer. That means that we should now call
1933 buf_send_output on the underlying buffer. */
1934 return buf_send_output (pb->buf);
1939 /* Flush data to a packetizing buffer. */
1941 packetizing_buffer_flush (closure)
1944 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1946 /* Flush the underlying buffer. Note that if the original call to
1947 buf_flush passed 1 for the BLOCK argument, then the buffer will
1948 already have been set into blocking mode, so we should always
1950 return buf_flush (pb->buf, 0);
1955 /* The block routine for a packetizing buffer. */
1957 packetizing_buffer_block (closure, block)
1961 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1964 return set_block (pb->buf);
1966 return set_nonblock (pb->buf);
1969 /* Shut down a packetizing buffer. */
1972 packetizing_buffer_shutdown (buf)
1975 struct packetizing_buffer *pb = (struct packetizing_buffer *) buf->closure;
1977 return buf_shutdown (pb->buf);
1980 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */