EmbeddedRelated.com
Forums

HTTP server storing client's IP address?

Started by racqueldesign November 2, 2012
I have a HTTP Server running on the rabbit board but I don't know how to store the IP Address of any user that send an HTTP REQUEST. My question is: is there a easy way for a server to acquire the IP address of its client?

Hi racquel,

Yes!, using getpeername function from Dynamic C.

Regards

Jesus

De: r... [mailto:r...] En
nombre de racqueldesign
Enviado el: viernes, 02 de noviembre de 2012 13:45
Para: r...
Asunto: [rabbit-semi] HTTP server storing client's IP address?

I have a HTTP Server running on the rabbit board but I don't know how to
store the IP Address of any user that send an HTTP REQUEST. My question is:
is there a easy way for a server to acquire the IP address of its client?
Hi Jesus,

Thanks for telling about the getpeername but I first need to know the name of the socket. For example:

void main()
{
sock_init_or_exit(1);
http_init();
tcp_reserveport(80);
while (1)
{
http_handler();
}
}

I am guessing the http_init() setup the socket. Am i right or am I missing something? I need my .cgi handler to get its client's ip address. Is there also a function the returns a pointer to the current socket? I think that might solve my problem with your advice.

--- In r..., "Jesus Manuel Conejo Sarabia" wrote:
>
>
>
> Hi racquel,
>
> Yes!, using getpeername function from Dynamic C.
>
>
>
> Regards
>
> Jesus
>
>
>
>
>
>
>
>
>
> De: r... [mailto:r...] En
> nombre de racqueldesign
> Enviado el: viernes, 02 de noviembre de 2012 13:45
> Para: r...
> Asunto: [rabbit-semi] HTTP server storing client's IP address?
>
>
>
>
>
> I have a HTTP Server running on the rabbit board but I don't know how to
> store the IP Address of any user that send an HTTP REQUEST. My question is:
> is there a easy way for a server to acquire the IP address of its client?
>

On 11/2/2012 10:11 AM, racqueldesign wrote:
> Hi Jesus,
>
> Thanks for telling about the getpeername but I first need to know the name of the socket. For example:
>
> void main()
> {
> sock_init_or_exit(1);
> http_init();
> tcp_reserveport(80);
> while (1)
> {
> http_handler();
> }
> }
>
> I am guessing the http_init() setup the socket. Am i right or am I missing something? I need my .cgi handler to get its client's ip address. Is there also a function the returns a pointer to the current socket? I think that might solve my problem with your advice.
>

Your CGI handler will be passed a HttpState pointer. Look at the
structure, the socket will be in there.



--
------
Scott G. Henion, Consultant
Web site: http://SHDesigns.org
------

Hi Raquel,

See this routine:

xmem void ip_to_bytes(longword ip){

ip1_ee = (byte)(ip >> 24);

ip2_ee = (byte)(ip >> 16);

ip3_ee = (byte)(ip >> 8);

ip4_ee = (byte)(ip);

}

xmem void analiza_ip_externa(tcp_Socket *s){

struct sockaddr sock_addr;

auto longword mi_ip;

auto unsigned char ip_externa[17];

if(0 == getpeername((tcp_Socket *) s,&sock_addr,NULL)){

ip_to_bytes(sock_addr.s_ip);

mi_ip = inet_addr(nip);

sprintf(ip_externa,"%d.%d.%d.%d",ip1_ee,ip2_ee,ip3_ee,ip4_ee);
}

}

Where ip1_ee,..,ip4_ee are global variables. As you can see all the info is
in the socket. If what you want is to know the IP address of the client that
is watching your Rabbit web site perhaps you can call this routine from
submit(HttpState* state) and using &state->s like your argument .

Hope this helps

Jesus

De: r... [mailto:r...] En
nombre de racqueldesign
Enviado el: viernes, 02 de noviembre de 2012 15:12
Para: r...
Asunto: [rabbit-semi] Re: HTTP server storing client's IP address?

Hi Jesus,

Thanks for telling about the getpeername but I first need to know the name
of the socket. For example:

void main()
{
sock_init_or_exit(1);
http_init();
tcp_reserveport(80);
while (1)
{
http_handler();
}
}

