Get rid of 10.0.0.6 and replace it with 0.0.0.0 as my_ip address prior to
[samba.git] / source / libsmb / clidgram.c
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
8
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.
13    
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.
18    
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.
22 */
23
24 #define NO_SYSLOG
25
26 #include "includes.h"
27
28 /*
29  * cli_send_mailslot, send a mailslot for client code ...
30  */
31
32 static int dgram_sock = -1;
33
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)
39 {
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
46   bzero((char *)&p, sizeof(p));
47
48   /* 
49    * First, check if we have an open socket to the dest IP 
50    */
51
52   if (dgram_sock < 1) {
53
54     int name_size;
55
56     if ((dgram_sock = open_socket_out(SOCK_DGRAM, &dest_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
57
58       DEBUG(4, ("open_sock_out failed ..."));
59       return False;
60
61     }
62
63     /* Make it a broadcast socket ... */
64
65     set_socket_options(dgram_sock, "SO_BROADCAST");
66
67     /* Now, bind my addr to it ... */
68
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;
73
74     bind(dgram_sock, (struct sockaddr_in *)&sock_out, sizeof(sock_out));
75
76     /* Now, figure out what socket name we were bound to. We want the port */
77
78     name_size = sizeof(sock_out);
79
80     getsockname(dgram_sock, (struct sockaddr_in *)&sock_out, &name_size);
81
82     fprintf(stderr, "Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port));
83
84   }
85
86   /*
87    * Next, build the DGRAM ...
88    */
89
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;
100   
101   make_nmb_name(&dgram->source_name,srcname,src_type);
102   make_nmb_name(&dgram->dest_name,dstname,dest_type);
103
104   ptr = &dgram->data[0];
105
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);
111
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);
123
124   memcpy(p2,buf,len);
125   p2 += len;
126
127   dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
128
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;
134
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)));
138
139   return send_packet(&p);
140
141 }
142
143 /*
144  * cli_get_response: Get a response ...
145  */
146 int cli_get_response(BOOL unique, char *mailslot, char *buf, int bufsiz)
147 {
148   struct packet_struct *packet;
149
150   packet = read_packet(dgram_sock, DGRAM_PACKET);
151
152   if (packet) { /* We got one, pull what we want out of the SMB data ... */
153
154     struct dgram_packet *dgram = &packet->packet.dgram;
155
156     /*
157      * We should probably parse the SMB, but for now, we will pull what
158      * from fixed, known locations ...
159      */
160
161     /* Copy the data to buffer, respecting sizes ... */
162
163     bcopy(&dgram->data[92], buf, MIN(bufsiz, (dgram->datasize - 92)));
164
165   }
166   else 
167     return -1;
168
169 }
170
171 /*
172  * cli_get_backup_list: Send a get backup list request ...
173  */
174
175 static char cli_backup_list[1024];
176
177 int cli_get_backup_list(const char *myname, const char *send_to_name)
178 {
179   char outbuf[15];
180   char *p;
181   struct in_addr sendto_ip, my_ip;
182
183   if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
184
185     fprintf(stderr, "Could not resolve name: %s<1D>\n", send_to_name);
186
187   }
188
189   inet_aton("0.0.0.0", &my_ip);
190
191   if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
192
193     fprintf(stderr, "Could not resolve name: %s<00>\n", myname);
194
195   }
196
197   bzero(cli_backup_list, sizeof(cli_backup_list));
198   bzero(outbuf, sizeof(outbuf));
199
200   p = outbuf;
201
202   SCVAL(p, 0, ANN_GetBackupListReq);
203   p++;
204
205   SCVAL(p, 0, 1); /* Count pointer ... */
206   p++;
207
208   SIVAL(p, 0, 1); /* The sender's token ... */
209   p += 4;
210
211   cli_send_mailslot(True, "\\MAILSLOT\\BROWSE", outbuf, PTR_DIFF(p, outbuf),
212                     myname, 0, send_to_name, 0x1d, sendto_ip, my_ip, 138);
213
214   /* We should check the error and return if we got one */
215
216   /* Now, get the response ... */
217
218   cli_get_response(True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
219
220   /* Should check the response here ... FIXME */
221
222 }
223
224 /*
225  * cli_get_backup_server: Get the backup list and retrieve a server from it
226  */
227
228 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
229 {
230
231   /* Get the backup list first. We could pull this from the cache later */
232
233   cli_get_backup_list(my_name, target);  /* FIXME: Check the response */
234
235   strncpy(servername, cli_backup_list, MIN(16, namesize));
236
237 }