/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2007
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    Midnight Code is a registered trademark of Ian Latter.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* cast.c :: Midnight Code cast peer program				*/


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>


#include <libmidnightcode.h>


int
main(int argc, char **argv) {
  struct in_addr cfg_self;
  struct in_addr cfg_seed;
  int cfg_help = 0;
  char self_str[21];
  char seed_str[21];
  struct midnightcode_interface_element_t * now_interface;
  struct midnightcode_socket_head_t * sockets;
  struct midnightcode_cast_head_t * cast;
  struct midnightcode_cast_element_t * cast_entry;
  char * buffer;
  int buffer_size;
  void * tmp_void_p;
  int ret_i;

 
  // Prepare switches
  struct midnightcode_switch_t switches[] = {
    { "m", "mine", "My IP address",
      MIDNIGHTCODE_PARSE_TYPE_HOST, 0, 0, &cfg_self.s_addr },
    { "s", "seed", "Seed IP address",
      MIDNIGHTCODE_PARSE_TYPE_HOST, 0, 0, &cfg_seed.s_addr },
    { "h", "help", "Display this help/usage screen",
      MIDNIGHTCODE_PARSE_TYPE_NONE, 0, 0, &cfg_help },
    { 0 }
  };



  // Set-up initial values;
  memset(&self_str, 0, 21);
  memset(&seed_str, 0, 21);
  memset(&cfg_self, 0, sizeof(struct in_addr));
  memset(&cfg_seed, 0, sizeof(struct in_addr));



  // Process switches
  if((ret_i = midnightcode_switch_process(argc, argv, switches)) != 0) {
    fprintf(stdout, "\n");
    midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
    midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
      MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    fprintf(stdout, "    Error in switches, switch \"%s\" is unkown"
      " at parameter number %d.\n\n", argv[ret_i], ret_i);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT, argv[0],
      switches);
    exit(1);
  }
  if(cfg_help) {
    midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
    midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
      MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT, argv[0],
      switches);
    exit(1);
  }
  fflush(stdout);



  // Process license and logo
  midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
  midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
    MIDNIGHTCODE_LICENSE_TEXT_ALL);



  // Program title
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_H1,
    MIDNIGHTCODE_STATUS_STYLE_NORMAL, "Route tool example: %s", argv[0]);

  fprintf(stdout, "\n");
  fflush(stdout);



  // Validate user params
  snprintf(self_str, 21, "%s", inet_ntoa(cfg_self));
  snprintf(seed_str, 21, "%s", inet_ntoa(cfg_seed));

  if(cfg_self.s_addr == 0) {
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
      MIDNIGHTCODE_STATUS_STYLE_NORMAL,
      "Unspecified IP, finding default");
    if(midnightcode_init_nic_default(&now_interface) < 0) {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
      exit(1);
    } else {
      snprintf(self_str, 20, "%s", now_interface->ipv4_address_s);
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
    }
  } else {
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
      MIDNIGHTCODE_STATUS_STYLE_NORMAL,
      "IP address specified");
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_BLURB,
                           MIDNIGHTCODE_STATUS_STYLE_UNKNOWN,
    "Using IP address %s", self_str);
  fprintf(stdout, "\n");



  // Socket Initialisation
  midnightcode_list_init(socket_struct_head, midnightcode_socket_element_t);
  tmp_void_p = (void *)&socket_struct_head;
  sockets = tmp_void_p;

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Initialising socket library");
  if(midnightcode_socket_initialise(sockets) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }
  sleep(1);



  // Cast Initialisation
  midnightcode_list_init(cast_struct_head, midnightcode_cast_element_t);
  tmp_void_p = (void *)&cast_struct_head;
  cast = tmp_void_p;

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Initialising cast library");
  if((ret_i = midnightcode_cast_initialise(cast,
    MIDNIGHTCODE_CAST_OPTION_UNICAST,
    self_str, MIDNIGHTCODE_CAST_PORT)) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(ret_i);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }


  // Cast Seed
  if(cfg_seed.s_addr != 0) {
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
      MIDNIGHTCODE_STATUS_STYLE_NORMAL,
      "Seed IP specified, seeding cast agent");
    if(0) {
      buffer = NULL;
      buffer_size = 0;
    } else {
      buffer_size = 32;
      buffer = malloc(buffer_size);
      memset(buffer, 0, buffer_size);
    }
    if(midnightcode_cast_seed(cast, seed_str, MIDNIGHTCODE_CAST_PORT,
      buffer, buffer_size) < 0) {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
      exit(1);
    } else {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
    }
  }



  // Cast Write
  if(0) {
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
      MIDNIGHTCODE_STATUS_STYLE_NORMAL,
      "Write message to cast agent");
    if(0) {
      buffer = NULL;
      buffer_size = 0;
    } else {
      buffer_size = 32;
      buffer = malloc(buffer_size);
      memset(buffer, 0, buffer_size);
    }
    if(midnightcode_cast_write(cast, buffer, buffer_size) < 0) {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
      exit(1);
    } else {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
    }
  }


  // Walk Test
  midnightcode_list_for_each(cast_entry, cast) {
    fprintf(stdout, "Cast entry: [%s:%d] @[%d]\n",
      cast_entry->ipv4_address_s, cast_entry->port, cast_entry->timestamp);
  }



  // Sleep it off
  sleep(120);



  // Walk Test
  midnightcode_list_for_each(cast_entry, cast) {
    fprintf(stdout, "Cast entry: [%s:%d] @[%d]\n",
      cast_entry->ipv4_address_s, cast_entry->port, cast_entry->timestamp);
  }



  // Cast shutdown
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Shutdown cast library");
  if(midnightcode_cast_shutdown(cast) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  // Socket shutdown
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Shutdown socket library");
  if(midnightcode_socket_shutdown(sockets) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }


  fprintf(stdout, "\n\n");


  exit(0);
}
