File: jd.c

package info (click to toggle)
hplip 3.22.10%2Bdfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 171,296 kB
  • sloc: python: 83,373; ansic: 71,016; cpp: 65,202; sh: 4,408; perl: 4,397; makefile: 937
file content (810 lines) | stat: -rw-r--r-- 24,680 bytes parent folder | download | duplicates (3)
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*****************************************************************************\

  jd.c - JetDirect support for multi-point transport driver 
 
  (c) 2004-2007 Copyright HP Development Company, LP

  Permission is hereby granted, free of charge, to any person obtaining a copy 
  of this software and associated documentation files (the "Software"), to deal 
  in the Software without restriction, including without limitation the rights 
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
  of the Software, and to permit persons to whom the Software is furnished to do 
  so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  Client/Server generic message format (see messaging-protocol.doc):

  Author: Naga Samrat Chowdary Narla, Sarbeswar Meher
\*****************************************************************************/
#ifdef HAVE_LIBNETSNMP

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <signal.h>
#include <stdlib.h>
#include "hpmud.h"
#include "hpmudi.h"


mud_device_vf __attribute__ ((visibility ("hidden"))) jd_mud_device_vf = 
{
   .open = jd_open,
   .close = jd_close,
   .get_device_id = jd_get_device_id,
   .get_device_status = jd_get_device_status,
   .channel_open = jd_channel_open,
   .channel_close = jd_channel_close,
   .channel_write = jd_channel_write,
   .channel_read = jd_channel_read
};

static mud_channel_vf jd_channel_vf =
{
   .open = jd_s_channel_open, 
   .close = jd_s_channel_close,
   .channel_write = jd_s_channel_write,
   .channel_read = jd_s_channel_read
};

static const int PrintPort[] = { 0, 9100, 9100, 9101, 9102 };
static const int ScanPort0[] = { 0, 9290, 9290, 9291, 9292 };
static const int GenericPort[] = { 0, 9220, 9220, 9221, 9222 };
static const int ScanPort1[] = { 0, 8290, 8290, 0, 0 };        /* hack for CLJ28xx */
static const int GenericPort1[] = { 0, 8292, 8292, 0, 0 };     /* hack for CLJ28xx (fax) */

const char __attribute__ ((visibility ("hidden"))) *kStatusOID = "1.3.6.1.4.1.11.2.3.9.1.1.7.0";            /* device id snmp oid */

static int ReadReply(mud_channel *pc)
{
   char buf[HPMUD_LINE_SIZE];
   int len=0, num=0;
   char *tail;
   enum HPMUD_RESULT stat;

   stat = jd_s_channel_read(pc, buf, sizeof(buf), 2, &len);
   buf[len] = 0;

   if (stat == HPMUD_R_OK)
      num = strtol((char *)buf, &tail, 10);

   return num;
}

static int device_id(const char *iporhostname, int port, char *buffer, int size)
{
   int len=0, maxSize, result, dt, status;

   maxSize = (size > 1024) ? 1024 : size;   /* RH8 has a size limit for device id */

   if ((len = GetSnmp(iporhostname, port, (char *)kStatusOID, (unsigned char *)buffer, maxSize, &dt, &status, &result)) == 0)
   {       
        //Try one more time with previous default community name string "public.1" which was used for old HP printers.  
       if ((len = GetSnmp(iporhostname, PORT_PUBLIC_1, (char *)kStatusOID, (unsigned char *)buffer, maxSize, &dt, &status, &result)) == 0)
       {
            BUG("unable to read device-id\n");
       }
   }

   return len; /* length does not include zero termination */
}

