/*-
 * Copyright (c) 2004, 2005 Benedikt Stockebrand
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */


/* Code extracted from manuscript---change the document source files please. */


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>

#include <stdio.h>

#include "chkerror.h"

void printsocketinfo(struct addrinfo * i);

int main(int argc, char ** argv)
{
     int               status;
     struct addrinfo   hints;
     struct addrinfo * srv_addrinfo;
     struct addrinfo * i;
     int               sockd;

     chk(argc==2 || argc==3, "Need one or two arguments.");

     setvbuf(stdout, NULL, _IONBF, 0);

     bzero(&hints, sizeof(hints));
     hints.ai_flags     = 0;
     hints.ai_family    = AF_UNSPEC;
     hints.ai_socktype  = SOCK_STREAM;
     hints.ai_protocol  = 0;

     status = getaddrinfo(argv[1], argc==3 ? argv[2] : "6666",
                          &hints, &srv_addrinfo);
     if ( status )
     {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
          exit(EXIT_FAILURE);
     }

     for(i = srv_addrinfo; i; i = i->ai_next)
     {


          printsocketinfo(i);

          sockd = socket (i->ai_family, i->ai_socktype, i->ai_protocol);
          chkerrno(sockd >= 0, "socket()");
          status = connect(sockd, i->ai_addr, i->ai_addrlen);
          if (status != 0)
          {
               perror("connect");
               status = close(sockd);
               chkerrno(status == 0, "close()");
          }
          else
          {
               char buf;
               write (STDOUT_FILENO, "ok\n", 3);
               while (read(sockd, &buf, 1) > 0) write(STDOUT_FILENO,&buf,1);
               status = close(sockd);
               chkerrno(status == 0, "close()");
               break;
          }
     }

     freeaddrinfo (srv_addrinfo);
     if (i) return 0;
     fprintf(stderr, "Couldn't establish connection\n");
     return 1;
}

void printsocketinfo(struct addrinfo * i)
{
     char host[NI_MAXHOST];
     char srvc[NI_MAXSERV];
     int  status;

     status = getnameinfo(i->ai_addr, i->ai_addrlen,
                          host, NI_MAXHOST, srvc, NI_MAXSERV,
                          NI_NUMERICHOST | NI_NUMERICSERV);
     if (status)
     {
          fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
          exit(EXIT_FAILURE);
     }
     printf("Trying [%s]:%s ... ", host, srvc);
}

