]>
Commit | Line | Data |
---|---|---|
a129ddbd | 1 | /* |
18d7d592 | 2 | * "$Id: dirsvc.c,v 1.30 1999/06/24 12:41:17 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 | 43 | void |
44 | StartBrowsing(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 | 120 | void |
121 | StopBrowsing(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 | 147 | void |
148 | UpdateBrowseList(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 | ||
18d7d592 | 175 | #ifdef DEBUG |
176 | struct sockaddr_in addr; | |
177 | int len; | |
178 | ||
179 | ||
180 | len = sizeof(addr); | |
181 | if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet), 0, &addr, &len)) <= 0) | |
182 | { | |
183 | LogMessage(LOG_ERROR, "UpdateBrowseList: recv failed - %s.", | |
184 | strerror(errno)); | |
185 | return; | |
186 | } | |
187 | ||
188 | packet[bytes] = '\0'; | |
189 | printf("UpdateBrowseList: (%d bytes from %08x) %s", bytes, | |
190 | ntohl(addr.sin_addr.s_addr), packet); | |
191 | #else | |
d6de4648 | 192 | if ((bytes = recv(BrowseSocket, packet, sizeof(packet), 0)) <= 0) |
f9bacabb | 193 | { |
8828fd5f | 194 | LogMessage(LOG_ERROR, "UpdateBrowseList: recv failed - %s.", |
195 | strerror(errno)); | |
d6de4648 | 196 | return; |
f9bacabb | 197 | } |
d6de4648 | 198 | |
199 | packet[bytes] = '\0'; | |
18d7d592 | 200 | #endif /* DEBUG */ |
d6de4648 | 201 | |
202 | if (sscanf(packet, "%x%x%s", &type, &state, uri) != 3) | |
203 | { | |
204 | LogMessage(LOG_WARN, "UpdateBrowseList: Garbled browse packet - %s", | |
205 | packet); | |
206 | return; | |
207 | } | |
208 | ||
209 | /* | |
210 | * Pull the URI apart to see if this is a local or remote printer... | |
211 | */ | |
212 | ||
213 | httpSeparate(uri, method, username, host, &port, resource); | |
214 | ||
215 | if (strcasecmp(host, ServerName) == 0) | |
216 | return; | |
217 | ||
218 | /* | |
219 | * OK, this isn't a local printer; see if we already have it listed in | |
220 | * the Printers list, and add it if not... | |
221 | */ | |
222 | ||
8b43895a | 223 | hptr = strchr(host, '.'); |
224 | sptr = strchr(ServerName, '.'); | |
225 | ||
226 | if (hptr != NULL && sptr != NULL && | |
227 | strcasecmp(hptr, sptr) == 0) | |
228 | *hptr = '\0'; | |
d6de4648 | 229 | |
c7fa9d06 | 230 | if (type & CUPS_PRINTER_CLASS) |
d6de4648 | 231 | { |
232 | /* | |
c7fa9d06 | 233 | * Remote destination is a class... |
d6de4648 | 234 | */ |
235 | ||
c7fa9d06 | 236 | if (strncmp(resource, "/classes/", 9) == 0) |
a71f40c4 | 237 | sprintf(name, "%s@%s", resource + 9, host); |
c7fa9d06 | 238 | else |
239 | return; | |
240 | ||
241 | if ((p = FindClass(name)) == NULL) | |
a71f40c4 | 242 | if ((p = FindClass(resource + 9)) != NULL) |
eef5ad1b | 243 | { |
a71f40c4 | 244 | if (strcasecmp(p->hostname, host) != 0) |
245 | { | |
246 | /* | |
247 | * Nope, this isn't the same host; if the hostname isn't the local host, | |
248 | * add it to the other class and then find a class using the full host | |
249 | * name... | |
250 | */ | |
251 | ||
18d7d592 | 252 | if (p->type & CUPS_PRINTER_REMOTE) |
a71f40c4 | 253 | { |
254 | strcat(p->name, "@"); | |
255 | strcat(p->name, p->hostname); | |
23050703 | 256 | SetPrinterAttrs(p); |
1cab4605 | 257 | SortPrinters(); |
a71f40c4 | 258 | } |
259 | ||
260 | p = NULL; | |
261 | } | |
eef5ad1b | 262 | } |
263 | else | |
264 | strcpy(name, resource + 9); | |
a71f40c4 | 265 | |
266 | if (p == NULL) | |
c7fa9d06 | 267 | { |
268 | /* | |
269 | * Class doesn't exist; add it... | |
270 | */ | |
d6de4648 | 271 | |
782359ca | 272 | p = AddClass(name); |
c7fa9d06 | 273 | |
274 | /* | |
782359ca | 275 | * Force the URI to point to the real server... |
c7fa9d06 | 276 | */ |
277 | ||
278 | strcpy(p->uri, uri); | |
279 | strcpy(p->device_uri, uri); | |
67a6c918 | 280 | strcpy(p->hostname, host); |
c7fa9d06 | 281 | free(p->attrs->attrs->values[0].string.text); |
282 | p->attrs->attrs->values[0].string.text = strdup(uri); | |
283 | } | |
284 | } | |
285 | else | |
286 | { | |
d6de4648 | 287 | /* |
c7fa9d06 | 288 | * Remote destination is a printer... |
d6de4648 | 289 | */ |
290 | ||
c7fa9d06 | 291 | if (strncmp(resource, "/printers/", 10) == 0) |
a71f40c4 | 292 | sprintf(name, "%s@%s", resource + 10, host); |
c7fa9d06 | 293 | else |
294 | return; | |
295 | ||
296 | if ((p = FindPrinter(name)) == NULL) | |
a71f40c4 | 297 | if ((p = FindPrinter(resource + 10)) != NULL) |
eef5ad1b | 298 | { |
a71f40c4 | 299 | if (strcasecmp(p->hostname, host) != 0) |
300 | { | |
301 | /* | |
302 | * Nope, this isn't the same host; if the hostname isn't the local host, | |
303 | * add it to the other printer and then find a printer using the full host | |
304 | * name... | |
305 | */ | |
306 | ||
18d7d592 | 307 | if (p->type & CUPS_PRINTER_REMOTE) |
a71f40c4 | 308 | { |
309 | strcat(p->name, "@"); | |
310 | strcat(p->name, p->hostname); | |
23050703 | 311 | SetPrinterAttrs(p); |
1cab4605 | 312 | SortPrinters(); |
a71f40c4 | 313 | } |
314 | ||
315 | p = NULL; | |
316 | } | |
eef5ad1b | 317 | } |
318 | else | |
319 | strcpy(name, resource + 10); | |
a71f40c4 | 320 | |
321 | if (p == NULL) | |
c7fa9d06 | 322 | { |
323 | /* | |
324 | * Printer doesn't exist; add it... | |
325 | */ | |
326 | ||
327 | p = AddPrinter(name); | |
328 | ||
329 | /* | |
330 | * First the URI to point to the real server... | |
331 | */ | |
332 | ||
333 | strcpy(p->uri, uri); | |
334 | strcpy(p->device_uri, uri); | |
67a6c918 | 335 | strcpy(p->hostname, host); |
c7fa9d06 | 336 | free(p->attrs->attrs->values[0].string.text); |
337 | p->attrs->attrs->values[0].string.text = strdup(uri); | |
338 | } | |
d6de4648 | 339 | } |
340 | ||
341 | /* | |
342 | * Update the state... | |
343 | */ | |
344 | ||
f925c8de | 345 | p->type = type; |
d6de4648 | 346 | p->state = state; |
0948b55b | 347 | p->accepting = state != IPP_PRINTER_STOPPED; |
d6de4648 | 348 | p->browse_time = time(NULL); |
8b43895a | 349 | |
350 | /* | |
351 | * Do auto-classing if needed... | |
352 | */ | |
353 | ||
354 | if (ImplicitClasses) | |
355 | { | |
356 | /* | |
357 | * Loop through all available printers and create classes as needed... | |
358 | */ | |
359 | ||
eef5ad1b | 360 | for (p = Printers, len = 0, offset = 0; p != NULL; p = p->next) |
8b43895a | 361 | { |
362 | /* | |
363 | * Skip classes... | |
364 | */ | |
365 | ||
366 | if (p->type & CUPS_PRINTER_CLASS) | |
367 | { | |
368 | len = 0; | |
369 | continue; | |
370 | } | |
371 | ||
372 | /* | |
373 | * If len == 0, get the length of this printer name up to the "@" | |
374 | * sign (if any). | |
375 | */ | |
376 | ||
377 | if (len > 0 && | |
eef5ad1b | 378 | strncasecmp(p->name, name + offset, len) == 0 && |
8b43895a | 379 | (p->name[len] == '\0' || p->name[len] == '@')) |
380 | { | |
381 | /* | |
382 | * We have more than one printer with the same name; see if | |
383 | * we have a class, and if this printer is a member... | |
384 | */ | |
385 | ||
386 | if ((pclass = FindClass(name)) == NULL) | |
18d7d592 | 387 | { |
8b43895a | 388 | pclass = AddClass(name); |
18d7d592 | 389 | pclass->type |= CUPS_PRINTER_IMPLICIT; |
390 | } | |
8b43895a | 391 | |
392 | if (first != NULL) | |
393 | { | |
394 | for (i = 0; i < pclass->num_printers; i ++) | |
395 | if (pclass->printers[i] == first) | |
396 | break; | |
397 | ||
398 | if (i >= pclass->num_printers) | |
399 | AddPrinterToClass(pclass, first); | |
400 | ||
401 | first = NULL; | |
402 | } | |
403 | ||
404 | for (i = 0; i < pclass->num_printers; i ++) | |
405 | if (pclass->printers[i] == p) | |
406 | break; | |
407 | ||
408 | if (i >= pclass->num_printers) | |
409 | AddPrinterToClass(pclass, p); | |
410 | } | |
411 | else | |
412 | { | |
413 | /* | |
414 | * First time around; just get name length and mark it as first | |
415 | * in the list... | |
416 | */ | |
417 | ||
418 | if ((hptr = strchr(p->name, '@')) != NULL) | |
419 | len = hptr - p->name; | |
420 | else | |
421 | len = strlen(p->name); | |
422 | ||
782359ca | 423 | strncpy(name, p->name, len); |
424 | name[len] = '\0'; | |
eef5ad1b | 425 | offset = 0; |
782359ca | 426 | |
427 | if (FindPrinter(name) != NULL) | |
428 | { | |
429 | /* | |
430 | * Can't use same name as printer; add "Any" to the front of the | |
431 | * name... | |
432 | */ | |
433 | ||
434 | strcpy(name, "Any"); | |
435 | strncpy(name + 3, p->name, len); | |
436 | name[len + 3] = '\0'; | |
eef5ad1b | 437 | offset = 3; |
782359ca | 438 | } |
439 | ||
8b43895a | 440 | first = p; |
441 | } | |
442 | } | |
443 | } | |
fd8b1cf8 | 444 | } |
445 | ||
446 | ||
d6de4648 | 447 | /* |
448 | * 'SendBrowseList()' - Send new browsing information. | |
449 | */ | |
450 | ||
fd8b1cf8 | 451 | void |
452 | SendBrowseList(void) | |
453 | { | |
d6de4648 | 454 | int i; /* Looping var */ |
455 | printer_t *p, /* Current printer */ | |
456 | *np; /* Next printer */ | |
f9d6449d | 457 | time_t ut, /* Minimum update time */ |
458 | to; /* Timeout time */ | |
d6de4648 | 459 | int bytes; /* Length of packet */ |
460 | char packet[1540]; | |
461 | /* Browse data packet */ | |
462 | ||
463 | ||
464 | /* | |
465 | * Compute the update time... | |
466 | */ | |
467 | ||
f9d6449d | 468 | ut = time(NULL) - BrowseInterval; |
469 | to = time(NULL) - BrowseTimeout; | |
d6de4648 | 470 | |
471 | /* | |
472 | * Loop through all of the printers and send local updates as needed... | |
473 | */ | |
474 | ||
475 | for (p = Printers; p != NULL; p = np) | |
476 | { | |
477 | np = p->next; | |
478 | ||
479 | if (p->type & CUPS_PRINTER_REMOTE) | |
480 | { | |
481 | /* | |
482 | * See if this printer needs to be timed out... | |
483 | */ | |
484 | ||
f9d6449d | 485 | if (p->browse_time < to) |
d6de4648 | 486 | DeletePrinter(p); |
487 | } | |
18d7d592 | 488 | else if (p->browse_time < ut && !(p->type & CUPS_PRINTER_IMPLICIT)) |
d6de4648 | 489 | { |
490 | /* | |
491 | * Need to send an update... | |
492 | */ | |
493 | ||
494 | p->browse_time = time(NULL); | |
495 | ||
ae5f9b38 | 496 | sprintf(packet, "%x %x %s\n", p->type | CUPS_PRINTER_REMOTE, p->state, |
497 | p->uri); | |
d6de4648 | 498 | bytes = strlen(packet); |
8828fd5f | 499 | DEBUG_printf(("SendBrowseList: (%d bytes) %s", bytes, packet)); |
d6de4648 | 500 | |
501 | /* | |
502 | * Send a packet to each browse address... | |
503 | */ | |
504 | ||
505 | for (i = 0; i < NumBrowsers; i ++) | |
f9bacabb | 506 | if (sendto(BrowseSocket, packet, bytes, 0, Browsers + i, |
507 | sizeof(Browsers[0])) <= 0) | |
8828fd5f | 508 | LogMessage(LOG_ERROR, "SendBrowseList: sendto failed for browser %d - %s.", |
fd3ece61 | 509 | i + 1, strerror(errno)); |
d6de4648 | 510 | } |
511 | } | |
fd8b1cf8 | 512 | } |
a129ddbd | 513 | |
514 | ||
515 | /* | |
18d7d592 | 516 | * End of "$Id: dirsvc.c,v 1.30 1999/06/24 12:41:17 mike Exp $". |
a129ddbd | 517 | */ |