]> arthur.barton.de Git - netdata.git/blobdiff - src/socket.c
added custom log format to web_log.conf #1792 #1793
[netdata.git] / src / socket.c
index 6d60be772e506e1f59bb8e729a6769f6cb78c7e8..643811e446786a834fdb7ba49f0762433ff5fbd9 100644 (file)
@@ -7,7 +7,7 @@
 //    [PROTOCOL:]IP[%INTERFACE][:PORT]
 //
 // PROTOCOL  = tcp or udp
-// IP        = IPv4 or IPv6 IP or hostname, optionaly enclosed in [] (required for IPv6)
+// IP        = IPv4 or IPv6 IP or hostname, optionally enclosed in [] (required for IPv6)
 // INTERFACE = for IPv6 only, the network interface to use
 // PORT      = port number or service name
 
@@ -22,16 +22,18 @@ int connect_to(const char *definition, int default_port, struct timeval *timeout
     snprintfz(default_service, 10, "%d", default_port);
 
     char *host = buffer, *service = default_service, *interface = "";
-    int protocol = 0;
+    int protocol = IPPROTO_TCP, socktype = SOCK_STREAM;
     uint32_t scope_id = 0;
 
     if(strncmp(host, "tcp:", 4) == 0) {
         host += 4;
         protocol = IPPROTO_TCP;
+        socktype = SOCK_STREAM;
     }
     else if(strncmp(host, "udp:", 4) == 0) {
         host += 4;
         protocol = IPPROTO_UDP;
+        socktype = SOCK_DGRAM;
     }
 
     char *e = host;
@@ -76,14 +78,14 @@ int connect_to(const char *definition, int default_port, struct timeval *timeout
     if(!*service)
         service = default_service;
 
-    memset(&hints, 0, sizeof(struct addrinfo));
+    memset(&hints, 0, sizeof(hints));
     hints.ai_family   = PF_UNSPEC;   /* Allow IPv4 or IPv6 */
-    hints.ai_socktype = SOCK_DGRAM;  /* Datagram socket */
-    hints.ai_protocol = protocol;    /* The required protocol */
+    hints.ai_socktype = socktype;
+    hints.ai_protocol = protocol;
 
     int ai_err = getaddrinfo(host, service, &hints, &ai_head);
     if (ai_err != 0) {
-        error("Cannot resolve host '%s', port '%s': %s\n", host, service, gai_strerror(ai_err));
+        error("Cannot resolve host '%s', port '%s': %s", host, service, gai_strerror(ai_err));
         return -1;
     }