File: bjnp-discover.c

package info (click to toggle)
cups-bjnp 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 628 kB
  • ctags: 329
  • sloc: ansic: 2,519; sh: 1,254; makefile: 27
file content (526 lines) | stat: -rw-r--r-- 17,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 * Printer discovery implementation for
 * bjnp backend for the Common UNIX Printing System (CUPS).
 * Copyright 2008-2014 by Louis Lagendijk
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#ifdef HAVE_GETIFADDRS
#include <ifaddrs.h>
#endif
#include "bjnp.h"
#include "bjnp-protocol.h"
#include "bjnp-commands.h"

static int create_broadcast_socket(const http_addr_t *local_addr)
{
    int sockfd = -1;
    int broadcast = 1;
    int ipv6_v6only = 1;


    if ((sockfd = socket(local_addr-> addr.sa_family, SOCK_DGRAM, 0)) == -1) {
        bjnp_debug
        (LOG_CRIT, "create_broadcast_socket: cannot open socket - %s",
         strerror(errno));
        return -1;
    }

    /* Set broadcast flag on socket */

    if (setsockopt
        (sockfd, SOL_SOCKET, SO_BROADCAST, (const char *) &broadcast,
         sizeof(broadcast)) != 0) {
        bjnp_debug
        (LOG_CRIT,
         "create_broadcast_socket: setting socket option SO_BROADCAST failed - %s",
         strerror(errno));
        close(sockfd);
        return -1;
    };

#ifdef ENABLE_IPV6

    /* For an IPv6 socket, bind to v6 only so a V6 socket can co-exist with a v4 socket */
    if ((local_addr -> addr.sa_family == AF_INET6) && (setsockopt
            (sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *) &ipv6_v6only,
             sizeof(ipv6_v6only)) != 0)) {
        bjnp_debug
        (LOG_CRIT,
         "create_broadcast_socket: setting socket option IPV6_V6ONLY failed - %s",
         strerror(errno));
        close(sockfd);
        return -1;
    };

#endif

    if (bind(sockfd, &(local_addr->addr), (socklen_t) sa_size(*local_addr)) != 0) {
        bjnp_debug
        (LOG_CRIT,
         "create_broadcast_socket: bind socket to local address failed - %s\n",
         strerror(errno));
        close(sockfd);
        return -1;
    }

    return sockfd;
}

static int
prepare_socket(const char *if_name, const http_addr_t *local_sa,
               const http_addr_t *broadcast_sa, http_addr_t *dest_sa)
{
    /*
     * Prepare a socket for broadcast or multicast
     * Input:
     * if_name: the name of the interface
     * local_sa: local address to use
     * broadcast_sa: broadcast address to use, if NULL we use all hosts
     * dest_sa: (write) where to return destination address of broadcast
     * retuns: open socket or -1
     */

    int socket = -1;
    http_addr_t local_sa_copy;

    if (local_sa == NULL) {
        bjnp_debug(LOG_DEBUG,
                   "%s is not a valid IPv4 interface, skipping...\n",
                   if_name);
        return -1;
    }

    memset(&local_sa_copy, 0, sizeof(local_sa_copy));
    memcpy(&local_sa_copy, local_sa, sa_size(*local_sa));

    switch (local_sa_copy.addr.sa_family) {
        case AF_INET: {
                local_sa_copy.ipv4.sin_port = htons(BJNP_PORT_PRINT);

                if (local_sa_copy.ipv4.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
                    /* not a valid interface */

                    bjnp_debug(LOG_DEBUG,
                               "%s is not a valid IPv4 interface, skipping...\n",
                               if_name);
                    return -1;
                }


                /* send broadcasts to the broadcast address of the interface */

                memcpy(dest_sa, broadcast_sa, sa_size(*dest_sa));
                dest_sa -> ipv4.sin_port = htons(BJNP_PORT_PRINT);

                if ((socket = create_broadcast_socket(&local_sa_copy)) != -1) {
                    bjnp_debug(LOG_DEBUG, "%s is IPv4 capable, sending broadcast, socket = %d\n",
                               if_name, socket);
                } else {
                    bjnp_debug(LOG_DEBUG, "%s is IPv4 capable, but failed to create a socket.\n",
                               if_name);
                    return -1;
                }
            }
            break;
#ifdef ENABLE_IPV6

        case AF_INET6: {
                local_sa_copy.ipv6.sin6_port = htons(BJNP_PORT_PRINT);

                if (IN6_IS_ADDR_LOOPBACK(&(local_sa_copy.ipv6.sin6_addr))) {
                    /* not a valid interface */

                    bjnp_debug(LOG_DEBUG,
                               "%s is not a valid IPv6 interface, skipping...\n",
                               if_name);
                    return -1;
                } else {
                    dest_sa -> ipv6.sin6_family = AF_INET6;
                    dest_sa -> ipv6.sin6_port = htons(BJNP_PORT_PRINT);
                    inet_pton(AF_INET6, "ff02::1", dest_sa -> ipv6.sin6_addr.s6_addr);

                    if ((socket = create_broadcast_socket(&local_sa_copy)) != -1) {
                        bjnp_debug(LOG_DEBUG, "%s is IPv6 capable, sending broadcast, socket = %d\n",
                                   if_name, socket);
                    } else {
                        bjnp_debug(LOG_DEBUG, "%s is IPv6 capable, but failed to create a socket.\n",
                                   if_name);
                        return -1;
                    }
                }
            }
            break;
#endif

        default:
            socket = -1;
    }

    return socket;
}

