Removed fprintf(stderr, ...); calls which should not be present in library
[metze/samba/wip.git] / source3 / 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 #include "includes.h"
25
26 /*
27  * cli_send_mailslot, send a mailslot for client code ...
28  */
29
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)
36 {
37   struct packet_struct p;
38   struct dgram_packet *dgram = &p.packet.dgram;
39   char *ptr, *p2;
40   char tmp[4];
41
42   memset((char *)&p, '\0', sizeof(p));
43
44   /*
45    * Next, build the DGRAM ...
46    */
47
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;
58   
59   make_nmb_name(&dgram->source_name,srcname,src_type);
60   make_nmb_name(&dgram->dest_name,dstname,dest_type);
61
62   ptr = &dgram->data[0];
63
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);
69
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);
81
82   memcpy(p2,buf,len);
83   p2 += len;
84
85   dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
86
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;
92
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)));
96
97   return send_packet(&p);
98
99 }
100
101 /*
102  * cli_get_response: Get a response ...
103  */
104 int cli_get_response(int dgram_sock, BOOL unique, char *mailslot, char *buf, int bufsiz)
105 {
106   struct packet_struct *packet;
107
108   packet = receive_dgram_packet(dgram_sock, 5, mailslot);
109
110   if (packet) { /* We got one, pull what we want out of the SMB data ... */
111
112     struct dgram_packet *dgram = &packet->packet.dgram;
113
114     /*
115      * We should probably parse the SMB, but for now, we will pull what
116      * from fixed, known locations ...
117      */
118
119     /* Copy the data to buffer, respecting sizes ... */
120
121     bcopy(&dgram->data[92], buf, MIN(bufsiz, (dgram->datasize - 92)));
122
123   }
124   else 
125     return -1;
126
127   return 0;
128
129 }
130
131 /*
132  * cli_get_backup_list: Send a get backup list request ...
133  */
134
135 static char cli_backup_list[1024];
136
137 int cli_get_backup_list(const char *myname, const char *send_to_name)
138 {
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;
145
146   if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
147
148     DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
149     return False;
150
151   }
152
153   my_ip.s_addr = inet_addr("0.0.0.0");
154  
155   if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
156
157     DEBUG(0, ("Could not resolve name: %s<00>\n", myname));
158
159   }
160
161   if ((dgram_sock = open_socket_out(SOCK_DGRAM, &sendto_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
162
163     DEBUG(4, ("open_sock_out failed ..."));
164     return False;
165
166   }
167
168   /* Make it a broadcast socket ... */
169
170   set_socket_options(dgram_sock, "SO_BROADCAST");
171
172   /* Make it non-blocking??? */
173
174   if (fcntl(dgram_sock, F_SETFL, O_NONBLOCK) < 0) {
175
176     DEBUG(0, ("Unable to set non blocking on dgram sock\n"));
177
178   }
179
180   /* Now, bind a local addr to it ... Try port 138 first ... */
181
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;
186
187   if (bind(dgram_sock, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
188
189     /* Try again on any port ... */
190
191     sock_out.sin_port = INADDR_ANY;
192
193     if (bind(dgram_sock, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
194
195       DEBUG(4, ("failed to bind socket to address ...\n"));
196       return False;
197         
198     }
199
200   }
201
202   /* Now, figure out what socket name we were bound to. We want the port */
203
204   name_size = sizeof(sock_out);
205
206   getsockname(dgram_sock, (struct sockaddr *)&sock_out, &name_size);
207
208   DEBUG(5, ("Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port)));
209
210   /* Now, build the request */
211
212   memset(cli_backup_list, '\0', sizeof(cli_backup_list));
213   memset(outbuf, '\0', sizeof(outbuf));
214
215   p = outbuf;
216
217   SCVAL(p, 0, ANN_GetBackupListReq);
218   p++;
219
220   SCVAL(p, 0, 1); /* Count pointer ... */
221   p++;
222
223   SIVAL(p, 0, 1); /* The sender's token ... */
224   p += 4;
225
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);
229
230   /* We should check the error and return if we got one */
231
232   /* Now, get the response ... */
233
234   cli_get_response(dgram_sock, True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
235
236   /* Should check the response here ... FIXME */
237
238   close(dgram_sock);
239
240   return True;
241
242 }
243
244 /*
245  * cli_get_backup_server: Get the backup list and retrieve a server from it
246  */
247
248 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
249 {
250
251   /* Get the backup list first. We could pull this from the cache later */
252
253   cli_get_backup_list(my_name, target);  /* FIXME: Check the response */
254
255   if (!cli_backup_list[0]) { /* Empty list ... try again */
256
257     cli_get_backup_list(my_name, target);
258
259   }
260
261   strncpy(servername, cli_backup_list, MIN(16, namesize));
262
263   return True;
264
265 }
266
267
268