• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService


  • Subject: Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService
  • From: Josh Graessley <email@hidden>
  • Date: Mon, 13 Apr 2015 13:44:28 -0700

> On Apr 13, 2015, at 1:25 PM, Jens Alfke <email@hidden> wrote:
>
>
>> On Apr 13, 2015, at 10:12 AM, Jeff Johnson <email@hidden> wrote:
>>
>> I want the host and port is to send a "Host" header. I might be able to get away without it, but I'd prefer to adhere to standards.
>
> I ended up having to implement this earlier today; here’s the code I came up with. “_inputStream” is an NSInputStream opened on a TCP socket. Disclaimer: I haven’t tested it much yet, and not at all with IPv6 addresses.
>
> - (NSString*) remoteHost {
>     // First recover the socket handle from the stream:
>     NSData* handleData = CFBridgingRelease(CFReadStreamCopyProperty(
>                                                   (__bridge CFReadStreamRef)_inputStream,
>                                                   kCFStreamPropertySocketNativeHandle));
>     if (!handleData || handleData.length != sizeof(CFSocketNativeHandle))
>         return nil;
>     CFSocketNativeHandle socketHandle = *(const CFSocketNativeHandle*)handleData.bytes;
>     // Get the remote/peer address in binary form:
>     struct sockaddr_in addr;
>     unsigned addrLen = sizeof(addr);
>     if (getpeername(socketHandle, (struct sockaddr*)&addr,&addrLen) < 0)
>         return nil;
>     // Format it in readable (e.g. dotted-quad) form, with the port number:
>     char nameBuf[INET6_ADDRSTRLEN];
>     if (inet_ntop(addr.sin_family, &addr.sin_addr, nameBuf, (socklen_t)sizeof(nameBuf)) == NULL)
>         return nil;
>     return [NSString stringWithFormat: @"%s:%hu", nameBuf, ntohs(addr.sin_port)];
> }

This seems sub-optimal as there can be many hostnames that resolve to a single address. A reverse-lookup of the address will yield one of those hostnames, but it might not be the one you’re looking for.

Also, what does this do if you connect through a proxy? The native handle could be a socket to a SOCKS (or other) proxy, right?

A radar requesting a better way to do this might be a good idea. If you file one, please forward me the radar #, thanks.

-josh


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

This email sent to email@hidden


  • Follow-Ups:
    • Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService
      • From: Jeff Johnson <email@hidden>
References: 
 >Can't get remote host and port after CFStreamCreatePairWithSocketToNetService (From: Jeff Johnson <email@hidden>)
 >Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService (From: Jens Alfke <email@hidden>)
 >Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService (From: Jeff Johnson <email@hidden>)
 >Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService
  • Next by Date: Re: Best practices for GCD-based socket I/O with SSL support?
  • Previous by thread: Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService
  • Next by thread: Re: Can't get remote host and port after CFStreamCreatePairWithSocketToNetService
  • Index(es):
    • Date
    • Thread