static int
bjnp_send_broadcast(int sockfd, const http_addr_t *broadcast_addr,
                    bjnp_command_t cmd, int size)
{
    int num_bytes;

    /* set address to send packet to */
    /* usebroadcast address of interface */

    if ((num_bytes = sendto(sockfd, &cmd, size, 0,
                            &(broadcast_addr->addr),
                            sa_size(*broadcast_addr))) != size) {
        bjnp_debug(LOG_DEBUG,
                   "bjnp_send_broadcast: Socket: %d: sent only %x = %d bytes of packet, error = %s\n",
                   sockfd, num_bytes, num_bytes, strerror(errno));
        /* not allowed, skip this interface */

        return -1;
    }

    return sockfd;
}

static void add_printer_address(http_addr_t *printer_sa, char *mac_address,
                                int *no_printers, struct printer_list *printers)
{
    char ip_addr[BJNP_HOST_MAX];
    int port;
    char family[BJNP_FAMILY_MAX];
    int i;
    bjnp_address_type_t host_type;
    char host[BJNP_HOST_MAX];


    host_type = get_printer_host(*printer_sa, host, &port, family);

    bjnp_debug(LOG_DEBUG, "add_printer_address(%s) %d\n", host, *no_printers);

    /* Check if a device number is already allocated to any of the printer's addresses */
    for (i = 0; i < *no_printers; i++) {
        /* we check for mac_addresses as */
        /* an IPv6 host can have multiple adresses */

        if (strcmp(printers[i].mac_address, mac_address) == 0) {
            if (host_type > printers[i].host_type) {
                /* This is a better address as it resolves to a FQDN or */
                /* it is a global address, so we do not rely on a scope */
                /* which could change when the number of IP-interfaces changes */
                memcpy(printers[i].addr, printer_sa, sa_size(*printer_sa));
                strcpy(printers[i].hostname, host);
                printers[i].host_type = host_type;
                bjnp_debug(LOG_DEBUG, "Printer at %s found before, but found better address!\n",
                           host);
            } else {
                bjnp_debug(LOG_DEBUG, "Printer at %s found before, not added!\n",
                           host);
            }

            return;
        }
    }

    /* create a new device structure for this address */

    if (*no_printers == BJNP_PRINTERS_MAX) {
        bjnp_debug(LOG_CRIT,
                   "Too many devices, ran out of device structures, cannot add %s\n",
                   host);
        return;
    }

    printers[*no_printers].addr = malloc(sizeof(http_addr_t));
    memset(printers[*no_printers].addr, 0, sizeof(http_addr_t));
    memcpy(printers[*no_printers].addr, printer_sa, sa_size(*printer_sa));
    printers[*no_printers].host_type = host_type;
    printers[*no_printers].port = port;
    strcpy(printers[*no_printers].hostname, host);
    strcpy(printers[*no_printers].mac_address, mac_address);

    if (get_printer_id(printer_sa,  printers[*no_printers].model,
                       printers[*no_printers].IEEE1284_id) != 0) {
        bjnp_debug(LOG_CRIT, "Cannot read printer make & model: %s\n", host);
        free(printers[*no_printers].addr);
        return;
    }

    get_address_info(printer_sa, ip_addr, &port, family);
    bjnp_debug(LOG_DEBUG, "Printer not yet in our list, added it: %s:%d(%s)\n",
               host, port, family);
    (*no_printers)++;
}

