trying to get HEAD building again. If you want the code
[gd/samba-autobuild/.git] / source3 / libsmb / climessage.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client message handling routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25
26 /****************************************************************************
27 start a message sequence
28 ****************************************************************************/
29 int cli_message_start_build(struct cli_state *cli, char *host, char *username)
30 {
31         char *p;
32
33         /* construct a SMBsendstrt command */
34         memset(cli->outbuf,'\0',smb_size);
35         set_message(cli->outbuf,0,0,True);
36         SCVAL(cli->outbuf,smb_com,SMBsendstrt);
37         SSVAL(cli->outbuf,smb_tid,cli->cnum);
38         cli_setup_packet(cli);
39         
40         p = smb_buf(cli->outbuf);
41         *p++ = 4;
42         p += clistr_push(cli, p, username, -1, STR_ASCII|STR_TERMINATE);
43         *p++ = 4;
44         p += clistr_push(cli, p, host, -1, STR_ASCII|STR_TERMINATE);
45         
46         cli_setup_bcc(cli, p);
47
48         return(PTR_DIFF(p, cli->outbuf));
49 }
50
51 BOOL cli_message_start(struct cli_state *cli, char *host, char *username,
52                               int *grp)
53 {
54         cli_message_start_build(cli, host, username);
55         
56         cli_send_smb(cli);      
57         
58         if (!cli_receive_smb(cli)) {
59                 return False;
60         }
61
62         if (cli_is_error(cli)) return False;
63
64         *grp = SVAL(cli->inbuf,smb_vwv0);
65
66         return True;
67 }
68
69
70 /****************************************************************************
71 send a message 
72 ****************************************************************************/
73 int cli_message_text_build(struct cli_state *cli, char *msg, int len, int grp)
74 {
75         char *msgdos;
76         int lendos;
77         char *p;
78
79         memset(cli->outbuf,'\0',smb_size);
80         set_message(cli->outbuf,1,0,True);
81         SCVAL(cli->outbuf,smb_com,SMBsendtxt);
82         SSVAL(cli->outbuf,smb_tid,cli->cnum);
83         cli_setup_packet(cli);
84
85         SSVAL(cli->outbuf,smb_vwv0,grp);
86         
87         p = smb_buf(cli->outbuf);
88         *p++ = 1;
89
90         if ((lendos = convert_string_allocate(CH_UNIX, CH_DOS, msg,len, (void **) &msgdos)) < 0 || !msgdos) {
91                 DEBUG(3,("Conversion failed, sending message in UNIX charset\n"));
92                 SSVAL(p, 0, len); p += 2;
93                 memcpy(p, msg, len);
94                 p += len;
95         } else {
96                 SSVAL(p, 0, lendos); p += 2;
97                 memcpy(p, msgdos, lendos);
98                 p += lendos;
99                 SAFE_FREE(msgdos);
100         }
101
102         cli_setup_bcc(cli, p);
103
104         return(PTR_DIFF(p, cli->outbuf));
105 }
106
107 BOOL cli_message_text(struct cli_state *cli, char *msg, int len, int grp)
108 {
109         cli_message_text_build(cli, msg, len, grp);
110
111         cli_send_smb(cli);
112
113         if (!cli_receive_smb(cli)) {
114                 return False;
115         }
116
117         if (cli_is_error(cli)) return False;
118
119         return True;
120 }      
121
122 /****************************************************************************
123 end a message 
124 ****************************************************************************/
125 int cli_message_end_build(struct cli_state *cli, int grp)
126 {
127         char *p;
128
129         memset(cli->outbuf,'\0',smb_size);
130         set_message(cli->outbuf,1,0,True);
131         SCVAL(cli->outbuf,smb_com,SMBsendend);
132         SSVAL(cli->outbuf,smb_tid,cli->cnum);
133
134         SSVAL(cli->outbuf,smb_vwv0,grp);
135
136         cli_setup_packet(cli);
137
138         p = smb_buf(cli->outbuf);
139
140         return(PTR_DIFF(p, cli->outbuf));
141 }
142
143 BOOL cli_message_end(struct cli_state *cli, int grp)
144 {
145         cli_message_end_build(cli, grp);
146
147         cli_send_smb(cli);
148
149         if (!cli_receive_smb(cli)) {
150                 return False;
151         }
152
153         if (cli_is_error(cli)) return False;
154
155         return True;
156 }