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