2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Richard Sharpe 2001
6 Copyright (C) John Terpstra 2001
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * cli_send_mailslot, send a mailslot for client code ...
28 bool cli_send_mailslot(struct messaging_context *msg_ctx,
29 bool unique, const char *mailslot,
32 const char *srcname, int src_type,
33 const char *dstname, int dest_type,
34 const struct sockaddr_storage *dest_ss)
36 struct packet_struct p;
37 struct dgram_packet *dgram = &p.packet.dgram;
41 char addr[INET6_ADDRSTRLEN];
43 if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
44 DEBUG(3, ("No nmbd found\n"));
48 if (dest_ss->ss_family != AF_INET) {
49 DEBUG(3, ("cli_send_mailslot: can't send to IPv6 address.\n"));
53 memset((char *)&p, '\0', sizeof(p));
56 * Next, build the DGRAM ...
59 /* DIRECT GROUP or UNIQUE datagram. */
60 dgram->header.msg_type = unique ? 0x10 : 0x11;
61 dgram->header.flags.node_type = M_NODE;
62 dgram->header.flags.first = True;
63 dgram->header.flags.more = False;
64 dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) +
65 ((unsigned)sys_getpid()%(unsigned)100);
66 /* source ip is filled by nmbd */
67 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
68 dgram->header.packet_offset = 0;
70 make_nmb_name(&dgram->source_name,srcname,src_type);
71 make_nmb_name(&dgram->dest_name,dstname,dest_type);
73 ptr = &dgram->data[0];
75 /* Setup the smb part. */
76 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
78 set_message(ptr,17,strlen(mailslot) + 1 + len,True);
81 SCVAL(ptr,smb_com,SMBtrans);
82 SSVAL(ptr,smb_vwv1,len);
83 SSVAL(ptr,smb_vwv11,len);
84 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
85 SSVAL(ptr,smb_vwv13,3);
86 SSVAL(ptr,smb_vwv14,1);
87 SSVAL(ptr,smb_vwv15,priority);
88 SSVAL(ptr,smb_vwv16,2);
91 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
99 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
101 p.packet_type = DGRAM_PACKET;
102 p.ip = ((const struct sockaddr_in *)&dest_ss)->sin_addr;
103 p.timestamp = time(NULL);
105 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
106 mailslot, nmb_namestr(&dgram->source_name)));
107 print_sockaddr(addr, sizeof(addr), dest_ss);
109 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
111 return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
112 pid_to_procid(nmbd_pid),
114 (uint8 *)&p, sizeof(p)));
118 * cli_get_response: Get a response ...
120 bool cli_get_response(const char *mailslot, char *buf, int bufsiz)
122 struct packet_struct *p;
124 p = receive_unexpected(DGRAM_PACKET, 0, mailslot);
129 memcpy(buf, &p->packet.dgram.data[92],
130 MIN(bufsiz, p->packet.dgram.datasize-92));
136 * cli_get_backup_list: Send a get backup list request ...
139 static char cli_backup_list[1024];
141 int cli_get_backup_list(struct messaging_context *msg_ctx,
142 const char *myname, const char *send_to_name)
146 struct sockaddr_storage sendto_ss;
148 if (!resolve_name(send_to_name, &sendto_ss, 0x1d)) {
150 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
155 memset(cli_backup_list, '\0', sizeof(cli_backup_list));
156 memset(outbuf, '\0', sizeof(outbuf));
160 SCVAL(p, 0, ANN_GetBackupListReq);
163 SCVAL(p, 0, 1); /* Count pointer ... */
166 SIVAL(p, 0, 1); /* The sender's token ... */
169 cli_send_mailslot(msg_ctx, True, "\\MAILSLOT\\BROWSE", 1, outbuf,
170 PTR_DIFF(p, outbuf), myname, 0, send_to_name,
173 /* We should check the error and return if we got one */
175 /* Now, get the response ... */
177 cli_get_response("\\MAILSLOT\\BROWSE",
178 cli_backup_list, sizeof(cli_backup_list));
185 * cli_get_backup_server: Get the backup list and retrieve a server from it
188 int cli_get_backup_server(struct messaging_context *msg_ctx,
189 char *my_name, char *target, char *servername,
193 /* Get the backup list first. We could pull this from the cache later */
195 cli_get_backup_list(msg_ctx, my_name, target); /* FIXME: Check the response */
197 if (!cli_backup_list[0]) { /* Empty list ... try again */
199 cli_get_backup_list(msg_ctx, my_name, target);
203 strncpy(servername, cli_backup_list, MIN(16, namesize));