2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Richard Sharpe 2001
7 Copyright (C) John Terpstra 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * cli_send_mailslot, send a mailslot for client code ...
32 int cli_send_mailslot(int dgram_sock
, BOOL unique
, char *mailslot
,
34 const char *srcname
, int src_type
,
35 const char *dstname
, int dest_type
,
36 struct in_addr dest_ip
, struct in_addr src_ip
,
37 int dest_port
, int src_port
)
39 struct packet_struct p
;
40 struct dgram_packet
*dgram
= &p
.packet
.dgram
;
44 memset((char *)&p
, '\0', sizeof(p
));
47 * Next, build the DGRAM ...
50 /* DIRECT GROUP or UNIQUE datagram. */
51 dgram
->header
.msg_type
= unique
? 0x10 : 0x11;
52 dgram
->header
.flags
.node_type
= M_NODE
;
53 dgram
->header
.flags
.first
= True
;
54 dgram
->header
.flags
.more
= False
;
55 dgram
->header
.dgm_id
= ((unsigned)time(NULL
)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
56 dgram
->header
.source_ip
.s_addr
= src_ip
.s_addr
;
57 /*fprintf(stderr, "Source IP = %0X\n", dgram->header.source_ip); */
58 dgram
->header
.source_port
= ntohs(src_port
);
59 fprintf(stderr
, "Source Port = %0X\n", dgram
->header
.source_port
);
60 dgram
->header
.dgm_length
= 0; /* Let build_dgram() handle this. */
61 dgram
->header
.packet_offset
= 0;
63 make_nmb_name(&dgram
->source_name
,srcname
,src_type
);
64 make_nmb_name(&dgram
->dest_name
,dstname
,dest_type
);
66 ptr
= &dgram
->data
[0];
68 /* Setup the smb part. */
69 ptr
-= 4; /* XXX Ugliness because of handling of tcp SMB length. */
71 set_message(ptr
,17,17 + len
,True
);
74 SCVAL(ptr
,smb_com
,SMBtrans
);
75 SSVAL(ptr
,smb_vwv1
,len
);
76 SSVAL(ptr
,smb_vwv11
,len
);
77 SSVAL(ptr
,smb_vwv12
,70 + strlen(mailslot
));
78 SSVAL(ptr
,smb_vwv13
,3);
79 SSVAL(ptr
,smb_vwv14
,1);
80 SSVAL(ptr
,smb_vwv15
,1);
81 SSVAL(ptr
,smb_vwv16
,2);
84 p2
= skip_string(p2
,1);
89 dgram
->datasize
= PTR_DIFF(p2
,ptr
+4); /* +4 for tcp length. */
94 p
.timestamp
= time(NULL
);
95 p
.packet_type
= DGRAM_PACKET
;
97 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot
,
98 nmb_namestr(&dgram
->source_name
), inet_ntoa(src_ip
)));
99 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram
->dest_name
), inet_ntoa(dest_ip
)));
101 return send_packet(&p
);
106 * cli_get_response: Get a response ...
108 int cli_get_response(int dgram_sock
, BOOL unique
, char *mailslot
, char *buf
, int bufsiz
)
110 struct packet_struct
*packet
;
112 packet
= receive_dgram_packet(dgram_sock
, 5, mailslot
);
114 if (packet
) { /* We got one, pull what we want out of the SMB data ... */
116 struct dgram_packet
*dgram
= &packet
->packet
.dgram
;
119 * We should probably parse the SMB, but for now, we will pull what
120 * from fixed, known locations ...
123 /* Copy the data to buffer, respecting sizes ... */
125 memcpy(buf
, &dgram
->data
[92], MIN(bufsiz
, (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(const char *myname
, const char *send_to_name
)
145 struct in_addr sendto_ip
, my_ip
;
147 struct sockaddr_in sock_out
;
150 if (!resolve_name(send_to_name
, &sendto_ip
, 0x1d)) {
152 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name
));
157 my_ip
.s_addr
= inet_addr("0.0.0.0");
159 if (!resolve_name(myname
, &my_ip
, 0x00)) { /* FIXME: Call others here */
161 DEBUG(0, ("Could not resolve name: %s<00>\n", myname
));
165 if ((dgram_sock
= open_socket_out(SOCK_DGRAM
, &sendto_ip
, 138, LONG_CONNECT_TIMEOUT
)) < 0) {
167 DEBUG(4, ("open_sock_out failed ..."));
172 /* Make it a broadcast socket ... */
174 set_socket_options(dgram_sock
, "SO_BROADCAST");
176 /* Make it non-blocking??? */
178 if (fcntl(dgram_sock
, F_SETFL
, O_NONBLOCK
) < 0) {
180 DEBUG(0, ("Unable to set non blocking on dgram sock\n"));
184 /* Now, bind a local addr to it ... Try port 138 first ... */
186 memset((char *)&sock_out
, '\0', sizeof(sock_out
));
187 sock_out
.sin_addr
.s_addr
= INADDR_ANY
;
188 sock_out
.sin_port
= htons(138);
189 sock_out
.sin_family
= AF_INET
;
191 if (bind(dgram_sock
, (struct sockaddr
*)&sock_out
, sizeof(sock_out
)) < 0) {
193 /* Try again on any port ... */
195 sock_out
.sin_port
= INADDR_ANY
;
197 if (bind(dgram_sock
, (struct sockaddr
*)&sock_out
, sizeof(sock_out
)) < 0) {
199 DEBUG(4, ("failed to bind socket to address ...\n"));
206 /* Now, figure out what socket name we were bound to. We want the port */
208 name_size
= sizeof(sock_out
);
210 getsockname(dgram_sock
, (struct sockaddr
*)&sock_out
, &name_size
);
212 DEBUG(5, ("Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out
.sin_addr
), ntohs(sock_out
.sin_port
)));
214 /* Now, build the request */
216 memset(cli_backup_list
, '\0', sizeof(cli_backup_list
));
217 memset(outbuf
, '\0', sizeof(outbuf
));
221 SCVAL(p
, 0, ANN_GetBackupListReq
);
224 SCVAL(p
, 0, 1); /* Count pointer ... */
227 SIVAL(p
, 0, 1); /* The sender's token ... */
230 cli_send_mailslot(dgram_sock
, True
, "\\MAILSLOT\\BROWSE", outbuf
,
231 PTR_DIFF(p
, outbuf
), myname
, 0, send_to_name
,
232 0x1d, sendto_ip
, my_ip
, 138, sock_out
.sin_port
);
234 /* We should check the error and return if we got one */
236 /* Now, get the response ... */
238 cli_get_response(dgram_sock
, True
, "\\MAILSLOT\\BROWSE", cli_backup_list
, sizeof(cli_backup_list
));
240 /* Should check the response here ... FIXME */
249 * cli_get_backup_server: Get the backup list and retrieve a server from it
252 int cli_get_backup_server(char *my_name
, char *target
, char *servername
, int namesize
)
255 /* Get the backup list first. We could pull this from the cache later */
257 cli_get_backup_list(my_name
, target
); /* FIXME: Check the response */
259 if (!cli_backup_list
[0]) { /* Empty list ... try again */
261 cli_get_backup_list(my_name
, target
);
265 strncpy(servername
, cli_backup_list
, MIN(16, namesize
));