Mandoo’s WLAN story

[C] OpenWRT에서 Gateway 정보 가져오기 본문

Mandoo's IT Story/Dev

[C] OpenWRT에서 Gateway 정보 가져오기

mandoo12 2022. 5. 13. 10:51

리눅스 터미널에서 ifconfig로 조회되는 정보들은 ioctl 함수를 이용해 받아올 수 있다!

(ip, netmask, mac주소, broadcast addr, destination addr 등)

 

gateway 주소는 ifconfig로 조회되지 않기 때문에 popen을 쓰면 꽤 쉽게 받아올 수 있다!

 

popen은 pipe 통신을 이용해 리눅스에서 명령어를 실행시키고 그 결과를 file 형태로 받아오는 것이다.

(openwrt에서는 uci 명령어를 이용해 ubuntu나 cent os와는 사용법이 다르다.)

 

아래 코드를 보자

#include<stdio.h>
#include<stdbool.h>

int main(){
	char network_info[32] = {0,};
	FILE *fp;
		
	fp = popen("uci get network.wan.ipaddr", "r");
	if(fp == NULL){
    		printf("[error] pipe open fail!!\n");
    		return FALSE;
    }
	fgets(network_info, sizeof(network_info), fp);
	network_info[strlen(network_info) - 1] = '\0';
	pclose(fp);
        
	printf("gateway addr : %s\n", network_info);

	return TRUE;
}

 

openwrt 네트워크 설정 관련한 uci 명령어는 아래 게시글을 확인하자

2021.11.11 - [Linux] - [Linux/OpenWrt] Network 설정