/* Create channel object given the requested socket id and service name. */
static int new_channel(mud_device *pd, int index, const char *sn)
{
   int stat=1;

   /* Check for existing name service already open. */
   if (pd->channel[index].client_cnt)
   {
#if 0
      if (index == HPMUD_EWS_CHANNEL)
      {
         pd->channel[index].client_cnt++;  /* allow multiple clients for separate USB interfaces only */
         stat = 0;
         DBG("reused %s channel=%d clientCnt=%d channelCnt=%d\n", sn, index, pd->channel[index].client_cnt, pd->channel_cnt);
      }
      else
#endif
         BUG("%s channel=%d is busy, used by [%d], clientCnt=%d channelCnt=%d\n", sn, index, pd->channel[index].pid, pd->channel[index].client_cnt, pd->channel_cnt);
      goto bugout; 
   }

   pd->channel[index].vf = jd_channel_vf;
   pd->channel[index].index = index;
   pd->channel[index].client_cnt = 1;
   pd->channel[index].sockid = index;
   pd->channel[index].pid = getpid();
   pd->channel[index].dindex = pd->index;
   pd->channel[index].fd = 0;
   pd->channel[index].socket = -1;
   strcpy(pd->channel[index].sn, sn);
   pd->channel_cnt++;

   stat = 0;
   DBG("new %s channel=%d clientCnt=%d channelCnt=%d\n", sn, index, pd->channel[index].client_cnt, pd->channel_cnt);

bugout:
   return stat;
}

/* Remove channel object given the channel decriptor. */
static int del_channel(mud_device *pd, mud_channel *pc)
{
   pc->client_cnt--;

   if (pc->client_cnt <= 0)
   {
      pd->channel_cnt--;
   }
   DBG("removed %s channel=%d clientCnt=%d channelCnt=%d\n", pc->sn, pc->index, pc->client_cnt, pd->channel_cnt);
   return 0;
}

