IP를 설정하려고 하는데 자꾸 라우팅 테이블에서 Default Gateway가 지워집니다.
ifconfig 등의 명령어를 사용하지 않고 함수내에서 IP를 바꾸고 싶은데
IP는 제대로 바뀌지만 제목처럼 자꾸 Gateway가 지워집니다.
왜 이러는지 정말 모르겠네요;;
조언 좀 부탁드립니다.
아래는 소스입니다.
int main(int argc, char *argv[])
{
const char *address = argv[ 1 ];
struct ifreq *ifr = NULL;
struct sockaddr_in *sin = NULL;
struct ifconf ifcfg;
int fd = -1,
n = 0,
numreqs = 30;
fd = socket( AF_INET, SOCK_STREAM, 0 );
memset( &ifcfg, 0, sizeof( ifcfg ) );
ifcfg.ifc_buf = NULL;
ifcfg.ifc_len = sizeof( struct ifreq ) * numreqs;
ifcfg.ifc_buf = malloc( ifcfg.ifc_len );
if( ioctl( fd, SIOCGIFCONF, (char*)&ifcfg ) < 0 )
{
goto failure;
}
ifr = ifcfg.ifc_req;
for( n = 0; n < ifcfg.ifc_len; n += sizeof( struct ifreq ) )
{
char class_a[ 64 ] = "";
sin = (struct sockaddr_in*)&ifr->ifr_addr;
strncpy( class_a, inet_ntoa( sin->sin_addr ), 3 );
if( strncmp( class_a, "127", 3 ) != 0 )
{
sin->sin_addr.s_addr = inet_addr( address );
if( ioctl( fd, SIOCSIFADDR, ifr, sizeof( struct ifreq ) ) < 0 )
{
goto failure;
}
break;
}
ifr++;
}
close( fd );
free( ifcfg.ifc_buf );
return 0;
failure:
if( fd != -1 )
{
close( fd );
}
if( ifcfg.ifc_buf != NULL )
{
free( ifcfg.ifc_buf );
}
return -1;
}
댓글 달기