[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22
23 /****************************************************************************
24 start a message sequence
25 ****************************************************************************/
26 int cli_message_start_build(struct cli_state *cli, char *host, char *username)
27 {
28         char *p;
29
30         /* construct a SMBsendstrt command */
31         memset(cli->outbuf,'\0',smb_size);
32         set_message(cli->outbuf,0,0,True);
33         SCVAL(cli->outbuf,smb_com,SMBsendstrt);
34         SSVAL(cli->outbuf,smb_tid,cli->cnum);
35         cli_setup_packet(cli);
36         
37         p = smb_buf(cli->outbuf);
38         *p++ = 4;
39         p += clistr_push(cli, p, username, -1, STR_ASCII|STR_TERMINATE);
40         *p++ = 4;
41         p += clistr_push(cli, p, host, -1, STR_ASCII|STR_TERMINATE);
42         
43         cli_setup_bcc(cli, p);
44
45         return(PTR_DIFF(p, cli->outbuf));
46 }
47
48 BOOL cli_message_start(struct cli_state *cli, char *host, char *username,
49                               int *grp)
50 {
51         cli_message_start_build(cli, host, username);
52         
53         cli_send_smb(cli);      
54         
55         if (!cli_receive_smb(cli)) {
56                 return False;
57         }
58
59         if (cli_is_error(cli)) return False;
60
61         *grp = SVAL(cli->inbuf,smb_vwv0);
62
63         return True;
64 }
65
66
67 /****************************************************************************
68 send a message 
69 ****************************************************************************/
70 int cli_message_text_build(struct cli_state *cli, char *msg, int len, int grp)
71 {
72         char *msgdos;
73         int lendos;
74         char *p;
75
76         memset(cli->outbuf,'\0',smb_size);
77         set_message(cli->outbuf,1,0,True);
78         SCVAL(cli->outbuf,smb_com,SMBsendtxt);
79         SSVAL(cli->outbuf,smb_tid,cli->cnum);
80         cli_setup_packet(cli);
81
82         SSVAL(cli->outbuf,smb_vwv0,grp);
83         
84         p = smb_buf(cli->outbuf);
85         *p++ = 1;
86
87         if ((lendos = (int)convert_string_allocate(NULL,CH_UNIX, CH_DOS, msg,len, (void **)(void *)&msgdos, True)) < 0 || !msgdos) {
88                 DEBUG(3,("Conversion failed, sending message in UNIX charset\n"));
89                 SSVAL(p, 0, len); p += 2;
90                 memcpy(p, msg, len);
91                 p += len;
92         } else {
93                 SSVAL(p, 0, lendos); p += 2;
94                 memcpy(p, msgdos, lendos);
95                 p += lendos;
96                 SAFE_FREE(msgdos);
97         }
98
99         cli_setup_bcc(cli, p);
100
101         return(PTR_DIFF(p, cli->outbuf));
102 }
103
104 BOOL cli_message_text(struct cli_state *cli, char *msg, int len, int grp)
105 {
106         cli_message_text_build(cli, msg, len, grp);
107
108         cli_send_smb(cli);
109
110         if (!cli_receive_smb(cli)) {
111                 return False;
112         }
113
114         if (cli_is_error(cli)) return False;
115
116         return True;
117 }      
118
119 /****************************************************************************
120 end a message 
121 ****************************************************************************/
122 int cli_message_end_build(struct cli_state *cli, int grp)
123 {
124         char *p;
125
126         memset(cli->outbuf,'\0',smb_size);
127         set_message(cli->outbuf,1,0,True);
128         SCVAL(cli->outbuf,smb_com,SMBsendend);
129         SSVAL(cli->outbuf,smb_tid,cli->cnum);
130
131         SSVAL(cli->outbuf,smb_vwv0,grp);
132
133         cli_setup_packet(cli);
134
135         p = smb_buf(cli->outbuf);
136
137         return(PTR_DIFF(p, cli->outbuf));
138 }
139
140 BOOL cli_message_end(struct cli_state *cli, int grp)
141 {
142         cli_message_end_build(cli, grp);
143
144         cli_send_smb(cli);
145
146         if (!cli_receive_smb(cli)) {
147                 return False;
148         }
149
150         if (cli_is_error(cli)) return False;
151
152         return True;
153 }