static struct ospf6 *
ospf6_create (void)
{
  struct ospf6 *o;

  o = XCALLOC (MTYPE_OSPF6_TOP, sizeof (struct ospf6));

  /* initialize */
  quagga_gettime (QUAGGA_CLK_MONOTONIC, &o->starttime);
  o->area_list = list_new ();
  o->area_list->cmp = ospf6_area_cmp;
  o->lsdb = ospf6_lsdb_create (o);
  o->lsdb_self = ospf6_lsdb_create (o);
  o->lsdb->hook_add = ospf6_top_lsdb_hook_add;
  o->lsdb->hook_remove = ospf6_top_lsdb_hook_remove;

  o->route_table = OSPF6_ROUTE_TABLE_CREATE (GLOBAL, ROUTES);
  o->route_table->scope = o;
  o->route_table->hook_add = ospf6_top_route_hook_add;
  o->route_table->hook_remove = ospf6_top_route_hook_remove;

  o->brouter_table = OSPF6_ROUTE_TABLE_CREATE (GLOBAL, BORDER_ROUTERS);
  o->brouter_table->scope = o;
  o->brouter_table->hook_add = ospf6_top_brouter_hook_add;
  o->brouter_table->hook_remove = ospf6_top_brouter_hook_remove;

  o->external_table = OSPF6_ROUTE_TABLE_CREATE (GLOBAL, EXTERNAL_ROUTES);
  o->external_table->scope = o;

  o->external_id_table = route_table_init ();

  return o;
}
Exemple #2
0
/* After TCP connection is established.  Get remote address and port. */
union sockunion *
sockunion_getpeername (int fd)
{
  int ret;
  socklen_t len;
  union
  {
    struct sockaddr sa;
    struct sockaddr_in sin;
    char tmp_buffer[128];
  } name;
  union sockunion *su;

  memset (&name, 0, sizeof name);
  len = sizeof name;
  ret = getpeername (fd, (struct sockaddr *)&name, &len);
  if (ret < 0)
    {
      mlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
	    safe_strerror (errno));
      return NULL;
    }

  if (name.sa.sa_family == AF_INET)
    {
      su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
      memcpy (su, &name, sizeof (struct sockaddr_in));
      return su;
    }
  return NULL;
}
Exemple #3
0
struct isis_circuit *
isis_circuit_new ()
{
  struct isis_circuit *circuit;
  int i;

  circuit = XCALLOC (MTYPE_ISIS_CIRCUIT, sizeof (struct isis_circuit));
  if (circuit == NULL)
    {
      zlog_err ("Can't malloc isis circuit");
      return NULL;
    }

  /*
   * Default values
   */
  circuit->is_type = IS_LEVEL_1_AND_2;
  circuit->flags = 0;
  circuit->pad_hellos = 1;
  for (i = 0; i < 2; i++)
    {
      circuit->hello_interval[i] = DEFAULT_HELLO_INTERVAL;
      circuit->hello_multiplier[i] = DEFAULT_HELLO_MULTIPLIER;
      circuit->csnp_interval[i] = DEFAULT_CSNP_INTERVAL;
      circuit->psnp_interval[i] = DEFAULT_PSNP_INTERVAL;
      circuit->priority[i] = DEFAULT_PRIORITY;
      circuit->metric[i] = DEFAULT_CIRCUIT_METRIC;
      circuit->te_metric[i] = DEFAULT_CIRCUIT_METRIC;
    }

  circuit->mtc = mpls_te_circuit_new();

  return circuit;
}
Exemple #4
0
struct config *
config_new ()
{
  struct config *config;
  config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config));
  return config;
}
Exemple #5
0
int go() {
    int i;
    int *deck = XCALLOC(cards * sizeof(int));
    int ret;
    pixel *image;
    /* init deck */
    for (i = 0; i < cards; i++) {
        deck[i] = i * 360 / cards;
    }
    /* shuffle */
    for (i = 0; i < reps; i++) {
        switch (mode) {
        case PILE:
            shuffle_pile  (deck);
            break;
        case RIFFLE:
            shuffle_riffle(deck);
            break;
        case RANDOM:
            shuffle_random(deck);
            break;
        default:
            assert(0);
        }
    }
    image = flatten(deck);
    free(deck);
    ret = writeFile(file, ROWS(), COLS(), image);
    free(image);
    return ret;
}
Exemple #6
0
int
bfd_ioctl_peer_list (struct vty *vty)
{
	int ret;
	int num;
	int i;
	struct bfd_nl_peerinfo *peer, *org;

	if(bfd_ioctl_sock < 0)
		return -1;

	ret = ioctl(bfd_ioctl_sock, BFD_GETPEER_NUM, &num);
	if(ret == -1)
		zlog_warn("ioctl(GET_NUM)");

	peer = XCALLOC(MTYPE_BFD_PEER, num * sizeof(struct bfd_nl_peerinfo));
	if(!peer)
		zlog_warn("malloc");

	org = peer;

	ret = ioctl(bfd_ioctl_sock, BFD_GETPEER, peer);
	if(ret == -1)
		zlog_warn("ioctl(GET_PEER)");

	for(i=0; i<num; i++){
		bfd_ioctl_peer_info(peer, vty);
		peer++;
	}
	XFREE(MTYPE_BFD_PEER, org);

	return 0;
}
Exemple #7
0
union sockunion *
sockunion_str2su (const char *str)
{
  int ret;
  union sockunion *su;

  su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));

  ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
  if (ret > 0)			/* Valid IPv4 address format. */
    {
      su->sin.sin_family = AF_INET;
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
      su->sin.sin_len = sizeof(struct sockaddr_in);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
      return su;
    }
