Please help!
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 > Please help!




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

    Please help!  
Fredrik Håkansson


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


 
07-30-04 12:52 PM

Hello!

I'm a beginner and are struggling to learn.

I'm on Linux and i know that i have an IP address in the member
variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
sure if this is a char[], long or what it is.

I have tried to have the contents of RealHostAddr.sin.sin_addr.s_addr to
be put into syslog.

I have tried the following:
syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr);
and
syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr, 10);
and
syslog(LOG_DEBUG,"%d",(int)RealHostAddr.sin.sin_addr.s_addr, 10);
and
syslog(LOG_DEBUG,"%d",(long)RealHostAddr.sin.sin_addr.s_addr, 10);

And many other but nothing seems to work. I have reasons to believe  that
the IP is stored backwards so that 192.168.20.50 is 50.20.168.192. So i
also later want to use ntohl().

Fredrik






[ Post a follow-up to this message ]



    Re: Please help!  
Lev Walkin


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


 
07-30-04 12:52 PM

Fredrik H=E5kansson wrote:
> Hello!
>=20
> I'm a beginner and are struggling to learn.
>=20
> I'm on Linux and i know that i have an IP address in the member
> variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm no=
t
> sure if this is a char[], long or what it is.

its in_addr_t, which is typically defined through u_int32_t.

> I have tried to have the contents of RealHostAddr.sin.sin_addr.s_addr t=
o
> be put into syslog.
>=20
> I have tried the following:
> syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr);
> and
> syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr, 10);
> and
> syslog(LOG_DEBUG,"%d",(int)RealHostAddr.sin.sin_addr.s_addr, 10);
> and
> syslog(LOG_DEBUG,"%d",(long)RealHostAddr.sin.sin_addr.s_addr, 10);
>=20
> And many other but nothing seems to work. I have reasons to believe  th=
at
> the IP is stored backwards so that 192.168.20.50 is 50.20.168.192. So i=

> also later want to use ntohl().=20

man inet_ntoa, inet_ntop

--=20
Lev Walkin
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: Please help!  
Fredrik Håkansson


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


 
07-30-04 12:52 PM

On Fri, 30 Jul 2004 03:00:01 -0700, Lev Walkin wrote:

How do i print the content of a u_int32_t to syslog?

Thanks!






[ Post a follow-up to this message ]



    Re: Please help!  
Lev Walkin


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


 
07-30-04 12:52 PM

Fredrik H=E5kansson wrote:
> On Fri, 30 Jul 2004 03:00:01 -0700, Lev Walkin wrote:
>=20
> How do i print the content of a u_int32_t to syslog?=20

syslog(..., "%s", inet_ntoa(sin.sin_addr));

--=20
Lev Walkin
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: Please help!  
Jens.Toerring@physik.fu-berlin.de


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


 
07-30-04 12:52 PM

Fredrik Håkansson <fredrik@spamme.younix.se> wrote:
> I'm on Linux and i know that i have an IP address in the member
> variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
> sure if this is a char[], long or what it is.

It's a variable of type in_addr_t, which is a 32-bit integer in
network byte ordering. And network byte order is most significant
byte in the lowest address (i.e. first byte) and the least signi-
ficant one in the highest (i.e. what's traditionally often called
"big-endian"). When you're on an Intel-machine it's the "wrong"-way
round compared to how numbers are stored there.

> I have tried to have the contents of RealHostAddr.sin.sin_addr.s_addr to
> be put into syslog.

> I have tried the following:
> syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr);
> and
> syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr, 10);
> and
> syslog(LOG_DEBUG,"%d",(int)RealHostAddr.sin.sin_addr.s_addr, 10);
> and
> syslog(LOG_DEBUG,"%d",(long)RealHostAddr.sin.sin_addr.s_addr, 10);

None of these will work properly since, as far as I can see, syslog(2)
expects the string to be printed as the second argument and its length
as the third and last argument. But the s_addr thing isn't a string
but an unsigned 32-bit integer, so you will have to convert it into a
string and then pass that to syslog.

> And many other but nothing seems to work. I have reasons to believe  that
> the IP is stored backwards so that 192.168.20.50 is 50.20.168.192. So i
> also later want to use ntohl().

I guess the following should do the trick of printing out the address
in the way it's usually formatted (i.e. four integers with dots in
between) while being safe on all architectures:

