summaryrefslogtreecommitdiff
path: root/src/config_net.c
blob: fe600d77e793068e78dbb2f5c22ec9cae25c89eb (plain)
    1 /**
    2  * I3cstatus prints a configurable status bar for the i3 window manager
    3  * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #include "config_net.h"
   19 
   20 void config_net_init(struct node* n) {
   21   n->data = malloc(sizeof(struct config_net));
   22   n->type = CTYPE_NET;
   23   n->loadfunc = &config_net_load;
   24   n->loadkey = &load_net_key;
   25 }
   26 
   27 
   28 void load_net_key(struct node* n, char* key, char* val) {
   29   struct config_net* data = (struct config_net*) n->data;
   30 
   31   if(strcmp(key, "ifname") == 0)
   32     strcpy(data->ifname, val);
   33   else
   34     printf("ERROR: Unknown net key %s\n", key);
   35 }
   36 
   37 
   38 int config_net_load(struct node* n) {
   39   struct ifaddrs *addrs,*tmp;
   40   char host[224] = "--";
   41   strcpy(n->color, C_RED);
   42 
   43   getifaddrs(&addrs);
   44   tmp = addrs;
   45   
   46   while (tmp) {
   47     if(strcmp(tmp->ifa_name, ((struct config_net*)n->data)->ifname) == 0) {
   48       // Skip if no address is present
   49       if(! tmp->ifa_addr) {
   50         tmp = tmp->ifa_next;
   51         continue;
   52       }
   53 
   54       // Ensure sa_family is type 2 (17 doesn't have an IP)
   55       if(tmp->ifa_addr->sa_family != 2) {
   56         tmp = tmp->ifa_next;
   57         continue;
   58       }
   59 
   60       // Get the device IP address
   61       getnameinfo(
   62           tmp->ifa_addr,
   63           sizeof(struct sockaddr_in),
   64           host,
   65           NI_MAXHOST,
   66           NULL,
   67           0,
   68           NI_NUMERICHOST);
   69 
   70       strcpy(n->color, C_GREEN);
   71       break;
   72     }
   73     tmp = tmp->ifa_next;
   74   }
   75   freeifaddrs(addrs);
   76 
   77   strcpy(n->label_color, C_LGREY);
   78   strcpy(n->text, host);
   79   return 0;
   80 }

Generated by cgit