#ifdef HAVE_IPV6
  ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
  if (ret > 0)			/* Valid IPv6 address format. */
    {
      su->sin6.sin6_family = AF_INET6;
#ifdef SIN6_LEN
      su->sin6.sin6_len = sizeof(struct sockaddr_in6);
#endif /* SIN6_LEN */
      return su;
    }
#endif /* HAVE_IPV6 */

  XFREE (MTYPE_SOCKUNION, su);
  return NULL;
}
static int
ospf6_interface_register_neighborcost (struct ospf6_interface *oi,
				       struct vty *vty)
{
  int err;
  struct ospf6_interface_neighborcost *inc;
  struct ospf6_interface_neighbor_metric_params nbrmetric_params = {
    .name = neighborcost_name,
    .delete = ospf6_interface_delete_neighborcost,
    .config_write = ospf6_interface_config_write_neighborcost,
    .cost_update = ospf6_interface_cost_update_neighborcost,
    .nbrops = {
      .create = ospf6_neighbor_create_neighborcost,
    },
  };

  inc = XCALLOC (MTYPE_OSPF6_IF, sizeof (*inc));
  inc->neighbor_cost_list = list_new ();
  inc->neighbor_cost_list->del = delete_neighbor_cost_entry;

  nbrmetric_params.data = inc;
  err =
    ospf6_interface_register_neighbor_metric (oi, &neighborcost_nbrmetric_id,
					      &nbrmetric_params, vty);
  if (err)
    {
      vty_out (vty, "could not register neighbor metric %s on interface %s%s",
	       nbrmetric_params.name, oi->interface->name, VNL);
      list_delete (inc->neighbor_cost_list);
      XFREE (MTYPE_OSPF6_IF, inc);
      return -1;
    }

