[From nobody Mon Mar 25 16:15:48 2013
Return-Path: &lt;peppe@erix.ericsson.se&gt;
Received: from [169.132.25.65] (HELO tahini.corp.idt.net)
 by mail.corp.idt.net (CommuniGate Pro SMTP 4.2)
 with ESMTP-TLS id 106343137 for serge@hq.idt.net;
 Fri, 01 Jul 2005 10:54:41 -0400
Received: from psmtp.com (exprod6mx118.postini.com [64.18.1.110])
 by tahini.corp.idt.net (Switch-3.1.7/Switch-3.1.7) with SMTP id j61EsdlJ020972
 for &lt;serge@hq.idt.net&gt;; Fri, 1 Jul 2005 10:54:40 -0400 (EDT)
Received: from source ([193.180.251.62]) by exprod6mx118.postini.com
 ([64.18.5.10]) with SMTP; Fri, 01 Jul 2005 09:54:38 CDT
Received: from esealmw129.eemea.ericsson.se (unknown [153.88.254.120])
 by mailgw4.ericsson.se (Symantec Mail Security) with ESMTP id 7DD18FEB
 for &lt;serge@hq.idt.net&gt;; Fri,  1 Jul 2005 16:54:37 +0200 (CEST)
Received: from esealmw127.eemea.ericsson.se ([153.88.254.171]) by
 esealmw129.eemea.ericsson.se with Microsoft SMTPSVC(6.0.3790.211); 
 Fri, 1 Jul 2005 16:54:36 +0200
Received: from super.du.uab.ericsson.se ([134.138.176.16]) by
 esealmw127.eemea.ericsson.se with Microsoft SMTPSVC(6.0.3790.211); 
 Fri, 1 Jul 2005 16:54:36 +0200
Received: from erix.ericsson.se (peppe@finwe [134.138.177.105])
 by super.du.uab.ericsson.se (8.10.1/8.10.1/erix-1.8) with ESMTP id
 j61EsZL07184
 for &lt;serge@hq.idt.net&gt;; Fri, 1 Jul 2005 16:54:36 +0200 (MET DST)
Sender: peppe@erix.ericsson.se
Message-ID: &lt;42C5592B.3CF818BB@erix.ericsson.se&gt;
Date: Fri, 01 Jul 2005 16:54:35 +0200
From: UAB L/K Peter Andersson &lt;peppe@erix.ericsson.se&gt;
X-Mailer: Mozilla 4.79 [en] (X11; U; SunOS 5.8 sun4u)
X-Accept-Language: en
MIME-Version: 1.0
To: Serge Aleynikov &lt;serge@hq.idt.net&gt;
Subject: Re: inets crashing in R10B-6
References: &lt;42BC843E.5070405@hq.idt.net&gt; &lt;42BC8B1D.9040007@hq.idt.net&gt;
 &lt;42BFACFF.7CAAF8A4@erix.ericsson.se&gt; &lt;42C009CA.8080103@hq.idt.net&gt;
 &lt;42C0131B.6EA44A70@erix.ericsson.se&gt; &lt;42C01489.2010303@hq.idt.net&gt;
Content-Type: multipart/mixed; boundary=&quot;------------0166B9705EF8CD54FD0F2DB9&quot;
X-OriginalArrivalTime: 01 Jul 2005 14:54:36.0338 (UTC)
 FILETIME=[CC7E7120:01C57E4C]
X-Brightmail-Tracker: AAAAAA==
X-pstn-levels: (S:99.90000/99.90000 R:95.9108 P:95.9108 M:97.0232 C:98.7678 )
X-pstn-settings: 4 (1.5000:1.5000) s gt3 gt2 gt1 r p m c 
X-pstn-addresses: from &lt;peppe@erix.ericsson.se&gt; [20/1] 


This is a multi-part message in MIME format.
--------------0166B9705EF8CD54FD0F2DB9
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


Hi Serge,

The fix we've made will be available with the nest open sourse release.
Until then, here's the modified file. Thanks for helping and have a
really good summer!

Best regards,
Peter

Serge Aleynikov wrote:
&gt; 
&gt; Yes, I verified that this works indeed.  I hope you'll be able to
&gt; release a patch soon since this work-around not very convenient, as the
&gt; inets' configuration script is not easily portable between computers.
&gt; 
&gt; Regards,
&gt; 
&gt; Serge
&gt; 
&gt; UAB L/K Peter Andersson wrote:
&gt; 
&gt; &gt; Hi Serge,
&gt; &gt;
&gt; &gt; We've been looking at this and we think we know what the problem is.
&gt; &gt; We're working on a fix, but we'd like to test it and check that there
&gt; &gt; are no other issues.
&gt; &gt;
&gt; &gt; Meanwhile a workaround for you would be to specify the BindAddress
&gt; &gt; config parameter explicitly instead of using *. That should hopefully
&gt; &gt; help, for now. Will you let us know if it does/doesn't?
&gt; &gt;
&gt; &gt; Best regards,
&gt; &gt; Peter
&gt; &gt;
&gt; &gt; Serge Aleynikov wrote:
&gt; &gt;
&gt; &gt;&gt;Not sure if this helps, but I confirmed this bug of inets crashing on
&gt; &gt;&gt;the following OS versions:
&gt; &gt;&gt;
&gt; &gt;&gt;Linux version 2.4.18-14smp (gcc version 3.2 20020903 (Red Hat Linux 8.0
&gt; &gt;&gt;3.2-7)) #1 SMP Wed Sep 4 12:34:47 EDT 2002
&gt; &gt;&gt;
&gt; &gt;&gt;Linux version 2.4.20 (gcc version 2.96 20000731 (Red Hat Linux 7.3
&gt; &gt;&gt;2.96-110)) #2 SMP Wed Jan 29 18:40:47 EST 2003
&gt; &gt;&gt;
&gt; &gt;&gt;and it is *not* crashing on:
&gt; &gt;&gt;
&gt; &gt;&gt;Linux version 2.6.8.1 (gcc version 3.2.3 20030502 (Red Hat Linux
&gt; &gt;&gt;3.2.3-20)) #2 SMP Tue Sep 28 16:04:54 EDT 2004
&gt; &gt;&gt;
&gt; &gt;&gt;I would appreciate any advice on how to work around this issue.
&gt; &gt;&gt;
&gt; &gt;&gt;Thanks.
&gt; &gt;&gt;
&gt; &gt;&gt;Serge
&gt; &gt;&gt;
&gt; &gt;&gt;UAB L/K Peter Andersson wrote:
&gt; &gt;&gt;
&gt; &gt;&gt;
&gt; &gt;&gt;&gt;Hi Serge,
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;Yes, we've made some updates in both kernel and inets with regards to
&gt; &gt;&gt;&gt;IPv6 handling. We had a resolver problem that could cause inets to
&gt; &gt;&gt;&gt;believe IPv6 was working properly when, in fact, there was no support
&gt; &gt;&gt;&gt;for IPv6 on the host.
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;The behaviour you get when starting inets looks bad. inets is supposed
&gt; &gt;&gt;&gt;to be able to fall back to IPv4 if IPv6 is not working properly. For
&gt; &gt;&gt;&gt;some reason, the fix we made seems to rather have introduced the problem
&gt; &gt;&gt;&gt;on your system. Weird. We'll have to look into this promptly!
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;What OS are you using?
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;Best regards,
&gt; &gt;&gt;&gt;Peppe
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;-----------------------
&gt; &gt;&gt;&gt;Erlang/OTP, Ericsson AB
&gt; &gt;&gt;&gt;-----------------------
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;Serge Aleynikov wrote:
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;I set up a trace on gen_tcp:listen, and see that httpd is trying to bind
&gt; &gt;&gt;&gt;&gt;to inet6, which is indeed the root of this problem on this host:
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;5&gt; dbg:tracer().
&gt; &gt;&gt;&gt;&gt;{ok,&lt;0.47.0&gt;}
&gt; &gt;&gt;&gt;&gt;6&gt; dbg:p(all,call).
&gt; &gt;&gt;&gt;&gt;{ok,[{matched,nonode@nohost,25}]}
&gt; &gt;&gt;&gt;&gt;7&gt; dbg:tpl(httpd_transport, listen, dbg:fun2ms(fun(_) -&gt; return_trace()
&gt; &gt;&gt;&gt;&gt;end)).
&gt; &gt;&gt;&gt;&gt;{ok,[{matched,nonode@nohost,0},{saved,1}]}
&gt; &gt;&gt;&gt;&gt;8&gt; dbg:tpl(gen_tcp, listen, dbg:fun2ms(fun(_) -&gt; return_trace() end)).
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;{ok,[{matched,nonode@nohost,1},{saved,1}]}
&gt; &gt;&gt;&gt;&gt;9&gt; httpd:start_link([&quot;../priv/inets.conf&quot;]).
&gt; &gt;&gt;&gt;&gt;(&lt;0.59.0&gt;) call
&gt; &gt;&gt;&gt;&gt;gen_tcp:listen(8080,[inet6,{ip,any},{packet,0},{active,false},{backlog,128},{reuseaddr,true}])
&gt; &gt;&gt;&gt;&gt;(&lt;0.59.0&gt;) returned from gen_tcp:listen/2 -&gt; {error,eafnosupport}
&gt; &gt;&gt;&gt;&gt;** exited: shutdown **
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;How can I force httpd not to use inet6?  Did some default setting change
&gt; &gt;&gt;&gt;&gt;in R10B-6?  I see this statement in the release notes, but not sure if
&gt; &gt;&gt;&gt;&gt;it is related:
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;  OTP-5576 When doing an inet6 name lookup on an IPv4 address it was
&gt; &gt;&gt;&gt;&gt;           possible to get an address on IPv4 format back. This has been
&gt; &gt;&gt;&gt;&gt;          corrected. Some other minor inconsistencies regarding IPv6
&gt; &gt;&gt;&gt;&gt;          name lookup have also been corrected.
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;Thanks,
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;Serge
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;Serge Aleynikov wrote:
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;Hi,
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;I upgraded Erlang to R10B-6 and for some reason inets keeps crashing at
&gt; &gt;&gt;&gt;&gt;&gt;startup.  Before upgrading it worked under R10B-5 with no problems.
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;&gt;erl
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;Erlang (BEAM) emulator version 5.4.8 [source] [hipe] [threads:0]
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;Eshell V5.4.8  (abort with ^G)
&gt; &gt;&gt;&gt;&gt;&gt;1&gt;httpd:start_link([&quot;../priv/inets.conf&quot;]).
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;=SUPERVISOR REPORT==== 24-Jun-2005::17:58:36 ===
&gt; &gt;&gt;&gt;&gt;&gt;    Supervisor: {local,httpd_instance_sup_8080}
&gt; &gt;&gt;&gt;&gt;&gt;    Context:    start_error
&gt; &gt;&gt;&gt;&gt;&gt;    Reason:     {{listen,eafnosupport},
&gt; &gt;&gt;&gt;&gt;&gt;                 {child,undefined,
&gt; &gt;&gt;&gt;&gt;&gt;                        {httpd_acceptor,any,8080},
&gt; &gt;&gt;&gt;&gt;&gt;                        {httpd_acceptor,
&gt; &gt;&gt;&gt;&gt;&gt;                            start_link,
&gt; &gt;&gt;&gt;&gt;&gt;                            [&lt;0.66.0&gt;,
&gt; &gt;&gt;&gt;&gt;&gt;                             ip_comm,
&gt; &gt;&gt;&gt;&gt;&gt;                             any,
&gt; &gt;&gt;&gt;&gt;&gt;                             8080,
&gt; &gt;&gt;&gt;&gt;&gt;                             httpd_conf_8080,
&gt; &gt;&gt;&gt;&gt;&gt;                             silence]},
&gt; &gt;&gt;&gt;&gt;&gt;                        permanent,
&gt; &gt;&gt;&gt;&gt;&gt;                        1000,
&gt; &gt;&gt;&gt;&gt;&gt;                        worker,
&gt; &gt;&gt;&gt;&gt;&gt;                        [httpd_acceptor]}}
&gt; &gt;&gt;&gt;&gt;&gt;...
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;Here is the content of &quot;../priv/inets.conf&quot;:
&gt; &gt;&gt;&gt;&gt;&gt;------BEGIN---------
&gt; &gt;&gt;&gt;&gt;&gt;BindAddress    *
&gt; &gt;&gt;&gt;&gt;&gt;Port           8080
&gt; &gt;&gt;&gt;&gt;&gt;ServerName     wwwproxy
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;ServerRoot     ../priv
&gt; &gt;&gt;&gt;&gt;&gt;DocumentRoot   ../priv/docroot
&gt; &gt;&gt;&gt;&gt;&gt;Modules        mod_alias mod_auth mod_actions mod_include mod_dir
&gt; &gt;&gt;&gt;&gt;&gt;mod_get mod_head
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;DirectoryIndex index.html
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;ErlScriptAlias /erl io io_lib server_mgr
&gt; &gt;&gt;&gt;&gt;&gt;------END---------
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;Any idea how to configure it in order to avoid this
&gt; &gt;&gt;&gt;&gt;&gt;{listen,eafnosupport} &quot;address family not supported by protocol family&quot;
&gt; &gt;&gt;&gt;&gt;&gt;error?
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;&gt;Serge
&gt; &gt;&gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;
&gt; &gt;&gt;&gt;&gt;--
&gt; &gt;&gt;&gt;&gt;Serge Aleynikov
&gt; &gt;&gt;&gt;&gt;R&amp;D Telecom, IDT Corp.
&gt; &gt;&gt;&gt;&gt;Tel: (973) 438-3436
&gt; &gt;&gt;&gt;&gt;Fax: (973) 438-1464
&gt; &gt;&gt;&gt;&gt;serge@hq.idt.net

-- 
-----------------------
Erlang/OTP, Ericsson AB
-----------------------
--------------0166B9705EF8CD54FD0F2DB9
Content-Type: text/plain; charset=us-ascii;
 name=&quot;http_transport.erl&quot;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename=&quot;http_transport.erl&quot;

%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the &quot;License&quot;); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an &quot;AS IS&quot;
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%% 
%%     $Id$
%
-module(http_transport).

