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

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > how to get the numbers-and-dots notation back from network byte order long?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

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

2004-08-27, 6: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
Wim Deprez

2004-08-27, 6: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


David Schwartz

2004-08-27, 6: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


James Antill

2004-08-27, 6: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/

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com