  return 0;
}
/* Create new interface structure. */
struct interface *
if_create (const char *name, int namelen)
{
  struct interface *ifp;

  ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
  ifp->ifindex = IFINDEX_INTERNAL;
  
  assert (name);
  assert (namelen <= INTERFACE_NAMSIZ);	/* Need space for '\0' at end. */
  strncpy (ifp->name, name, namelen);
  ifp->name[namelen] = '\0';
  if (if_lookup_by_name(ifp->name) == NULL)
    listnode_add_sort (iflist, ifp);
  else
    zlog_err("if_create(%s): corruption detected -- interface with this "
	     "name exists already!", ifp->name);
  ifp->connected = list_new ();
  ifp->connected->del = (void (*) (void *)) connected_free;

  if (if_master.if_new_hook)
    (*if_master.if_new_hook) (ifp);

  return ifp;
}
Exemple #10
0
void
isis_dynhn_insert (u_char * id, struct hostname *hostname, int level)
{
  struct isis_dynhn *dyn;

  dyn = dynhn_find_by_id (id);
  if (dyn)
    {
      memcpy (&dyn->name, hostname, hostname->namelen + 1);
      memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
      dyn->refresh = time (NULL);
      return;
    }
  dyn = XCALLOC (MTYPE_ISIS_DYNHN, sizeof (struct isis_dynhn));
  if (!dyn)
    {
      zlog_warn ("isis_dynhn_insert(): out of memory!");
      return;
    }

  /* we also copy the length */
  memcpy (&dyn->name, hostname, hostname->namelen + 1);
  memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
  dyn->refresh = time (NULL);
  dyn->level = level;

  listnode_add (dyn_cache, dyn);

  return;
}
Exemple #11
0
static struct common_plugin_data *
init_func(void)
{
  struct rldb_mysql_state *state = 0;
  XCALLOC(state, 1);
  return (struct common_plugin_data*) state;
}
Exemple #12
0
static void
expand_runs(struct runlog_state *rls, int run_id)
{
  int new_a, i;
  struct run_entry *new_v;

  if (run_id < rls->run_u) return;
  if (run_id < rls->run_a) {
    rls->run_u = run_id + 1;
    return;
  }

  if (!(new_a = rls->run_a)) new_a = 128;
  while (run_id >= new_a) new_a *= 2;
  XCALLOC(new_v, new_a);
  for (i = 0; i < new_a; ++i) {
    new_v[i].run_id = i;
    new_v[i].status = RUN_EMPTY;
  }
  if (rls->run_u) memcpy(new_v, rls->runs, rls->run_u * sizeof(new_v[0]));
  xfree(rls->runs);
  rls->runs = new_v;
  rls->run_a = new_a;
  rls->run_u = run_id + 1;
}
static int
ospf6_interface_register_metricfunction (struct ospf6_interface *oi,
					 struct vty *vty)
{
  int err;
  struct ospf6_interface_metricfunction *imf;
  struct ospf6_interface_neighbor_metric_params nbrmetric_params = {
    .name = metricfunction_name,
    .delete = ospf6_interface_delete_metricfunction,
    .config_write = ospf6_interface_config_write_metricfunction,
    .nbrops = {
      .create = ospf6_neighbor_create_metricfunction,
    },
  };

  imf = XCALLOC (MTYPE_OSPF6_IF, sizeof (*imf));

  nbrmetric_params.data = imf;
  err =
    ospf6_interface_register_neighbor_metric (oi, &metricfunction_nbrmetric_id,
					      &nbrmetric_params, vty);
  if (err)
    {
      vty_out (vty, "could not register neighbor metric %s on interface %s%s",
	       nbrmetric_params.name, oi->interface->name, VNL);
      XFREE (MTYPE_OSPF6_IF, imf);
      return -1;
    }

