Get rid of 10.0.0.6 and replace it with 0.0.0.0 as my_ip address prior to
[Samba/gebeck_regimport.git] / source3 / libsmb / clidgram.c
bloba45cc009ac88f5826558a27ccc90445d064eca7f
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];
46 bzero((char *)&p, sizeof(p));
48 /*
49 * First, check if we have an open socket to the dest IP
52 if (dgram_sock < 1) {
54 int name_size;
56 if ((dgram_sock = open_socket_out(SOCK_DGRAM, &dest_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
58 DEBUG(4, ("open_sock_out failed ..."));
59 return False;
63 /* Make it a broadcast socket ... */
65 set_socket_options(dgram_sock, "SO_BROADCAST");
67 /* Now, bind my addr to it ... */
69 bzero((char *)&sock_out, sizeof(sock_out));
70 putip((char *)&sock_out.sin_addr, (char *)&src_ip);
71 sock_out.sin_port = INADDR_ANY;
72 sock_out.sin_family = AF_INET;
74 bind(dgram_sock, (struct sockaddr_in *)&sock_out, sizeof(sock_out));
76 /* Now, figure out what socket name we were bound to. We want the port */
78 name_size = sizeof(sock_out);
80 getsockname(dgram_sock, (struct sockaddr_in *)&sock_out, &name_size);
82 fprintf(stderr, "Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port));
87 * Next, build the DGRAM ...
90 /* DIRECT GROUP or UNIQUE datagram. */
91 dgram->header.msg_type = unique ? 0x10 : 0x11;
92 dgram->header.flags.node_type = M_NODE;
93 dgram->header.flags.first = True;
94 dgram->header.flags.more = False;
95 dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
96 dgram->header.source_ip = src_ip;
97 dgram->header.source_port = ntohs(sock_out.sin_port);
98 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
99 dgram->header.packet_offset = 0;
101 make_nmb_name(&dgram->source_name,srcname,src_type);
102 make_nmb_name(&dgram->dest_name,dstname,dest_type);
104 ptr = &dgram->data[0];
106 /* Setup the smb part. */
107 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
108 memcpy(tmp,ptr,4);
109 set_message(ptr,17,17 + len,True);
110 memcpy(ptr,tmp,4);
112 CVAL(ptr,smb_com) = SMBtrans;
113 SSVAL(ptr,smb_vwv1,len);
114 SSVAL(ptr,smb_vwv11,len);
115 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
116 SSVAL(ptr,smb_vwv13,3);
117 SSVAL(ptr,smb_vwv14,1);
118 SSVAL(ptr,smb_vwv15,1);
119 SSVAL(ptr,smb_vwv16,2);
120 p2 = smb_buf(ptr);
121 pstrcpy(p2,mailslot);
122 p2 = skip_string(p2,1);
124 memcpy(p2,buf,len);
125 p2 += len;
127 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
129 p.ip = dest_ip;
130 p.port = dest_port;
131 p.fd = dgram_sock;
132 p.timestamp = time(NULL);
133 p.packet_type = DGRAM_PACKET;
135 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
136 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
137 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
139 return send_packet(&p);
144 * cli_get_response: Get a response ...
146 int cli_get_response(BOOL unique, char *mailslot, char *buf, int bufsiz)
148 struct packet_struct *packet;
150 packet = read_packet(dgram_sock, DGRAM_PACKET);
152 if (packet) { /* We got one, pull what we want out of the SMB data ... */
154 struct dgram_packet *dgram = &packet->packet.dgram;
157 * We should probably parse the SMB, but for now, we will pull what
158 * from fixed, known locations ...
161 /* Copy the data to buffer, respecting sizes ... */
163 bcopy(&dgram->data[92], buf, MIN(bufsiz, (dgram->datasize - 92)));
166 else
167 return -1;
172 * cli_get_backup_list: Send a get backup list request ...
175 static char cli_backup_list[1024];
177 int cli_get_backup_list(const char *myname, const char *send_to_name)
179 char outbuf[15];
180 char *p;
181 struct in_addr sendto_ip, my_ip;
183 if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
185 fprintf(stderr, "Could not resolve name: %s<1D>\n", send_to_name);
189 inet_aton("0.0.0.0", &my_ip);
191 if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
193 fprintf(stderr, "Could not resolve name: %s<00>\n", myname);
197 bzero(cli_backup_list, sizeof(cli_backup_list));
198 bzero(outbuf, sizeof(outbuf));
200 p = outbuf;
202 SCVAL(p, 0, ANN_GetBackupListReq);
203 p++;
205 SCVAL(p, 0, 1); /* Count pointer ... */
206 p++;
208 SIVAL(p, 0, 1); /* The sender's token ... */
209 p += 4;
211 cli_send_mailslot(True, "\\MAILSLOT\\BROWSE", outbuf, PTR_DIFF(p, outbuf),
212 myname, 0, send_to_name, 0x1d, sendto_ip, my_ip, 138);
214 /* We should check the error and return if we got one */
216 /* Now, get the response ... */
218 cli_get_response(True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
220 /* Should check the response here ... FIXME */
225 * cli_get_backup_server: Get the backup list and retrieve a server from it
228 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
231 /* Get the backup list first. We could pull this from the cache later */
233 cli_get_backup_list(my_name, target); /* FIXME: Check the response */
235 strncpy(servername, cli_backup_list, MIN(16, namesize));