<commit_before>/**
 * @file NCDInterfaceMonitor.c
 * @author <NAME> <<EMAIL>>
 * 
 * @section LICENSE
 * 
 * This file is part of BadVPN.
 * 
 * BadVPN is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 * 
 * BadVPN 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 <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>

#include <sys/socket.h>
#include <net/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <asm/types.h>
#include <asm/types.h>

#include <misc/debug.h>
#include <misc/nonblocking.h>

#include <ncd/NCDInterfaceMonitor.h>

static void netlink_fd_handler (NCDInterfaceMonitor *o, int events);
static void process_buffer (NCDInterfaceMonitor *o);
static void more_job_handler (NCDInterfaceMonitor *o);

void netlink_fd_handler (NCDInterfaceMonitor *o, int events)
{
    DebugObject_Access(&o->d_obj);
    ASSERT(o->buf_left == -1)
    
    // read from netlink fd
    int len = read(o->netlink_fd, o->buf, sizeof(o->buf));
    if (len < 0) {
        DEBUG("read failed");
        return;
    }
    
    // set buffer
    o->buf_nh = (struct nlmsghdr *)o->buf;
    o->buf_left = len;
    
    // process buffer
    process_buffer(o);
    return;
}

void process_buffer (NCDInterfaceMonitor *o)
{
    ASSERT(o->buf_left >= 0)
    
    for (; NLMSG_OK(o->buf_nh, o->buf_left); o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left)) {
        if (o->buf_nh->nlmsg_type == NLMSG_DONE) {
            break;
        }
        
        if (o->buf_nh->nlmsg_type != RTM_NEWLINK && o->buf_nh->nlmsg_type != RTM_DELLINK) {
            continue;
        }
        
        void *pl = NLMSG_DATA(o->buf_nh);
        int pl_len = NLMSG_PAYLOAD(o->buf_nh, 0);
        
        if (pl_len < sizeof(struct ifinfomsg)) {
            DEBUG("missing infomsg");
            continue;
        }
        struct ifinfomsg *im = (void *)pl;
        
        int report_flags = 0;
        
        if (o->buf_nh->nlmsg_type == RTM_NEWLINK) {
            report_flags |= NCDIFCONFIG_FLAG_EXISTS;
            
            if ((im->ifi_flags&IFF_RUNNING)) {
                report_flags |= NCDIFCONFIG_FLAG_RUNNING;
            }
        }
        
        // parse attributes to get interface name
        
        char *ifname = NULL;
        
        int rta_len = pl_len - sizeof(struct ifinfomsg);
        
        for (struct rtattr *rta = (void *)(im + 1); RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
            uint8_t *attr = RTA_DATA(rta);
            int attr_len = RTA_PAYLOAD(rta);
            
            if (rta->rta_type == IFLA_IFNAME && attr_len > 0 && attr[attr_len - 1] == '\0') {
                ifname = (char *)attr;
            }
        }
        
        if (!ifname) {
            continue;
        }
        
        // schedule more job
        BPending_Set(&o->more_job);
        
        // dispatch event
        o->handler(o->user, ifname, report_flags);
        return;
    }
    
    // set no buffer
    o->buf_left = -1;
}

void more_job_handler (NCDInterfaceMonitor *o)
{
    DebugObject_Access(&o->d_obj);
    ASSERT(o->buf_left >= 0)
    
    // finish this message
    o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left);
    
    // process buffer
    process_buffer(o);
    return;
}

int NCDInterfaceMonitor_Init (NCDInterfaceMonitor *o, BReactor *reactor, NCDInterfaceMonitor_handler handler, void *user)<commit_msg>NCDInterfaceMonitor: don't rely on interface flags provided from netlink; instead, query them  manually. Fixes an issue when a wireless device is pulled out and the device is reported to be down  and then again up, incorrectly.<commit_after>/**
 * @file NCDInterfaceMonitor.c
 * @author <NAME> <<EMAIL>>
 * 
 * @section LICENSE
 * 
 * This file is part of BadVPN.
 * 
 * BadVPN is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 * 
 * BadVPN 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 <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>

#include <sys/socket.h>
#include <net/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <asm/types.h>
#include <asm/types.h>

#include <misc/debug.h>
#include <misc/nonblocking.h>

#include <ncd/NCDInterfaceMonitor.h>

static void netlink_fd_handler (NCDInterfaceMonitor *o, int events);
static void process_buffer (NCDInterfaceMonitor *o);
static void more_job_handler (NCDInterfaceMonitor *o);

void netlink_fd_handler (NCDInterfaceMonitor *o, int events)
{
    DebugObject_Access(&o->d_obj);
    ASSERT(o->buf_left == -1)
    
    // read from netlink fd
    int len = read(o->netlink_fd, o->buf, sizeof(o->buf));
    if (len < 0) {
        DEBUG("read failed");
        return;
    }
    
    // set buffer
    o->buf_nh = (struct nlmsghdr *)o->buf;
    o->buf_left = len;
    
    // process buffer
    process_buffer(o);
    return;
}

void process_buffer (NCDInterfaceMonitor *o)
{
    ASSERT(o->buf_left >= 0)
    
    for (; NLMSG_OK(o->buf_nh, o->buf_left); o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left)) {
        if (o->buf_nh->nlmsg_type == NLMSG_DONE) {
            break;
        }
        
        if (o->buf_nh->nlmsg_type != RTM_NEWLINK && o->buf_nh->nlmsg_type != RTM_DELLINK) {
            continue;
        }
        
        void *pl = NLMSG_DATA(o->buf_nh);
        int pl_len = NLMSG_PAYLOAD(o->buf_nh, 0);
        
        if (pl_len < sizeof(struct ifinfomsg)) {
            DEBUG("missing infomsg");
            continue;
        }
        struct ifinfomsg *im = (void *)pl;
        
        // parse attributes to get interface name
        
        char *ifname = NULL;
        
        int rta_len = pl_len - sizeof(struct ifinfomsg);
        
        for (struct rtattr *rta = (void *)(im + 1); RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
            uint8_t *attr = RTA_DATA(rta);
            int attr_len = RTA_PAYLOAD(rta);
            
            if (rta->rta_type == IFLA_IFNAME && attr_len > 0 && attr[attr_len - 1] == '\0') {
                ifname = (char *)attr;
            }
        }
        
        if (!ifname) {
            continue;
        }
        
        // schedule more job
        BPending_Set(&o->more_job);
        
        // dispatch event
        o->handler(o->user, ifname, NCDIfConfig_query(ifname));
        return;
    }
    
    // set no buffer
    o->buf_left = -1;
}

void more_job_handler (NCDInterfaceMonitor *o)
{
    DebugObject_Access(&o->d_obj);
    ASSERT(o->buf_left >= 0)
    
    // finish this message
    o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left);
    
    // process buffer
    process_buffer(o);
    return;
}

int NCDInterfaceMonitor_Init (NCDInterfaceMonitor *o, BReactor *reactor, NCDInterfaceMonitor_handler handler, void *user)
{'fraction_non_alphanumeric': 0.11695002575991757, 'fraction_numerical': 0.006053580628541989, 'mean_word_length': 2.3585640138408306, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 108, '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': '1847001', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2558, 'n_words': 690}