[질문] 자기IP를 가져오는 방법이 알고 싶습니다.

pleasantman의 이미지

자기 IP를 잡아오고 싶습니다.

FTP소스에 있는 부분을 참고해서 만들어봤는데..

동작하지 않는군요..

  	/* ftp data connection */    
	ftp_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (ftp_fd < 0) {
		perror("socket");
        exit(1);
	}

	if (setsockopt(ftp_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) 
	{
		perror("setsockopt (reuse address)");
		exit(1);
	}
	
	ftp_addr.sin_family 		= AF_INET;
    ftp_addr.sin_port 			= htons(0);           /* choose an unused port at random */
    ftp_addr.sin_addr.s_addr 	= htonl(INADDR_ANY);  /* use my IP address */	
	bzero(&(ftp_addr.sin_zero), 8);
		
	if (bind(ftp_fd, (struct sockaddr *)&ftp_addr, sizeof(ftp_addr)) < 0) {
		perror("bind");
        exit(1);
	}

	if (options & SO_DEBUG && setsockopt(ftp_fd, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
		perror("setsockopt");
		
	len = sizeof (ftp_addr);
	if (getsockname(ftp_fd, (struct sockaddr *)&ftp_addr, &len) < 0) {
		perror("getsockname");
		exit(1);
	}
			
	if (listen(ftp_fd, 1) < 0){
		perror("listen");
        exit(1);
	}
		
	a = (char *)&ftp_addr.sin_addr;
	p = (char *)&ftp_addr.sin_port;
	
#define	UC(b)	(((int)b)&0xff)
    sprintf(PORT_temp, "PORT %d,%d,%d,%d,%d,%d", UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
    //sprintf(PORT_temp, "PORT %d,%d,%d,%d,%d,%d", 10, 253, 253, 86, UC(p[0]), UC(p[1]));

PORT_temp를 프린트하면 다음과 같네요..

<PORT 0,0,0,0,4,4

보시는 데로 포트 번호는 잘 잡아 옵니다. 위의 코드는 간단한 ftp를 윈한

코드이며, 소스는 inetutils의 ftp소스를 참고 했습니다.

한가지 추가한다면.. 현재 IP가 내부 이더넷 IP와 ppp0가 있습니다..

ppp0의 IP를 가져와야 하는데.. default로 어느 값을 가져오게 되나요?

불량청년의 이미지

게시판 검색을 많이 하십시요. *^^*

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <sys/ioctl.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stropts.h> 

#if defined(sun) 
#include <sys/sockio.h> 
#endif 

#include <net/if.h> 
#if defined(linux) 
#include <linux/sockios.h> 
#endif 

#define BUFFERSIZE 1024 
const char * localip = "0.0.0.0"; 

const char * myip() 
{ 
   const int MAX_NIC = 10; 
    struct ifconf ifc; 
    struct ifreq ifr[MAX_NIC]; 

    int s; 
    int nNumIFs; 
    int i; 
    int count; 
    int max=2; 
    static char ip[BUFFERSIZE]; 
    int cmd = SIOCGIFCONF; 

    max++; 

    ifc.ifc_len = sizeof ifr; 
    ifc.ifc_ifcu.ifcu_req = ifr; 

    if( (s=socket(AF_INET,SOCK_STREAM,0)) < 0) 
    { 
       perror("socket"); 
        exit(1); 
    } 

#if defined(_AIX) 
    cmd = CSIOCGIFCONF; 
#endif 

    if( (s = ioctl(s, cmd, &ifc) ) < 0) 
    { 
       perror("ioctl"); 
        exit(1); 
    } 

    nNumIFs = ifc.ifc_len / sizeof ( struct ifreq ); 
    count = 0; 
    strcpy( ip, localip ); 
    for( i=0; i<nNumIFs; i++ ) 
    { 
         struct in_addr addr; 
       if( ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr.sa_family != AF_INET) 
       { 
          continue; 
       } 

        addr = ((struct sockaddr_in *) & ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr)->sin_addr; 
        if( addr.s_addr == htonl( 0x7f000001 ) ) 
        { 
           continue; 
        } 
        strcpy( ip, inet_ntoa( addr ) ); 
        printf( "%s IP: %s\n", ifc.ifc_ifcu.ifcu_req[i].ifr_ifrn.ifrn_name, ip ); 
   } 
   return ip; 
} 

int main(void) 
{ 
   printf("One of my IP is %s\n", myip() ); 
    return 0; 
} 

어떤분이 예전에 올리셨던 소스입니다. 그럼...

H/W가 컴퓨터의 심장이라면 S/W는 컴퓨터의 영혼이다!

pleasantman의 이미지

검색을 한다고 했었는데..
찾지못했네요..

코드에 감사합니다. 수고하세요..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.