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