2 Unix SMB/Netbios implementation.
4 SMB client generic functions
5 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
27 extern int DEBUGLEVEL;
28 static void cli_process_oplock(struct cli_state *cli);
31 * Change the port number used to call on
33 int cli_set_port(struct cli_state *cli, int port)
41 /****************************************************************************
43 ****************************************************************************/
44 BOOL cli_receive_smb(struct cli_state *cli)
48 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
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;
66 /****************************************************************************
67 send an smb to a fd and re-establish if necessary
68 ****************************************************************************/
69 BOOL cli_send_smb(struct cli_state *cli)
74 BOOL reestablished=False;
76 len = smb_len(cli->outbuf) + 4;
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)) {
88 DEBUG(0,("Error writing %d bytes to client. %d\n",
98 /****************************************************************************
99 setup basics in a outgoing packet
100 ****************************************************************************/
101 void cli_setup_packet(struct cli_state *cli)
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);
116 /****************************************************************************
117 process an oplock break request from the server
118 ****************************************************************************/
119 static void cli_process_oplock(struct cli_state *cli)
121 char *oldbuf = cli->outbuf;
125 fnum = SVAL(cli->inbuf,smb_vwv2);
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;
134 memset(buf,'\0',smb_size);
135 set_message(buf,8,0,True);
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 */
150 cli->outbuf = oldbuf;
154 /****************************************************************************
155 initialise a client structure
156 ****************************************************************************/
157 struct cli_state *cli_initialise(struct cli_state *cli)
160 cli = (struct cli_state *)malloc(sizeof(*cli));
166 if (cli->initialised) {
175 cli->pid = (uint16)getpid();
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)
189 memset(cli->outbuf, '\0', cli->bufsize);
190 memset(cli->inbuf, '\0', cli->bufsize);
192 cli->initialised = 1;
197 /****************************************************************************
198 shutdown a client structure
199 ****************************************************************************/
200 void cli_shutdown(struct cli_state *cli)
212 sslutil_disconnect(cli->fd);
213 #endif /* WITH_SSL */
216 memset(cli, 0, sizeof(*cli));
220 /****************************************************************************
221 set socket options on a open connection
222 ****************************************************************************/
223 void cli_sockopt(struct cli_state *cli, char *options)
225 set_socket_options(cli->fd, options);
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)
233 uint16 ret = cli->pid;