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