/*********************************************************************************************************************************
 * JetDirect mud_device functions.
 */

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_open(mud_device *pd)
{
   char uri_model[128];
   char model[128];
   char *p, *tail;
   int len=0;
   int printqueue = 0;
   enum HPMUD_RESULT stat = HPMUD_R_IO_ERROR;

   pthread_mutex_lock(&pd->mutex);

   //Check whether the URI is generated dynamically by using bonjour discovery. If yes, then
   //there is no need of validating the MDL string against Device ID MDL string. If URI belongs to
   //an existing print queue then we need to validate the MDL, because the same IP address might get
   //assigned to some other device after adding a print queue.
   printqueue = (strstr(pd->uri, "queue=false") )? 0:1;

   //Scanimage discards "&queue=false" from the device URI and hence above check will not work.
   //Since Scanjets do not support SNMP hence skip device_id call by setting printqueue to 0
   if(strstr(pd->uri, "scanjet"))
      printqueue = 0;

   if (pd->id[0] == 0)
   {
      /* First client. */
      hpmud_get_uri_datalink(pd->uri, pd->ip, sizeof(pd->ip));

      if ((p = strcasestr(pd->uri, "port=")) != NULL)
         pd->port = strtol(p+5, &tail, 10);
      else
         pd->port = PORT_PUBLIC;

      if (pd->port > PORT_PUBLIC_3)
      {
         stat = HPMUD_R_INVALID_IP_PORT;
         BUG("invalid ip port=%d\n", pd->port);
         goto blackout;
      }

      if(printqueue)
      {
          len = device_id(pd->ip, pd->port, pd->id, sizeof(pd->id));  /* get new copy and cache it  */
          if (len == 0)
          {
             stat = HPMUD_R_IO_ERROR;
             goto blackout;
          }
      }
   }

   if(printqueue)
   {
       /* Make sure uri model matches device id model. */
       hpmud_get_uri_model(pd->uri, uri_model, sizeof(uri_model));
       hpmud_get_model(pd->id, model, sizeof(model));
       if (strcasecmp(uri_model, model) != 0)
       {
          stat = HPMUD_R_INVALID_URI;  /* different device plugged in */
          BUG("invalid uri model %s != %s\n", uri_model, model);
          goto blackout;
       }
   }

   stat = HPMUD_R_OK;

blackout:
   pthread_mutex_unlock(&pd->mutex);

   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_close(mud_device *pd)
{
   enum HPMUD_RESULT stat = HPMUD_R_OK;

   pthread_mutex_lock(&pd->mutex);
   pd->id[0] = 0;
   pthread_mutex_unlock(&pd->mutex);

   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_get_device_id(mud_device *pd, char *buf, int size, int *len)
{
   enum HPMUD_RESULT stat = HPMUD_R_IO_ERROR;
   
   *len=0;

   pthread_mutex_lock(&pd->mutex);

   *len = device_id(pd->ip, pd->port, pd->id, sizeof(pd->id));  /* get new copy and cache it  */ 

   if (*len)
   {
      memcpy(buf, pd->id, *len > size ? size : *len); 
      stat = HPMUD_R_OK;
   }

   pthread_mutex_unlock(&pd->mutex);
   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_get_device_status(mud_device *pd, unsigned int *status)
{
   *status = NFAULT_BIT;    /* there is no 8-bit status, so fake it */
   return HPMUD_R_OK;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_channel_write(mud_device *pd, mud_channel *pc, const void *buf, int length, int sec_timeout, int *bytes_wrote)
{   
   enum HPMUD_RESULT stat;

   pthread_mutex_lock(&pd->mutex);
   stat  = (pc->vf.channel_write)(pc, buf, length, sec_timeout, bytes_wrote);
   pthread_mutex_unlock(&pd->mutex);
   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_channel_read(mud_device *pd, mud_channel *pc, void *buf, int length, int sec_timeout, int *bytes_read)
{   
   enum HPMUD_RESULT stat;

   if (pd->io_mode == HPMUD_UNI_MODE)
   {
      stat = HPMUD_R_INVALID_STATE;
      BUG("invalid channel_read io_mode=%d\n", pd->io_mode);
   }
 
   pthread_mutex_lock(&pd->mutex);
   stat  = (pc->vf.channel_read)(pc, buf, length, sec_timeout, bytes_read);
   pthread_mutex_unlock(&pd->mutex);
   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_channel_open(mud_device *pd, const char *sn, HPMUD_CHANNEL *cd)
{
   int index;
   enum HPMUD_RESULT stat;

   /* Check for valid service requests. */
   if ((stat = service_to_channel(pd, sn, &index)) != HPMUD_R_OK)
      goto bugout;

   pthread_mutex_lock(&pd->mutex);

   if (new_channel(pd, index, sn))
   {
      stat = HPMUD_R_DEVICE_BUSY;
   }
   else
   {
      if ((stat = (pd->channel[index].vf.open)(&pd->channel[index])) != HPMUD_R_OK)  /* call transport specific open */
         del_channel(pd, &pd->channel[index]);   /* open failed, cleanup */
      else
         *cd = index;
   }

   pthread_mutex_unlock(&pd->mutex);

bugout:
   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_channel_close(mud_device *pd, mud_channel *pc)
{
   enum HPMUD_RESULT stat = HPMUD_R_OK;

   pthread_mutex_lock(&pd->mutex);
   stat = (pc->vf.close)(pc);      /* call trasport specific close */
   del_channel(pd, pc);
   pthread_mutex_unlock(&pd->mutex);

   return stat;
}

/*******************************************************************************************************************************
 * JetDirect channel functions.
 */

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_channel *pc)
{
   mud_device *pd = &msp->device[pc->dindex];
   struct sockaddr_in pin,tmp_pin;  
   struct hostent *he;
   char buf[HPMUD_LINE_SIZE];
   int r, len, port;
   enum HPMUD_RESULT stat = HPMUD_R_IO_ERROR;

   bzero(&tmp_pin, sizeof(tmp_pin)); 
   bzero(&pin, sizeof(pin));  
   pin.sin_family = AF_INET;  

   if(inet_pton(AF_INET, pd->ip, &(tmp_pin.sin_addr))) //Returns 0 when IP is invalid.
        pin.sin_addr.s_addr = inet_addr(pd->ip);  
   else
   {
        if((he=gethostbyname(pd->ip)) == NULL)
        {
            BUG("gethostbyname() returned NULL\n");
            goto bugout;  
        }

        pin.sin_addr = *((struct in_addr *)he->h_addr);
   }

   switch (pc->index)
   {
      case HPMUD_PRINT_CHANNEL:
         port = PrintPort[pd->port];
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open print port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to print port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         break;
      case HPMUD_SCAN_CHANNEL:
         if (pd->io_mode == HPMUD_DOT4_PHOENIX_MODE)
            port = ScanPort1[pd->port];
         else
            port = ScanPort0[pd->port];
         pin.sin_port = htons(port);

         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open scan port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to scan err=%d port %d: %m %s\n", errno, port, pd->uri);  
            goto bugout;  
         }
         if (pd->io_mode != HPMUD_DOT4_PHOENIX_MODE)
         {
            r = ReadReply(pc);
            if (r != 0)
            {
               BUG("invalid scan response %d port %d %s\n", r, port, pd->uri);  
               goto bugout;  
            } 
         }
         break;
      case HPMUD_MEMORY_CARD_CHANNEL:
      case HPMUD_FAX_SEND_CHANNEL:
      case HPMUD_CONFIG_UPLOAD_CHANNEL:
      case HPMUD_CONFIG_DOWNLOAD_CHANNEL:
         if (pd->io_mode == HPMUD_DOT4_PHOENIX_MODE)
            port = GenericPort1[pd->port];
         else
            port = GenericPort[pd->port];
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         } 

         if (pd->io_mode != HPMUD_DOT4_PHOENIX_MODE)
         {
            r = ReadReply(pc);
            if (r != 220)
            {  
               BUG("invalid response %d port %d %s\n", r, port, pd->uri);  
               goto bugout;  
            } 
            len = sprintf(buf, "open %d\n", pc->index);
            send(pc->socket, buf, len, 0);
            r = ReadReply(pc);
            if (r != 200)
            {  
               BUG("invalid response %d port %d %s\n", r, port, pd->uri);  
               goto bugout;  
            } 
            len = sprintf(buf, "data\n");
            send(pc->socket, "data\n", len, 0);
            r = ReadReply(pc);
            if (r != 200)
            {  
               BUG("invalid response %d port %d %s\n", r, port, pd->uri);  
               goto bugout;  
            }
         }

         break;
      case HPMUD_EWS_CHANNEL:
         port = 80;
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open ews port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to ews port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }
         break; 
      case HPMUD_SOAPSCAN_CHANNEL:
         port = 8289;
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open soap-scan port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to soap-scan port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }
         break; 
      case HPMUD_SOAPFAX_CHANNEL:
         port = 8295;
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open soap-fax port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to soap-fax port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }
         break; 
      case HPMUD_MARVELL_SCAN_CHANNEL:
         port = 8290;  /* same as ScanPort1[1] */
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open marvell-scan port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to marvell-scan port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }
         break;
      case HPMUD_LEDM_SCAN_CHANNEL:
      case HPMUD_EWS_LEDM_CHANNEL:
         port = 8080;
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
         {
            BUG("unable to open ledm-scan port %d: %m %s\n", port, pd->uri);
            goto bugout;
         }
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1)
         {
            BUG("unable to connect to ledm-scan port %d: %m %s\n", port, pd->uri);
            goto bugout;
         }
         break;            
      case HPMUD_ESCL_SCAN_CHANNEL:
         port = 80;
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
         {
            BUG("unable to open escl-scan port %d: %m %s\n", port, pd->uri);
            goto bugout;
         }
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1)
         {
            BUG("unable to connect to escl-scan port %d: %m %s\n", port, pd->uri);
            goto bugout;
         }
         break;            
      case HPMUD_IPP_CHANNEL:
         port = 80;
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
         {
            BUG("unable to open ipp port %d: %m %s\n", port, pd->uri);
            port = 631;
            pin.sin_port = htons(port);
            if((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
              goto bugout;
         }
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1)
         {
            BUG("unable to connect to ipp port %d: %m %s\n", port, pd->uri);
            goto bugout;
         }
         break;
      case HPMUD_MARVELL_FAX_CHANNEL:
         port = 8285;  
         pin.sin_port = htons(port);
         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
         {  
            BUG("unable to open marvell-fax port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }  
         if (connect(pc->socket, (struct sockaddr *)&pin, sizeof(pin)) == -1) 
         {  
            BUG("unable to connect to marvell-fax port %d: %m %s\n", port, pd->uri);  
            goto bugout;  
         }
         break; 
      case HPMUD_PML_CHANNEL:
         /* Do nothing here, use GetPml/SetPml instead of ReadData/WriteData. */
         break;
      default:
         BUG("unsupported service %d %s\n", pc->index, pd->uri);
         stat = HPMUD_R_INVALID_SN;
         goto bugout;
         break;
   }  

   stat = HPMUD_R_OK;

bugout:
   return stat;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_close(mud_channel *pc)
{
   if (pc->socket >= 0)
   {
      close(pc->socket);

      /* Delay for back-to-back scanning using scanimage. Otherwise next channel_open(HPMUD_SCAN_CHANNEL) can fail. */
      usleep(100000);
   }

   pc->socket = -1;  

   return HPMUD_R_OK;
}

enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_write(mud_channel *pc, const void *buf, int length, int sec_timeout, int *bytes_wrote)
{
   mud_device *pd = &msp->device[pc->dindex];
   int len, size, total=0;
   struct timeval tmo;
   fd_set master;
   fd_set writefd;
   int maxfd, ret;
   enum HPMUD_RESULT stat = HPMUD_R_IO_ERROR;

   *bytes_wrote=0;
   size = length;

   if (pc->socket<0)
   {
      stat = HPMUD_R_INVALID_STATE;
      BUG("invalid data link socket=%d %s\n", pc->socket, pd->uri);
      goto bugout;
   }

   FD_ZERO(&master);
   FD_SET(pc->socket, &master);
   maxfd = pc->socket;
   size = length;

   while (size > 0)
   {
      tmo.tv_sec = HPMUD_EXCEPTION_SEC_TIMEOUT;  /* note linux select will modify tmo */
      tmo.tv_usec = 0;
      writefd = master;
      if ((ret = select(maxfd+1, NULL, &writefd, NULL, &tmo)) == 0)
      {
         stat = HPMUD_R_IO_TIMEOUT;
         BUG("timeout write_channel %s\n", pd->uri);
         goto bugout;   /* timeout */
      }
      len = send(pc->socket, buf+total, size, 0);
      if (len < 0)
      {
         BUG("unable to write_channel: %m %s\n", pd->uri);
         goto bugout;
      }
      size-=len;
      total+=len;
      *bytes_wrote+=len;
   }
   
   DBG("write socket=%d len=%d size=%d\n", pc->socket, len, length);
   DBG_DUMP(buf, len < 32 ? len : 32);

   stat = HPMUD_R_OK;

bugout:
   return stat;
}

/*
 * Channel_read() tries to read "length" bytes from the peripheral. The returned read count may be zero
 * (timeout, no data available), less than "length" or equal "length".
 */
enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_read(mud_channel *pc, void *buf, int length, int sec_timeout, int *bytes_read)
{
   mud_device *pd = &msp->device[pc->dindex];
   int len=0;
   struct timeval tmo;
   fd_set master;
   fd_set readfd;
   int maxfd, ret;
   enum HPMUD_RESULT stat = HPMUD_R_IO_ERROR;

   *bytes_read = 0;

   if (pc->socket<0)
   {
      stat = HPMUD_R_INVALID_STATE;
      BUG("invalid data link socket=%d %s\n", pc->socket, pd->uri);
      goto bugout;
   }

   FD_ZERO(&master);
   FD_SET(pc->socket, &master);
   maxfd = pc->socket;
   tmo.tv_sec = sec_timeout;
   tmo.tv_usec = 0;

   readfd = master;
   ret = select(maxfd+1, &readfd, NULL, NULL, &tmo);
   if (ret < 0)
   {
      BUG("unable to read_channel: %m %s\n", pd->uri);
      goto bugout;
   }
   if (ret == 0)
   {
      stat = HPMUD_R_IO_TIMEOUT;
//      if (sec_timeout >= HPMUD_EXCEPTION_SEC_TIMEOUT)
         BUG("timeout read_channel sec=%d %s\n", sec_timeout, pd->uri);
      goto bugout;
   }
   else
   {
      if ((len = recv(pc->socket, buf, length, 0)) < 0)
      {
         BUG("unable to read_channel: %m %s\n", pd->uri);
         goto bugout;
      }
   }

   DBG("read socket=%d len=%d size=%d\n", pc->socket, len, length);
   DBG_DUMP(buf, len < 32 ? len : 32);

   *bytes_read = len;
   stat = HPMUD_R_OK;

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_make_net_uri(const char *ip, int port, char *uri, int uri_size, int *bytes_read)
{
   char id[1024];
   char model[128];
   enum HPMUD_RESULT stat;

   DBG("[%d] hpmud_make_net_uri() ip=%s port=%d\n", getpid(), ip, port);

   *bytes_read=0;

   uri[0]=0;

   if (ip == 0 || ip[0]==0)
   {
      BUG("invalid ip %s\n", ip);
      stat = HPMUD_R_INVALID_IP;
      goto bugout;
   }

   if (device_id(ip, port, id, sizeof(id)) > 0 && is_hp(id))
   {
      hpmud_get_model(id, model, sizeof(model));
      if (port == PORT_PUBLIC)
         *bytes_read = snprintf(uri, uri_size, "hp:/net/%s?ip=%s", model, ip); 
      else
         *bytes_read = snprintf(uri, uri_size, "hp:/net/%s?ip=%s&port=%d", model, ip, port); 
   }
   else
   {
      BUG("invalid ip %s\n", ip);
      stat = HPMUD_R_INVALID_IP;
      goto bugout;
   }

   stat = HPMUD_R_OK;

bugout:
   return stat;
}

enum HPMUD_RESULT hpmud_make_mdns_uri(const char *host, int port, char *uri, int uri_size, int *bytes_read)
{
   char id[1024];
   char model[128];
   char ip[HPMUD_LINE_SIZE];              /* internet address */
   enum HPMUD_RESULT stat;

   DBG("[%d] hpmud_make_mdns_uri() host=%s port=%d\n", getpid(), host, port);

   *bytes_read=0;

   uri[0]=0;

   if (host == 0 || host[0]==0)
   {
      BUG("invalid host %s\n", host);
      stat = HPMUD_R_INVALID_MDNS;
      goto bugout;
   }
   #ifdef HAVE_LIBAVAHI
   if (avahi_lookup(host) != AVAHI_STATUS_OK)
   {
      BUG("invalid host %s, check firewall UDP/5353 or try using IP\n", host);
      stat = HPMUD_R_INVALID_MDNS;
      goto bugout;
   }
   #endif
   if (device_id(ip, port, id, sizeof(id)) > 0 && is_hp(id))
   {
      hpmud_get_model(id, model, sizeof(model));
      if (port == 1)
         *bytes_read = snprintf(uri, uri_size, "hp:/net/%s?zc=%s", model, host);
      else
         *bytes_read = snprintf(uri, uri_size, "hp:/net/%s?zc=%s&port=%d", model, host, port);
   }
   else
   {
      BUG("invalid host %s, or try using IP\n", host);
      stat = HPMUD_R_INVALID_MDNS;
      goto bugout;
   }

   stat = HPMUD_R_OK;

bugout:
   return stat;
}

#endif  /* HAVE_LIBNETSNMP */