static void add_printer(http_addr_t *printer_sa, bjnp_response_t *resp,
                        int *no_printers, struct printer_list *printers)
{
    char mac_address_string[BJNP_SERIAL_MAX];
    int i;

    /* create serial/mac_address string */

    u8tohex_string(resp -> udp_discover_response.mac_addr, mac_address_string,
                   sizeof(resp -> udp_discover_response.mac_addr));

    if (printer_sa->addr.sa_family == AF_INET) {
        add_printer_address(printer_sa, mac_address_string, no_printers, printers);
    }

#ifdef ENABLE_IPV6
    else {
        int no_addresses;
        http_addr_t printer_addr;

        /* IPV6, we also need to try all adresses received in the discover response */

        add_printer_address(printer_sa, mac_address_string, no_printers, printers);
        no_addresses = resp -> udp_discover_response.addr_len >> 4;
        memset(&printer_addr, 0, sizeof(printer_addr));
        memcpy(&printer_addr, printer_sa, sa_size(*printer_sa));

        for (i = 0; i < no_addresses; i++) {
            memcpy(printer_addr.ipv6.sin6_addr.s6_addr,
                   &(resp -> udp_discover_response.addresses.ipv6.ipv6_addr[i]), 16);

            /* we only add link-local address if the response came from a */
            /* link-local aadress (done above) */

            if (! IN6_IS_ADDR_LINKLOCAL(&(printer_addr.ipv6.sin6_addr))) {
                /* address is already in network byte order */

                add_printer_address(&printer_addr, mac_address_string, no_printers, printers);
            }
        }
    }

#endif
}

