MAC 어드레스 얻는것과 마찬가지로 걍 ioctl 쓰는건데.. 역시 샘플로 만들어둔 코드 그냥 지우기 뭐해서 적어둔다. 랜선이 껴진 상태인지 판단하기 위해서 만든 코드인데 현재 내 PC 네트웍을 죽일수가 없어서 아직 테스트는 안해봤다.
내일 출근하면 랜선 뺐다 껴보고, 인터페이스 올리고 내리고 등등 하면서 플래그값 변화를 체크해보자.
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void show_ifr_flags(unsigned int flags);
int main()
{
/* 인터페이스 이름을 eth0 으로 고정했는데 시스템 내의
* 인터페이스들을 얻어오려면 SIOCGIFCONF 참고 */
const char* ifname = "eth0";
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
int s = socket(PF_INET, SOCK_STREAM, 0);
if(s<0) perror("socket failed"); /* TODO 더 나은 에러처리 */
int r = ioctl(s, SIOCGIFFLAGS, &ifr);
if(r<0) perror("ioctl failed"); /* TODO 더 나은 에러처리 */
show_ifr_flags(ifr.ifr_flags);
close(s);
}
/* 내 시스템에서 net/if.h 를 보면 이 flag 의 타입으로 unsigned short 가
* 쓰였다. 환경마다 다를것 같기도 헌데.. */
void show_ifr_flags(unsigned int flags)
{
#define SHOW_FLAG(flag, desc) printf("%-16s = %d // %s\n", #flag, (flags&flag)!=0, desc);
SHOW_FLAG(IFF_UP, "Interface is up");
SHOW_FLAG(IFF_BROADCAST, "Broadcast address valid");
SHOW_FLAG(IFF_DEBUG, "Turn on debugging");
SHOW_FLAG(IFF_LOOPBACK, "Is a loopback net");
SHOW_FLAG(IFF_POINTOPOINT, "Interface is point-to-point link");
SHOW_FLAG(IFF_NOTRAILERS, "Avoid use of trailers");
SHOW_FLAG(IFF_RUNNING, "Resources allocated");
SHOW_FLAG(IFF_NOARP, "No address resolution protocol");
SHOW_FLAG(IFF_PROMISC, "Receive all packets");
SHOW_FLAG(IFF_ALLMULTI, "Receive all multicast packets");
SHOW_FLAG(IFF_MASTER, "Master of a load balancer");
SHOW_FLAG(IFF_SLAVE, "Slave of a load balancer");
SHOW_FLAG(IFF_MULTICAST, "Supports multicast");
SHOW_FLAG(IFF_PORTSEL, "Can set media type");
SHOW_FLAG(IFF_AUTOMEDIA, "Auto media select active");
SHOW_FLAG(IFF_DYNAMIC, "Dialup device with changing addresses");
#undef SHOW_FLAG
}
음.. 돌려보고 결과를 적어둔다.
각 상황별 결과 보기
정상
IFF_UP = 1 // Interface is up
IFF_BROADCAST = 1 // Broadcast address valid
IFF_DEBUG = 0 // Turn on debugging
IFF_LOOPBACK = 0 // Is a loopback net
IFF_POINTOPOINT = 0 // Interface is point-to-point link
IFF_NOTRAILERS = 0 // Avoid use of trailers
IFF_RUNNING = 1 // Resources allocated
IFF_NOARP = 0 // No address resolution protocol
IFF_PROMISC = 0 // Receive all packets
IFF_ALLMULTI = 0 // Receive all multicast packets
IFF_MASTER = 0 // Master of a load balancer
IFF_SLAVE = 0 // Slave of a load balancer
IFF_MULTICAST = 1 // Supports multicast
IFF_PORTSEL = 0 // Can set media type
IFF_AUTOMEDIA = 0 // Auto media select active
IFF_DYNAMIC = 0 // Dialup device with changing addresses
ifdown
IFF_UP = 0 // Interface is up
IFF_BROADCAST = 1 // Broadcast address valid
IFF_DEBUG = 0 // Turn on debugging
IFF_LOOPBACK = 0 // Is a loopback net
IFF_POINTOPOINT = 0 // Interface is point-to-point link
IFF_NOTRAILERS = 0 // Avoid use of trailers
IFF_RUNNING = 0 // Resources allocated
IFF_NOARP = 0 // No address resolution protocol
IFF_PROMISC = 0 // Receive all packets
IFF_ALLMULTI = 0 // Receive all multicast packets
IFF_MASTER = 0 // Master of a load balancer
IFF_SLAVE = 0 // Slave of a load balancer
IFF_MULTICAST = 1 // Supports multicast
IFF_PORTSEL = 0 // Can set media type
IFF_AUTOMEDIA = 0 // Auto media select active
IFF_DYNAMIC = 0 // Dialup device with changing addresses
ifup 상태이지만 랜선 뺀경우
IFF_UP = 1 // Interface is up
IFF_BROADCAST = 1 // Broadcast address valid
IFF_DEBUG = 0 // Turn on debugging
IFF_LOOPBACK = 0 // Is a loopback net
IFF_POINTOPOINT = 0 // Interface is point-to-point link
IFF_NOTRAILERS = 0 // Avoid use of trailers
IFF_RUNNING = 0 // Resources allocated
IFF_NOARP = 0 // No address resolution protocol
IFF_PROMISC = 0 // Receive all packets
IFF_ALLMULTI = 0 // Receive all multicast packets
IFF_MASTER = 0 // Master of a load balancer
IFF_SLAVE = 0 // Slave of a load balancer
IFF_MULTICAST = 1 // Supports multicast
IFF_PORTSEL = 0 // Can set media type
IFF_AUTOMEDIA = 0 // Auto media select active
IFF_DYNAMIC = 0 // Dialup device with changing addresses
랜선뽑고 ifdown
IFF_UP = 0 // Interface is up
IFF_BROADCAST = 1 // Broadcast address valid
IFF_DEBUG = 0 // Turn on debugging
IFF_LOOPBACK = 0 // Is a loopback net
IFF_POINTOPOINT = 0 // Interface is point-to-point link
IFF_NOTRAILERS = 0 // Avoid use of trailers
IFF_RUNNING = 0 // Resources allocated
IFF_NOARP = 0 // No address resolution protocol
IFF_PROMISC = 0 // Receive all packets
IFF_ALLMULTI = 0 // Receive all multicast packets
IFF_MASTER = 0 // Master of a load balancer
IFF_SLAVE = 0 // Slave of a load balancer
IFF_MULTICAST = 1 // Supports multicast
IFF_PORTSEL = 0 // Can set media type
IFF_AUTOMEDIA = 0 // Auto media select active
IFF_DYNAMIC = 0 // Dialup device with changing addresses
정리하자면
랜선끼고 ifup
IFF_UP = 1 // Interface is up
IFF_RUNNING = 1 // Resources allocated
랜선끼고 ifdown
IFF_UP = 0 // Interface is up
IFF_RUNNING = 0 // Resources allocated
랜선뽑고 ifup
IFF_UP = 1 // Interface is up
IFF_RUNNING = 0 // Resources allocated
랜선뽑고 ifdown
IFF_UP = 0 // Interface is up
IFF_RUNNING = 0 // Resources allocated