File: bjnp-commands.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 (349 lines) | stat: -rw-r--r-- 10,458 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
/*
 * Low level TCP and UDP IO communication 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 <errno.h>
#include <stdio.h>
#include "bjnp.h"
#include "bjnp-protocol.h"
#include "bjnp-commands.h"
#include "bjnp-io.h"

void
clear_cmd(bjnp_command_t *cmd)
{
    memset(cmd, 0, sizeof(*cmd));
}

static void
bjnp_set_command_header(uint8_t dev_type, uint16_t seq_no, uint16_t session_id, bjnp_command_t *cmd, char cmd_code,
                        int command_len)
{
    /*
     * Set command buffer with command code, session_id and lenght of payload
     * Returns: sequence number of command
     */
    strncpy(cmd->header.BJNP_id, BJNP_STRING, sizeof(cmd->header.BJNP_id));
    cmd->header.dev_type = dev_type;
    cmd->header.cmd_code = cmd_code;
    cmd->header.unknown1 = htons(0);
    cmd->header.payload_len = htonl(command_len - bjnp_header_size);
    cmd->header.seq_no = htons(seq_no);
    cmd->header.session_id = htons(session_id);
}

void bjnp_defaults_set_command_header(bjnp_command_t *cmd, char cmd_code,
                                     int command_len)
{
    bjnp_set_command_header(BJNP_CMD_PRINT, 0, 0, cmd, cmd_code, command_len);
}


int
bjnp_printer_set_command_header(printer_t *printer, bjnp_command_t *cmd, char cmd_code,
                        int command_len)
{
    bjnp_set_command_header( /* printer-> dev_type */ BJNP_CMD_PRINT, ++printer->serial,
                                    printer->session_id, cmd, cmd_code, command_len);
    return printer->serial;
}

static int
bjnp_process_udp_command(http_addr_t *addr, bjnp_command_t *command,
                         int cmd_len, bjnp_response_t *response)
{
    /*
     * Send UDP command and retrieve response
     * Returns: length of response or -1 in case of error
     */

    int sockfd;
    int numbytes;
    fd_set fdset;
    struct timeval timeout;
    int attempt;
    char ipaddress[128];
    int port;
    int size;
    char family[BJNP_FAMILY_MAX];

    get_address_info(addr, ipaddress, &port, family);
    bjnp_debug(LOG_DEBUG, "Sending UDP command to %s port %d(%s)\n",
               ipaddress, port, family);

    if ((sockfd = socket(get_protocol_family(*addr), SOCK_DGRAM, 0)) == -1) {
        bjnp_debug(LOG_CRIT, "bjnp_process_udp_command: sockfd - %s\n",
                   strerror(errno));
        return -1;
    }

    size = sa_size(*addr);

    if (connect(sockfd, &(addr->addr), (socklen_t) size)
        != 0) {
        bjnp_debug(LOG_CRIT, "bjnp_process_udp_command: connect - %s\n",
                   strerror(errno));
        close(sockfd);
        return -1;
    }

    for (attempt = 0; attempt < 3; attempt++) {
        if ((numbytes = send(sockfd, command, cmd_len, 0)) != cmd_len) {
            bjnp_debug(LOG_CRIT, "bjnp_process_udp_command: Sent only %d bytes of packet",
                       numbytes);
        }

        FD_ZERO(&fdset);
        FD_SET(sockfd, &fdset);
        timeout.tv_sec = 3;
        timeout.tv_usec = 0;

        if (select(sockfd + 1, &fdset, NULL, NULL, &timeout) <= 0) {
            /* no data received OR error, in either case retry */
            continue;
        }

        if ((numbytes = recv(sockfd, response, sizeof(*response), MSG_WAITALL)) == -1) {
            bjnp_debug(LOG_CRIT, "bjnp_process_udp_command: no data received (recv)");
            continue;
        }

        close(sockfd);
        return numbytes;
    }

    /* max attempts reached, return failure */
    close(sockfd);
    return -1;
}

int
get_printer_id(http_addr_t *addr, char *model, char *IEEE1284_id)
{
    /*
     * get printer identity
     * Sets model (make and model) and IEEE1284_id
     */

    bjnp_command_t cmd;
    bjnp_response_t id;
    char printer_id[BJNP_IEEE1284_MAX];
    int resp_len;
    int id_len;
    int min_size;

    /* set defaults */

    strcpy(model, "Unidentified printer");
    strcpy(IEEE1284_id, "");

    clear_cmd(&cmd);
    bjnp_defaults_set_command_header(&cmd, CMD_UDP_GET_ID, sizeof(cmd.udp_get_id));

    bjnp_hexdump(LOG_DEBUG2, "Get printer identity", (char *) &cmd,
                 sizeof(cmd.udp_get_id));

    resp_len =
        bjnp_process_udp_command(addr, &cmd, sizeof(cmd.udp_get_id),
                                 &id);

    min_size = bjnp_header_size + sizeof(id.udp_identity_response.id_len);

    if (resp_len <= min_size) {
        return -1;
    }

    bjnp_hexdump(LOG_DEBUG2, "Printer identity:", &id, resp_len);

    id_len = ntohs(id.udp_identity_response.id_len) - sizeof(
                 id.udp_identity_response.id_len);

    /* check id_len */

    if ((id_len < 0) || (id_len > (resp_len - bjnp_header_size)) ||
        (id_len > BJNP_IEEE1284_MAX)) {
        bjnp_debug(LOG_ERROR,
                   "Id - length received is invalid: %d (total response length = %d\n",
                   id_len, resp_len);
        return -1;
    }

    /* set IEEE1284_id */

    if (printer_id != NULL) {
        strncpy(printer_id, id.udp_identity_response.id, id_len);
        printer_id[id_len] = '\0';
    }

    bjnp_debug(LOG_INFO, "Identity = %s\n", printer_id);

    if (IEEE1284_id != NULL) {
        strcpy(IEEE1284_id, printer_id);
    }

    /* get make&model from IEEE1284 id  */

    if (model != NULL) {
        model[0] = '\0';
        parse_IEEE1284_to_model(printer_id, model);
        bjnp_debug(LOG_DEBUG, "Printer model = %s\n", model);
    }

    return 0;
}

