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