  return 0;
}
/* set metric compilation. */
static void *
route_set_metric_compile (const char *arg)
{
  u_int32_t *metric;
  int32_t ret;

  /* OSPF doesn't support the +/- in
     set metric <+/-metric> check
     Ignore the +/- component */
  if (! all_digit (arg))
    {
      if ((strncmp (arg, "+", 1) == 0 || strncmp (arg, "-", 1) == 0) &&
	  all_digit (arg+1))
	{
	  zlog_warn ("OSPF does not support 'set metric +/-'");
	  arg++;
	}
      else
	return NULL;
    }

  metric = XCALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (u_int32_t));
  ret = atoi (arg);

  if (ret >= 0)
    {
      *metric = (u_int32_t)ret;
      return metric;
    }

  XFREE (MTYPE_ROUTE_MAP_COMPILED, metric);
  return NULL;
}
Exemple #15
0
/* Make stream buffer. */
struct stream *
stream_new (size_t size)
{
  struct stream *s;

  assert (size > 0);
  
  if (size == 0)
    {
      zlog_warn ("stream_new(): called with 0 size!");
      return NULL;
    }
  
  s = XCALLOC (MTYPE_STREAM, sizeof (struct stream));

  if (s == NULL)
    return s;
  
  if ( (s->data = XMALLOC (MTYPE_STREAM_DATA, size)) == NULL)
    {
      XFREE (MTYPE_STREAM, s);
      return NULL;
    }
  
  s->size = size;
  return s;
}
Exemple #16
0
/* Allocate new route node. */
static struct route_node *
route_node_new (void)
{
  struct route_node *node;
  node = XCALLOC (MTYPE_ROUTE_NODE, sizeof (struct route_node));
  return node;
}
Exemple #17
0
static struct keychain *keychain_new(void)
{
	struct keychain *keychain;
	keychain = XCALLOC(MTYPE_KEYCHAIN, sizeof(struct keychain));
	QOBJ_REG(keychain, keychain);
	return keychain;
}
Exemple #18
0
static struct isis_nexthop *
isis_nexthop_create (struct in_addr *ip, unsigned int ifindex)
{
  struct listnode *node;
  struct isis_nexthop *nexthop;

  for (ALL_LIST_ELEMENTS_RO (isis->nexthops, node, nexthop))
    {
      if (nexthop->ifindex != ifindex)
	continue;
      if (ip && memcmp (&nexthop->ip, ip, sizeof (struct in_addr)) != 0)
	continue;

      nexthop->lock++;
      return nexthop;
    }

  nexthop = XCALLOC (MTYPE_ISIS_NEXTHOP, sizeof (struct isis_nexthop));
  if (!nexthop)
    {
      zlog_err ("ISIS-Rte: isis_nexthop_create: out of memory!");
    }

  nexthop->ifindex = ifindex;
  memcpy (&nexthop->ip, ip, sizeof (struct in_addr));
  listnode_add (isis->nexthops, nexthop);
  nexthop->lock++;

  return nexthop;
}
/*
   Encode a SET type
   @param list      The list of items to encode
   @param inlen     The number of items in the list
   @param out       [out] The destination 
   @param outlen    [in/out] The size of the output
   @return CRYPT_OK on success
*/
int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
                   unsigned char *out,  unsigned long *outlen)
{
   ltc_asn1_list  *copy;
   unsigned long   x;
   int             err;
   
   /* make copy of list */
   copy = XCALLOC(inlen, sizeof(*copy));
   if (copy == NULL) {
      return CRYPT_MEM;
   }      
   
   /* fill in used member with index so we can fully sort it */
   for (x = 0; x < inlen; x++) {
       copy[x]      = list[x];
       copy[x].used = x;
   }       
   
   /* sort it by the "type" field */
   XQSORT(copy, inlen, sizeof(*copy), &qsort_helper);   
   
   /* call der_encode_sequence_ex() */
   err = der_encode_sequence_ex(copy, inlen, out, outlen, LTC_ASN1_SET);   
   
   /* free list */
   XFREE(copy);
   
   return err;
}                   
Exemple #20
0
static struct isis_route_info *
isis_route_info_new (struct prefix *prefix, uint32_t cost, uint32_t depth,
                     struct list *adjacencies)
{
  struct isis_route_info *rinfo;
  struct isis_adjacency *adj;
  struct listnode *node;

  rinfo = XCALLOC (MTYPE_ISIS_ROUTE_INFO, sizeof (struct isis_route_info));
  if (!rinfo)
    {
      zlog_err ("ISIS-Rte: isis_route_info_new: out of memory!");
      return NULL;
    }