I am guessing the http_init() setup the socket. Am i right or am I missing
something? I need my .cgi handler to get its client's ip address. Is there
also a function the returns a pointer to the current socket? I think that
might solve my problem with your advice.

--- In r... ,
"Jesus Manuel Conejo Sarabia" wrote:
>
> Hi racquel,
>
> Yes!, using getpeername function from Dynamic C.
>
> Regards
>
> Jesus
>
> De: r...
[mailto:r... ]
En
> nombre de racqueldesign
> Enviado el: viernes, 02 de noviembre de 2012 13:45
> Para: r...
> Asunto: [rabbit-semi] HTTP server storing client's IP address?
>
> I have a HTTP Server running on the rabbit board but I don't know how to
> store the IP Address of any user that send an HTTP REQUEST. My question
is:
> is there a easy way for a server to acquire the IP address of its client?
>
Thanks guys,

So all I need to do is upon entering the cgi function is to call http_getSocket(state) then getpeername. For example,

CurrentSoctket = http_getSocket(state);
getpeername((sock_type *)CurrentSoctket, &socket_address, NULL);

Then get the client's ip address and port # from 'socket_address'
Thanks again.

--- In r..., "Jesus Manuel Conejo Sarabia" wrote:
>
>
>
>
>
> Hi Raquel,
>
> See this routine:
>
>
>
>
>
>
>
>
>
> xmem void ip_to_bytes(longword ip){
>
>
>
> ip1_ee = (byte)(ip >> 24);
>
> ip2_ee = (byte)(ip >> 16);
>
> ip3_ee = (byte)(ip >> 8);
>
> ip4_ee = (byte)(ip);
>
>
>
> }
>
>
>
> xmem void analiza_ip_externa(tcp_Socket *s){
>
> struct sockaddr sock_addr;
>
> auto longword mi_ip;
>
> auto unsigned char ip_externa[17];
>
>
>
> if(0 == getpeername((tcp_Socket *) s,&sock_addr,NULL)){
>
> ip_to_bytes(sock_addr.s_ip);
>
> mi_ip = inet_addr(nip);
>
> sprintf(ip_externa,"%d.%d.%d.%d",ip1_ee,ip2_ee,ip3_ee,ip4_ee);
> }
>
> }
>
>
>
> Where ip1_ee,..,ip4_ee are global variables. As you can see all the info is
> in the socket. If what you want is to know the IP address of the client that
> is watching your Rabbit web site perhaps you can call this routine from
> submit(HttpState* state) and using &state->s like your argument .
>
>
>
> Hope this helps
>
> Jesus
>
>
>
>
>
>
>
>
>
> De: r... [mailto:r...] En
> nombre de racqueldesign
> Enviado el: viernes, 02 de noviembre de 2012 15:12
> Para: r...
> Asunto: [rabbit-semi] Re: HTTP server storing client's IP address?
>
>
>
>
>
> Hi Jesus,
>
> Thanks for telling about the getpeername but I first need to know the name
> of the socket. For example:
>
> void main()
> {
> sock_init_or_exit(1);
> http_init();
> tcp_reserveport(80);
> while (1)
> {
> http_handler();
> }
> }
>
> I am guessing the http_init() setup the socket. Am i right or am I missing
> something? I need my .cgi handler to get its client's ip address. Is there
> also a function the returns a pointer to the current socket? I think that
> might solve my problem with your advice.
>
> --- In r... ,
> "Jesus Manuel Conejo Sarabia" wrote:
> >
> >
> >
> > Hi racquel,
> >
> > Yes!, using getpeername function from Dynamic C.
> >
> >
> >
> > Regards
> >
> > Jesus
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > De: r...
> [mailto:r... ]
> En
> > nombre de racqueldesign
> > Enviado el: viernes, 02 de noviembre de 2012 13:45
> > Para: r...
> > Asunto: [rabbit-semi] HTTP server storing client's IP address?
> >
> >
> >
> >
> >
> > I have a HTTP Server running on the rabbit board but I don't know how to
> > store the IP Address of any user that send an HTTP REQUEST. My question
> is:
> > is there a easy way for a server to acquire the IP address of its client?
>