<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix"><br>
<br>
On 07/21/2015 11:23 PM, Kelvin Chua wrote:<br>
</div>
<blockquote
cite="mid:CAMF97-edZ+6nGc0gWjMeiPhrUrxL0RYimgsv4GXNUkeacXvejw@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>i am building a proof of concept for a new module, </div>
<div><br>
</div>
<div>my first step was to create a socket to listen to, accept
connections,</div>
<div>show remote IP, accept data and print it in asterisk
console.</div>
<div><br>
</div>
<div>so far i am able to load, unload, setup the socket with a
few lines of code.</div>
<div>then i got stuck. any pointers?</div>
<div><br>
</div>
<div>here is what i have so far:</div>
<div><br>
</div>
<div>
<div>static struct ast_tcptls_session_args desc = {</div>
<div> .accept_fd = -1,</div>
<div> .master = AST_PTHREADT_NULL,</div>
<div> .tls_cfg = NULL,</div>
<div> .poll_timeout = 5000, /* wake up every 5
seconds */</div>
<div> .periodic_fn = cleanup,</div>
<div> .name = "Control Module",</div>
<div> .accept_fn = ast_tcptls_server_root, /* thread
doing the accept() */</div>
<div> .worker_fn = session_do, /* thread
handling the session */</div>
<div>};</div>
</div>
<div><br>
</div>
<div>
<div>static int load_module(void)</div>
<div>{</div>
<div> struct ast_sockaddr bindaddr;</div>
<div> ast_sockaddr_parse(&bindaddr, "<a
moz-do-not-send="true" href="http://0.0.0.0:2223">0.0.0.0:2223</a>",
0);</div>
<div> ast_sockaddr_copy(&desc.local_address,
&bindaddr);</div>
<div> ast_tcptls_server_start(&desc);</div>
<div><br>
</div>
<div> return AST_MODULE_LOAD_SUCCESS;</div>
<div>}</div>
</div>
<div><br>
</div>
<div>question:</div>
<div>what would be the general flow of session_do() in this
scenario?</div>
<div><br>
</div>
<div><br>
</div>
<br clear="all">
<div>
<div class="gmail_signature">Kelvin Chua</div>
</div>
</div>
</blockquote>
<br>
When your session_do() function is called, it is because a client
has made a connection to you. The session_do() function takes an
argument of type ast_tcptls_session_instance, whose definition you
can find in include/asterisk/tcptls.h. This sructure contains the
file descriptor from which data can be read from the socket, and it
also contains an ast_sockaddr structure that contains IP/port
information about the remote client that connected to you.<br>
<br>
If you are interested in printing the remote IP address/port, I
suggest looking at the various flavors of ast_sockaddr_stringify in
include/asterisk/netsock2.h. As for reading data from the TCP
connection, you can use typical *nix/BSD read() or recv().<br>
<br>
As for the general flow, that's going to depend on what your
module's overall goal is. Typically, your session_do() will need to
run a loop that will last as long as the TCP connection is active.
This loop can poll the TCP file descriptor in order to determine
when new data has arrived from the client, then read that data and
act on it. The big thing to keep in mind is that the TCP/TLS
framework in Asterisk basically just takes care of the boilerplate
of setting up a listening socket, accepting connections, plus some
helper functions. Once the connection is accepted and your
session_do() function is called, you now have sole responsibility
over the connection and can essentially do what you please with it.
When you are finished with your connection, you can call
ast_tcptls_close_session_file() in order to tear down the TCP
connection.<br>
<br>
Hopefully that points you in the correct direction.<br>
Mark Michelson<br>
</body>
</html>