  if (prefix->family == AF_INET)
    {
      rinfo->nexthops = list_new ();
      for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
        {
          /* check for force resync this route */
          if (CHECK_FLAG (adj->circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
            SET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
          /* update neighbor router address */
          if (depth == 2 && prefix->prefixlen == 32)
            adj->router_address = prefix->u.prefix4;
          adjinfo2nexthop (rinfo->nexthops, adj);
        }
    }
Exemple #21
0
/* Duplicate sockunion. */
union sockunion *sockunion_dup(const union sockunion *su)
{
	union sockunion *dup =
		XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
	memcpy(dup, su, sizeof(union sockunion));
	return dup;
}
Exemple #22
0
/* Allocate a new hash.  */
static ctclib_hash_t *
_ctclib_hash_create_size (uint32 size, hash_mem_type_t type,
      uint32 (*hash_key) (), bool (*hash_cmp) ())
{
    ctclib_hash_t *hash;

    hash = sal_malloc (sizeof (ctclib_hash_t));
    if (NULL == hash)
        return NULL;

#ifdef _USER_KERNEL_SHM_
    if(CTCLIB_HASH_SYS_MEM == type)
        hash->index = XCALLOC (CTCLLB_MEM_HASH_MODULE, sizeof (ctclib_hash_backet_t *) * size);
    else
        hash->index = XCALLOC_SHM (CTCLLB_MEM_HASH_MODULE, sizeof (ctclib_hash_backet_t *) * size);
#else
    hash->index = sal_calloc (sizeof (ctclib_hash_backet_t *) * size);
#endif

    if (NULL == hash->index)
    {
        sal_free(hash);
        return NULL;
    }

    hash->size = size;
    hash->hash_key = hash_key;
    hash->hash_cmp = hash_cmp;
    hash->count = 0;

    return hash;
}
/**
  Encode a SEQUENCE type using a VA list
  @param out    [out] Destination for data
  @param outlen [in/out] Length of buffer and resulting length of output
  @remark <...> is of the form <type, size, data> (int, unsigned long, void*)
  @return CRYPT_OK on success
*/
int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
        unsigned int algorithm, void* public_key, unsigned long* public_key_len,
        unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len)
{
   int err, len;
   oid_st oid;
   unsigned char *tmpbuf;
   unsigned long  tmpoid[16];
   ltc_asn1_list alg_id[2];
   ltc_asn1_list subject_pubkey[2];

   LTC_ARGCHK(in    != NULL);
   LTC_ARGCHK(inlen != 0);

   err = pk_get_oid(algorithm, &oid);
   if (err != CRYPT_OK) {
        return err;
   }

   /* see if the OpenSSL DER format RSA public key will work */
   tmpbuf = XCALLOC(1, MAX_RSA_SIZE*8);
   if (tmpbuf == NULL) {
       err = CRYPT_MEM;
       goto LBL_ERR;
   }

   /* this includes the internal hash ID and optional params (NULL in this case) */
   LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0]));
   LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, parameters_len);

   /* the actual format of the SSL DER key is odd, it stores a RSAPublicKey in a **BIT** string ... so we have to extract it
      then proceed to convert bit to octet
    */
   LTC_SET_ASN1(subject_pubkey, 0, LTC_ASN1_SEQUENCE, alg_id, 2);
   LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, MAX_RSA_SIZE*8);

   err=der_decode_sequence(in, inlen, subject_pubkey, 2UL);
   if (err != CRYPT_OK) {
           goto LBL_ERR;
   }

   len = subject_pubkey[1].size/8;
   if (*public_key_len > len) {
       memcpy(public_key, subject_pubkey[1].data, len);
       *public_key_len = len;
    } else {
        *public_key_len = len;
        err = CRYPT_BUFFER_OVERFLOW;
        goto LBL_ERR;
    }

    err = CRYPT_OK;

LBL_ERR:

    XFREE(tmpbuf);

    return err;
}
Exemple #24
0
struct route_table *
route_table_init (void)
{
  struct route_table *rt;

  rt = XCALLOC (MTYPE_ROUTE_TABLE, sizeof (struct route_table));
  return rt;
}
Exemple #25
0
struct super_run_in_global_packet *
super_run_in_global_packet_alloc(void)
{
  struct super_run_in_global_packet *p = NULL;
  XCALLOC(p, 1);
  super_run_in_global_packet_init((struct generic_section_config*) p);
  return p;
}
Exemple #26
0
/*******************************************************************************
 函数名称  : bgp_info_mpath_new
 功能描述  : bgp_info_mpath结构内存申请
 输入参数  : 
 输出参数  : 
 返 回 值  : 无
--------------------------------------------------------------------------------
 最近一次修改记录 :
 修改作者   :      
 修改目的   :      新添加函数
 修改日期   :       2012-8-15
*******************************************************************************/
struct bgp_info_mpath *bgp_info_mpath_new (void)
{
	struct bgp_info_mpath *new_mpath;
	
	new_mpath = XCALLOC (MTYPE_BGP_MPATH_INFO, sizeof (struct bgp_info_mpath));
	