char buf[ 17 ];
sprintf( buf, "%u.%u.%u.%u\n",
( unsigned int )
( ntohl( RealHostAddr.sin.sin_addr.s_addr ) >> 24 & 0xFF ),
( unsigned int )
( ntohl( RealHostAddr.sin.sin_addr.s_addr ) >> 16 & 0xFF ),
( unsigned int )
( ntohl( RealHostAddr.sin.sin_addr.s_addr ) >>  8 & 0xFF ),
( unsigned int )
( ntohl( RealHostAddr.sin.sin_addr.s_addr )       & 0xFF ) );
syslog( LOG_DEBUG, buf, strlen( buf ) );

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: Please help!  
Jens.Toerring@physik.fu-berlin.de


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


 
07-30-04 12:52 PM

Jens.Toerring@physik.fu-berlin.de wrote:

> syslog( LOG_DEBUG, buf, strlen( buf ) );

Sorry, you're probably looking for the libc syslog function, so
just follow Lev Walkin suggestion (I also had forgotten about
the inet_ntoa() function, which is the best thing to use here).

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: Please help!  
Fredrik Håkansson


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


 
07-30-04 12:52 PM

On Fri, 30 Jul 2004 10:38:02 +0000, Jens.Toerring wrote:

> Fredrik Håkansson <fredrik@spamme.younix.se> wrote: 
>
> It's a variable of type in_addr_t, which is a 32-bit integer in
> network byte ordering. And network byte order is most significant
> byte in the lowest address (i.e. first byte) and the least signi-
> ficant one in the highest (i.e. what's traditionally often called
> "big-endian"). When you're on an Intel-machine it's the "wrong"-way
> round compared to how numbers are stored there.
> 
> 
>
> None of these will work properly since, as far as I can see, syslog(2)
> expects the string to be printed as the second argument and its length
> as the third and last argument. But the s_addr thing isn't a string
> but an unsigned 32-bit integer, so you will have to convert it into a
> string and then pass that to syslog.
> 
>
> I guess the following should do the trick of printing out the address
> in the way it's usually formatted (i.e. four integers with dots in
> between) while being safe on all architectures:
>
> char buf[ 17 ];
> sprintf( buf, "%u.%u.%u.%u\n",
>          ( unsigned int )
>              ( ntohl( RealHostAddr.sin.sin_addr.s_addr ) >> 24 & 0xFF ),
>          ( unsigned int )
>              ( ntohl( RealHostAddr.sin.sin_addr.s_addr ) >> 16 & 0xFF ),
>          ( unsigned int )
>              ( ntohl( RealHostAddr.sin.sin_addr.s_addr ) >>  8 & 0xFF ),
>          ( unsigned int )
>              ( ntohl( RealHostAddr.sin.sin_addr.s_addr )       & 0xFF ) );
> syslog( LOG_DEBUG, buf, strlen( buf ) );
>
>                                    Regards, Jens

hehe i thought type casting to a pointer to an array would do the
trick (char *) RealHostAddr.sin.sin_addr.s_addr to five syslog a string
like you said. Whats wrong with this??

BTW thanks for helping!







[ Post a follow-up to this message ]



    Re: Please help!  
Jens.Toerring@physik.fu-berlin.de


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


 
07-30-04 12:52 PM

Fredrik Håkansson <fredrik@spamme.younix.se> wrote:
> On Fri, 30 Jul 2004 10:38:02 +0000, Jens.Toerring wrote:
> hehe i thought type casting to a pointer to an array would do the
> trick (char *) RealHostAddr.sin.sin_addr.s_addr to five syslog a string
> like you said. Whats wrong with this??

No, a string is a completely different beastie from a number.
Numbers (like the IP address) are stored in binary (in this case
four bytes, one for each of the octetts). Strings are arrays of
chars with the last char being a '\0' character to mark the end
of the string. And in your case you need a single char for each
of the digits of the number (plus three for the dots plus one for
the final '\0' character), so an IP address written as a string
will need up to 16 cars and not just 4 bytes like the number.

Casting won't help you there, a cast just tells the compiler
that it shold treat the castee as if it had the type it is
cast to, and when you do

( char * ) x

and x is some integer variable it tells the compiler to assume
that the content of x is a pointer to a char - unless done very
carefully that's usually a nice recipe for a catastrophy. The
cast doesn't convert anything at all.

Anyway, just go with Lev Walkin's suggestion, it's absolutely
fine and does exactly what you want with the least amount of
hassle.
Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:30 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