how to get the numbers-and-dots notation back from network byte order long?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how to get the numbers-and-dots notation back from network byte order long?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    how to get the numbers-and-dots notation back from network byte order long?  
Wim Deprez


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

Hello group,

my input is a network byte order long that represents an ip address,
and I want to the char * numbers-and-dots notation back, for example
to write it out to the user or something. How do I do that?

a stupid example:


int main (int argc, char* argv[])
{
unsigned long ma = inet_addr("185.21.0.9");

printf("%s",XXX(ma));
return 0;
}

where XXX(ma) becomes "185.21.0.9" again.

Kind greetings,

--wim





[ Post a follow-up to this message ]



    Re: how to get the numbers-and-dots notation back from network byte order long?  
Wim Deprez


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM


"Måns Rullgård" <mru@mru.ath.cx> escribió en el mensaje
news:yw1xekltfok1.fsf@mru.ath.cx...
> wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:
> 
>
> inet_ntoa()
>
> --
> Måns Rullgård
> mru@mru.ath.cx

So the easiest way to do this, is something like:

int main (int argc, char* argv[])
{
unsigned long ma = inet_addr("222.21.0.9");

struct in_addr ia;
ia.s_addr = ma;

printf("%d - %s \n",ma,inet_ntoa(ia));
return 0;
}


thanks and kind greetings,

--wim







[ Post a follow-up to this message ]



    Re: how to get the numbers-and-dots notation back from network byte order long?  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM


"Måns Rullgård" <mru@mru.ath.cx> wrote in message
news:yw1xekltfok1.fsf@mru.ath.cx...

> wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:
 
[vbcol=seagreen]
> inet_ntoa()

And if you want to code it yourself, this should work:

uint32_t ip=ntohl(ma);
printf("%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff,
(ip>>8)&0xff, ip&0xff);

DS







[ Post a follow-up to this message ]



    Re: how to get the numbers-and-dots notation back from network byte order long?  
James Antill


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

On Fri, 27 Aug 2004 00:17:31 +0000, Wim Deprez wrote:

> So the easiest way to do this, is something like:
>
> int main (int argc, char* argv[])
> {
>     unsigned long ma = inet_addr("222.21.0.9");
>
>     struct in_addr ia;
>            ia.s_addr = ma;
>
>      printf("%d - %s \n",ma,inet_ntoa(ia));
>      return 0;
> }

The big thing to watch out for is that you don't overwrite the internal
buffer. Ie. this will not work...

printf("%d - %s\n%d - %s\n",
ma1, inet_ntoa(ia1),
ma2, inet_ntoa(ia2));

...which is why some people find it easier to do something like...

printf("%d - %u.%u.%u.%u\n%d - %u.%u.%u.%u\n",
ma1, IPv4(ia1),
ma2, IPv4(ia2));

...which always works fine, although looks a little ugly. You
can also have a custom inet_ntoa which uses a passed in buffer,
and just pass in multiple buffers.
Personally I solved it using custom formatters with my own printf like
function:

http://www.and.org/vstr/#cust-fmt

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:40 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register