[완료]10진수로 된 ip주소를 변환할려고 합니다.

huhspunk의 이미지

10진수 형태로 된 ip주소를 파일에서 불러와 점으로 찍힌 ip주소로 출력할려고 하는데 잘못된 주소가 출력이 되네요.
주소를 잘못 참조 하는건지;; 잘못된 점 지적 부탁바랍니다~

#include
#include
#include
#include
#include
#include
#include
#include
#include

#define NAME_SIZE 50
#define BUFSIZE 30
#define LINE_SIZE 256
#define LINE_BUF 10000

char *trimLeft(char *string);
void ip_convert(char *p);
int main(void)
{
char ip_addr[NAME_SIZE];
char ip_addr1[NAME_SIZE];
char *a;

memset(ip_addr,'\0',NAME_SIZE);

const char del_line[] = "=\t";
int size;
char *token;
char file_buf[LINE_BUF];

FILE *fp;
if((fp = fopen("ipaddr.txt","r")) == NULL) // (ip_addr = 3555800683)파일에는 이렇되 있습니다.
{
printf("125 Can't read file\n");
exit(1);
}
while(!feof(fp))
{
memset(file_buf,'\0',LINE_BUF);
fgets(file_buf,LINE_BUF,fp);
if(strchr(file_buf,'#') == NULL)
{
if(strstr(file_buf,"ip_addr"))
{
strtok(file_buf,del_line);
token = strtok(NULL,del_line);
size = strlen(token);
strncpy(ip_addr,token,size);
strncpy(ip_addr,token,NAME_SIZE);
if(ip_addr[size-1] == '\n')
{
ip_addr[size-1] = '\0';
}

}
}
}
a = trimLeft(ip_addr);
ip_convert(a);
}
char *trimLeft(char *string)
{
while(*string)
{
if(isspace(*string))
{
++string;
}
else
{
break;
}
}
return string;
}
void ip_convert(char *p)
{
char *ul = "UL";
strcat(p,ul);
printf("%s\n",p);

struct in_addr in;
unsigned long int ip = (unsigned long)p;
in.s_addr = htonl(ip);

printf("%s\n",inet_ntoa(in));

}

sabihanl의 이미지

분명히 텍스트에 저장된 데이터는 ip정보일테고..
그렇다면 대충 ip정보가 8bit씩 4덩이 일테고..
그렇다면 저 정보를 4덩이로 나워서 찍어보면 나오겠지요

Prentice의 이미지

3555800683
D3F1366B
D3 F1 36 6B
211 241 54 107

어느 단계에서 오류가 나나요?

huhspunk의 이미지

컴파일 해서 실행할때 마다 매 다른 주소가 출력 됩니다.
그리고 htonl함수를 이용해서 16진수 변환과정을 볼 수 없는데 방법이 있는지요?;

Prentice의 이미지

일정해야 하는 것이 일정치 않으면 그게 오류죠.. 어느 단계부터 엉뚱한 값이 끼어드는지요?

김정균의 이미지

php source code 에서 long2ip function 을 함번 참고해 보심이

doldori의 이미지

ip_convert()가 이상하네요.

unsigned long int ip = (unsigned long)p;

이건 문자열의 주소를 ip address로 쓰는 겁니다.
실행할 때마다 값이 달라지는 건 그 때문이고요.
실은 문자열의 내용을 읽어야죠.
unsigned long int ip;
sscanf(p, "%lu", &ip);

huhspunk의 이미지

문자열 주소를 읽어드려서 그랬군요..
해결점을 고쳐주셔서 감사합니다~^^

dragonkun의 이미지

팁을 드리자면.. 10진 ip address의 경우 Union 을 이용하시면 편하게 변환하실 수 있습니다.

union ipaddress {
     unsigned long addr;
     unsigned char sub[4];
};
 
int main() {
  union ipaddress i;
  i.addr = 3555800683;
  printf("%d.%d.%d.%d",i.sub[3],i.sub[2],i.sub[1],i.sub[0]);
  return 0;
}

안 돌려봤지만 되지 않을까 싶네요.. :)
--
Emerging the World!

Emerging the World!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.