	return new_mpath;
}
Exemple #27
0
static struct route_node *
isis_redist_route_node_create(route_table_delegate_t *delegate,
                              struct route_table *table)
{
  struct route_node *node;
  node = XCALLOC(MTYPE_ROUTE_NODE, sizeof(*node));
  return node;
}
Exemple #28
0
struct prefix *
prefix_new ()
{
  struct prefix *p;

  p = XCALLOC (MTYPE_PREFIX, sizeof *p);
  return p;
}
Exemple #29
0
/* Create new ospf6 interface structure */
struct ospf6_interface *
ospf6_interface_create (struct interface *ifp)
{
  struct ospf6_interface *oi;
  unsigned int iobuflen;

  oi = (struct ospf6_interface *)
    XCALLOC (MTYPE_OSPF6_IF, sizeof (struct ospf6_interface));

  if (!oi)
    {
      zlog_err ("Can't malloc ospf6_interface for ifindex %d", ifp->ifindex);
      return (struct ospf6_interface *) NULL;
    }

  oi->area = (struct ospf6_area *) NULL;
  oi->neighbor_list = list_new ();
  oi->neighbor_list->cmp = ospf6_neighbor_cmp;
  oi->linklocal_addr = (struct in6_addr *) NULL;
  oi->instance_id = OSPF6_INTERFACE_INSTANCE_ID;
  oi->transdelay = OSPF6_INTERFACE_TRANSDELAY;
  oi->priority = OSPF6_INTERFACE_PRIORITY;

  oi->hello_interval = OSPF6_INTERFACE_HELLO_INTERVAL;
  oi->dead_interval = OSPF6_INTERFACE_DEAD_INTERVAL;
  oi->rxmt_interval = OSPF6_INTERFACE_RXMT_INTERVAL;
  oi->cost = OSPF6_INTERFACE_COST;
  oi->state = OSPF6_INTERFACE_DOWN;
  oi->flag = 0;
  oi->mtu_ignore = 0;

  /* Try to adjust I/O buffer size with IfMtu */
  oi->ifmtu = ifp->mtu6;
  iobuflen = ospf6_iobuf_size (ifp->mtu6);
  if (oi->ifmtu > iobuflen)
    {
      if (IS_OSPF6_DEBUG_INTERFACE)
        zlog_debug ("Interface %s: IfMtu is adjusted to I/O buffer size: %d.",
		    ifp->name, iobuflen);
      oi->ifmtu = iobuflen;
    }

  oi->lsupdate_list = ospf6_lsdb_create (oi);
  oi->lsack_list = ospf6_lsdb_create (oi);
  oi->lsdb = ospf6_lsdb_create (oi);
  oi->lsdb->hook_add = ospf6_interface_lsdb_hook;
  oi->lsdb->hook_remove = ospf6_interface_lsdb_hook;
  oi->lsdb_self = ospf6_lsdb_create (oi);

  oi->route_connected = OSPF6_ROUTE_TABLE_CREATE (INTERFACE, CONNECTED_ROUTES);
  oi->route_connected->scope = oi;

  /* link both */
  oi->interface = ifp;
  ifp->info = oi;

  return oi;
}
Exemple #30
0
static struct userlist_user *
allocate_login_on_pool(
        struct uldb_mysql_state *state,
        int user_id)
{
  struct users_cache *uc = &state->users;
  struct userlist_user *u;
  struct xml_tree *u_xml, *l, *r;

  if (user_id <= 0) return 0;
  if (user_id > EJ_MAX_USER_ID) return 0;

  if (user_id >= uc->size) {
    int new_size = uc->size;
    struct userlist_user **new_map;

    if (!new_size) new_size = 1024;
    while (user_id >= new_size) new_size *= 2;
    XCALLOC(new_map, new_size);
    if (uc->size > 0)
      memcpy(new_map, uc->user_map, uc->size * sizeof(uc->user_map[0]));
    xfree(uc->user_map);
    uc->size = new_size;
    uc->user_map = new_map;
  }

  if ((u = uc->user_map[user_id])) {
    // don't even try to free nested cntsregs and cookies
    if (u->contests) {
      u->contests->first_down = u->contests->last_down = 0;
    }
    if (u->cookies) {
      u->cookies->first_down = u->cookies->last_down = 0;
    }
    u_xml = (struct xml_tree*) u;
    l = u_xml->left;
    r = u_xml->right;
    userlist_elem_free_data(u_xml);
    u->id = user_id;
    u_xml->left = l;
    u_xml->right = r;

    MOVE_TO_FRONT(u_xml, uc->first, uc->last, left, right);
    return u;
  }

  if (uc->count == USERS_POOL_SIZE) {
    do_remove_login_from_pool(uc, (struct userlist_user*) uc->last);
  }

  u = (struct userlist_user*) userlist_node_alloc(USERLIST_T_USER);
  u->id = user_id;
  u_xml = (struct xml_tree*) u;
  LINK_FIRST(u_xml, uc->first, uc->last, left, right);
  uc->user_map[user_id] = u;
  uc->count++;
  return u;
}