0

i am trying to read interface stats from the netlink library

below is the code:

#include <iostream>
#include <stdio.h>
#include <netlink/netlink.h>
#include <netlink/route/link.h>
#include <unistd.h>

using namespace std;

int main(int argc, char *argv[])
{
    //cout << "Hello world!" << endl;

    //if (argc != 2){
    //    printf("usage %s interface\n ", argv[0]);
    //}

    int ret = 0;
    struct nl_handle *handle;

    //Allocate and initialize a netlink handle
    handle = nl_handle_alloc();
    if (handle == 0){
        printf("Failed to allocate a netlink handle\n");
        ret = -1;
        goto error_handle;
    }

    //bind and connect the socket to a protocol
    ret = nl_connect(handle, NETLINK_ROUTE);
    if (ret != 0){
        printf("failed to connect to kernel\n");
        ret = -1;
        goto error_connect;
    }

    //join a group to receive notifications
    ret = nl_socket_add_membership(handle, RTNLGRP_LINK);
    if (ret != 0){
        printf("joining group failed\n");
        ret = -1;
        goto error_connect;
    }

    //retrieve list of all interfaces and put them in cache
    struct nl_cache *cache = rtnl_link_alloc_cache(handle);
    if ( cache == 0 ){
        printf("error creating link cache\n");
        ret = -1;
        goto error_connect;
    }

    //lookup interface
    struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
    if (link == 0){
        printf("No such interface %s\n", "ens33");
        ret = -1;
        goto error_cache;
    }

    printf("packets sent %llu\n", rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS));
    printf("packets received %llu\n",rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS));



    //give the object back to the cache
    rtnl_link_put(link);
    error_cache:
        nl_cache_free(cache);

    error_connect:
        nl_handle_destroy(handle);

    error_handle:
        return ret;

    return 0;
}

however, i get this error....

-------------- Build: Debug in KSHK-Netlink01 (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -I/usr/include/netlink -c /home/kshk/ic++/KSHK-Netlink01/main.cpp -o obj/Debug/main.o
/home/kshk/ic++/KSHK-Netlink01/main.cpp: In function ‘int main(int, char**)’:
/home/kshk/ic++/KSHK-Netlink01/main.cpp:73:5: error: jump to label ‘error_connect’ [-fpermissive]
     error_connect:
     ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:44:14: error:   from here [-fpermissive]
         goto error_connect;
              ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:56:23: error:   crosses initialization of ‘rtnl_link* link’
     struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
                       ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:48:22: error:   crosses initialization of ‘nl_cache* cache’
     struct nl_cache *cache = rtnl_link_alloc_cache(handle);
                      ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:73:5: error: jump to label ‘error_connect’ [-fpermissive]
     error_connect:
     ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:36:14: error:   from here [-fpermissive]
         goto error_connect;
              ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:56:23: error:   crosses initialization of ‘rtnl_link* link’
     struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
                       ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:48:22: error:   crosses initialization of ‘nl_cache* cache’
     struct nl_cache *cache = rtnl_link_alloc_cache(handle);
                      ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:76:5: error: jump to label ‘error_handle’ [-fpermissive]
     error_handle:
     ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:28:14: error:   from here [-fpermissive]
         goto error_handle;
              ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:56:23: error:   crosses initialization of ‘rtnl_link* link’
     struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
                       ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:48:22: error:   crosses initialization of ‘nl_cache* cache’
     struct nl_cache *cache = rtnl_link_alloc_cache(handle);
                      ^
Process terminated with status 1 (0 minute(s), 0 second(s))
12 error(s), 0 warning(s) (0 minute(s), 0 second(s))

any suggestions?

krisdigitx
  • 7,068
  • 20
  • 61
  • 97
  • You have a mix of `printf` and `cout`, plus feel the need to use `goto`? This would be much cleaner with better flow control (for example, use `else` after each `if`). – crashmstr Jun 30 '14 at 14:46
  • 1
    Regarding your use of `goto`, I think this is one of the very few occasions where it could be warranted and used "correctly". However, instead of setting `ret` in each and every case, just initialize it to `-1` at the beginning, and after all check you know everything's okay and set it to zero then. – Some programmer dude Jun 30 '14 at 16:12

1 Answers1

2

You can't jump over variable declarations. If you want to use goto, you have to declare all variables before the first goto.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621