<commit_before>
/*
 * The olsr.org Optimized Link-State Routing daemon(olsrd)
 * Copyright (c) 2005, <NAME>(<EMAIL>)
 * All rights reserved.
 *
 * 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 copyright 
 *   notice, this list of conditions and the following disclaimer in 
 *   the documentation and/or other materials provided with the 
 *   distribution.
 * * Neither the name of olsr.org, olsrd nor the names of its 
 *   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.
 *
 * Visit http://www.olsr.org for more information.
 *
 * If you find this software useful feel free to make a donation
 * to the project. For more information see the website or contact
 * the copyright holders.
 *
 * $Id: main.c,v 1.14 2005/06/03 06:12:23 kattemat Exp $
 */

/* olsrd host-switch daemon */

#include "olsr_host_switch.h"
#include "link_rules.h"
#include "ohs_cmd.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#ifdef WIN32
#undef EINTR
#define EINTR WSAEINTR
#undef errno
#define errno WSAGetLastError()
#undef strerror
#define strerror(x) StrError(x)
#endif

static int srv_socket;

#define OHS_BUFSIZE 1500
static olsr_u8_t data_buffer[OHS_BUFSIZE];

struct ohs_connection *ohs_conns;

static int ip_version;
int ipsize;
static char ipv6_buf[100]; /* for address coversion */

olsr_u32_t logbits;

/* local functions */
static int
ohs_init_new_connection(int);

inline static int
ohs_route_data(struct ohs_connection *);

static int
ohs_init_connect_sockets(void);

static int
ohs_configure(void);

static void
ohs_listen_loop(void);

static int
ohs_delete_connection(struct ohs_connection *);




char *
olsr_ip_to_string(union olsr_ip_addr *addr)
{
  static int index = 0;
  static char buff[4][100];
  char *ret;
  struct in_addr in;
  
  if(ip_version == AF_INET)
    {
      in.s_addr=addr->v4;
      ret = inet_ntoa(in);
    }
  else
    {
      /* IPv6 */
      ret = (char *)inet_ntop(AF_INET6, &addr->v6, ipv6_buf, sizeof(ipv6_buf));
    }

  strncpy(buff[index], ret, 100);

  ret = buff[index];

  index = (index + 1) & 3;

  return ret;
}


#ifdef WIN32
int __stdcall
ohs_close(unsigned long signal)
#else
void
ohs_close(int signal)
#endif
{
  printf("OHS: exit\n");

  close(srv_socket);

  exit(0);
}

struct ohs_connection *
get_client_by_addr(union olsr_ip_addr *adr)
{
  struct ohs_connection *oc = ohs_conns;

  while(oc)
    {
      if(COMP_IP(adr, &oc->ip_addr))
        return oc;
      oc = oc->next;
    }
  return NULL;
}


static int
ohs_init_new_connection(int s)
{
  struct ohs_connection *oc;
  olsr_u8_t new_addr[4];

  if(logbits & LOG_CONNECT)
    printf("ohs_init_new_connection\n");

  /* Create new client node */
  oc = malloc(sizeof(struct ohs_connection));
  if(!oc)
    OHS_OUT_OF_MEMORY("New connection");

  memset(oc, 0, sizeof(oc));

  oc->socket = s;

  oc->links = NULL;
  oc->rx = 0;
  oc->tx = 0;
  oc->linkcnt = 0;

  /* Queue */
  oc->next = ohs_conns;
  ohs_conns = oc;

  /* Get "fake IP" */
  if(recv(oc->socket, new_addr, 4, 0) != 4)
    {
      printf("Failed to fetch IP address!\n");
      return -1;
    }
  memcpy(&oc->ip_addr, new_addr, 4);
  oc->ip_addr.v4 = ntohl(oc->ip_addr.v4);
  if(logbits & LOG_CONNECT)
    printf("IP: %s\n", olsr_ip_to_string(&oc->ip_addr));

  return 1;
}

