]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/dirsvc.c
Didn't always tell the user that there is no default destination.
[thirdparty/cups.git] / scheduler / dirsvc.c
CommitLineData
a129ddbd 1/*
2c15c9d9 2 * "$Id: dirsvc.c,v 1.26 1999/06/23 15:58:14 mike Exp $"
a129ddbd 3 *
d6de4648 4 * Directory services routines for the Common UNIX Printing System (CUPS).
a129ddbd 5 *
a9de544f 6 * Copyright 1997-1999 by Easy Software Products, all rights reserved.
a129ddbd 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
8784b6a6 17 * 44141 Airport View Drive, Suite 204
a129ddbd 18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
cbbfcc63 26 * StartBrowsing() - Start sending and receiving broadcast information.
27 * StopBrowsing() - Stop sending and receiving broadcast information.
28 * UpdateBrowseList() - Update the browse lists for any new browse data.
29 * SendBrowseList() - Send new browsing information.
a129ddbd 30 */
31
32/*
33 * Include necessary headers...
34 */
35
fd8b1cf8 36#include "cupsd.h"
37
38
d6de4648 39/*
40 * 'StartBrowsing()' - Start sending and receiving broadcast information.
41 */
42
fd8b1cf8 43void
44StartBrowsing(void)
45{
d6de4648 46 int val; /* Socket option value */
47 struct sockaddr_in addr; /* Broadcast address */
48
49
8828fd5f 50 if (!Browsing)
d6de4648 51 return;
52
53 /*
54 * Create the broadcast socket...
55 */
56
900b10ea 57 if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
d6de4648 58 {
59 LogMessage(LOG_ERROR, "StartBrowsing: Unable to create broadcast socket - %s.",
60 strerror(errno));
61 return;
62 }
63
64 /*
900b10ea 65 * Set the "broadcast" flag...
d6de4648 66 */
67
68 val = 1;
e9e40963 69 if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
fd3ece61 70 {
71 LogMessage(LOG_ERROR, "StartBrowsing: Unable to set broadcast mode - %s.",
72 strerror(errno));
73
74#if defined(WIN32) || defined(__EMX__)
75 closesocket(BrowseSocket);
76#else
77 close(BrowseSocket);
78#endif /* WIN32 || __EMX__ */
79
80 BrowseSocket = -1;
81 return;
82 }
d6de4648 83
d6de4648 84 /*
85 * Bind the socket to browse port...
86 */
87
d6de4648 88 memset(&addr, 0, sizeof(addr));
89 addr.sin_addr.s_addr = htonl(INADDR_ANY);
90 addr.sin_family = AF_INET;
91 addr.sin_port = htons(BrowsePort);
92
93 if (bind(BrowseSocket, &addr, sizeof(addr)))
94 {
95 LogMessage(LOG_ERROR, "StartBrowsing: Unable to bind broadcast socket - %s.",
96 strerror(errno));
97
98#if defined(WIN32) || defined(__EMX__)
99 closesocket(BrowseSocket);
100#else
101 close(BrowseSocket);
102#endif /* WIN32 || __EMX__ */
103
104 BrowseSocket = -1;
105 return;
106 }
107
108 /*
109 * Finally, add the socket to the input selection set...
110 */
111
112 FD_SET(BrowseSocket, &InputSet);
fd8b1cf8 113}
114
115
d6de4648 116/*
117 * 'StopBrowsing()' - Stop sending and receiving broadcast information.
118 */
119
fd8b1cf8 120void
121StopBrowsing(void)
122{
8828fd5f 123 if (!Browsing)
d6de4648 124 return;
125
126 /*
127 * Close the socket and remove it from the input selection set.
128 */
129
3c7bb4a2 130 if (BrowseSocket >= 0)
131 {
d6de4648 132#if defined(WIN32) || defined(__EMX__)
3c7bb4a2 133 closesocket(BrowseSocket);
d6de4648 134#else
3c7bb4a2 135 close(BrowseSocket);
d6de4648 136#endif /* WIN32 || __EMX__ */
137
3c7bb4a2 138 FD_CLR(BrowseSocket, &InputSet);
139 }
fd8b1cf8 140}
141
142
d6de4648 143/*
144 * 'UpdateBrowseList()' - Update the browse lists for any new browse data.
145 */
146
fd8b1cf8 147void
148UpdateBrowseList(void)
149{
8b43895a 150 int i; /* Looping var */
eef5ad1b 151 int len, /* Length of name string */
152 offset; /* Offset in name string */
d6de4648 153 int bytes; /* Number of bytes left */
154 char packet[1540]; /* Broadcast packet */
155 cups_ptype_t type; /* Printer type */
156 ipp_pstate_t state; /* Printer state */
157 char uri[HTTP_MAX_URI], /* Printer URI */
158 method[HTTP_MAX_URI], /* Method portion of URI */
159 username[HTTP_MAX_URI], /* Username portion of URI */
160 host[HTTP_MAX_URI], /* Host portion of URI */
161 resource[HTTP_MAX_URI]; /* Resource portion of URI */
162 int port; /* Port portion of URI */
163 char name[IPP_MAX_NAME], /* Name of printer */
8b43895a 164 *hptr, /* Pointer into hostname */
165 *sptr; /* Pointer into ServerName */
166 printer_t *p, /* Printer information */
167 *pclass, /* Printer class */
168 *first; /* First printer in class */
d6de4648 169
170
171 /*
172 * Read a packet from the browse socket...
173 */
174
175 if ((bytes = recv(BrowseSocket, packet, sizeof(packet), 0)) <= 0)
f9bacabb 176 {
8828fd5f 177 LogMessage(LOG_ERROR, "UpdateBrowseList: recv failed - %s.",
178 strerror(errno));
d6de4648 179 return;
f9bacabb 180 }
d6de4648 181
182 packet[bytes] = '\0';
8828fd5f 183 DEBUG_printf(("UpdateBrowseList: (%d bytes) %s", bytes, packet));
d6de4648 184
185 if (sscanf(packet, "%x%x%s", &type, &state, uri) != 3)
186 {
187 LogMessage(LOG_WARN, "UpdateBrowseList: Garbled browse packet - %s",
188 packet);
189 return;
190 }
191
192 /*
193 * Pull the URI apart to see if this is a local or remote printer...
194 */
195
196 httpSeparate(uri, method, username, host, &port, resource);
197
198 if (strcasecmp(host, ServerName) == 0)
199 return;
200
201 /*
202 * OK, this isn't a local printer; see if we already have it listed in
203 * the Printers list, and add it if not...
204 */
205
c7fa9d06 206 type |= CUPS_PRINTER_REMOTE;
d6de4648 207
8b43895a 208 hptr = strchr(host, '.');
209 sptr = strchr(ServerName, '.');
210
211 if (hptr != NULL && sptr != NULL &&
212 strcasecmp(hptr, sptr) == 0)
213 *hptr = '\0';
d6de4648 214
c7fa9d06 215 if (type & CUPS_PRINTER_CLASS)
d6de4648 216 {
217 /*
c7fa9d06 218 * Remote destination is a class...
d6de4648 219 */
220
c7fa9d06 221 if (strncmp(resource, "/classes/", 9) == 0)
a71f40c4 222 sprintf(name, "%s@%s", resource + 9, host);
c7fa9d06 223 else
224 return;
225
226 if ((p = FindClass(name)) == NULL)
a71f40c4 227 if ((p = FindClass(resource + 9)) != NULL)
eef5ad1b 228 {
a71f40c4 229 if (strcasecmp(p->hostname, host) != 0)
230 {
231 /*
232 * Nope, this isn't the same host; if the hostname isn't the local host,
233 * add it to the other class and then find a class using the full host
234 * name...
235 */
236
237 if (strcasecmp(p->hostname, ServerName) != 0)
238 {
239 strcat(p->name, "@");
240 strcat(p->name, p->hostname);
23050703 241 SetPrinterAttrs(p);
a71f40c4 242 }
243
244 p = NULL;
245 }
eef5ad1b 246 }
247 else
248 strcpy(name, resource + 9);
a71f40c4 249
250 if (p == NULL)
c7fa9d06 251 {
252 /*
253 * Class doesn't exist; add it...
254 */
d6de4648 255
782359ca 256 p = AddClass(name);
c7fa9d06 257
258 /*
782359ca 259 * Force the URI to point to the real server...
c7fa9d06 260 */
261
262 strcpy(p->uri, uri);
263 strcpy(p->device_uri, uri);
67a6c918 264 strcpy(p->hostname, host);
c7fa9d06 265 free(p->attrs->attrs->values[0].string.text);
266 p->attrs->attrs->values[0].string.text = strdup(uri);
267 }
268 }
269 else
270 {
d6de4648 271 /*
c7fa9d06 272 * Remote destination is a printer...
d6de4648 273 */
274
c7fa9d06 275 if (strncmp(resource, "/printers/", 10) == 0)
a71f40c4 276 sprintf(name, "%s@%s", resource + 10, host);
c7fa9d06 277 else
278 return;
279
280 if ((p = FindPrinter(name)) == NULL)
a71f40c4 281 if ((p = FindPrinter(resource + 10)) != NULL)
eef5ad1b 282 {
a71f40c4 283 if (strcasecmp(p->hostname, host) != 0)
284 {
285 /*
286 * Nope, this isn't the same host; if the hostname isn't the local host,
287 * add it to the other printer and then find a printer using the full host
288 * name...
289 */
290
291 if (strcasecmp(p->hostname, ServerName) != 0)
292 {
293 strcat(p->name, "@");
294 strcat(p->name, p->hostname);
23050703 295 SetPrinterAttrs(p);
a71f40c4 296 }
297
298 p = NULL;
299 }
eef5ad1b 300 }
301 else
302 strcpy(name, resource + 10);
a71f40c4 303
304 if (p == NULL)
c7fa9d06 305 {
306 /*
307 * Printer doesn't exist; add it...
308 */
309
310 p = AddPrinter(name);
311
312 /*
313 * First the URI to point to the real server...
314 */
315
316 strcpy(p->uri, uri);
317 strcpy(p->device_uri, uri);
67a6c918 318 strcpy(p->hostname, host);
c7fa9d06 319 free(p->attrs->attrs->values[0].string.text);
320 p->attrs->attrs->values[0].string.text = strdup(uri);
321 }
d6de4648 322 }
323
324 /*
325 * Update the state...
326 */
327
2c15c9d9 328 p->type = type | CUPS_PRINTER_REMOTE;
d6de4648 329 p->state = state;
0948b55b 330 p->accepting = state != IPP_PRINTER_STOPPED;
d6de4648 331 p->browse_time = time(NULL);
8b43895a 332
333 /*
334 * Do auto-classing if needed...
335 */
336
337 if (ImplicitClasses)
338 {
339 /*
340 * Loop through all available printers and create classes as needed...
341 */
342
eef5ad1b 343 for (p = Printers, len = 0, offset = 0; p != NULL; p = p->next)
8b43895a 344 {
345 /*
346 * Skip classes...
347 */
348
349 if (p->type & CUPS_PRINTER_CLASS)
350 {
351 len = 0;
352 continue;
353 }
354
355 /*
356 * If len == 0, get the length of this printer name up to the "@"
357 * sign (if any).
358 */
359
360 if (len > 0 &&
eef5ad1b 361 strncasecmp(p->name, name + offset, len) == 0 &&
8b43895a 362 (p->name[len] == '\0' || p->name[len] == '@'))
363 {
364 /*
365 * We have more than one printer with the same name; see if
366 * we have a class, and if this printer is a member...
367 */
368
369 if ((pclass = FindClass(name)) == NULL)
370 pclass = AddClass(name);
371
372 if (first != NULL)
373 {
374 for (i = 0; i < pclass->num_printers; i ++)
375 if (pclass->printers[i] == first)
376 break;
377
378 if (i >= pclass->num_printers)
379 AddPrinterToClass(pclass, first);
380
381 first = NULL;
382 }
383
384 for (i = 0; i < pclass->num_printers; i ++)
385 if (pclass->printers[i] == p)
386 break;
387
388 if (i >= pclass->num_printers)
389 AddPrinterToClass(pclass, p);
390 }
391 else
392 {
393 /*
394 * First time around; just get name length and mark it as first
395 * in the list...
396 */
397
398 if ((hptr = strchr(p->name, '@')) != NULL)
399 len = hptr - p->name;
400 else
401 len = strlen(p->name);
402
782359ca 403 strncpy(name, p->name, len);
404 name[len] = '\0';
eef5ad1b 405 offset = 0;
782359ca 406
407 if (FindPrinter(name) != NULL)
408 {
409 /*
410 * Can't use same name as printer; add "Any" to the front of the
411 * name...
412 */
413
414 strcpy(name, "Any");
415 strncpy(name + 3, p->name, len);
416 name[len + 3] = '\0';
eef5ad1b 417 offset = 3;
782359ca 418 }
419
8b43895a 420 first = p;
421 }
422 }
423 }
fd8b1cf8 424}
425
426
d6de4648 427/*
428 * 'SendBrowseList()' - Send new browsing information.
429 */
430
fd8b1cf8 431void
432SendBrowseList(void)
433{
d6de4648 434 int i; /* Looping var */
435 printer_t *p, /* Current printer */
436 *np; /* Next printer */
f9d6449d 437 time_t ut, /* Minimum update time */
438 to; /* Timeout time */
d6de4648 439 int bytes; /* Length of packet */
440 char packet[1540];
441 /* Browse data packet */
442
443
444 /*
445 * Compute the update time...
446 */
447
f9d6449d 448 ut = time(NULL) - BrowseInterval;
449 to = time(NULL) - BrowseTimeout;
d6de4648 450
451 /*
452 * Loop through all of the printers and send local updates as needed...
453 */
454
455 for (p = Printers; p != NULL; p = np)
456 {
457 np = p->next;
458
459 if (p->type & CUPS_PRINTER_REMOTE)
460 {
461 /*
462 * See if this printer needs to be timed out...
463 */
464
f9d6449d 465 if (p->browse_time < to)
d6de4648 466 DeletePrinter(p);
467 }
f9d6449d 468 else if (p->browse_time < ut)
d6de4648 469 {
470 /*
471 * Need to send an update...
472 */
473
474 p->browse_time = time(NULL);
475
476 sprintf(packet, "%x %x %s\n", p->type, p->state, p->uri);
477 bytes = strlen(packet);
8828fd5f 478 DEBUG_printf(("SendBrowseList: (%d bytes) %s", bytes, packet));
d6de4648 479
480 /*
481 * Send a packet to each browse address...
482 */
483
484 for (i = 0; i < NumBrowsers; i ++)
f9bacabb 485 if (sendto(BrowseSocket, packet, bytes, 0, Browsers + i,
486 sizeof(Browsers[0])) <= 0)
8828fd5f 487 LogMessage(LOG_ERROR, "SendBrowseList: sendto failed for browser %d - %s.",
fd3ece61 488 i + 1, strerror(errno));
d6de4648 489 }
490 }
fd8b1cf8 491}
a129ddbd 492
493
494/*
2c15c9d9 495 * End of "$Id: dirsvc.c,v 1.26 1999/06/23 15:58:14 mike Exp $".
a129ddbd 496 */