<commit_before>/*
 * Command-line tool to manage and monitor the rlite stack.
 *
 * Copyright (C) 2015-2016 Nextworks
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of rlite.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <pthread.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <assert.h>
#include <sys/ioctl.h>

#include <rlite/list.h>
#include <rlite/uipcps-msg.h>
#include <rlite/conf.h>

#include "../helpers.h"

/* IPCP attributes. */
struct ipcp_attrs {
    rl_ipcp_id_t id;
    char *name;
    rlm_addr_t addr;
    unsigned int txhdroom;
    unsigned int rxhdroom;
    unsigned int tailroom;
    unsigned int max_sdu_size;
    char *dif_type;
    char *dif_name;
    int wait_for_delete;

    struct list_head node;
};

/* Keeps the list of IPCPs in the system. */
static struct list_head ipcps;

struct cmd_descriptor {
    const char *name;
    const char *usage;
    unsigned int num_args;
    int (*func)(int argc, char **argv, struct cmd_descriptor *cd);
};

static struct ipcp_attrs *
lookup_ipcp_by_name(const char *name)
{
    struct ipcp_attrs *attrs;

    if (rina_sername_valid(name)) {
        list_for_each_entry (attrs, &ipcps, node) {
            if (rina_sername_valid(attrs->name) &&
                strcmp(attrs->name, name) == 0) {
                return attrs;
            }
        }
    }

    return NULL;
}

static struct ipcp_attrs *
ipcp_by_dif(const char *dif_name)
{
    struct ipcp_attrs *attrs;

    list_for_each_entry (attrs, &ipcps, node) {
        if (strcmp(attrs->dif_name, dif_name) == 0) {
            return attrs;
        }
    }

    return NULL;
}

/* Select the IPCP with the smaller MSS, which is
 * probably the one with the higher rank. */
static struct ipcp_attrs *
select_ipcp()
{
    unsigned int smaller_mss = ~0U;
    struct ipcp_attrs *ret   = NULL;
    struct ipcp_attrs *attrs;

    list_for_each_entry (attrs, &ipcps, node) {
        if (attrs->max_sdu_size < smaller_mss) {
            smaller_mss = attrs->max_sdu_size;
            ret         = attrs;
        }
    }

    return ret;
}

static int
uipcps_connect(void)
{<commit_msg>tools: rlite-ctl: let select_ipcp() select only normal IPCPs<commit_after>/*
 * Command-line tool to manage and monitor the rlite stack.
 *
 * Copyright (C) 2015-2016 Nextworks
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of rlite.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <pthread.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <assert.h>
#include <sys/ioctl.h>

#include <rlite/list.h>
#include <rlite/uipcps-msg.h>
#include <rlite/conf.h>

#include "../helpers.h"

/* IPCP attributes. */
struct ipcp_attrs {
    rl_ipcp_id_t id;
    char *name;
    rlm_addr_t addr;
    unsigned int txhdroom;
    unsigned int rxhdroom;
    unsigned int tailroom;
    unsigned int max_sdu_size;
    char *dif_type;
    char *dif_name;
    int wait_for_delete;

    struct list_head node;
};

/* Keeps the list of IPCPs in the system. */
static struct list_head ipcps;

struct cmd_descriptor {
    const char *name;
    const char *usage;
    unsigned int num_args;
    int (*func)(int argc, char **argv, struct cmd_descriptor *cd);
};

static struct ipcp_attrs *
lookup_ipcp_by_name(const char *name)
{
    struct ipcp_attrs *attrs;

    if (rina_sername_valid(name)) {
        list_for_each_entry (attrs, &ipcps, node) {
            if (rina_sername_valid(attrs->name) &&
                strcmp(attrs->name, name) == 0) {
                return attrs;
            }
        }
    }

    return NULL;
}

static struct ipcp_attrs *
ipcp_by_dif(const char *dif_name)
{
    struct ipcp_attrs *attrs;

    list_for_each_entry (attrs, &ipcps, node) {
        if (strcmp(attrs->dif_name, dif_name) == 0) {
            return attrs;
        }
    }

    return NULL;
}

/* Select the IPCP with the smaller MSS, which is
 * probably the one with the higher rank. */
static struct ipcp_attrs *
select_ipcp()
{
    unsigned int smaller_mss = ~0U;
    struct ipcp_attrs *ret   = NULL;
    struct ipcp_attrs *attrs;

    list_for_each_entry (attrs, &ipcps, node) {
        if (type_is_normal_ipcp(attrs->dif_type) &&
            attrs->max_sdu_size < smaller_mss) {
            smaller_mss = attrs->max_sdu_size;
            ret         = attrs;
        }
    }

    return ret;
}

static int
uipcps_connect(void)
{
{'fraction_non_alphanumeric': 0.10232328225407811, 'fraction_numerical': 0.00757950238919097, 'mean_word_length': 2.962140992167102, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1423193', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2029, 'n_words': 660}