int
bjnp_get_status(printer_t *printer, char *status_buf)
{
    /*
     * get printer status
     */

    bjnp_command_t cmd;
    bjnp_response_t response;
    int resp_len;
    uint32_t status_len;

    /* set defaults */

    clear_cmd(&cmd);
    bjnp_printer_set_command_header(printer, &cmd, CMD_UDP_GET_STATUS,
                            sizeof(cmd.udp_get_status));

    bjnp_hexdump(LOG_DEBUG2, "Get printer status", (char *) &cmd,
                 sizeof(cmd.udp_get_status));

    resp_len =
        bjnp_process_udp_command(&(printer->printer_sa), &cmd,
                                 sizeof(cmd.udp_get_status),
                                 &response);

    if (resp_len <= 0) {
        return BJNP_IO_ERROR;
    }

    bjnp_hexdump(LOG_DEBUG2, "Printer status:", &response, resp_len);

    status_len = ntohl(response.udp_status_response.header.payload_len);

    if (status_len >= BJNP_STATUS_MAX ||  status_len < 16) {
        bjnp_debug(LOG_ERROR, "Get printer status: ERROR, invalid response length (%d)\n", status_len);
        return BJNP_IO_ERROR;
    }

    if (status_len != (resp_len - sizeof(response.udp_status_response.header))) {
        bjnp_debug(LOG_ERROR,
                   "status length (%d) does not match response length (%d)!!\n", status_len,
                   resp_len - sizeof(response.udp_status_response.header));
        return BJNP_IO_ERROR;
    }

    /* check which version of the response we received */

    if (strncmp(response.udp_status_response_v2.status, "<?xml", 5) == 0) {

        /* version 2 of the status response */

        response.udp_status_response_v2.status[status_len] = '\0';
        strcpy(status_buf, response.udp_status_response_v2.status);
    } else {
        /* old format response message */
        status_len = status_len - sizeof(response.udp_status_response.status_len);
        response.udp_status_response.status[status_len] = '\0';
        strcpy(status_buf, response.udp_status_response.status);
    }

    bjnp_debug(LOG_INFO, "Printer status: %s\n", status_buf);
    return BJNP_OK;
}

int bjnp_send_job_details(http_addr_t *addr, const char *user, const char *title)
{
    /*
     * send details of printjob to printer
     * Returns: 0 = ok, -1 = error
     */

    char hostname[BJNP_HOST_MAX];
    int resp_len;
    bjnp_command_t cmd;
    bjnp_response_t resp;
    int session_id = 0;

    /* send job details command */

    clear_cmd(&cmd);
    bjnp_defaults_set_command_header(&cmd, CMD_UDP_PRINT_JOB_DET,
                            sizeof(cmd.udp_job_details));

    /* create payload */

    gethostname(hostname, BJNP_HOST_MAX - 1);

    charTo2byte(cmd.udp_job_details.unknown, "",
                sizeof(cmd.udp_job_details.unknown));
    charTo2byte(cmd.udp_job_details.hostname, hostname,
                sizeof(cmd.udp_job_details.hostname));
    charTo2byte(cmd.udp_job_details.username, user,
                sizeof(cmd.udp_job_details.username));
    charTo2byte(cmd.udp_job_details.jobtitle, title, sizeof(cmd.udp_job_details));

    bjnp_hexdump(LOG_DEBUG2, "Job details", &cmd,
                 sizeof(cmd.udp_job_details));
    resp_len = bjnp_process_udp_command(addr, &cmd, sizeof(cmd.udp_job_details),
                                        &resp);

    if (resp_len > 0) {
        bjnp_hexdump(LOG_DEBUG2, "Job details response:", &resp,
                     resp_len);
        session_id = ntohs(resp.udp_print_job_details_response.header.session_id);
        return session_id;
    }

    return -1;
}

int bjnp_send_close(printer_t *printer)
{
    /*
     * Signal end of printjob to printer
     */

    int resp_len;
    bjnp_command_t cmd;
    bjnp_response_t resp;

    clear_cmd(&cmd);
    bjnp_printer_set_command_header(printer, &cmd, CMD_UDP_CLOSE, sizeof(cmd.udp_close));

    bjnp_hexdump(LOG_DEBUG2, "bjnp_send_close", (char *) &cmd,
                 sizeof(cmd.udp_close));
    resp_len =
        bjnp_process_udp_command(&(printer->printer_sa), &cmd, sizeof(cmd.udp_close),
                                 &resp);

    if (resp_len != sizeof(resp.udp_close_response)) {
        bjnp_debug(LOG_CRIT,
                   "Received %d characters in close response, expected %d\n",
                   resp_len, sizeof(resp.udp_close_response));
        return -1;
    }

    bjnp_hexdump(LOG_DEBUG2, "Finish printjob response", &resp, resp_len);
    return 0;
}