chesterpolo / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

Get User Ip #106

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
1.How to Get User login IP?
2.How to request User login in ?

Original issue reported on code.google.com by flair...@gmail.com on 2 Dec 2009 at 1:13

GoogleCodeExporter commented 9 years ago
1. This is the helper function I use:

char *mg_GetClientIPAddress (const struct mg_request_info *ri)
{
    static char clientIPAddress[50+1] = {0};
    SOCKADDR_IN sa;

    bzero (clientIPAddress, sizeof(clientIPAddress));
    bzero (&sa, sizeof(sa));

    sa.sin_addr.S_un.S_addr = htonl(ri->remote_ip);
    sa.sin_port = htons(ri->remote_port);
    sa.sin_family = AF_INET;
    (void)snprintf (clientIPAddress, sizeof(clientIPAddress), "%s",
        inet_ntoa(sa.sin_addr));

    return (clientIPAddress);
}

2. check out authentication.c in the examples area.

Original comment by PKuo...@gmail.com on 3 Dec 2009 at 5:07

GoogleCodeExporter commented 9 years ago
Please note that mg_GetClientIPAddress uses static buffer for the result.
Since Mongoose is threaded, you are risking here.
I would modify that function slightly to make it safe by passing a buffer to it:
void mg_GetClientIPAddress (const struct mg_request_info *ri, char *buf, size_t 
len)

Original comment by valenok on 3 Dec 2009 at 10:03

GoogleCodeExporter commented 9 years ago

Original comment by valenok on 25 Feb 2010 at 12:45