From owner-freebsd-questions@FreeBSD.ORG Thu Jul 12 17:53:36 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 146C71065700 for ; Thu, 12 Jul 2012 17:53:36 +0000 (UTC) (envelope-from guru@unixarea.de) Received: from ms16-1.1blu.de (ms16-1.1blu.de [89.202.0.34]) by mx1.freebsd.org (Postfix) with ESMTP id 9EBF88FC20 for ; Thu, 12 Jul 2012 17:53:35 +0000 (UTC) Received: from [89.204.137.251] (helo=tiny.Sisis.de) by ms16-1.1blu.de with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1SpKJX-0005Fe-IW for freebsd-questions@freebsd.org; Thu, 12 Jul 2012 16:24:20 +0200 Received: from tiny.Sisis.de (localhost [127.0.0.1]) by tiny.Sisis.de (8.14.5/8.14.3) with ESMTP id q6CEOHMh001647 for ; Thu, 12 Jul 2012 16:24:17 +0200 (CEST) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by tiny.Sisis.de (8.14.5/8.14.3/Submit) id q6CEOG8Z001646 for freebsd-questions@freebsd.org; Thu, 12 Jul 2012 16:24:16 +0200 (CEST) (envelope-from guru@unixarea.de) X-Authentication-Warning: tiny.Sisis.de: guru set sender to guru@unixarea.de using -f Date: Thu, 12 Jul 2012 16:24:16 +0200 From: Matthias Apitz To: freebsd-questions@freebsd.org Message-ID: <20120712142415.GA1624@tiny.Sisis.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Operating-System: FreeBSD 10.0-CURRENT r226986 (i386) User-Agent: Mutt/1.5.21 (2010-09-15) X-Con-Id: 51246 X-Con-U: 0-guru X-Originating-IP: 89.204.137.251 Subject: IPv6 && getaddrinfo(3C) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Matthias Apitz List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Jul 2012 17:53:36 -0000 Hello, I'm playing around with IPv6 code on a FreeBSD 9 system and can't get getaddrinfo(3C) to do what it should do as stated in its man page: accept an IPv6 and IPv4 IP addr, it only works with the IPv6 form: $ ./a.out ::1 host: ::1 read: SSH-2.0-OpenSSH_5.6p1 FreeBSD-20101111 $ ./a.out 127.0.0.1 host: 127.0.0.1 ssh: getaddrinfo failed code 8: hostname nor servname provided, or not known $ telnet 127.0.0.1 22 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. SSH-2.0-OpenSSH_5.6p1 FreeBSD-20101111 the used C-code is attached below; what I'm doing wrong in the code? Thanks matthias /* IPv6 client code using getaddrinfo */ #include #include #include #include #include #include #include main(argc, argv) /* client side */ int argc; char *argv[]; { struct addrinfo req, *ans; int code, s, n; char buf[1024]; memset(&req, 0, sizeof(req)); req.ai_flags = AI_ADDRCONFIG|AI_NUMERICHOST; req.ai_family = AF_INET6; /* Same as AF_INET6. */ req.ai_socktype = SOCK_STREAM; /* */ /* Use default protocol (in this case tcp) */ /* */ req.ai_protocol = 0; printf("host: %s\n", argv[1]); if ((code = getaddrinfo(argv[1], "ssh", &req, &ans)) != 0) { fprintf(stderr, "ssh: getaddrinfo failed code %d: %s\n", code, gai_strerror(code)); exit(1); } /* */ /* ans must contain at least one addrinfo, use */ /* the first. */ /* */ s = socket(ans->ai_family, ans->ai_socktype, ans->ai_protocol); if (s < 0) { perror("ssh: socket"); exit(3); } /* Connect does the bind for us */ if (connect(s, ans->ai_addr, ans->ai_addrlen) < 0) { perror("ssh: connect"); exit(5); } n = read(s, buf, 1024); printf ("read: %s", buf); /* */ /* Free answers after use */ /* */ freeaddrinfo(ans); exit(0); } -- Matthias Apitz e - w http://www.unixarea.de/ UNIX since V7 on PDP-11, UNIX on mainframe since ESER 1055 (IBM /370) UNIX on x86 since SVR4.2 UnixWare 2.1.2, FreeBSD since 2.2.5