PJSIP TLS connect() error: End of file (PJ_EEOF) [code=70016] - ios

I'm getting this EOF error when attempting to register my iOS project with my Asterisk server. Without TLS everything is working. So I'm now going through the process of enabling TLS. I've recompiled the PJSIP libraries and configured the Asterisk server. I believe that is all correct. I can see the traffic occurring in an encrypted form over port 5161 so that appears to be correct as well.
1) What I'm curious about is what could cause this error? Could it be the certificates that I generated? Based on the fact that I see encrypted traffic on wireshark but not in the Asterisk pjsip logger is making me thing it's the cert?
2) Second question is how do you create a certificate for a client who's hostname will be unknown (i.e. their IP will obviously change as they roam on cellular or to different WIFI networks). When I created them according to the Asterisk tutorial (https://wiki.asterisk.org/wiki/display/AST/Secure+Calling+Tutorial) it required a value for the -C parameter to specify the client hostname. Which again this would assumed to be dynamic.
Here's the log output (personal info [REDACTED])...
15:26:15.857 os_core_unix.c !pjlib 2.6 for POSIX initialized
15:26:15.858 sip_endpoint.c .Creating endpoint instance...
15:26:15.859 pjlib .select() I/O Queue created (0x11e809228)
15:26:15.859 sip_endpoint.c .Module "mod-msg-print" registered
15:26:15.859 sip_transport. .Transport manager created.
15:26:15.859 pjsua_core.c .PJSUA state changed: NULL --> CREATED
15:26:15.859 sip_endpoint.c .Module "mod-pjsua-log" registered
15:26:15.859 sip_endpoint.c .Module "mod-tsx-layer" registered
15:26:15.859 sip_endpoint.c .Module "mod-stateful-util" registered
15:26:15.859 sip_endpoint.c .Module "mod-ua" registered
15:26:15.859 sip_endpoint.c .Module "mod-100rel" registered
15:26:15.859 sip_endpoint.c .Module "mod-pjsua" registered
15:26:15.860 sip_endpoint.c .Module "mod-invite" registered
15:26:15.888 coreaudio_dev. .. dev_id 0: iPhone IO device (in=1, out=1) 8000Hz
15:26:15.888 coreaudio_dev. ..core audio initialized
15:26:15.888 pjlib ..select() I/O Queue created (0x11e16fc28)
15:26:15.888 speex_codec.c ..Adjusting quality to 5 for uwb
15:26:15.889 conference.c ..Creating conference bridge with 12 ports
15:26:15.889 Master/sound ..Using delay buffer with WSOLA.
15:26:15.907 sip_endpoint.c .Module "mod-evsub" registered
15:26:15.907 sip_endpoint.c .Module "mod-presence" registered
15:26:15.907 evsub.c .Event pkg "presence" registered by mod-presence
15:26:15.907 sip_endpoint.c .Module "mod-mwi" registered
15:26:15.907 evsub.c .Event pkg "message-summary" registered by mod-mwi
15:26:15.907 sip_endpoint.c .Module "mod-refer" registered
15:26:15.907 evsub.c .Event pkg "refer" registered by mod-refer
15:26:15.907 sip_endpoint.c .Module "mod-pjsua-pres" registered
15:26:15.907 sip_endpoint.c .Module "mod-pjsua-im" registered
15:26:15.907 sip_endpoint.c .Module "mod-pjsua-options" registered
15:26:15.908 pjsua_core.c .1 SIP worker threads created
15:26:15.908 pjsua_core.c .pjsua version 2.6 for iOS-10.2.1/arm-iPad6,7/iOS-SDK initialized
15:26:15.908 pjsua_core.c .PJSUA state changed: CREATED --> INIT
15:26:15.910 tlstp:5161 SIP TLS listener is ready for incoming connections at 10.200.154.118:5161
15:26:15.910 pjsua_core.c PJSUA state changed: INIT --> STARTING
15:26:15.910 sip_endpoint.c .Module "mod-unsolicited-mwi" registered
15:26:15.910 pjsua_core.c .PJSUA state changed: STARTING --> RUNNING
15:26:15.910 pjsua_acc.c Adding account: id=sip:[REDACTED]#[REDACTED]
15:26:15.910 pjsua_acc.c .Account sip:[REDACTED]#[REDACTED] added with id 0
15:26:15.910 pjsua_acc.c .Acc 0: setting registration..
15:26:15.912 tlsc0x11e18882 ..TLS client transport created
15:26:15.912 tlsc0x11e18882 ..TLS transport 10.200.154.118:58635 is connecting to [REDACTED]:5161...
15:26:15.912 pjsua_acc.c ..Contact for acc 0 updated: <sip:[REDACTED]#10.200.154.118:58635;transport=TLS;ob>;reg-id=1;+sip.instance="<urn:uuid:00000000-0000-0000-0000-00009c3fe9b2>"
15:26:15.912 endpoint ..Request msg REGISTER/cseq=10712 (tdta0x11e950400) created.
15:26:15.913 tsx0x11e9524a8 ...Transaction created for Request msg REGISTER/cseq=10713 (tdta0x11e950400)
15:26:15.913 tsx0x11e9524a8 ..Sending Request msg REGISTER/cseq=10713 (tdta0x11e950400) in state Null
15:26:15.913 sip_resolve.c ...Target '[REDACTED]:5161' type=TLS resolved to '[REDACTED]:5161' type=TLS (TLS transport)
15:26:15.913 pjsua_core.c ...TX 730 bytes Request msg REGISTER/cseq=10713 (tdta0x11e950400) to TLS [REDACTED]:5161:
REGISTER sip:[REDACTED]:5161;transport=tls SIP/2.0
Via: SIP/2.0/TLS 10.200.154.118:58635;rport;branch=z9hG4bKPjYEl90guBh1gvKz2dDJZlgxzyy7AzLMpx;alias
Max-Forwards: 70
From: <sip:[REDACTED]#[REDACTED]>;tag=V6bPvLX5S0jBnpssQQBhG1W2xhXz01gj
To: <sip:[REDACTED]#[REDACTED]>
Call-ID: 5h.iiZFsYAX2vtMCNx.ZmH9NsicZoQEX
CSeq: 10713 REGISTER
User-Agent: [REDACTED]
Supported: outbound, path
Contact: <sip:[REDACTED]#10.200.154.118:58635;transport=TLS;ob>;reg-id=1;+sip.instance="<urn:uuid:00000000-0000-0000-0000-00009c3fe9b2>"
Expires: 300
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Content-Length: 0
--end msg--
15:26:15.914 tsx0x11e9524a8 ...State changed from Null to Calling, event=TX_MSG
15:26:15.914 pjsua_acc.c ..Acc 0: Registration sent
15:26:16.001 tlsc0x11e18882 !TLS connect() error: End of file (PJ_EEOF) [code=70016]
15:26:16.001 tsx0x11e9524a8 Failed to send Request msg REGISTER/cseq=10713 (tdta0x11e950400)! err=70016 (End of file (PJ_EEOF))
15:26:16.001 tsx0x11e9524a8 State changed from Calling to Terminated, event=TRANSPORT_ERROR
15:26:16.001 pjsua_acc.c ..SIP registration failed, status=503 (End of file (PJ_EEOF))
15:26:16.001 pjsua_acc.c ..Scheduling re-registration retry for acc 0 in 2 seconds..
15:26:16.001 PJSIPInterface ..AccountID 0 reg_state=503
15:26:16.001 tlsc0x11e18882 TLS send() error, sent=-70016
15:26:16.001 pjsua_acc.c Disconnected notification for transport tlsc0x11e188828
15:26:16.001 tsx0x11e9524a8 Timeout timer event
15:26:16.001 tsx0x11e9524a8 .State changed from Terminated to Destroyed, event=TIMER
15:26:16.001 tdta0x11e95040 ..Destroying txdata Request msg REGISTER/cseq=10713 (tdta0x11e950400)
15:26:16.001 tsx0x11e9524a8 Transaction destroyed!
15:26:16.001 tlsc0x11e18882 TLS transport destroyed with reason 70016: End of file (PJ_EEOF)
Here's my transport setup code in my iOS app...
pjsua_transport_config tlsTransportConfig;
pjsua_transport_config_default(&tlsTransportConfig);
tlsTransportConfig.port = 5161;
tlsTransportConfig.tls_setting.ca_list_file = pj_str((char*)[[[NSBundle mainBundle] pathForResource:#"ca.crt" ofType:#"pem"] cStringUsingEncoding:NSUTF8StringEncoding]);
tlsTransportConfig.tls_setting.cert_file = pj_str((char*)[[[NSBundle mainBundle] pathForResource:#"myapp.crt" ofType:#"pem"] cStringUsingEncoding:NSUTF8StringEncoding]);
tlsTransportConfig.tls_setting.privkey_file = pj_str((char*)[[[NSBundle mainBundle] pathForResource:#"myapp.key" ofType:#"pem"] cStringUsingEncoding:NSUTF8StringEncoding]);
tlsTransportConfig.tls_setting.password = pj_str("[REDACTED]");
tlsTransportConfig.tls_setting.verify_client = PJ_TRUE;
tlsTransportConfig.tls_setting.verify_server = PJ_TRUE;
tlsTransportConfig.tls_setting.method = PJSIP_SSLV2_METHOD;
status = pjsua_transport_create(PJSIP_TRANSPORT_TLS, &tlsTransportConfig, NULL);

In case anyone else runs into this issue. As per a tutorial I was following I had set the tls method to PJSIP_SSLV2_METHOD as you can see in my code above. However when I changed this to PJSIP_TLSV1_METHOD everything started working. This also requires this to be changed in the PJSIP settings for the SSL method as well.
I'm not sure why SSLV2 didn't work but tlsv1 works just fine.

I think this also might relate to the server side. If the server you are using is Opensips it might be that SIP message is too fragmented and OpenSIPS closes the connection to prevent packets fragmentation attacks, default value is 4.
Changing the .cfg file on the server option should fix the issue
modparam("proto_tls", "tls_max_msg_chunks", 8)

Related

Why is the SIP ACK sent from our PBX not received by Twilio?

Our outbound calls through Twilio are all being dropped after 32 seconds (inbound calls are fine). The error we receive from Twilio is Error 32022 "Ack not received from your SIP endpoint." I have been going through tutorials and the RFC but I must really be missing something.
The calls are encrypted TLS. SIP ALG has been disabled in our gateway router and our Grandstream PBX is configured for NAT.
Any help is appreciated!
Here is the 200 OK and the ACK that never arrives:
<--- Received SIP response (1177 bytes) from TLS:54.172.60.2:5061 --->
SIP/2.0 200 OK
CSeq: 2218 INVITE
Call-ID: b9e1de31-ce44-4d34-b63b-4c8636e151bf
From: <sip:+13049487110#XXXX>;tag=814fce4e-a1dc-49c7-a917-59496ad182a7
To: <sip:+15132607811#xxxx.pstn.twilio.com>;tag=84858964_c3356d0b_d52c7a82-314f-4be6-88f4-e73110bdbdd8
Via: SIP/2.0/TLS 74.195.14.236:5061;received=74.195.14.236;rport=52191;branch=z9hG4bKPj42ac7abe-f4b7-40dc-a314-8c6a90584481;alias
Record-Route: <sip:54.172.60.2:5060;r2=on;lr;twnat=sip:74.195.14.236:52191>
Record-Route: <sip:54.172.60.2:5061;transport=tls;r2=on;lr;twnat=sip:74.195.14.236:52191>
Server: Twilio
Contact: <sip:172.25.75.195:5060>
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,NOTIFY
Content-Type: application/sdp
X-Twilio-CallSid: CAb39a1e013e6ad6c03bd5bd2827612646
X-Twilio-TlsPolicy: TLSv1.0+
Content-Length: 362
v=0
o=root 1866335776 1866335776 IN IP4 172. ...
<--- Transmitting SIP request (1010 bytes) to TLS:54.172.60.2:5061 --->
ACK sip:54.172.60.2:5061;transport=TLS SIP/2.0
Via: SIP/2.0/TLS 74.195.14.236:5061;rport;branch=z9hG4bKPj483e2dff-e510-4908-89ee-25ec200a4072;alias
From: <sip:+13049487110#XXXX>;tag=814fce4e-a1dc-49c7-a917-59496ad182a7
To: <sip:+15132607811#xxxx.pstn.twilio.com>;tag=84858964_c3356d0b_d52c7a82-314f-4be6-88f4-e73110bdbdd8
Call-ID: b9e1de31-ce44-4d34-b63b-4c8636e151bf
CSeq: 2218 ACK
Route: <sip:54.172.60.2:5061;transport=tls;lr;r2=on;twnat=sip:74.195.14.236:52191>
Route: <sip:54.172.60.2:5060;lr;r2=on;twnat=sip:74.195.14.236:52191>
...
The ACK request s addressed incorrectly: per 3261 with loose routing it should be addressed to the Contact URI, but sent to the topmost Route (built from the bottommost Record-Route, the route-set in your ACK appears to be correct). Correct the request URI for the ACK, that should help.

SRTP Issue : PJSIP Error initializing media channel: Not Acceptable Here [status=170488]

I'm trying to get SRTP going in my iOS application using PJSIP. I have TLS working and without SRTP I can make and receive calls. However with SRTP I'm getting this odd 488 error on the INVITE. It's not able to initialize the media.
I've read other articles mentioning about the codecs. But I've insured that the codes being used by my Asterisk server and those compiled with PJSIP library on my iOS app are the same. The only thing I'm seeing here is that I'm enabled the crypto and PJSIP isn't liking it. Any thoughts?
INVITE sip:[REDACTED]#[REDACTED]:47229;transport=TLS;ob SIP/2.0
Via: SIP/2.0/TLS [REDACTED]:5161;rport;branch=z9hG4bKPj8ea1a332-0748-438f-ae74-5d17b038891d;alias
From: "Test" <sip:asterisk#172.31.18.138>;tag=7c3663cb-b5f5-4762-8526-8425d18b2466
To: <sip:[REDACTED]#[REDACTED];ob>
Contact: <sip:asterisk#[REDACTED]:5161;transport=TLS>
Call-ID: f454ef36-01ea-4f29-9482-4a10768bf1b7
CSeq: 24942 INVITE
Allow: OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REGISTER, MESSAGE, REFER
Supported: 100rel, timer, replaces, norefersub, path
Session-Expires: 1800
Min-SE: 90
Max-Forwards: 70
User-Agent: FPBX-AsteriskNOW-13.0.190.12(13.13.1)
Content-Type: application/sdp
Content-Length: 398
v=0
o=- 1582453973 1582453973 IN IP4 172.31.18.138
s=Asterisk
c=IN IP4 [REDACTED]
t=0 0
m=audio 11410 RTP/AVP 3 110 9 97 101
a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:84m7hqGvMjTU21xzkhBS3RQpQQjJ+aep0VwSlhx+
a=rtpmap:3 GSM/8000
a=rtpmap:110 speex/8000
a=rtpmap:9 G722/8000
a=rtpmap:97 iLBC/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20
a=maxptime:60
a=sendrecv
--end msg--
19:10:11.601 pjsua_call.c .Incoming Request msg INVITE/cseq=24942 (rdata0x1421f0540)
19:10:11.603 tsx0x1421fe0a8 ...Transaction created for Request msg INVITE/cseq=24942 (rdata0x1421f0540)
19:10:11.603 tsx0x1421fe0a8 ..Incoming Request msg INVITE/cseq=24942 (rdata0x1421f0540) in state Null
19:10:11.603 tsx0x1421fe0a8 ...State changed from Null to Trying, event=RX_MSG
19:10:11.603 dlg0x1421fd8a8 ....Transaction tsx0x1421fe0a8 state changed to Trying
19:10:11.603 dlg0x1421fd8a8 ..UAS dialog created
19:10:11.603 dlg0x1421fd8a8 ..Module mod-invite added as dialog usage, data=0x141de7588
19:10:11.603 dlg0x1421fd8a8 ...Session count inc to 3 by mod-invite
19:10:11.603 inv0x1421fd8a8 ..UAS invite session created for dialog dlg0x1421fd8a8
19:10:11.603 dlg0x1421fd8a8 ...Session count inc to 3 by mod-pjsua
19:10:11.603 pjsua_media.c ..Call 0: initializing media..
19:10:11.603 pjsua_call.c ..Error initializing media channel: Not Acceptable Here [status=170488]
19:10:11.604 endpoint ..Response msg 488/INVITE/cseq=24942 (tdta0x1421fe800) created
19:10:11.604 dlg0x1421fd8a8 ...Sending Response msg 488/INVITE/cseq=24942 (tdta0x1421fe800)
19:10:11.606 tsx0x1421fe0a8 ...Sending Response msg 488/INVITE/cseq=24942 (tdta0x1421fe800) in state Trying
19:10:11.606 pjsua_core.c ....TX 429 bytes Response msg 488/INVITE/cseq=24942 (tdta0x1421fe800) to TLS [REDACTED]:5161:
SIP/2.0 488 Not Acceptable Here
Via: SIP/2.0/TLS [REDACTED]:5161;rport=5161;received=[REDACTED];branch=z9hG4bKPj8ea1a332-0748-438f-ae74-5d17b038891d;alias
Call-ID: f454ef36-01ea-4f29-9482-4a10768bf1b7
From: "Test" <sip:asterisk#172.31.18.138>;tag=7c3663cb-b5f5-4762-8526-8425d18b2466
To: <sip:[REDACTED]#[REDACTED];ob>;tag=5oFGceZO4ZaKpLFEg7piOrM7IV2yeDLT
CSeq: 24942 INVITE
Content-Length: 0
--end msg--
In case anyone else has this issue. I'll tell you what solved this for me.
On Asterisk in my endpoint (pjsip show endpoint myendpoint) setting I had media_encryption_optimistic set to true. When I set this to false everything started working.
I'm not sure why as the how to on asterisk stated to turn this on. But I confirmed at all traffic was indeed encrypted by using wireshark to check the actual voice data.
If anyone know why this would be the case that this needed to be set to false it would help me better understand this. But for now I'm up and running.
I had this error on PJSIP ios and I solved on my end after DISABLING "SRTP" or "S-RTP" or "Secure RTP" or "Secure Transport". I removed this for TLS configuration.
//acc_cfg.srtp_secure_signaling = 1;
//acc_cfg.use_srtp = PJMEDIA_SRTP_MANDATORY;
488 / Not Acceptable Here
I also get the same problem with my server, atlast i find the solution
by getting to know that it is because codec, use right codec or
disable codec works great for you.

Seeing "No route to Host" when sending 200 OK response to INVITE

I have a voip application for iOS based on pjsip 2.3.0.
The device in question runs iOS 9.3.4 and the problem also seem to happen on iOS 10 beta. It is connected to IPV6 wifi network created from macbook's nat64/dns64.
The client is able to register properly with our server and also has no trouble making outbound call in UDP mode ( Everything works fine in TCP ).
The trouble is with inbound call where it fails intermittently due to the following reason when responding to INVITE with 200 OK message even though it is able to send 100 TRYING successfully just before it.
pjsua_core.c ....
Sent:
2016-07-31T16:49:32.443
<srcipv6:port> -> <dstipv6:port>
1005
SIP/2.0 200 OK Via: SIP/2.0/UDP
--end msg--
tsx0x1401d82a8 ....Error sending Response msg
200/INVITE/cseq=95989621(tdta0x140866000): No route to host
....Transport error, terminating transaction. Err=120065 (No route to host)
pjsua_call.c .Error sending response: No route to host [status=120065]
APIException: API call '::pjsua_call_answer( CALL_REFERENCE,
PJSUA_SIP_RESPONSE_STATUS_OKAY, NULL, NULL )'
failed with result code = 120065 ('No route to host')
Note that this happens only intermittently. Once an inbound call succeeds after sending 200 OK to INVITE then the subsequent calls do succeed. The problem does not exist in IPV4 networks.
I also did a packet capture and found that there is no trace of 200 OK message in it even though pjsua_core.c's trace tells otherwise as seen above.
Sincerely welcome any inputs on how to proceed further or if there are any fixes.
Edit 1 :
I have managed to debug all the way from ::pjsua_call_answer to the method “sendto” () calledin pjlib/src/pj/sock_bsd.c
/*
* Send data.
*/
PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock,
const void *buf,
pj_ssize_t *len,
unsigned flags,
const pj_sockaddr_t *to,
int tolen)
{
PJ_CHECK_STACK();
PJ_ASSERT_RETURN(len, PJ_EINVAL);
CHECK_ADDR_LEN(to, tolen);
*len = sendto(sock, (const char*)buf, (int)(*len), flags,
(const struct sockaddr*)to, tolen);
if (*len < 0)
return PJ_RETURN_OS_ERROR(pj_get_native_netos_error());
else
return PJ_SUCCESS;
}
sendto here returns -1 when trying to send 200 OK message alone in ipv6.

Erlang : Tcp Server

Hi I am a new bee in Erlang but managed to create a simple TCP server which accepts client in passive mode and displays message.
I spawn a new process every time new client connects to the server. Is there a way I could send message to the client using the process which gets spawned when client connects.
Here is code.
-module(test).
-export([startserver/0]).
startserver()->
{ok, ListenSocket}=gen_tcp:listen(1235,[binary,{active, false}]),
connect(ListenSocket).
connect(ListenSocket)->
{ok, UserSocket}=gen_tcp:accept(ListenSocket),
Pid=spawn(? MODULE, user,[UserSocket]),
gen_tcp:controlling_process(UserSocket, Pid),
connect(ListenSocket).
user(UserSocket)->
case gen_tcp:recv(UserSocket, 0) of.
{ok, Binary}->% Send basic message.
{error, closed}->% operation on close.
end.
Can I have some thing like if I do.
Pid!{"Some Message"}. And the message is send to the socket associated with the process with non blocking io,
You could try this tutorial for writing a TCP server using OTP principles: http://learnyousomeerlang.com/buckets-of-sockets#sockserv-revisited
If you use a gen_server instead of your connect loop, you can store the Pids in the state. Then you can use gen_server:cast/2 to send a message to one of the Pids.
The function you want to send a message to the client from the controlling process is gen_tcp:send(Socket, Message), so for example if you wanted to send a one off message on connection you could do this:
user(UserSocket)->
gen_tcp:send(UserSocket, "hello"),
case gen_tcp:recv(UserSocket, 0) of
{ok, Binary}->% Send basic message.
gen_tcp:send(UserSocket, "basic message"),
{error, closed}->% operation on close.
gen_tcp:send(UserSocket, "this socket is closing now"),
end.

Erlang spawn problems

I am having problem with 'spawn' in erlang, it seems processes just die after awhile. Here's the simple code:
-module(simple).
-export([server/1, client/1, owner/1, spawn_n/2, start/1, main/1]).
server(State) ->
receive
{request,Return_PID} ->
io:format("SERVER ~w: Client request recieved from ~w~n", [self(),Return_PID]),
NewState = State + 1,
Return_PID ! {hit_count,NewState},
server(NewState);
{server_owner,Owner_PID} ->
io:format("SERVER ~w: Owner request recieved from ~w~n", [self(), Owner_PID]),
Owner_PID ! {hit_count,State},
server(State);
reset ->
io:format("SERVER ~w: State reset to zero.~n", [self()]),
server(0)
end.
client(Server_Address) ->
Server_Address ! {request, self()},
receive
{hit_count,Number} ->
io:format("CLIENT ~w: Hit count was ~w~n", [self(),Number])
end.
owner(Server_PID) ->
timer:sleep(random:uniform(100)),
Server_PID ! {server_owner,self()},
receive
{hit_count,Number} when Number > 5 ->
io:format("OWNER: Hit count is ~w , reseting counter. ~n", [Number]),
Server_PID ! reset,
owner(Server_PID);
{hit_count,Number} when Number < 5 ->
io:format("OWNER ~w: Hit count was ~w~n", [self(), Number]),
owner(Server_PID)
end.
spawn_n(N, Server_PID) ->
if
N>0 ->
spawn(simple,client,[Server_PID]),
timer:sleep(random:uniform(100)),
spawn_n(N-1,Server_PID);
N==0 ->
io:format("Last client spawned. ~n")
end.
start(N) ->
Server_PID = spawn(simple,server,[0]),
spawn(simple,owner,[Server_PID]),
spawn(simple,spawn_n,[N,Server_PID]).
main([Arg]) ->
N = list_to_integer(atom_to_list(Arg)),
start(N),
init:stop().
Here's an example I get when running it:
erl -noshell -s simple main 20
SERVER <0.28.0>: Client request recieved from <0.31.0>
CLIENT <0.31.0>: Hit count was 1
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.32.0>
OWNER <0.29.0>: Hit count was 1
CLIENT <0.32.0>: Hit count was 2
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.33.0>
OWNER <0.29.0>: Hit count was 2
CLIENT <0.33.0>: Hit count was 3
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.34.0>
OWNER <0.29.0>: Hit count was 3
CLIENT <0.34.0>: Hit count was 4
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.35.0>
OWNER <0.29.0>: Hit count was 4
CLIENT <0.35.0>: Hit count was 5
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.36.0>
CLIENT <0.36.0>: Hit count was 6
SERVER <0.28.0>: Client request recieved from <0.37.0>
CLIENT <0.37.0>: Hit count was 7
SERVER <0.28.0>: Client request recieved from <0.38.0>
CLIENT <0.38.0>: Hit count was 8
SERVER <0.28.0>: Client request recieved from <0.39.0>
CLIENT <0.39.0>: Hit count was 9
SERVER <0.28.0>: Client request recieved from <0.40.0>
CLIENT <0.40.0>: Hit count was 10
SERVER <0.28.0>: Client request recieved from <0.41.0>
CLIENT <0.41.0>: Hit count was 11
SERVER <0.28.0>: Client request recieved from <0.42.0>
CLIENT <0.42.0>: Hit count was 12
SERVER <0.28.0>: Client request recieved from <0.43.0>
CLIENT <0.43.0>: Hit count was 13
SERVER <0.28.0>: Client request recieved from <0.44.0>
CLIENT <0.44.0>: Hit count was 14
SERVER <0.28.0>: Client request recieved from <0.45.0>
CLIENT <0.45.0>: Hit count was 15
SERVER <0.28.0>: Client request recieved from <0.46.0>
CLIENT <0.46.0>: Hit count was 16
SERVER <0.28.0>: Client request recieved from <0.47.0>
CLIENT <0.47.0>: Hit count was 17
SERVER <0.28.0>: Client request recieved from <0.48.0>
CLIENT <0.48.0>: Hit count was 18
{error_logger,{{2011,6,27},{12,57,8}},"~s~n",["Error in process <0.28.0> with ex
it value: {terminated,[{io,format,[<0.22.0>,\"SERVER ~w: Client request recieved
from ~w~n\",[<0.28.0>,<0.49.0>]]},{simple,server,1}]}\n"]}
I don't get it. The processes just die or something? It shouldnt terminate!
I am running on windows 7 if it might be something windows-related.
Thanks
EDIT: heres what happens by doing application:start(sasl). before:
C:\Program Files\erl5.8.4\bin>erl
Eshell V5.8.4 (abort with ^G)
1> application:start(sasl).
ok
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.37.0>},
{name,alarm_handler},
{mfargs,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.38.0>},
{name,overload},
{mfargs,{overload,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.36.0>},
{name,sasl_safe_sup},
{mfargs,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.39.0>},
{name,release_handler},
{mfargs,{release_handler,start_link,[]}}
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
application: sasl
started_at: nonode#nohost
2> simple:main(['20']).
ok
SERVER <0.42.0>: Client request recieved from <0.45.0>
3> CLIENT <0.45.0>: Hit count was 1
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.47.0>
3> OWNER <0.43.0>: Hit count was 1
3> CLIENT <0.47.0>: Hit count was 2
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.48.0>
3> OWNER <0.43.0>: Hit count was 2
3> CLIENT <0.48.0>: Hit count was 3
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.49.0>
3> OWNER <0.43.0>: Hit count was 3
3> CLIENT <0.49.0>: Hit count was 4
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.50.0>
3> OWNER <0.43.0>: Hit count was 4
3> CLIENT <0.50.0>: Hit count was 5
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.51.0>
3> CLIENT <0.51.0>: Hit count was 6
3> SERVER <0.42.0>: Client request recieved from <0.52.0>
3> CLIENT <0.52.0>: Hit count was 7
3> SERVER <0.42.0>: Client request recieved from <0.53.0>
3> CLIENT <0.53.0>: Hit count was 8
3> SERVER <0.42.0>: Client request recieved from <0.54.0>
3> CLIENT <0.54.0>: Hit count was 9
3> SERVER <0.42.0>: Client request recieved from <0.55.0>
3> CLIENT <0.55.0>: Hit count was 10
3> SERVER <0.42.0>: Client request recieved from <0.56.0>
3> CLIENT <0.56.0>: Hit count was 11
3> SERVER <0.42.0>: Client request recieved from <0.57.0>
3> CLIENT <0.57.0>: Hit count was 12
3> SERVER <0.42.0>: Client request recieved from <0.58.0>
3> CLIENT <0.58.0>: Hit count was 13
3> SERVER <0.42.0>: Client request recieved from <0.59.0>
3> CLIENT <0.59.0>: Hit count was 14
3> SERVER <0.42.0>: Client request recieved from <0.60.0>
3> CLIENT <0.60.0>: Hit count was 15
3> SERVER <0.42.0>: Client request recieved from <0.61.0>
3> CLIENT <0.61.0>: Hit count was 16
3> SERVER <0.42.0>: Client request recieved from <0.62.0>
3> CLIENT <0.62.0>: Hit count was 17
3> SERVER <0.42.0>: Client request recieved from <0.63.0>
3> CLIENT <0.63.0>: Hit count was 18
3> {error_logger,{{2011,6,27},{16,3,58}},"~s~n",["Error in proc
exit value: {terminated,[{io,format,[<0.23.0>,\"SERVER ~w: Cli
ved from ~w~n\",[<0.42.0>,<0.64.0>]]},{simple,server,1}]}\n"]}
The other answer is correct, but it doesn't explain why.
It's a tricky question, and the answer is (sort of) in your log output:
{error_logger,{{2011,6,27},{12,57,8}},"~s~n",["Error in process <0.28.0> with exit value: {terminated,[{io,format,[<0.22.0>,\"SERVER ~w: Client request recieved from ~w~n\",[<0.28.0>,<0.49.0>]]},{simple,server,1}]}\n"]}
The first clue is that io:format/3 exits with 'terminated' and some data.
Looking at the documentation and searching for terminated gives ... nothing. Looking inside io.erl gives us the answer though.
Line 456 and 462 of io.erl returns {error, terminated} when the io device is down. So now we know that io:format can potentially exit with status terminated. Looking further we can see that this return value turns into the above error message on line 74 (with context):
case request(Io, Request) of
{error, Reason} ->
[_Name | Args] = tuple_to_list(to_tuple(Request)),
{'EXIT',{undef,[_Current|Mfas]}} = (catch erlang:error(undef)),
MFA = {io, Func, [Io | Args]},
exit({conv_reason(Func, Reason),[MFA|Mfas]});
If you follow the call path for request/2 you will find that one possible branch is at the {error, terminated} mentioned earlier.
So, long story short, standard out is down.
Most likely since you killed your system with init:stop().
You are calling init:stop() immediately after your spawns. How are you going to make sure all your processes are properly finished? You probably need to block on receive in main and notify main process when others are done.

Resources