Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Getting sender's IP address from CFHTTPMessageRef
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting sender's IP address from CFHTTPMessageRef



Thanks Jens

I'm using BSD sockets so couldn't use CFSocketCopyPeerAddress. Here's the solution (with error checking removed)

socklen_t addrLen;
struct sockaddr addr;
char addrstr[INET6_ADDRSTRLEN];

requestFD = accept(listenFD, &addr, &addrLen);
if (addr.sa_family == AF_INET)
{
struct sockaddr_in temp = *(struct sockaddr_in *)addr.sa_data;
inet_ntop(AF_INET, &temp, addrstr, sizeof(addrstr));
}
else if (addr.sa_family == AF_INET6)
{
struct sockaddr_in6 temp = *(struct sockaddr_in6 *)addr.sa_data;
inet_ntop(AF_INET6, &temp, addrstr, sizeof(addrstr));
}

callerAddress = [[NSString alloc] initWithBytes: addrstr length: addrLen encoding: NSUTF8StringEncoding];

- Ken

On May 29, 2011, at 4:43 PM, Jens Alfke wrote:


On May 29, 2011, at 2:38 AM, Ken Tozier wrote:

I'm writing a web server class and have everything working nicely, but want to get my hands on the sender's IP address, so my server can track traffic from various sources. I read the CFHTTPMessage docs and there doesn't seem to be any accessors for request sender address. I tried NSLog(@headers: %@", CFHTTPMessageCopyAllHeaderFields(message)) but didn't see anything. Is there something I'm missing? Or do I have to resort to BSD socket calls on the file descriptor?

The sender’s address is an attribute of the TCP connection, so it would be redundant to put it in an HTTP header. So yes, you’ll need to ask the socket for it (through whatever API you’re using to access the socket.) If you’re using CFSocket, the call you want is CFSocketCopyPeerAddress.

—Jens

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Web-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Getting sender's IP address from CFHTTPMessageRef (From: Ken Tozier <email@hidden>)
 >Re: Getting sender's IP address from CFHTTPMessageRef (From: Jens Alfke <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.