Fixes to libsmbclient so it will work when browsing real Windows systems which
[Samba/gbeck.git] / source3 / libsmb / clidgram.c
blob68ac3b5c455e53cedb3e455d88bb753a5d63db53
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 #define NO_SYSLOG
26 #include "includes.h"
29 * cli_send_mailslot, send a mailslot for client code ...
32 static int dgram_sock = -1;
34 int cli_send_mailslot(BOOL unique, char *mailslot, char *buf, int len,
35 const char *srcname, int src_type,
36 const char *dstname, int dest_type,
37 struct in_addr dest_ip, struct in_addr src_ip,
38 int dest_port)
40 struct packet_struct p;
41 struct dgram_packet *dgram = &p.packet.dgram;
42 struct sockaddr_in sock_out;
43 char *ptr, *p2;
44 char tmp[4];
45 int name_size;
47 bzero((char *)&p, sizeof(p));
49 /*
50 * First, check if we have an open socket to the dest IP
53 if (dgram_sock < 1) {
55 if ((dgram_sock = open_socket_out(SOCK_DGRAM, &dest_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
57 DEBUG(4, ("open_sock_out failed ..."));
58 return False;
62 /* Make it a broadcast socket ... */
64 set_socket_options(dgram_sock, "SO_BROADCAST");
66 /* Now, bind my addr to it ... */
68 bzero((char *)&sock_out, sizeof(sock_out));
69 sock_out.sin_addr.s_addr = INADDR_ANY;
70 sock_out.sin_port = htons(138);
71 sock_out.sin_family = AF_INET;
73 if (bind(dgram_sock, (struct sockaddr_in *)&sock_out, sizeof(sock_out)) < 0) {
75 /* Try again on any port ... */
77 sock_out.sin_port = INADDR_ANY;
79 if (bind(dgram_sock, (struct sockaddr_in *)&sock_out, sizeof(sock_out)) < 0) {
81 DEBUG(4, ("failed to bind socket to address ...\n"));
82 return False;
90 /* Now, figure out what socket name we were bound to. We want the port */
92 name_size = sizeof(sock_out);
94 getsockname(dgram_sock, (struct sockaddr_in *)&sock_out, &name_size);
96 fprintf(stderr, "Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port));
99 * Next, build the DGRAM ...
102 /* DIRECT GROUP or UNIQUE datagram. */
103 dgram->header.msg_type = unique ? 0x10 : 0x11;
104 dgram->header.flags.node_type = M_NODE;
105 dgram->header.flags.first = True;
106 dgram->header.flags.more = False;
107 dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
108 dgram->header.source_ip.s_addr = sock_out.sin_addr.s_addr;
109 fprintf(stderr, "Source IP = %0X\n", dgram->header.source_ip);
110 dgram->header.source_port = ntohs(sock_out.sin_port);
111 fprintf(stderr, "Source Port = %0X\n", dgram->header.source_port);
112 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
113 dgram->header.packet_offset = 0;
115 make_nmb_name(&dgram->source_name,srcname,src_type);
116 make_nmb_name(&dgram->dest_name,dstname,dest_type);
118 ptr = &dgram->data[0];
120 /* Setup the smb part. */
121 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
122 memcpy(tmp,ptr,4);
123 set_message(ptr,17,17 + len,True);
124 memcpy(ptr,tmp,4);
126 CVAL(ptr,smb_com) = SMBtrans;
127 SSVAL(ptr,smb_vwv1,len);
128 SSVAL(ptr,smb_vwv11,len);
129 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
130 SSVAL(ptr,smb_vwv13,3);
131 SSVAL(ptr,smb_vwv14,1);
132 SSVAL(ptr,smb_vwv15,1);
133 SSVAL(ptr,smb_vwv16,2);
134 p2 = smb_buf(ptr);
135 pstrcpy(p2,mailslot);
136 p2 = skip_string(p2,1);
138 memcpy(p2,buf,len);
139 p2 += len;
141 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
143 p.ip = dest_ip;
144 p.port = dest_port;
145 p.fd = dgram_sock;
146 p.timestamp = time(NULL);
147 p.packet_type = DGRAM_PACKET;
149 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
150 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
151 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
153 return send_packet(&p);
158 * cli_get_response: Get a response ...
160 int cli_get_response(BOOL unique, char *mailslot, char *buf, int bufsiz)
162 struct packet_struct *packet;
164 packet = receive_dgram_packet(dgram_sock, 2, mailslot);
166 if (packet) { /* We got one, pull what we want out of the SMB data ... */
168 struct dgram_packet *dgram = &packet->packet.dgram;
171 * We should probably parse the SMB, but for now, we will pull what
172 * from fixed, known locations ...
175 /* Copy the data to buffer, respecting sizes ... */
177 bcopy(&dgram->data[92], buf, MIN(bufsiz, (dgram->datasize - 92)));
180 else
181 return -1;
186 * cli_get_backup_list: Send a get backup list request ...
189 static char cli_backup_list[1024];
191 int cli_get_backup_list(const char *myname, const char *send_to_name)
193 char outbuf[15];
194 char *p;
195 struct in_addr sendto_ip, my_ip;
197 if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
199 fprintf(stderr, "Could not resolve name: %s<1D>\n", send_to_name);
203 my_ip.s_addr = inet_addr("0.0.0.0");
205 if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
207 fprintf(stderr, "Could not resolve name: %s<00>\n", myname);
211 bzero(cli_backup_list, sizeof(cli_backup_list));
212 bzero(outbuf, sizeof(outbuf));
214 p = outbuf;
216 SCVAL(p, 0, ANN_GetBackupListReq);
217 p++;
219 SCVAL(p, 0, 1); /* Count pointer ... */
220 p++;
222 SIVAL(p, 0, 1); /* The sender's token ... */
223 p += 4;
225 cli_send_mailslot(True, "\\MAILSLOT\\BROWSE", outbuf, PTR_DIFF(p, outbuf),
226 myname, 0, send_to_name, 0x1d, sendto_ip, my_ip, 138);
228 /* We should check the error and return if we got one */
230 /* Now, get the response ... */
232 cli_get_response(True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
234 /* Should check the response here ... FIXME */
239 * cli_get_backup_server: Get the backup list and retrieve a server from it
242 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
245 /* Get the backup list first. We could pull this from the cache later */
247 cli_get_backup_list(my_name, target); /* FIXME: Check the response */
249 strncpy(servername, cli_backup_list, MIN(16, namesize));