Package: hplip
Version: 3.18.10+dfsg0-2
Severity: wishlist
Tags: patch ipv6

Please see https://bugs.launchpad.net/hplip/+bug/1724089 for the
original report, which includes a patch that adds IPv6 support to
libhpmud.

The patch ensures that libhpmud properly resolves IPv6 addresses and
hostnames. I've tested it against the latest hplip (3.18.10) and it
works well.

Please consider merging it into Debian's package.

Thanks,
Christopher Martin
From e7bc4bd41239d2e60f0406415ae0cc3d7a40cc2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20B=C3=A4chler?= <tho...@archlinux.org>
Date: Wed, 30 Aug 2017 00:25:32 +0200
Subject: [PATCH] Add ipv6 support to libhpmud

---
 io/hpmud/jd.c | 147 ++++++++++++++++++----------------------------------------
 1 file changed, 45 insertions(+), 102 deletions(-)

diff --git a/io/hpmud/jd.c b/io/hpmud/jd.c
index 06f5072ee..45d6d36cc 100644
--- a/io/hpmud/jd.c
+++ b/io/hpmud/jd.c
@@ -332,44 +332,49 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_channel_close(mud_d
  * JetDirect channel functions.
  */
 
+static int try_connect(const char* address, int port)
+{
+   int s = -1;
+   struct addrinfo *addrinfo = NULL;
+
+   struct addrinfo hints;
+   bzero(&hints, sizeof(struct addrinfo));
+   hints.ai_family = AF_UNSPEC;
+   hints.ai_socktype = SOCK_STREAM;
+   hints.ai_protocol = IPPROTO_TCP;
+   hints.ai_flags = AI_ADDRCONFIG;
+   char service[6];
+   snprintf(service, sizeof(service) / sizeof(service[0]), "%d", port);
+   getaddrinfo(address, service, &hints, &addrinfo);
+
+   const struct addrinfo *a;
+   for(a = addrinfo; a != NULL; a = a->ai_next)
+   {
+     if ((s = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) == -1)
+       continue;
+     if (connect(s, a->ai_addr, a->ai_addrlen) == 0)
+       break;
+     close(s);
+     s = -1;
+   }
+   if (addrinfo != NULL)
+     freeaddrinfo(addrinfo);
+   return s;
+}
+
 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) 
-         {  
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
+         {
             BUG("unable to connect to print port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
          }  
@@ -379,14 +384,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
             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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to scan err=%d port %d: %m %s\n", errno, port, pd->uri);  
             goto bugout;  
@@ -409,13 +407,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
             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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
@@ -450,13 +442,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to ews port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
@@ -464,13 +450,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to soap-scan port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
@@ -478,13 +458,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to soap-fax port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
@@ -492,13 +466,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to marvell-scan port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
@@ -506,14 +474,8 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          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)
+         port = 8080;  
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {
             BUG("unable to connect to ledm-scan port %d: %m %s\n", port, pd->uri);
             goto bugout;
@@ -521,13 +483,7 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          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)
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {
             BUG("unable to connect to escl-scan port %d: %m %s\n", port, pd->uri);
             goto bugout;
@@ -535,30 +491,17 @@ enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) jd_s_channel_open(mud_
          break;            
       case HPMUD_IPP_CHANNEL:
          port = 80;
-         pin.sin_port = htons(port);
-         if ((pc->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {
-            BUG("unable to open ipp port %d: %m %s\n", port, pd->uri);
+            BUG("unable to connect to 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)
+            if ((pc->socket = try_connect(pd->ip, port)) == -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) 
+         if ((pc->socket = try_connect(pd->ip, port)) == -1)
          {  
             BUG("unable to connect to marvell-fax port %d: %m %s\n", port, pd->uri);  
             goto bugout;  
-- 
2.14.2

Reply via email to