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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * cli_send_mailslot, send a mailslot for client code ...
29 int cli_send_mailslot(int dgram_sock
, BOOL unique
, const char *mailslot
,
31 const char *srcname
, int src_type
,
32 const char *dstname
, int dest_type
,
33 struct in_addr dest_ip
, struct in_addr src_ip
,
34 int dest_port
, int src_port
)
36 struct packet_struct p
;
37 struct dgram_packet
*dgram
= &p
.packet
.dgram
;
41 memset((char *)&p
, '\0', sizeof(p
));
44 * Next, build the DGRAM ...
47 /* DIRECT GROUP or UNIQUE datagram. */
48 dgram
->header
.msg_type
= unique
? 0x10 : 0x11;
49 dgram
->header
.flags
.node_type
= M_NODE
;
50 dgram
->header
.flags
.first
= True
;
51 dgram
->header
.flags
.more
= False
;
52 dgram
->header
.dgm_id
= ((unsigned)time(NULL
)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
53 dgram
->header
.source_ip
.s_addr
= src_ip
.s_addr
;
54 dgram
->header
.source_port
= ntohs(src_port
);
55 dgram
->header
.dgm_length
= 0; /* Let build_dgram() handle this. */
56 dgram
->header
.packet_offset
= 0;
58 make_nmb_name(&dgram
->source_name
,srcname
,src_type
);
59 make_nmb_name(&dgram
->dest_name
,dstname
,dest_type
);
61 ptr
= &dgram
->data
[0];
63 /* Setup the smb part. */
64 ptr
-= 4; /* XXX Ugliness because of handling of tcp SMB length. */
66 set_message(ptr
,17,17 + len
,True
);
69 SCVAL(ptr
,smb_com
,SMBtrans
);
70 SSVAL(ptr
,smb_vwv1
,len
);
71 SSVAL(ptr
,smb_vwv11
,len
);
72 SSVAL(ptr
,smb_vwv12
,70 + strlen(mailslot
));
73 SSVAL(ptr
,smb_vwv13
,3);
74 SSVAL(ptr
,smb_vwv14
,1);
75 SSVAL(ptr
,smb_vwv15
,1);
76 SSVAL(ptr
,smb_vwv16
,2);
79 p2
= skip_string(p2
,1);
84 dgram
->datasize
= PTR_DIFF(p2
,ptr
+4); /* +4 for tcp length. */
89 p
.timestamp
= time(NULL
);
90 p
.packet_type
= DGRAM_PACKET
;
92 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot
,
93 nmb_namestr(&dgram
->source_name
), inet_ntoa(src_ip
)));
94 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram
->dest_name
), inet_ntoa(dest_ip
)));
96 return send_packet(&p
);
101 * cli_get_response: Get a response ...
103 int cli_get_response(int dgram_sock
, BOOL unique
, const char *mailslot
, char *buf
, int bufsiz
)
105 struct packet_struct
*packet
;
107 packet
= receive_dgram_packet(dgram_sock
, 5, mailslot
);
109 if (packet
) { /* We got one, pull what we want out of the SMB data ... */
111 struct dgram_packet
*dgram
= &packet
->packet
.dgram
;
114 * We should probably parse the SMB, but for now, we will pull what
115 * from fixed, known locations ...
118 /* Copy the data to buffer, respecting sizes ... */
120 memcpy(buf
, &dgram
->data
[92], MIN(bufsiz
, (dgram
->datasize
- 92)));
131 * cli_get_backup_list: Send a get backup list request ...
134 static char cli_backup_list
[1024];
136 int cli_get_backup_list(const char *myname
, const char *send_to_name
)
140 struct in_addr sendto_ip
, my_ip
;
142 struct sockaddr_in sock_out
;
145 if (!resolve_name(send_to_name
, &sendto_ip
, 0x1d)) {
147 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name
));
152 my_ip
.s_addr
= inet_addr("0.0.0.0");
154 if (!resolve_name(myname
, &my_ip
, 0x00)) { /* FIXME: Call others here */
156 DEBUG(0, ("Could not resolve name: %s<00>\n", myname
));
160 if ((dgram_sock
= open_socket_out(SOCK_DGRAM
, &sendto_ip
, 138, LONG_CONNECT_TIMEOUT
)) < 0) {
162 DEBUG(4, ("open_sock_out failed ..."));
167 /* Make it a broadcast socket ... */
169 set_socket_options(dgram_sock
, "SO_BROADCAST");
171 /* Make it non-blocking??? */
173 if (fcntl(dgram_sock
, F_SETFL
, O_NONBLOCK
) < 0) {
175 DEBUG(0, ("Unable to set non blocking on dgram sock\n"));
179 /* Now, bind a local addr to it ... Try port 138 first ... */
181 memset((char *)&sock_out
, '\0', sizeof(sock_out
));
182 sock_out
.sin_addr
.s_addr
= INADDR_ANY
;
183 sock_out
.sin_port
= htons(138);
184 sock_out
.sin_family
= AF_INET
;
186 if (bind(dgram_sock
, (struct sockaddr
*)&sock_out
, sizeof(sock_out
)) < 0) {
188 /* Try again on any port ... */
190 sock_out
.sin_port
= INADDR_ANY
;
192 if (bind(dgram_sock
, (struct sockaddr
*)&sock_out
, sizeof(sock_out
)) < 0) {
194 DEBUG(4, ("failed to bind socket to address ...\n"));
201 /* Now, figure out what socket name we were bound to. We want the port */
203 name_size
= sizeof(sock_out
);
205 getsockname(dgram_sock
, (struct sockaddr
*)&sock_out
, &name_size
);
207 DEBUG(5, ("Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out
.sin_addr
), ntohs(sock_out
.sin_port
)));
209 /* Now, build the request */
211 memset(cli_backup_list
, '\0', sizeof(cli_backup_list
));
212 memset(outbuf
, '\0', sizeof(outbuf
));
216 SCVAL(p
, 0, ANN_GetBackupListReq
);
219 SCVAL(p
, 0, 1); /* Count pointer ... */
222 SIVAL(p
, 0, 1); /* The sender's token ... */
225 cli_send_mailslot(dgram_sock
, True
, "\\MAILSLOT\\BROWSE", outbuf
,
226 PTR_DIFF(p
, outbuf
), myname
, 0, send_to_name
,
227 0x1d, sendto_ip
, my_ip
, 138, sock_out
.sin_port
);
229 /* We should check the error and return if we got one */
231 /* Now, get the response ... */
233 cli_get_response(dgram_sock
, True
, "\\MAILSLOT\\BROWSE", cli_backup_list
, sizeof(cli_backup_list
));
235 /* Should check the response here ... FIXME */
244 * cli_get_backup_server: Get the backup list and retrieve a server from it
247 int cli_get_backup_server(char *my_name
, char *target
, char *servername
, int namesize
)
250 /* Get the backup list first. We could pull this from the cache later */
252 cli_get_backup_list(my_name
, target
); /* FIXME: Check the response */
254 if (!cli_backup_list
[0]) { /* Empty list ... try again */
256 cli_get_backup_list(my_name
, target
);
260 strncpy(servername
, cli_backup_list
, MIN(16, namesize
));