summaryrefslogtreecommitdiff
path: root/libpipe/pq.c
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2012-04-27 15:32:39 +0200
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2012-04-27 15:32:39 +0200
commit980e2112cf7a987df40b3157a417ad0e3a831476 (patch)
treee447c8095073e225102360ccbfce8a42eda83f4d /libpipe/pq.c
parent69bd9b1d600debf7b758cc7f8353b747430259b4 (diff)
Add MSG_PEEK support to pflocal
* libpipe/pq.h (packet_peek): Declare new function. * libpipe/pq.c (packet_read): Move code to new `packet_fetch' function, call it with `remove' set to 1. (packet_fetch): New function with code from `packet_read', but do not remove data if `remove' is 0. (packet_peek): New function, calls `packet_fetch' with `remove' set to 0. * libpipe/dgram.c (dgram_read): When MSG_PEEK is in *flags, do not dequeue and only peek data. * libpipe/seqpack.c (seqpack_read): Likewise. * libpipe/stream.c (stream_read): Likewise. * pflocal/socket.c (S_socket_recv): Pass MSG_PEEK flag to libpipe.
Diffstat (limited to 'libpipe/pq.c')
-rw-r--r--libpipe/pq.c37
1 files changed, 30 insertions, 7 deletions
diff --git a/libpipe/pq.c b/libpipe/pq.c
index afdda051..102f3ee3 100644
--- a/libpipe/pq.c
+++ b/libpipe/pq.c
@@ -349,13 +349,13 @@ packet_write (struct packet *packet,
return 0;
}
-/* Removes up to AMOUNT bytes from the beginning of the data in PACKET, and
+/* Remove or peek up to AMOUNT bytes from the beginning of the data in PACKET, and
puts it into *DATA, and the amount read into DATA_LEN. If more than the
original *DATA_LEN bytes are available, new memory is vm_allocated, and
the address and length of this array put into DATA and DATA_LEN. */
-error_t
-packet_read (struct packet *packet,
- char **data, size_t *data_len, size_t amount)
+static error_t
+packet_fetch (struct packet *packet,
+ char **data, size_t *data_len, size_t amount, int remove)
{
char *start = packet->buf_start;
char *end = packet->buf_end;
@@ -367,7 +367,7 @@ packet_read (struct packet *packet,
{
char *buf = packet->buf;
- if (packet->buf_vm_alloced && amount >= vm_page_size)
+ if (remove && packet->buf_vm_alloced && amount >= vm_page_size)
/* We can return memory from BUF directly without copying. */
{
if (buf + vm_page_size <= start)
@@ -414,7 +414,7 @@ packet_read (struct packet *packet,
bcopy (start, *data, amount);
start += amount;
- if (start - buf > 2 * PACKET_SIZE_LARGE)
+ if (remove && start - buf > 2 * PACKET_SIZE_LARGE)
/* Get rid of unused space at the beginning of the buffer -- we
know it's vm_alloced because of the size, and this will allow
the buffer to just slide through memory. Because we wait for
@@ -430,10 +430,33 @@ packet_read (struct packet *packet,
packet->buf_len -= dealloc;
}
- packet->buf_start = start;
+ if (remove)
+ packet->buf_start = start;
}
}
*data_len = amount;
return 0;
}
+
+/* Removes up to AMOUNT bytes from the beginning of the data in PACKET, and
+ puts it into *DATA, and the amount read into DATA_LEN. If more than the
+ original *DATA_LEN bytes are available, new memory is vm_allocated, and
+ the address and length of this array put into DATA and DATA_LEN. */
+error_t
+packet_read (struct packet *packet,
+ char **data, size_t *data_len, size_t amount)
+{
+ return packet_fetch (packet, data, data_len, amount, 1);
+}
+
+/* Peek up to AMOUNT bytes from the beginning of the data in PACKET, and
+ puts it into *DATA, and the amount read into DATA_LEN. If more than the
+ original *DATA_LEN bytes are available, new memory is vm_allocated, and
+ the address and length of this array put into DATA and DATA_LEN. */
+error_t
+packet_peek (struct packet *packet,
+ char **data, size_t *data_len, size_t amount)
+{
+ return packet_fetch (packet, data, data_len, amount, 0);
+}