int bjnp_discover_printers(struct printer_list *printers)
{
    int numbytes = 0;
    bjnp_command_t cmd;
    bjnp_response_t disc_resp;
    int socket_fd[BJNP_SOCK_MAX];
    int no_sockets = 0;
    int i;
    int attempt;
    int last_socketfd = 0;
    fd_set fdset;
    fd_set active_fdset;
    struct timeval timeout;
    int no_printers = 0;
    http_addr_t broadcast_addr[BJNP_SOCK_MAX];
    http_addr_t printer_sa;
    socklen_t socklen;
    char host[BJNP_HOST_MAX];
    int port;
    char family[BJNP_FAMILY_MAX];

    clear_cmd(&cmd);
    memset(broadcast_addr, 0, sizeof(broadcast_addr));
    memset(&printer_sa, 0 , sizeof(printer_sa));
    bjnp_debug(LOG_DEBUG, "sanei_bjnp_find_devices:\n");

    for (i = 0; i < BJNP_SOCK_MAX; i++) {
        socket_fd[i] = -1;
    }

    bjnp_debug
    (LOG_DEBUG,
     "Do auto detection of printers...\n");

    /*
     * Send UDP DISCOVER to discover printers and return the list of printers found
     */

    FD_ZERO(&fdset);
    bjnp_defaults_set_command_header(&cmd, CMD_UDP_DISCOVER, sizeof(cmd.udp_discover));

    no_sockets = 0;
#ifdef HAVE_GETIFADDRS
    {
        struct ifaddrs *interfaces = NULL;
        struct ifaddrs *interface;
        getifaddrs(&interfaces);

        /* create a socket for each suitable interface */

        interface = interfaces;

        while ((no_sockets < BJNP_SOCK_MAX) && (interface != NULL)) {
            if (!(interface -> ifa_flags & IFF_POINTOPOINT) &&
                ((socket_fd[no_sockets] =
                      prepare_socket(interface -> ifa_name,
                                     (http_addr_t *) interface -> ifa_addr,
                                     (http_addr_t *) interface -> ifa_broadaddr,
                                     &broadcast_addr[no_sockets])) != -1)) {
                /* track highest used socket for later use in select */
                if (socket_fd[no_sockets] > last_socketfd) {
                    last_socketfd = socket_fd[no_sockets];
                }

                FD_SET(socket_fd[no_sockets], &fdset);
                no_sockets++;
            }

            interface = interface->ifa_next;
        }

        freeifaddrs(interfaces);
    }
#else
    /* we have no easy way to find interfaces with their broadcast addresses. */
    /* use global broadcast and all-hosts instead */
    {
        http_addr_t local;
        http_addr_t bc_addr;

        memset(&local, 0, sizeof(local));
        local.ipv4.sin_family = AF_INET;
        local.ipv4.sin_addr.s_addr = htonl(INADDR_ANY);

        bc_addr.ipv4.sin_family = AF_INET;
        bc_addr.ipv4.sin_port = htons(BJNP_PORT_PRINT);
        bc_addr.ipv4.sin_addr.s_addr = htonl(INADDR_BROADCAST);

        socket_fd[no_sockets] = prepare_socket("any_interface",
                                               &local,
                                               &bc_addr,
                                               &broadcast_addr[no_sockets]);

        if (socket_fd[no_sockets] >= 0) {
            FD_SET(socket_fd[no_sockets], &fdset);

            if (socket_fd[no_sockets] > last_socketfd) {
                last_socketfd = socket_fd[no_sockets];
            }

            no_sockets++;
        }

#ifdef ENABLE_IPV6
        local.ipv6.sin6_family = AF_INET6;
        local.ipv6.sin6_addr = in6addr_any;

        socket_fd[no_sockets] = prepare_socket("any_interface",
                                               &local,
                                               NULL,
                                               &broadcast_addr[no_sockets]);

        if (socket_fd[no_sockets] >= 0) {
            FD_SET(socket_fd[no_sockets], &fdset);

            if (socket_fd[no_sockets] > last_socketfd) {
                last_socketfd = socket_fd[no_sockets];
            }

            no_sockets++;
        }

#endif /* ENABLE_IPV6 */
    }
#endif /* HAVE_GETIFADDRS */

    /* send BJNP_MAX_BROADCAST_ATTEMPTS broadcasts on each prepared socket */
    for (attempt = 0; attempt < BJNP_MAX_BROADCAST_ATTEMPTS; attempt++) {
        for (i = 0; i < no_sockets; i++) {
            bjnp_send_broadcast(socket_fd[i], &broadcast_addr[i], cmd,
                                sizeof(cmd.udp_discover));
        }

        /* wait for some time between broadcast packets */
        usleep(BJNP_BROADCAST_INTERVAL * USLEEP_MS);
    }

    /* wait for a UDP response */

    timeout.tv_sec = 0;
    timeout.tv_usec = BJNP_BC_RESPONSE_TIMEOUT * USLEEP_MS;


    active_fdset = fdset;

    while (select(last_socketfd + 1, &active_fdset, NULL, NULL, &timeout) > 0) {
        bjnp_debug(LOG_DEBUG, "Select returned, time left %d.%d....\n",
                   (int) timeout.tv_sec, (int) timeout.tv_usec);

        for (i = 0; i < no_sockets; i++) {
            if (FD_ISSET(socket_fd[i], &active_fdset)) {

                socklen =  sizeof(printer_sa);

                if ((numbytes =
                         recvfrom(socket_fd[i], &disc_resp, sizeof(disc_resp), 0,
                                  &(printer_sa.addr), &socklen)) == -1) {
                    bjnp_debug
                    (LOG_INFO, "find_devices: no data received, while socket is ready");
                    break;
                } else {
                    /* process incoming packet */

                    get_address_info(&printer_sa, host, &port, family);

                    bjnp_debug(LOG_DEBUG, "Response received from %s port %d(%s)\n", host, port,
                               family);
                    bjnp_hexdump(LOG_DEBUG2, "Discover response:\n", &disc_resp, numbytes);

                    /* check if something sensible is returned */
                    if ((numbytes < bjnp_header_size) ||
                        (strncmp("BJNP", disc_resp.header.BJNP_id, 4) != 0)) {
                        /* not a valid response, assume not a printer  */

                        char bjnp_id[5];
                        strncpy(bjnp_id,  disc_resp.header.BJNP_id, 4);
                        bjnp_id[4] = '\0';
                        bjnp_debug(LOG_INFO,
                                   "Invalid discover response! Length = %d, Id = %s\n",
                                   numbytes, bjnp_id);
                        break;
                    }

                    if (!((disc_resp.header.dev_type) & 0x80)) {
                        /* not a response, a discover command from somebody else or */
                        /* a discover command that we generated */
                        break;
                    }
                };

                /* valid response, register printer */
                add_printer(&printer_sa, &disc_resp, &no_printers, printers);

            }
        }

        active_fdset = fdset;
        timeout.tv_sec = 0;
        timeout.tv_usec = BJNP_BC_RESPONSE_TIMEOUT * USLEEP_MS;
    }

    bjnp_debug(LOG_DEBUG, "printer discovery finished...\n");

    for (i = 0; i < no_sockets; i++) {
        close(socket_fd[i]);
    }

    return no_printers;
}