2 Unix SMB/CIFS implementation.
3 SMB client transport context management functions
4 Copyright (C) Andrew Tridgell 1994-2003
5 Copyright (C) James Myers 2003 <myersjj@samba.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 create a transport structure based on an established socket
27 struct cli_transport *cli_transport_init(struct cli_socket *sock)
30 struct cli_transport *transport;
32 mem_ctx = talloc_init("cli_transport");
33 if (!mem_ctx) return NULL;
35 transport = talloc_zero(mem_ctx, sizeof(*transport));
36 if (!transport) return NULL;
38 transport->mem_ctx = mem_ctx;
39 transport->socket = sock;
40 transport->negotiate.protocol = PROTOCOL_NT1;
41 transport->negotiate.max_xmit = ~0;
42 cli_null_set_signing(transport);
43 transport->socket->reference_count++;
45 ZERO_STRUCT(transport->called);
51 decrease reference count on a transport, and destroy if it becomes
54 void cli_transport_close(struct cli_transport *transport)
56 transport->reference_count--;
57 if (transport->reference_count <= 0) {
58 cli_sock_close(transport->socket);
59 talloc_destroy(transport->mem_ctx);
65 /****************************************************************************
66 send a session request (if appropriate)
67 ****************************************************************************/
68 BOOL cli_transport_connect(struct cli_transport *transport,
69 struct nmb_name *calling,
70 struct nmb_name *called)
73 int len = NBT_HDR_SIZE;
74 struct cli_request *req;
77 transport->called = *called;
80 /* 445 doesn't have session request */
81 if (transport->socket->port == 445) {
85 /* allocate output buffer */
86 req = cli_request_setup_nonsmb(transport, NBT_HDR_SIZE + 2*nbt_mangled_name_len());
88 /* put in the destination name */
89 p = req->out.buffer + NBT_HDR_SIZE;
90 name_mangle(called->name, p, called->name_type);
94 p = req->out.buffer+len;
95 name_mangle(calling->name, p, calling->name_type);
98 _smb_setlen(req->out.buffer,len-4);
99 SCVAL(req->out.buffer,0,0x81);
101 if (!cli_request_send(req) ||
102 !cli_request_receive(req)) {
103 cli_request_destroy(req);
107 if (CVAL(req->in.buffer,0) != 0x82) {
108 transport->error.etype = ETYPE_NBT;
109 transport->error.e.nbt_error = CVAL(req->in.buffer,4);
110 cli_request_destroy(req);
114 cli_request_destroy(req);
119 /****************************************************************************
120 get next mid in sequence
121 ****************************************************************************/
122 uint16 cli_transport_next_mid(struct cli_transport *transport)
125 struct cli_request *req;
127 mid = transport->next_mid;
130 /* now check to see if this mid is being used by one of the
131 pending requests. This is quite efficient because the list is
132 usually very short */
134 /* the zero mid is reserved for requests that don't have a mid */
135 if (mid == 0) mid = 1;
137 for (req=transport->pending_requests; req; req=req->next) {
138 if (req->mid == mid) {
144 transport->next_mid = mid+1;
149 setup the idle handler for a transport
151 void cli_transport_idle_handler(struct cli_transport *transport,
152 void (*idle_func)(struct cli_transport *, void *),
156 transport->idle.func = idle_func;
157 transport->idle.private = private;
158 transport->idle.period = period;
163 determine if a packet is pending for receive on a transport
165 BOOL cli_transport_pending(struct cli_transport *transport)
167 return socket_pending(transport->socket->fd);
173 wait for data on a transport, periodically calling a wait function
174 if one has been defined
175 return True if a packet is received
177 BOOL cli_transport_select(struct cli_transport *transport)
182 struct timeval timeout;
184 fd = transport->socket->fd;
191 uint_t period = 1000;
196 if (transport->idle.func) {
197 period = transport->idle.period;
200 timeout.tv_sec = period / 1000;
201 timeout.tv_usec = 1000*(period%1000);
203 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
206 /* the fd is readable */
211 /* sys_select_intr() already handles EINTR, so this
212 is an error. The socket is probably dead */
216 /* only other possibility is that we timed out - call the idle function
218 if (transport->idle.func) {
219 transport->idle.func(transport, transport->idle.private);
221 } while (selrtn == 0);