내 자신의 서버 ip주소를 알아내는 방법 그런데 로칼 아이피만 나옵니다.
내 자신의 서버가 연결돤 현재의 유동 아이피 주소를 알고자 아래와 같은 소스를 구하여 돌려보니 네트워크에 연결된 유동 아이피 주소가 나오는게 아니고 Local Loopback 주소인 127.0.0.1 이나옵니다.
실제 네트워크에 연결된 유동아이피 주소를 알려면 아래 소스의 어느부분을 수정해야 하는지요?
아시는분 도움좀 부탁 드립니다.
소스 ;
#include <stdio.h> #include <sys/socket.h> #include <netdb.h> #include <sys/utsname.h> char **my_addrs(int *addrtype); int main(void) { struct in_addr in; char **addr; int type; addr=my_addrs(&type); memcpy(&in.s_addr, *addr, sizeof (in.s_addr)); printf("%s\n", inet_ntoa(in)); return 0; } /* * Description : Current localhost IP * Argument : addrtype - type of address (INET4 or INET6) * Return : the hostent structure or a NULL pointer */ char **my_addrs(int *addrtype) { struct hostent *hptr; struct utsname myname; if (uname(&myname) < 0) return NULL; if ((hptr = gethostbyname(myname.nodename)) == NULL) return NULL; *addrtype = hptr->h_addrtype; return (hptr->h_addr_list); }
ifconfig 결과;
eth1 Link encap:Ethernet HWaddr 00:02:B3:AF:BA:9F
inet addr:224.165.132.xxx Bcast:255.255.255.255 Mask:255.255.255.128
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:42666 errors:0 dropped:0 overruns:0 frame:0
TX packets:2505 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:3720870 (3.5 Mb) TX bytes:215677 (210.6 Kb)
Interrupt:20 Base address:0x8800 Memory:fc261000-fc261038
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:14 errors:0 dropped:0 overruns:0 frame:0
TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:900 (900.0 b) TX bytes:900 (900.0 b)
..
코드를 올려 드리는것 보다는 찾는 방법을 올려드리는게 더 좋을듯 싶어서
방법만 드립니다. 우선 자신의 로컬 IP를 알고자 하면 몇가지 단서를 달아야
만 됩니다. 그냥 내 로컬 IP라는것은 너무 막연하게 되거든요.
1. 전체 인터페이스의 모든 IP를 알고 싶은지.
2. 특정 인터페이스의 모든 IP를 알고 싶은지.
3. 특정 인터페이스의 특정 IP를 알고 싶은지.
이중에 자신이 만들어 놓은 조건을 가지고 IOCTL이나 NETLINK를 이용해서
원하는 IP를 얻어 오시면 됩니다. 코드는 인터넷에 널려 있고 구굴에서
조금만 검색해봐도 손쉽게 찾으실수 있습니다.
답변 감사합니다.
알려주신대로 소스 구해서 잘 해결 했습니다.
다시한번 바쁘신중에 답변 주신데 정말 감사드립니다.
나를 고생 시키고 있는 놈
리눅스, php, js, html, 스크립트, 플래쉬... 쥑일넘들...
살며 그리고 사랑하며...
정보는 공유할때 그 가치가 있는것.....
나의 조그만 지식공유는 남에게 엄청난 기쁨을 안겨 준다.
댓글 달기