'link staus'에 해당되는 글 1건

  1. 2009.07.30 [확인중] 이더넷 링크 상태 확인하기 - howto get link status
Linux API/network2009. 7. 30. 15:48
랜선을 뽑고 꼽으면 아래와 같은 메시지가 출력된다.

eth0 : link down
eth0 : link up

dmesg로 매번 감시할수도 없는 노릇이고 그래서 코드로 하는 법을 찾아 왔는데
아직까지는 컴파일도 못시켜 봤다..(써글넘의 타입)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <net/if.h>
#include <netinet/in.h>
#include <ctype.h>

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

#include <linux/sockios.h>
#include <linux/ethtool.h>

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Get the link status. This returns TRUE if the link is up and FALSE
// otherwise.
//
int32_t link_stat()
{
        int s;
        struct ifreq ifr;
        struct ethtool_value eth;

        if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == FAIL)
                ERRORS(Socket);

        bzero(&ifr, sizeof(ifr));
        strcpy(ifr.ifr_name, Eth0);
        ifr.ifr_data = (caddr_t) &eth;
        eth.cmd = ETHTOOL_GLINK;

        if(ioctl(s, SIOCETHTOOL, &ifr) == FAIL)
                ERRORS(Ioctl);
        (void)close(s);

        return (eth.data) ? TRUE:FALSE;
}

void main()
{
        int ret;
        ret = link_stat();

        if(ret == TRUE) printf("link up\n");
        else printF("link down\n");
}

[링크 : http://lkml.indiana.edu/hypermail/linux/kernel/0505.2/0754.html]

'Linux API > network' 카테고리의 다른 글

struct in_addr  (0) 2009.08.18
ifup/ifdown  (0) 2009.08.18
리눅스 소켓 프로그래밍 - linux socket programming  (0) 2009.07.17
fstat - 파일의 상태 얻어오기  (2) 2009.07.02
Linux File Descriptor / File pointer  (0) 2009.06.30
Posted by 구차니