簡體   English   中英

netlink接口統計信息goto交叉初始化錯誤

[英]netlink interface stat goto cross initialization error

我正在嘗試從netlink庫讀取接口統計信息

下面是代碼:

#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;
}

但是,我收到此錯誤...。

-------------- 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))

有什么建議么?

您不能跳過變量聲明。 如果要使用goto ,則必須在第一個goto之前聲明所有變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM