a378ac8aad79a604710e7a06484fb7a09e81222e
[jelmer/samba4-debian.git] / source / libcli / raw / clitransport.c
1 /* 
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>
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /*
25   create a transport structure based on an established socket
26 */
27 struct cli_transport *cli_transport_init(struct cli_socket *sock)
28 {
29         TALLOC_CTX *mem_ctx;
30         struct cli_transport *transport;
31
32         mem_ctx = talloc_init("cli_transport");
33         if (!mem_ctx) return NULL;
34
35         transport = talloc_zero(mem_ctx, sizeof(*transport));
36         if (!transport) return NULL;
37
38         transport->mem_ctx = mem_ctx;
39         transport->socket = sock;
40         transport->negotiate.protocol = PROTOCOL_NT1;
41         transport->options.use_spnego = lp_use_spnego();
42         transport->negotiate.max_xmit = ~0;
43         cli_null_set_signing(transport);
44         transport->socket->reference_count++;
45
46         ZERO_STRUCT(transport->called);
47
48         return transport;
49 }
50
51 /*
52   decrease reference count on a transport, and destroy if it becomes
53   zero
54 */
55 void cli_transport_close(struct cli_transport *transport)
56 {
57         transport->reference_count--;
58         if (transport->reference_count <= 0) {
59                 cli_sock_close(transport->socket);
60                 talloc_destroy(transport->mem_ctx);
61         }
62 }
63
64 /*
65   mark the transport as dead
66 */
67 void cli_transport_dead(struct cli_transport *transport)
68 {
69         cli_sock_dead(transport->socket);
70 }
71
72
73
74 /****************************************************************************
75 send a session request (if appropriate)
76 ****************************************************************************/
77 BOOL cli_transport_connect(struct cli_transport *transport,
78                            struct nmb_name *calling, 
79                            struct nmb_name *called)
80 {
81         char *p;
82         int len = NBT_HDR_SIZE;
83         struct cli_request *req;
84
85         if (called) {
86                 transport->called = *called;
87         }
88
89         /* 445 doesn't have session request */
90         if (transport->socket->port == 445) {
91                 return True;
92         }
93
94         /* allocate output buffer */
95         req = cli_request_setup_nonsmb(transport, NBT_HDR_SIZE + 2*nbt_mangled_name_len());
96
97         /* put in the destination name */
98         p = req->out.buffer + NBT_HDR_SIZE;
99         name_mangle(called->name, p, called->name_type);
100         len += name_len(p);
101
102         /* and my name */
103         p = req->out.buffer+len;
104         name_mangle(calling->name, p, calling->name_type);
105         len += name_len(p);
106
107         _smb_setlen(req->out.buffer,len-4);
108         SCVAL(req->out.buffer,0,0x81);
109
110         if (!cli_request_send(req) ||
111             !cli_request_receive(req)) {
112                 cli_request_destroy(req);
113                 return False;
114         }
115         
116         if (CVAL(req->in.buffer,0) != 0x82) {
117                 transport->error.etype = ETYPE_NBT;
118                 transport->error.e.nbt_error = CVAL(req->in.buffer,4);
119                 cli_request_destroy(req);
120                 return False;
121         }
122
123         cli_request_destroy(req);
124         return True;
125 }
126
127
128 /****************************************************************************
129 get next mid in sequence
130 ****************************************************************************/
131 uint16_t cli_transport_next_mid(struct cli_transport *transport)
132 {
133         uint16_t mid;
134         struct cli_request *req;
135
136         mid = transport->next_mid;
137
138 again:
139         /* now check to see if this mid is being used by one of the 
140            pending requests. This is quite efficient because the list is
141            usually very short */
142
143         /* the zero mid is reserved for requests that don't have a mid */
144         if (mid == 0) mid = 1;
145
146         for (req=transport->pending_requests; req; req=req->next) {
147                 if (req->mid == mid) {
148                         mid++;
149                         goto again;
150                 }
151         }
152
153         transport->next_mid = mid+1;
154         return mid;
155 }
156
157 /*
158   setup the idle handler for a transport
159 */
160 void cli_transport_idle_handler(struct cli_transport *transport, 
161                                 void (*idle_func)(struct cli_transport *, void *),
162                                 uint_t period,
163                                 void *private)
164 {
165         transport->idle.func = idle_func;
166         transport->idle.private = private;
167         transport->idle.period = period;
168 }
169
170
171 /*
172   determine if a packet is pending for receive on a transport
173 */
174 BOOL cli_transport_pending(struct cli_transport *transport)
175 {
176         return socket_pending(transport->socket->fd);
177 }
178
179
180
181 /*
182   wait for data on a transport, periodically calling a wait function
183   if one has been defined
184   return True if a packet is received
185 */
186 BOOL cli_transport_select(struct cli_transport *transport)
187 {
188         fd_set fds;
189         int selrtn;
190         int fd;
191         struct timeval timeout;
192
193         fd = transport->socket->fd;
194
195         if (fd == -1) {
196                 return False;
197         }
198
199         do {
200                 uint_t period = 1000;
201
202                 FD_ZERO(&fds);
203                 FD_SET(fd,&fds);
204         
205                 if (transport->idle.func) {
206                         period = transport->idle.period;
207                 }
208
209                 timeout.tv_sec = period / 1000;
210                 timeout.tv_usec = 1000*(period%1000);
211                 
212                 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
213                 
214                 if (selrtn == 1) {
215                         /* the fd is readable */
216                         return True;
217                 }
218                 
219                 if (selrtn == -1) {
220                         /* sys_select_intr() already handles EINTR, so this
221                            is an error. The socket is probably dead */
222                         return False;
223                 }
224                 
225                 /* only other possibility is that we timed out - call the idle function
226                    if there is one */
227                 if (transport->idle.func) {
228                         transport->idle.func(transport, transport->idle.private);
229                 }
230         } while (selrtn == 0);
231
232         return True;
233 }
234