source: branches/samba-3.2.x/source/lib/packet.c@ 201

Last change on this file since 201 was 133, checked in by Paul Smedley, 18 years ago

Update trunk to 3.2.0pre3

File size: 5.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Packet handling
4 Copyright (C) Volker Lendecke 2007
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "includes.h"
21
22struct packet_context {
23 int fd;
24 struct data_blob in, out;
25};
26
27/*
28 * Close the underlying fd
29 */
30static int packet_context_destructor(struct packet_context *ctx)
31{
32 return close(ctx->fd);
33}
34
35/*
36 * Initialize a packet context. The fd is given to the packet context, meaning
37 * that it is automatically closed when the packet context is freed.
38 */
39struct packet_context *packet_init(TALLOC_CTX *mem_ctx, int fd)
40{
41 struct packet_context *result;
42
43 if (!(result = TALLOC_ZERO_P(mem_ctx, struct packet_context))) {
44 return NULL;
45 }
46
47 result->fd = fd;
48 talloc_set_destructor(result, packet_context_destructor);
49 return result;
50}
51
52/*
53 * Pull data from the fd
54 */
55NTSTATUS packet_fd_read(struct packet_context *ctx)
56{
57 int res, available;
58 size_t new_size;
59 uint8 *in;
60
61 res = ioctl(ctx->fd, FIONREAD, &available);
62
63 if (res == -1) {
64 DEBUG(10, ("ioctl(FIONREAD) failed: %s\n", strerror(errno)));
65 return map_nt_error_from_unix(errno);
66 }
67
68 SMB_ASSERT(available >= 0);
69
70 if (available == 0) {
71 return NT_STATUS_END_OF_FILE;
72 }
73
74 new_size = ctx->in.length + available;
75
76 if (new_size < ctx->in.length) {
77 DEBUG(0, ("integer wrap\n"));
78 return NT_STATUS_NO_MEMORY;
79 }
80