/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: test-sock.ctr
*/

/*
Copyright (C) 2012-2013, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
*/

/**	@file test-sock.c The test-sock module.
*/


#line 10 "test-sock.ctr"

#define	TEST_PEERS		1

#define	TEST_HTTP_CLIENT	1

#define	TEST_PEER_COMPARE	1



#include "dk3all.h"
#include "dk3sock.h"





#line 24 "test-sock.ctr"

#define	USE_DNS_NAME	0

static dkChar const gn[] = { dkT("dkt3-test") } ;



static char const rq[] = {
"GET / HTTP/1.0\n\n"
};


static char const hn[] = {
#if USE_DNS_NAME
"studium.e-technik.fh-schmalkalden.de"
#else
"194.94.29.30"
#endif
};




static char const * const remote_addresses[] = {
/* 0 */
"127.0.0.1",

/* 1 */
"127.0.0.1/255.0.0.0",

/* 2 */
"127.0.0.1/8",

/* 3 */
"127.0.0.1/32",

/* 4 */
"194.94.29.100",

/* 5 */
"194.94.29.100/255.0.0.0",

/* 6 */
"194.94.29.100/8",

/* 7 */
"194.94.29.100/255.255.255.255",

/* 8 */
"194.94.29.100/32",

/* 9 */
"fs.e-technik.fh-schmalkalden.de",

/* 10 */
"fs.e-technik.fh-schmalkalden.de/24",

/* 11 */
"fs.e-technik.fh-schmalkalden.de/255.255.255.0",

/* 12 */
"fs.e-technik.fh-schmalkalden.de/255.255.255.255",

/* 13 */
"::1",

/* 14 */
"::1/128",

/* 15 */
"::1/64",

/* 16 */
"::1/::1",

NULL


#line 67 "test-sock.ctr"
};



static
void
show_allowed_peer(dk3_peer_allowed_t *pp)
{
  unsigned long		ip4;
  unsigned long		ma4;
#if DK3_HAVE_STRUCT_SOCKADDR_IN6
  int			 i;
  IN6_ADDR		*ip6;
  IN6_ADDR		*ma6;
#endif
  if(pp->wh) {
    printf("IPv6 ");
#if DK3_HAVE_STRUCT_SOCKADDR_IN6
    ip6 = &((pp->d).ip6.ad);
    for(i = 0; i < 16; i++) {
      if(i) { if(!(i % 2)) fputc(':', stdout); }
      printf("%02x", (int)(ip6->s6_addr[i]));
    }
    fputc('/', stdout);
    ip6 = &((pp->d).ip6.ma);
    for(i = 0; i < 16; i++) {
      if(i) { if(!(i% 2)) fputc(':', stdout); }
      printf("%02x", (int)(ip6->s6_addr[i]));
    }
#endif
    printf("\n");
  } else {
    ip4 = ntohl((pp->d).ip4.ad.s_addr);
    ma4 = ntohl((pp->d).ip4.ma.s_addr);
    printf(
      "IPv4 %lu.%lu.%lu.%lu/%lu.%lu.%lu.%lu\n",
      ((ip4 >> 24) & 0x000000FFUL),
      ((ip4 >> 16) & 0x000000FFUL),
      ((ip4 >>  8) & 0x000000FFUL),
      ((ip4      ) & 0x000000FFUL),
      ((ma4 >> 24) & 0x000000FFUL),
      ((ma4 >> 16) & 0x000000FFUL),
      ((ma4 >>  8) & 0x000000FFUL),
      ((ma4      ) & 0x000000FFUL)
    );
  }
}


DK3_MAIN
{
  char			 bu[4096];
  dk3_app_t		*app;
  dk3_socket_t		 sfd;
  int			 rdbytes;
  

#line 123 "test-sock.ctr"
  

#line 124 "test-sock.ctr"
  app = dk3app_open_command(argc, (dkChar const * const *)argv, gn);
  if(app) {							

#line 126 "test-sock.ctr"

#if TEST_HTTP_CLIENT
    {
      sfd = dk3socket_open_net_stream_client(hn, 80, 9876, 10L, 0L, NULL, app);
      if(INVALID_SOCKET != sfd) {					

#line 131 "test-sock.ctr"
        if(dk3socket_send(sfd, rq, strlen(rq), 10L, 0L, NULL, app)) { 

#line 132 "test-sock.ctr"
          if(dk3socket_shutdown(sfd, DK3_TCPIP_SHUTDOWN_WRITE, NULL, app)) { 

#line 133 "test-sock.ctr"
	    do {
	      rdbytes = dk3socket_recv(sfd, bu, sizeof(bu), 10L, 0L, NULL, app);
	      if(rdbytes > 0) {			

#line 136 "test-sock.ctr"
	        fwrite(bu, 1, (size_t)rdbytes, stdout);
	      }
	    } while(rdbytes > 0);
	    fputc('\n', stdout);
	    fflush(stdout);
	  } else {						

#line 142 "test-sock.ctr"
	  }
        } else {						

#line 144 "test-sock.ctr"
        }
        dk3socket_close(sfd, NULL, app);
      } else {							

#line 147 "test-sock.ctr"
      }
    }
#endif

#if TEST_PEERS
    {
      char const * const *raptr;
      dk3_peer_allowed_t	apeer;
      raptr = remote_addresses;
      while(*raptr) {
        fputs(*raptr, stdout); fputc('\n', stdout);
        if(dk3socket_set_peer(&apeer, *raptr, NULL, app)) {	

#line 159 "test-sock.ctr"
          show_allowed_peer(&apeer);
        } else {						

#line 161 "test-sock.ctr"
        }
        fputc('\n', stdout);
        raptr++;
      }
    }
#endif

#if TEST_PEER_COMPARE
    {
      dk3_peer_allowed_t	 apeer;
      dk3_peer_allowed_t	*pp;
      char const * const	*raptr;
      dk3_sto_t			*s_peers;
      dk3_sto_it_t		*i_peers;

      s_peers = dk3sto_open_app(app);
      if(s_peers) {
        dk3sto_set_comp(s_peers, dk3socket_compare_peer, 0);
        i_peers = dk3sto_it_open(s_peers);
	if(i_peers) {
	  raptr = remote_addresses;
	  while(*raptr) {
	    if(dk3socket_set_peer(&apeer, *raptr, NULL, app)) {
	      pp = dk3_new_app(dk3_peer_allowed_t,1,app);
	      if(pp) {
	        dk3mem_cpy(
		  (void *)pp,
		  (void *)(&apeer),
		  sizeof(dk3_peer_allowed_t)
		);
		if(!dk3sto_add(s_peers, (void *)pp)) {
		  dk3_delete(pp)
		}
	      }
	    }
	    raptr++;
	  }
	  dk3sto_it_reset(i_peers);
	  while(NULL != (pp = (dk3_peer_allowed_t *)dk3sto_it_next(i_peers))) {
	    show_allowed_peer(pp);
	    dk3_delete(pp)
	  }
	  dk3sto_it_close(i_peers);
	}
      }
    }
#endif
    dk3app_close(app);
  } else {							

#line 210 "test-sock.ctr"
    /* ##### ERROR: Memory */
  }
  

#line 213 "test-sock.ctr"
  

#line 214 "test-sock.ctr"
  exit(0); return 0;
}


