From 38da5552e8fe19a631f907533a1da21931d9c137 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sat, 2 Feb 2019 14:20:26 -0700
Subject: [PATCH] getaddrinfo: support for setting port from service argument
 (numeric only)

---
 src/header/netdb/mod.rs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/header/netdb/mod.rs b/src/header/netdb/mod.rs
index 8ad831ff..7c41cca2 100644
--- a/src/header/netdb/mod.rs
+++ b/src/header/netdb/mod.rs
@@ -686,8 +686,18 @@ pub unsafe extern "C" fn getaddrinfo(
 
     *res = ptr::null_mut();
 
+    let mut port = 0;
+    if let Some(service) = service_opt {
+        //TODO: Support other service definitions as well as AI_NUMERICSERV
+        match str::from_utf8_unchecked(service.to_bytes()).parse::<u16>() {
+            Ok(ok) => port = ok,
+            Err(_err) => ()
+        }
+    }
+
     //TODO: Check hosts file
     if let Some(node) = node_opt {
+        //TODO: Support AI_NUMERICHOST
         let lookuphost = match lookup_host(str::from_utf8_unchecked(node.to_bytes())) {
             Ok(lookuphost) => lookuphost,
             Err(e) => {
@@ -703,7 +713,7 @@ pub unsafe extern "C" fn getaddrinfo(
 
             let ai_addr = Box::into_raw(Box::new(sockaddr_in {
                 sin_family: AF_INET as sa_family_t,
-                sin_port: 0,
+                sin_port: htons(port),
                 sin_addr: in_addr,
                 sin_zero: [0; 8]
             })) as *mut sockaddr;
-- 
GitLab