This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.git] / source3 / libsmb / clidgram.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client dgram calls
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Richard Sharpe 2001
6    Copyright (C) John Terpstra 2001
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26  * cli_send_mailslot, send a mailslot for client code ...
27  */
28
29 int cli_send_mailslot(int dgram_sock, BOOL unique, char *mailslot, 
30                       char *buf, int len,
31                       const char *srcname, int src_type, 
32                       const char *dstname, int dest_type,
33                       struct in_addr dest_ip, struct in_addr src_ip,
34                       int dest_port, int src_port)
35 {
36   struct packet_struct p;
37   struct dgram_packet *dgram = &p.packet.dgram;
38   char *ptr, *p2;
39   char tmp[4];
40
41   memset((char *)&p, '\0', sizeof(p));
42
43   /*
44    * Next, build the DGRAM ...
45    */
46
47   /* DIRECT GROUP or UNIQUE datagram. */
48   dgram->header.msg_type = unique ? 0x10 : 0x11; 
49   dgram->header.flags.node_type = M_NODE;
50   dgram->header.flags.first = True;
51   dgram->header.flags.more = False;
52   dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
53   dgram->header.source_ip.s_addr = src_ip.s_addr;
54   dgram->header.source_port = ntohs(src_port);
55   dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
56   dgram->header.packet_offset = 0;
57   
58   make_nmb_name(&dgram->source_name,srcname,src_type);
59   make_nmb_name(&dgram->dest_name,dstname,dest_type);
60
61   ptr = &dgram->data[0];
62
63   /* Setup the smb part. */
64   ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
65   memcpy(tmp,ptr,4);
66   set_message(ptr,17,17 + len,True);
67   memcpy(ptr,tmp,4);
68
69   SCVAL(ptr,smb_com,SMBtrans);
70   SSVAL(ptr,smb_vwv1,len);
71   SSVAL(ptr,smb_vwv11,len);
72   SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
73   SSVAL(ptr,smb_vwv13,3);
74   SSVAL(ptr,smb_vwv14,1);
75   SSVAL(ptr,smb_vwv15,1);
76   SSVAL(ptr,smb_vwv16,2);
77   p2 = smb_buf(ptr);
78   pstrcpy(p2,mailslot);
79   p2 = skip_string(p2,1);
80
81   memcpy(p2,buf,len);
82   p2 += len;
83
84   dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
85
86   p.ip = dest_ip;
87   p.port = dest_port;
88   p.fd = dgram_sock;
89   p.timestamp = time(NULL);
90   p.packet_type = DGRAM_PACKET;
91
92   DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
93                     nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
94   DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
95
96   return send_packet(&p);
97
98 }
99
100 /*
101  * cli_get_response: Get a response ...
102  */
103 int cli_get_response(int dgram_sock, BOOL unique, char *mailslot, char *buf, int bufsiz)
104 {
105   struct packet_struct *packet;
106
107   packet = receive_dgram_packet(dgram_sock, 5, mailslot);
108
109   if (packet) { /* We got one, pull what we want out of the SMB data ... */
110
111     struct dgram_packet *dgram = &packet->packet.dgram;
112
113     /*
114      * We should probably parse the SMB, but for now, we will pull what
115      * from fixed, known locations ...
116      */
117
118     /* Copy the data to buffer, respecting sizes ... */
119
120     memcpy(buf, &dgram->data[92], MIN(bufsiz, (dgram->datasize - 92)));
121
122   }
123   else 
124     return -1;
125
126   return 0;
127
128 }
129
130 /*
131  * cli_get_backup_list: Send a get backup list request ...
132  */
133
134 static char cli_backup_list[1024];
135
136 int cli_get_backup_list(const char *myname, const char *send_to_name)
137 {
138   char outbuf[15];
139   char *p;
140   struct in_addr sendto_ip, my_ip;
141   int dgram_sock;
142   struct sockaddr_in sock_out;
143   socklen_t name_size;
144
145   if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
146
147     DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
148     return False;
149
150   }
151
152   my_ip.s_addr = inet_addr("0.0.0.0");
153  
154   if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
155
156     DEBUG(0, ("Could not resolve name: %s<00>\n", myname));
157
158   }
159
160   if ((dgram_sock = open_socket_out(SOCK_DGRAM, &sendto_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
161
162     DEBUG(4, ("open_sock_out failed ..."));
163     return False;
164
165   }
166
167   /* Make it a broadcast socket ... */
168
169   set_socket_options(dgram_sock, "SO_BROADCAST");
170
171   /* Make it non-blocking??? */
172
173   if (fcntl(dgram_sock, F_SETFL, O_NONBLOCK) < 0) {
174
175     DEBUG(0, ("Unable to set non blocking on dgram sock\n"));
176
177   }
178
179   /* Now, bind a local addr to it ... Try port 138 first ... */
180
181   memset((char *)&sock_out, '\0', sizeof(sock_out));
182   sock_out.sin_addr.s_addr = INADDR_ANY;
183   sock_out.sin_port = htons(138);
184   sock_out.sin_family = AF_INET;
185
186   if (bind(dgram_sock, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
187
188     /* Try again on any port ... */
189
190     sock_out.sin_port = INADDR_ANY;
191
192     if (bind(dgram_sock, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
193
194       DEBUG(4, ("failed to bind socket to address ...\n"));
195       return False;
196         
197     }
198
199   }
200
201   /* Now, figure out what socket name we were bound to. We want the port */
202
203   name_size = sizeof(sock_out);
204
205   getsockname(dgram_sock, (struct sockaddr *)&sock_out, &name_size);
206
207   DEBUG(5, ("Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port)));
208
209   /* Now, build the request */
210
211   memset(cli_backup_list, '\0', sizeof(cli_backup_list));
212   memset(outbuf, '\0', sizeof(outbuf));
213
214   p = outbuf;
215
216   SCVAL(p, 0, ANN_GetBackupListReq);
217   p++;
218
219   SCVAL(p, 0, 1); /* Count pointer ... */
220   p++;
221
222   SIVAL(p, 0, 1); /* The sender's token ... */
223   p += 4;
224
225   cli_send_mailslot(dgram_sock, True, "\\MAILSLOT\\BROWSE", outbuf, 
226                     PTR_DIFF(p, outbuf), myname, 0, send_to_name, 
227                     0x1d, sendto_ip, my_ip, 138, sock_out.sin_port);
228
229   /* We should check the error and return if we got one */
230
231   /* Now, get the response ... */
232
233   cli_get_response(dgram_sock, True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
234
235   /* Should check the response here ... FIXME */
236
237   close(dgram_sock);
238
239   return True;
240
241 }
242
243 /*
244  * cli_get_backup_server: Get the backup list and retrieve a server from it
245  */
246
247 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
248 {
249
250   /* Get the backup list first. We could pull this from the cache later */
251
252   cli_get_backup_list(my_name, target);  /* FIXME: Check the response */
253
254   if (!cli_backup_list[0]) { /* Empty list ... try again */
255
256     cli_get_backup_list(my_name, target);
257
258   }
259
260   strncpy(servername, cli_backup_list, MIN(16, namesize));
261
262   return True;
263
264 }
265
266
267