r22905: cli_send_mailslot had a message_send_pid inside
[samba.git] / source / libsmb / clidgram.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client dgram calls
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Richard Sharpe 2001
6    Copyright (C) John Terpstra 2001
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26  * cli_send_mailslot, send a mailslot for client code ...
27  */
28
29 BOOL cli_send_mailslot(struct messaging_context *msg_ctx,
30                        BOOL unique, const char *mailslot,
31                        uint16 priority,
32                        char *buf, int len,
33                        const char *srcname, int src_type, 
34                        const char *dstname, int dest_type,
35                        struct in_addr dest_ip)
36 {
37         struct packet_struct p;
38         struct dgram_packet *dgram = &p.packet.dgram;
39         char *ptr, *p2;
40         char tmp[4];
41         pid_t nmbd_pid;
42
43         if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
44                 DEBUG(3, ("No nmbd found\n"));
45                 return False;
46         }
47
48         if (!message_init())
49                 return False;
50
51         memset((char *)&p, '\0', sizeof(p));
52
53         /*
54          * Next, build the DGRAM ...
55          */
56
57         /* DIRECT GROUP or UNIQUE datagram. */
58         dgram->header.msg_type = unique ? 0x10 : 0x11; 
59         dgram->header.flags.node_type = M_NODE;
60         dgram->header.flags.first = True;
61         dgram->header.flags.more = False;
62         dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) +
63                 ((unsigned)sys_getpid()%(unsigned)100);
64         /* source ip is filled by nmbd */
65         dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
66         dgram->header.packet_offset = 0;
67   
68         make_nmb_name(&dgram->source_name,srcname,src_type);
69         make_nmb_name(&dgram->dest_name,dstname,dest_type);
70
71         ptr = &dgram->data[0];
72
73         /* Setup the smb part. */
74         ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
75         memcpy(tmp,ptr,4);
76         set_message(NULL,ptr,17,strlen(mailslot) + 1 + len,True);
77         memcpy(ptr,tmp,4);
78
79         SCVAL(ptr,smb_com,SMBtrans);
80         SSVAL(ptr,smb_vwv1,len);
81         SSVAL(ptr,smb_vwv11,len);
82         SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
83         SSVAL(ptr,smb_vwv13,3);
84         SSVAL(ptr,smb_vwv14,1);
85         SSVAL(ptr,smb_vwv15,priority);
86         SSVAL(ptr,smb_vwv16,2);
87         p2 = smb_buf(ptr);
88         fstrcpy(p2,mailslot);
89         p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
90         if (!p2) {
91                 return False;
92         }
93
94         memcpy(p2,buf,len);
95         p2 += len;
96
97         dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
98
99         p.packet_type = DGRAM_PACKET;
100         p.ip = dest_ip;
101         p.timestamp = time(NULL);
102
103         DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
104                  mailslot, nmb_namestr(&dgram->source_name)));
105         DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name),
106                     inet_ntoa(dest_ip)));
107
108         return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
109                                                   pid_to_procid(nmbd_pid),
110                                                   MSG_SEND_PACKET,
111                                                   (uint8 *)&p, sizeof(p)));
112 }
113
114 /*
115  * cli_get_response: Get a response ...
116  */
117 BOOL cli_get_response(const char *mailslot, char *buf, int bufsiz)
118 {
119         struct packet_struct *p;
120
121         p = receive_unexpected(DGRAM_PACKET, 0, mailslot);
122
123         if (p == NULL)
124                 return False;
125
126         memcpy(buf, &p->packet.dgram.data[92],
127                MIN(bufsiz, p->packet.dgram.datasize-92));
128
129         return True;
130 }
131
132 /*
133  * cli_get_backup_list: Send a get backup list request ...
134  */
135
136 static char cli_backup_list[1024];
137
138 int cli_get_backup_list(struct messaging_context *msg_ctx,
139                         const char *myname, const char *send_to_name)
140 {
141         pstring outbuf;
142         char *p;
143         struct in_addr sendto_ip;
144
145         if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
146
147                 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
148                 return False;
149
150         }
151
152         memset(cli_backup_list, '\0', sizeof(cli_backup_list));
153         memset(outbuf, '\0', sizeof(outbuf));
154
155         p = outbuf;
156
157         SCVAL(p, 0, ANN_GetBackupListReq);
158         p++;
159
160         SCVAL(p, 0, 1); /* Count pointer ... */
161         p++;
162
163         SIVAL(p, 0, 1); /* The sender's token ... */
164         p += 4;
165
166         cli_send_mailslot(msg_ctx, True, "\\MAILSLOT\\BROWSE", 1, outbuf, 
167                           PTR_DIFF(p, outbuf), myname, 0, send_to_name, 
168                           0x1d, sendto_ip);
169
170         /* We should check the error and return if we got one */
171
172         /* Now, get the response ... */
173
174         cli_get_response("\\MAILSLOT\\BROWSE",
175                          cli_backup_list, sizeof(cli_backup_list));
176
177         return True;
178
179 }
180
181 /*
182  * cli_get_backup_server: Get the backup list and retrieve a server from it
183  */
184
185 int cli_get_backup_server(struct messaging_context *msg_ctx,
186                           char *my_name, char *target, char *servername,
187                           int namesize)
188 {
189
190   /* Get the backup list first. We could pull this from the cache later */
191
192   cli_get_backup_list(msg_ctx, my_name, target);  /* FIXME: Check the response */
193
194   if (!cli_backup_list[0]) { /* Empty list ... try again */
195
196     cli_get_backup_list(msg_ctx, my_name, target);
197
198   }
199
200   strncpy(servername, cli_backup_list, MIN(16, namesize));
201
202   return True;
203
204 }