merge from 2.2 and regenerate
[Samba.git] / source / libsmb / clidgram.c
blob1dffe679680700a1f111007fb9db69c13fcc08b7
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 client dgram calls
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.
24 #include "includes.h"
27 * cli_send_mailslot, send a mailslot for client code ...
30 int cli_send_mailslot(int dgram_sock, BOOL unique, char *mailslot,
31 char *buf, int len,
32 const char *srcname, int src_type,
33 const char *dstname, int dest_type,
34 struct in_addr dest_ip, struct in_addr src_ip,
35 int dest_port, int src_port)
37 struct packet_struct p;
38 struct dgram_packet *dgram = &p.packet.dgram;
39 char *ptr, *p2;
40 char tmp[4];
42 memset((char *)&p, '\0', sizeof(p));
45 * Next, build the DGRAM ...
48 /* DIRECT GROUP or UNIQUE datagram. */
49 dgram->header.msg_type = unique ? 0x10 : 0x11;
50 dgram->header.flags.node_type = M_NODE;
51 dgram->header.flags.first = True;
52 dgram->header.flags.more = False;
53 dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
54 dgram->header.source_ip.s_addr = src_ip.s_addr;
55 dgram->header.source_port = ntohs(src_port);
56 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
57 dgram->header.packet_offset = 0;
59 make_nmb_name(&dgram->source_name,srcname,src_type);
60 make_nmb_name(&dgram->dest_name,dstname,dest_type);
62 ptr = &dgram->data[0];
64 /* Setup the smb part. */
65 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
66 memcpy(tmp,ptr,4);
67 set_message(ptr,17,17 + len,True);
68 memcpy(ptr,tmp,4);
70 SCVAL(ptr,smb_com,SMBtrans);
71 SSVAL(ptr,smb_vwv1,len);
72 SSVAL(ptr,smb_vwv11,len);
73 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
74 SSVAL(ptr,smb_vwv13,3);
75 SSVAL(ptr,smb_vwv14,1);
76 SSVAL(ptr,smb_vwv15,1);
77 SSVAL(ptr,smb_vwv16,2);
78 p2 = smb_buf(ptr);
79 pstrcpy(p2,mailslot);
80 p2 = skip_string(p2,1);
82 memcpy(p2,buf,len);
83 p2 += len;
85 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
87 p.ip = dest_ip;
88 p.port = dest_port;
89 p.fd = dgram_sock;
90 p.timestamp = time(NULL);
91 p.packet_type = DGRAM_PACKET;
93 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
94 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
95 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
97 return send_packet(&p);
102 * cli_get_response: Get a response ...
104 int cli_get_response(int dgram_sock, BOOL unique, char *mailslot, char *buf, int bufsiz)
106 struct packet_struct *packet;
108 packet = receive_dgram_packet(dgram_sock, 5, mailslot);
110 if (packet) { /* We got one, pull what we want out of the SMB data ... */
112 struct dgram_packet *dgram = &packet->packet.dgram;
115 * We should probably parse the SMB, but for now, we will pull what
116 * from fixed, known locations ...
119 /* Copy the data to buffer, respecting sizes ... */
121 bcopy(&dgram->data[92], buf, MIN(bufsiz, (dgram->datasize - 92)));
124 else
125 return -1;
127 return 0;
132 * cli_get_backup_list: Send a get backup list request ...
135 static char cli_backup_list[1024];
137 int cli_get_backup_list(const char *myname, const char *send_to_name)
139 char outbuf[15];
140 char *p;
141 struct in_addr sendto_ip, my_ip;
142 int dgram_sock;
143 struct sockaddr_in sock_out;
144 socklen_t name_size;
146 if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
148 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
149 return False;
153 my_ip.s_addr = inet_addr("0.0.0.0");
155 if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
157 DEBUG(0, ("Could not resolve name: %s<00>\n", myname));
161 if ((dgram_sock = open_socket_out(SOCK_DGRAM, &sendto_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
163 DEBUG(4, ("open_sock_out failed ..."));
164 return False;
168 /* Make it a broadcast socket ... */
170 set_socket_options(dgram_sock, "SO_BROADCAST");
172 /* Make it non-blocking??? */
174 if (fcntl(dgram_sock, F_SETFL, O_NONBLOCK) < 0) {
176 DEBUG(0, ("Unable to set non blocking on dgram sock\n"));
180 /* Now, bind a local addr to it ... Try port 138 first ... */
182 memset((char *)&sock_out, '\0', sizeof(sock_out));
183 sock_out.sin_addr.s_addr = INADDR_ANY;
184 sock_out.sin_port = htons(138);
185 sock_out.sin_family = AF_INET;
187 if (bind(dgram_sock, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
189 /* Try again on any port ... */
191 sock_out.sin_port = INADDR_ANY;
193 if (bind(dgram_sock, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
195 DEBUG(4, ("failed to bind socket to address ...\n"));
196 return False;
202 /* Now, figure out what socket name we were bound to. We want the port */
204 name_size = sizeof(sock_out);
206 getsockname(dgram_sock, (struct sockaddr *)&sock_out, &name_size);
208 DEBUG(5, ("Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port)));
210 /* Now, build the request */
212 memset(cli_backup_list, '\0', sizeof(cli_backup_list));
213 memset(outbuf, '\0', sizeof(outbuf));
215 p = outbuf;
217 SCVAL(p, 0, ANN_GetBackupListReq);
218 p++;
220 SCVAL(p, 0, 1); /* Count pointer ... */
221 p++;
223 SIVAL(p, 0, 1); /* The sender's token ... */
224 p += 4;
226 cli_send_mailslot(dgram_sock, True, "\\MAILSLOT\\BROWSE", outbuf,
227 PTR_DIFF(p, outbuf), myname, 0, send_to_name,
228 0x1d, sendto_ip, my_ip, 138, sock_out.sin_port);
230 /* We should check the error and return if we got one */
232 /* Now, get the response ... */
234 cli_get_response(dgram_sock, True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
236 /* Should check the response here ... FIXME */
238 close(dgram_sock);
240 return True;
245 * cli_get_backup_server: Get the backup list and retrieve a server from it
248 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
251 /* Get the backup list first. We could pull this from the cache later */
253 cli_get_backup_list(my_name, target); /* FIXME: Check the response */
255 if (!cli_backup_list[0]) { /* Empty list ... try again */
257 cli_get_backup_list(my_name, target);
261 strncpy(servername, cli_backup_list, MIN(16, namesize));
263 return True;