Added sys_fork() and sys_getpid() functions to stop the overhead
[ira/wip.git] / source3 / libsmb / clientgen.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client generic functions
5    Copyright (C) Andrew Tridgell 1994-1998
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 #define NO_SYSLOG
23
24 #include "includes.h"
25
26
27 extern int DEBUGLEVEL;
28 static void cli_process_oplock(struct cli_state *cli);
29
30 /*
31  * Change the port number used to call on 
32  */
33 int cli_set_port(struct cli_state *cli, int port)
34 {
35         if (port > 0)
36           cli->port = port;
37
38         return cli->port;
39 }
40
41 /****************************************************************************
42 recv an smb
43 ****************************************************************************/
44 BOOL cli_receive_smb(struct cli_state *cli)
45 {
46         BOOL ret;
47  again:
48         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
49         
50         if (ret) {
51                 /* it might be an oplock break request */
52                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
53                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
54                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
55                     SVAL(cli->inbuf,smb_vwv7) == 0) {
56                         if (cli->use_oplocks) cli_process_oplock(cli);
57                         /* try to prevent loops */
58                         CVAL(cli->inbuf,smb_com) = 0xFF;
59                         goto again;
60                 }
61         }
62
63         return ret;
64 }
65
66 /****************************************************************************
67   send an smb to a fd and re-establish if necessary
68 ****************************************************************************/
69 BOOL cli_send_smb(struct cli_state *cli)
70 {
71         size_t len;
72         size_t nwritten=0;
73         ssize_t ret;
74         BOOL reestablished=False;
75
76         len = smb_len(cli->outbuf) + 4;
77
78         while (nwritten < len) {
79                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
80                 if (ret <= 0 && errno == EPIPE && !reestablished) {
81                         if (cli_reestablish_connection(cli)) {
82                                 reestablished = True;
83                                 nwritten=0;
84                                 continue;
85                         }
86                 }
87                 if (ret <= 0) {
88                         DEBUG(0,("Error writing %d bytes to client. %d\n",
89                                  (int)len,(int)ret));
90                         return False;
91                 }
92                 nwritten += ret;
93         }
94         
95         return True;
96 }
97
98 /****************************************************************************
99 setup basics in a outgoing packet
100 ****************************************************************************/
101 void cli_setup_packet(struct cli_state *cli)
102 {
103         cli->rap_error = 0;
104         cli->nt_error = 0;
105         SSVAL(cli->outbuf,smb_pid,cli->pid);
106         SSVAL(cli->outbuf,smb_uid,cli->vuid);
107         SSVAL(cli->outbuf,smb_mid,cli->mid);
108         if (cli->protocol > PROTOCOL_CORE) {
109                 SCVAL(cli->outbuf,smb_flg,0x8);
110                 SSVAL(cli->outbuf,smb_flg2,0x1);
111         }
112 }
113
114
115
116 /****************************************************************************
117 process an oplock break request from the server
118 ****************************************************************************/
119 static void cli_process_oplock(struct cli_state *cli)
120 {
121         char *oldbuf = cli->outbuf;
122         pstring buf;
123         int fnum;
124
125         fnum = SVAL(cli->inbuf,smb_vwv2);
126
127         /* damn, we really need to keep a record of open files so we
128            can detect a oplock break and a close crossing on the
129            wire. for now this swallows the errors */
130         if (fnum == 0) return;
131
132         cli->outbuf = buf;
133
134         memset(buf,'\0',smb_size);
135         set_message(buf,8,0,True);
136
137         CVAL(buf,smb_com) = SMBlockingX;
138         SSVAL(buf,smb_tid, cli->cnum);
139         cli_setup_packet(cli);
140         SSVAL(buf,smb_vwv0,0xFF);
141         SSVAL(buf,smb_vwv1,0);
142         SSVAL(buf,smb_vwv2,fnum);
143         SSVAL(buf,smb_vwv3,2); /* oplock break ack */
144         SIVAL(buf,smb_vwv4,0); /* timoeut */
145         SSVAL(buf,smb_vwv6,0); /* unlockcount */
146         SSVAL(buf,smb_vwv7,0); /* lockcount */
147
148         cli_send_smb(cli);      
149
150         cli->outbuf = oldbuf;
151 }
152
153
154 /****************************************************************************
155 initialise a client structure
156 ****************************************************************************/
157 struct cli_state *cli_initialise(struct cli_state *cli)
158 {
159         if (!cli) {
160                 cli = (struct cli_state *)malloc(sizeof(*cli));
161                 if (!cli)
162                         return NULL;
163                 ZERO_STRUCTP(cli);
164         }
165
166         if (cli->initialised) {
167                 cli_shutdown(cli);
168         }
169
170         ZERO_STRUCTP(cli);
171
172         cli->port = 0;
173         cli->fd = -1;
174         cli->cnum = -1;
175         cli->pid = (uint16)sys_getpid();
176         cli->mid = 1;
177         cli->vuid = UID_FIELD_INVALID;
178         cli->protocol = PROTOCOL_NT1;
179         cli->timeout = 20000; /* Timeout is in milliseconds. */
180         cli->bufsize = CLI_BUFFER_SIZE+4;
181         cli->max_xmit = cli->bufsize;
182         cli->outbuf = (char *)malloc(cli->bufsize);
183         cli->inbuf = (char *)malloc(cli->bufsize);
184         if (!cli->outbuf || !cli->inbuf)
185         {
186                 return False;
187         }
188
189         memset(cli->outbuf, '\0', cli->bufsize);
190         memset(cli->inbuf, '\0', cli->bufsize);
191
192         cli->initialised = 1;
193
194         return cli;
195 }
196
197 /****************************************************************************
198 shutdown a client structure
199 ****************************************************************************/
200 void cli_shutdown(struct cli_state *cli)
201 {
202         if (cli->outbuf)
203         {
204                 free(cli->outbuf);
205         }
206         if (cli->inbuf)
207         {
208                 free(cli->inbuf);
209         }
210 #ifdef WITH_SSL
211     if (cli->fd != -1)
212       sslutil_disconnect(cli->fd);
213 #endif /* WITH_SSL */
214         if (cli->fd != -1) 
215       close(cli->fd);
216         memset(cli, 0, sizeof(*cli));
217 }
218
219
220 /****************************************************************************
221 set socket options on a open connection
222 ****************************************************************************/
223 void cli_sockopt(struct cli_state *cli, char *options)
224 {
225         set_socket_options(cli->fd, options);
226 }
227
228 /****************************************************************************
229 set the PID to use for smb messages. Return the old pid.
230 ****************************************************************************/
231 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
232 {
233         uint16 ret = cli->pid;
234         cli->pid = pid;
235         return ret;
236 }
237