-export([start/1, connect/2, listen/2, listen/3, accept/2, accept/3, close/2,
	 send/3, controlling_process/3, setopts/3,
	 peername/2, resolve/0]).

-include(&quot;http.hrl&quot;).

%%%=========================================================================
%%%  Internal application API
%%%=========================================================================

%%-------------------------------------------------------------------------
%% start(SocketType) -&gt; ok | {error, Reason}
%%      SocketType - ip_comm | {ssl, _}  
%%                                   
%% Description: Makes sure inet_db or ssl is started. 
%%-------------------------------------------------------------------------
start(ip_comm) -&gt;
    case inet_db:start() of
	{ok, _} -&gt;
	    ok;
	{error, {already_started, _}} -&gt;
	    ok;
	Error -&gt;
	    Error
    end;
start({ssl, _}) -&gt;
    case ssl:start() of
	ok -&gt;
	    ok;
	{ok, _} -&gt;
	    ok;
	{error, {already_started,_}} -&gt;
	    ok;
	Error -&gt;
	    Error
    end.

%%-------------------------------------------------------------------------
%% connect(HTTPRequest, IPV6) -&gt; ok | {error, Reason}
%%      HTTPRequest - #request{}
%%      IPV6 - disabled | enabled
%%                                   
%% Description: Connects to the Host and Port specified in HTTPRequest.
%%		uses ipv6 if possible.
%%-------------------------------------------------------------------------
connect(#request{scheme = http, address = {Host, Port}}, enabled) -&gt;
    {Opts, NewHost} = 
	case (catch inet:getaddr(Host, inet6)) of
	    {ok, IPAddr} -&gt;
		{[binary, {packet, 0}, {active, false},
		  {reuseaddr,true}, inet6], IPAddr};
	    _ -&gt;
		{[binary, {packet, 0}, {active, false},
		  {reuseaddr,true}], Host}
	end,
    gen_tcp:connect(NewHost, Port, Opts);

connect(#request{scheme = http, address = {Host, Port}}, disabled) -&gt;
    Opts = [binary, {packet, 0}, {active, false}, {reuseaddr,true}],
    gen_tcp:connect(Host, Port, Opts);

connect(#request{scheme = https, settings = Settings,
		 address = {Host, Port}}, _) -&gt;
    Opts = [binary, {active, false}] ++ Settings#http_options.ssl,
    ssl:connect(Host, Port, Opts).

%%-------------------------------------------------------------------------
%% listen(SocketType, Port) -&gt; ok | {error, Reason}
%%      SocketType - ip_comm | {ssl, SSLConfig}  
%%      Port - integer()                             
%%
%% Description: Sets up socket to listen on the port Port on the local
%% host using either gen_tcp or ssl. In the gen_tcp case the port
%% might allready have been initiated by a wrapper-program and is
%% given as an Fd that can be retrieved by init:get_argument. The
%% reason for this to enable a HTTP-server not runnig as root to use
%% port 80.
%%-------------------------------------------------------------------------
listen(SocketType, Port) -&gt;
    listen(SocketType, undefined, Port).

listen(ip_comm, Addr, Port) -&gt;
    FdName = list_to_atom(&quot;httpd_&quot; ++ integer_to_list(Port)),
    {NewPort, Opt} =
	case init:get_argument(FdName) of
	    {ok, [[FdStr]]} -&gt;
		Fd = list_to_integer(FdStr),
		{0,
		 sock_opt(ip_comm, Addr, [{backlog, 128}, 
					  {reuseaddr,true}, {fd,Fd}])};
	    error -&gt;
		{Port,
		 sock_opt(ip_comm, Addr, [{backlog, 128}, {reuseaddr, true}])}
	end,
    gen_tcp:listen(NewPort, Opt);

listen({ssl, SSLConfig} = Ssl, Addr, Port) -&gt;
    Opt = sock_opt(Ssl, Addr, SSLConfig),
    ssl:listen(Port, Opt).

%%-------------------------------------------------------------------------
%% accept(SocketType, ListenSocket) -&gt; ok | {error, Reason}
%% accept(SocketType, ListenSocket, Timeout) -&gt; ok | {error, Reason}
%%   SocketType - ip_comm | {ssl, SSLConfig}  
%%   ListenSocket - socket()    
%%   Timeout - infinity | integer() &gt;= 0
%%                                   
%% Description: Accepts an incoming connection request on a listen socket,
%% using either gen_tcl or ssl.
%%-------------------------------------------------------------------------
accept(SocketType, ListenSocket) -&gt;
    accept(SocketType, ListenSocket, infinity).
accept(ip_comm, ListenSocket, Timeout) -&gt;
    gen_tcp:accept(ListenSocket, Timeout);
accept({ssl,_SSLConfig}, ListenSocket, Timeout) -&gt;
    ssl:accept(ListenSocket, Timeout).

%%-------------------------------------------------------------------------
%% controlling_process(SocketType, Socket, NewOwner) -&gt; ok | {error, Reason}
%%   SocketType - ip_comm | {ssl, _}  
%%   Socket - socket()        
%%   NewOwner - pid()
%%                                
%% Description: Assigns a new controlling process to Socket. 
%%-------------------------------------------------------------------------
controlling_process(ip_comm, Socket, NewOwner) -&gt;
    gen_tcp:controlling_process(Socket, NewOwner);
controlling_process({ssl, _}, Socket, NewOwner) -&gt;
    ssl:controlling_process(Socket, NewOwner).

%%-------------------------------------------------------------------------
%% setopts(RequestType, Socket, Options) -&gt; ok | {error, Reason}
%%     RequestType - http | https
%%     Socket - socket()
%%     Options - list()                              
%% Description: Sets one or more options for a socket, using either
%% gen_tcl or ssl.
%%-------------------------------------------------------------------------
setopts(http, Socket, Options) -&gt;
    inet:setopts(Socket,Options);
setopts(ip_comm, Socket, Options) -&gt;
    setopts(http, Socket,Options);

setopts({ssl, _}, Socket, Options) -&gt;
    setopts(https, Socket, Options);
setopts(https, Socket, Options) -&gt;
    ssl:setopts(Socket, Options).

%%-------------------------------------------------------------------------
%% send(RequestOrSocketType, Socket, Message) -&gt; ok | {error, Reason}
%%     RequestOrSocketType - http | https | ip_comm | {ssl, _}
%%     Socket - socket()
%%     Message - list() | binary()                           
%% Description: Sends a packet on a socket, using either gen_tcp or ssl.
%%-------------------------------------------------------------------------
send(http, Socket, Message) -&gt;
    gen_tcp:send(Socket, Message);
send(ip_comm, S, M) -&gt;
    send(http, S, M);

send({ssl, _}, S, M) -&gt;
    send(https, S, M);
send(https, Socket, Message) -&gt;
    ssl:send(Socket, Message).

%%-------------------------------------------------------------------------
%% close(RequestOrSocketType, Socket) -&gt; ok | {error, Reason}
%%     RequestOrSocketType - http | https | ip_comm | {ssl, _}
%%     Socket - socket()  
%%                                   
%% Description: Closes a socket, using either gen_tcp or ssl.
%%-------------------------------------------------------------------------
close(http, Socket) -&gt;
    gen_tcp:close(Socket);
close(ip_comm, S) -&gt;
    close(http, S);

close({ssl, _}, S) -&gt;
    close(https, S);
close(https,Socket) -&gt;
    ssl:close(Socket).

%%-------------------------------------------------------------------------
%% peername(RequestOrSocketType, Socket) -&gt; ok | {error, Reason}
%%     RequestOrSocketType - http | https | ip_comm | {ssl, _}
%%     Socket - socket() 
%%                          
%% Description: Returns the address and port for the other end of a connection,
%% usning either gen_tcp or ssl.
%%-------------------------------------------------------------------------
peername(ip_comm, Socket) -&gt;
    peername(http, Socket);

peername(http, Socket) -&gt;
    case inet:peername(Socket) of
	{ok,{{A, B, C, D}, Port}} -&gt;
	    PeerName = integer_to_list(A)++&quot;.&quot;++integer_to_list(B)++&quot;.&quot;++
		integer_to_list(C)++&quot;.&quot;++integer_to_list(D),
	    {Port, PeerName};
	{ok,{{A, B, C, D, E, F, G, H}, Port}} -&gt;
	    PeerName =  http_util:integer_to_hexlist(A) ++ &quot;:&quot;++  
		http_util:integer_to_hexlist(B) ++ &quot;:&quot; ++  
		http_util:integer_to_hexlist(C) ++ &quot;:&quot; ++ 
		http_util:integer_to_hexlist(D) ++ &quot;:&quot; ++  
		http_util:integer_to_hexlist(E) ++ &quot;:&quot; ++  
		http_util:integer_to_hexlist(F) ++ &quot;:&quot; ++  
		http_util:integer_to_hexlist(G) ++&quot;:&quot;++  
		http_util:integer_to_hexlist(H),
	    {Port, PeerName};
	{error, _} -&gt;
	    {-1, &quot;unknown&quot;}
    end;

peername({ssl,_SSLConfig}, Socket) -&gt;
    peername(https, Socket);

peername(https, Socket) -&gt;
    case ssl:peername(Socket) of
	{ok,{{A, B, C, D}, Port}} -&gt;
	    PeerName = integer_to_list(A)++&quot;.&quot;++integer_to_list(B)++&quot;.&quot;++
		integer_to_list(C)++&quot;.&quot;++integer_to_list(D),
	    {Port, PeerName};
	{error, _} -&gt;
	    {-1, &quot;unknown&quot;}
    end.


%%-------------------------------------------------------------------------
%% resolve() -&gt; HostName
%%     HostName - string()
%%     
%% Description: Returns the local hostname. 
%%-------------------------------------------------------------------------
resolve() -&gt;
    {ok, Name} = inet:gethostname(),
    Name.


%%%========================================================================
%%% Internal functions
%%%========================================================================

%% Address any comes from directive: BindAddress &quot;*&quot;
sock_opt(ip_comm, any = Addr, Opt) -&gt; 
    sock_opt1([{ip, Addr} | Opt]);
sock_opt(ip_comm, undefined, Opt) -&gt; 
    sock_opt1(Opt);
sock_opt(_, any = Addr, Opt) -&gt;
    sock_opt2([{ip, Addr} | Opt]);
sock_opt(_, undefined, Opt) -&gt;
    sock_opt2(Opt);
sock_opt(_, Addr, Opt) when size(Addr) == 4 -&gt; 
    sock_opt2([{ip, Addr} | Opt]);
sock_opt(ip_comm, Addr, Opt) -&gt; 
    sock_opt2([inet6, {ip, Addr} | Opt]);
sock_opt(_, Addr, Opt) -&gt;
    sock_opt2([{ip, Addr} | Opt]).

sock_opt1(Opt) -&gt;
    case has_inet6_supported() of
	yes -&gt;
	    sock_opt2([inet6 | Opt]);
	no -&gt;
	    sock_opt2(Opt)
    end.

sock_opt2(Opt) -&gt;
    [{packet, 0}, {active, false} | Opt].

has_inet6_supported() -&gt;
    case (catch inet:getaddr(&quot;localhost&quot;, inet6)) of
	{ok, _} -&gt;
	    yes;
	_ -&gt;
	    no
    end.
		    

--------------0166B9705EF8CD54FD0F2DB9--


]