static int
ohs_delete_connection(struct ohs_connection *oc)<commit_msg>Duplicate IP check added on connection<commit_after>
/*
 * The olsr.org Optimized Link-State Routing daemon(olsrd)
 * Copyright (c) 2005, <NAME>(<EMAIL>)
 * All rights reserved.
 *
 * 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 copyright 
 *   notice, this list of conditions and the following disclaimer in 
 *   the documentation and/or other materials provided with the 
 *   distribution.
 * * Neither the name of olsr.org, olsrd nor the names of its 
 *   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.
 *
 * Visit http://www.olsr.org for more information.
 *
 * If you find this software useful feel free to make a donation
 * to the project. For more information see the website or contact
 * the copyright holders.
 *
 * $Id: main.c,v 1.15 2005/06/03 16:36:20 kattemat Exp $
 */

/* olsrd host-switch daemon */

#include "olsr_host_switch.h"
#include "link_rules.h"
#include "ohs_cmd.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#ifdef WIN32
#undef EINTR
#define EINTR WSAEINTR
#undef errno
#define errno WSAGetLastError()
#undef strerror
#define strerror(x) StrError(x)
#endif

static int srv_socket;

#define OHS_BUFSIZE 1500
static olsr_u8_t data_buffer[OHS_BUFSIZE];

struct ohs_connection *ohs_conns;

static int ip_version;
int ipsize;
static char ipv6_buf[100]; /* for address coversion */

olsr_u32_t logbits;

/* local functions */
static int
ohs_init_new_connection(int);

inline static int
ohs_route_data(struct ohs_connection *);

static int
ohs_init_connect_sockets(void);

static int
ohs_configure(void);

static void
ohs_listen_loop(void);

static int
ohs_delete_connection(struct ohs_connection *);




char *
olsr_ip_to_string(union olsr_ip_addr *addr)
{
  static int index = 0;
  static char buff[4][100];
  char *ret;
  struct in_addr in;
  
  if(ip_version == AF_INET)
    {
      in.s_addr=addr->v4;
      ret = inet_ntoa(in);
    }
  else
    {
      /* IPv6 */
      ret = (char *)inet_ntop(AF_INET6, &addr->v6, ipv6_buf, sizeof(ipv6_buf));
    }

  strncpy(buff[index], ret, 100);

  ret = buff[index];

  index = (index + 1) & 3;

  return ret;
}


#ifdef WIN32
int __stdcall
ohs_close(unsigned long signal)
#else
void
ohs_close(int signal)
#endif
{
  printf("OHS: exit\n");

  close(srv_socket);

  exit(0);
}

struct ohs_connection *
get_client_by_addr(union olsr_ip_addr *adr)
{
  struct ohs_connection *oc = ohs_conns;

  while(oc)
    {
      if(COMP_IP(adr, &oc->ip_addr))
        return oc;
      oc = oc->next;
    }
  return NULL;
}


static int
ohs_init_new_connection(int s)
{
  struct ohs_connection *oc;
  olsr_u8_t new_addr[4];

  if(logbits & LOG_CONNECT)
    printf("ohs_init_new_connection\n");

  /* Create new client node */
  oc = malloc(sizeof(struct ohs_connection));
  if(!oc)
    OHS_OUT_OF_MEMORY("New connection");

  memset(oc, 0, sizeof(oc));

  oc->socket = s;

  oc->links = NULL;
  oc->rx = 0;
  oc->tx = 0;
  oc->linkcnt = 0;

  /* Get "fake IP" */
  if(recv(oc->socket, new_addr, 4, 0) != 4)
    {
      printf("Failed to fetch IP address!\n");
      return -1;
    }
  memcpy(&oc->ip_addr, new_addr, 4);
  oc->ip_addr.v4 = ntohl(oc->ip_addr.v4);
  if(logbits & LOG_CONNECT)
    printf("IP: %s\n", olsr_ip_to_string(&oc->ip_addr));

  if(get_client_by_addr(&oc->ip_addr))
    {
      if(logbits & LOG_CONNECT)
	printf("IP: %s DUPLICATE! Disconecting client!\n", olsr_ip_to_string(&oc->ip_addr));

      close(s);
      return -1;
    }

  /* Queue */
  oc->next = ohs_conns;
  ohs_conns = oc;

  return 1;
}

static int
ohs_delete_connection(struct ohs_connection *oc)
{'fraction_non_alphanumeric': 0.1066486317760899, 'fraction_numerical': 0.014046405160753303, 'mean_word_length': 3.810810810810811, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1643813', 'n_tokens_mistral': 3545, 'n_tokens_neox': 3264, 'n_words': 1030}