Getting the Client IP Address and/or MAC Address

695 views
Skip to first unread message

Christopher Morris

unread,
Feb 22, 2012, 9:31:17 AM2/22/12
to cocoaht...@googlegroups.com
I am working on a project that uses CocoaHTTPServer and I need to identify clients by either their IP Address or preferably MAC Address.  I have to be able to know which device/computer is connecting to the server so that I can track state.  I have been looking through the code to see if there is a property on the HTTPConnection, etc. but can't find this information.  Is this possible?

Thanks in advance!

Chris

Yoann Gini

unread,
Feb 22, 2012, 11:30:28 AM2/22/12
to cocoaht...@googlegroups.com

Le 22 févr. 2012 à 15:31, Christopher Morris a écrit :

I am working on a project that uses CocoaHTTPServer and I need to identify clients by either their IP Address or preferably MAC Address.  I have to be able to know which device/computer is connecting to the server so that I can track state.  I have been looking through the code to see if there is a property on the HTTPConnection, etc. but can't find this information.  Is this possible?

You should learn more about TCP/IP and HTTP before asking something like this…

A MAC address is only available on the local network. If your software is used through the Internet, you can’t access to the client MAC address from the TCP stack

They have two kind of IP address, global unicast and private address reserved by RFC 1918. Again, if your software is used through the Internet, the client IP address will be changed by the NAT from a private address to a public one. So if you have two client behind the same NAT what do you do ?

Like ALL application using HTTP for the transport, you must implement a system of session ID in your client and your server.

Johnny Sung

unread,
Jun 16, 2013, 2:21:21 AM6/16/13
to cocoaht...@googlegroups.com
Hello, I know about TCP/IP is different from HTTP. It's not the same layer in OSI.
But my question is: How to get the client public IP when client connected to.

In CocoaHTTP, we can get the HTTP request header in 
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path

method. Due to HTTP pocket (Application layer), it does not contains client IP.

I think...I have to catch the client IP when GCDAsyncSocket Accept a connection.
But I don't know to do with the code.

Can you give me some advices?

Joe Francia

unread,
Jun 16, 2013, 4:38:45 PM6/16/13
to cocoaht...@googlegroups.com

You can subclass HTTPConnection and set the HTTPServer to use your subclass as its connectionClass.  Override -(id)initWithAsyncSocket:(GCDAsyncSocket *)newSocket configuration:(HTTPConfig *)aConfig (don't forget to call the superclass's method) and call [newSocket connectedHost] on the passed in GCDAsyncSocket.  You can stick the value in an ivar if you need it later, or you can just call [asyncSocket connectedHost] from anywhere else in your subclass.


I wouldn't use this as any sort of identifier, though, as it isn't going to give you very accurate data on who's connecting, as you're likely going to be getting the IP address of the NAT or proxy the user is behind instead, and all clients connecting through a NAT or a proxy will have the same IP address as every other client behind that same NAT or proxy.

Johnny Sung

unread,
Jun 24, 2013, 11:56:29 AM6/24/13
to cocoaht...@googlegroups.com, j...@joefrancia.com
Many thanks for Joe.
Because I use this server in local network.
I know I will get the same IP when client is behind the NAT. But it doesn't matter.

:D 

Dmitry Mikheev

unread,
Oct 21, 2013, 7:14:49 AM10/21/13
to cocoaht...@googlegroups.com, j...@joefrancia.com
Hi Joe, 
May I kindly ask you to clarify more - how to get MAC address? I understand that it will be available only in localhost. Basically, what I need is to get the MAC address of myself, opening "localhost" page. 
Thank You!!!

воскресенье, 16 июня 2013 г., 23:38:45 UTC+3 пользователь Joe Francia написал:

Joe Francia

unread,
Oct 31, 2013, 10:22:48 PM10/31/13
to Dmitry Mikheev, cocoaht...@googlegroups.com
As of iOS 7, all devices return the same value for the MAC, which is 02:00:00:00:00:00.  So unless you plan on never having your app run on anything after iOS 6, you probably should consider another solution.

On 21 October 2013 at 07:14:50, Dmitry Mikheev (mikhe...@gmail.com) wrote:

Hi Joe, 
May I kindly ask you to clarify more - how to get MAC address? I understand that it will be available only in localhost. Basically, what I need is to get the MAC address of myself, opening "localhost" page. 
Thank You!!!

×ÏÓËÒÅÓÅÎØÅ, 16 ÉÀÎÑ 2013 Ç., 23:38:45 UTC+3 ÐÏÌØÚÏ×ÁÔÅÌØ Joe Francia ÎÁÐÉÓÁÌ:

You can subclass HTTPConnection and set the HTTPServer to use your subclass as its connectionClass.  Override -(id)initWithAsyncSocket:(GCDAsyncSocket *)newSocket configuration:(HTTPConfig *)aConfig (don't forget to call the superclass's method) and call [newSocket connectedHost] on the passed in GCDAsyncSocket.  You can stick the value in an ivar if you need it later, or you can just call [asyncSocket connectedHost] from anywhere else in your subclass.


I wouldn't use this as any sort of identifier, though, as it isn't going to give you very accurate data on who's connecting, as you're likely going to be getting the IP address of the NAT or proxy the user is behind instead, and all clients connecting through a NAT or a proxy will have the same IP address as every other client behind that same NAT or proxy.




On June 16, 2013 at 2:21:23 AM, Johnny Sung (j7961...@gmail.com) wrote:
Hello, I know about TCP/IP is different from HTTP. It's not the same layer in OSI.
But my question is: How to get the client public IP when client connected to.

In CocoaHTTP, we can get the HTTP request header in 
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path

method. Due to HTTP pocket (Application layer), it does not contains client IP.

I think...I have to catch the client IP when GCDAsyncSocket Accept a connection.
But I don't know to do with the code.

Can you give me some advices?


--
You received this message because you are subscribed to the Google Groups "CocoaHTTPServer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoahttpserv...@googlegroups.com.
To post to this group, send email to cocoaht...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cocoahttpserver/1aeee460-f94f-49b1-aec5-efbe4cef0adb%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages