2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2007
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.
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.
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/>.
22 struct packet_context
{
24 struct data_blob in
, out
;
28 * Close the underlying fd
30 static int packet_context_destructor(struct packet_context
*ctx
)
32 return close(ctx
->fd
);
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.
39 struct packet_context
*packet_init(TALLOC_CTX
*mem_ctx
, int fd
)
41 struct packet_context
*result
;
43 if (!(result
= TALLOC_ZERO_P(mem_ctx
, struct packet_context
))) {
48 talloc_set_destructor(result
, packet_context_destructor
);
53 * Pull data from the fd
55 NTSTATUS
packet_fd_read(struct packet_context
*ctx
)
61 res
= ioctl(ctx
->fd
, FIONREAD
, &available
);
64 DEBUG(10, ("ioctl(FIONREAD) failed: %s\n", strerror(errno
)));
65 return map_nt_error_from_unix(errno
);
68 SMB_ASSERT(available
>= 0);
71 return NT_STATUS_END_OF_FILE
;
74 new_size
= ctx
->in
.length
+ available
;
76 if (new_size
< ctx
->in
.length
) {
77 DEBUG(0, ("integer wrap\n"));
78 return NT_STATUS_NO_MEMORY
;
81 if (!(in
= TALLOC_REALLOC_ARRAY(ctx
, ctx
->in
.data
, uint8
, new_size
))) {
82 DEBUG(10, ("talloc failed\n"));
83 return NT_STATUS_NO_MEMORY
;
88 res
= recv(ctx
->fd
, in
+ ctx
->in
.length
, available
, 0);
91 DEBUG(10, ("recv failed: %s\n", strerror(errno
)));
92 return map_nt_error_from_unix(errno
);
96 return NT_STATUS_END_OF_FILE
;
99 ctx
->in
.length
+= res
;
104 NTSTATUS
packet_fd_read_sync(struct packet_context
*ctx
)
110 FD_SET(ctx
->fd
, &r_fds
);
112 res
= sys_select(ctx
->fd
+1, &r_fds
, NULL
, NULL
, NULL
);
115 DEBUG(10, ("select returned %s\n", strerror(errno
)));
116 return map_nt_error_from_unix(errno
);
119 return packet_fd_read(ctx
);
122 bool packet_handler(struct packet_context
*ctx
,
123 bool (*full_req
)(const struct data_blob
*data
,
126 NTSTATUS (*callback
)(const struct data_blob
*data
,
132 struct data_blob data
;
134 if (!full_req(&ctx
->in
, &length
, private_data
)) {
138 SMB_ASSERT(length
<= ctx
->in
.length
);
140 data
= data_blob(ctx
->in
.data
, length
);
142 memmove(ctx
->in
.data
, ctx
->in
.data
+ length
,
143 ctx
->in
.length
- length
);
144 ctx
->in
.length
-= length
;
146 *status
= callback(&data
, private_data
);
148 data_blob_free(&data
);
154 * How many bytes of outgoing data do we have pending?
156 size_t packet_outgoing_bytes(struct packet_context
*ctx
)
158 return ctx
->out
.length
;
162 * Push data to the fd
164 NTSTATUS
packet_fd_write(struct packet_context
*ctx
)
168 sent
= send(ctx
->fd
, ctx
->out
.data
, ctx
->out
.length
, 0);
171 DEBUG(0, ("send failed: %s\n", strerror(errno
)));
172 return map_nt_error_from_unix(errno
);
175 memmove(ctx
->out
.data
, ctx
->out
.data
+ sent
,
176 ctx
->out
.length
- sent
);
177 ctx
->out
.length
-= sent
;
183 * Sync flush all outgoing bytes
185 NTSTATUS
packet_flush(struct packet_context
*ctx
)
187 while (ctx
->out
.length
!= 0) {
188 NTSTATUS status
= packet_fd_write(ctx
);
189 if (!NT_STATUS_IS_OK(status
)) {
197 * Send a list of DATA_BLOBs
199 * Example: packet_send(ctx, 2, data_blob_const(&size, sizeof(size)),
200 * data_blob_const(buf, size));
202 NTSTATUS
packet_send(struct packet_context
*ctx
, int num_blobs
, ...)
209 len
= ctx
->out
.length
;
211 va_start(ap
, num_blobs
);
212 for (i
=0; i
<num_blobs
; i
++) {
214 struct data_blob blob
= va_arg(ap
, struct data_blob
);
216 tmp
= len
+ blob
.length
;
218 DEBUG(0, ("integer overflow\n"));
220 return NT_STATUS_NO_MEMORY
;
230 if (!(out
= TALLOC_REALLOC_ARRAY(ctx
, ctx
->out
.data
, uint8
, len
))) {
231 DEBUG(0, ("talloc failed\n"));
232 return NT_STATUS_NO_MEMORY
;
237 va_start(ap
, num_blobs
);
238 for (i
=0; i
<num_blobs
; i
++) {
239 struct data_blob blob
= va_arg(ap
, struct data_blob
);
241 memcpy(ctx
->out
.data
+ctx
->out
.length
, blob
.data
, blob
.length
);
242 ctx
->out
.length
+= blob
.length
;
246 SMB_ASSERT(ctx
->out
.length
== len
);
251 * Get the packet context's file descriptor
253 int packet_get_fd(struct packet_context
*ctx
)