<div dir="ltr">Thanks mark,<div><br></div><div>will try this out</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">Kelvin Chua</div></div>
<br><div class="gmail_quote">On Wed, Jul 22, 2015 at 10:45 PM, Mark Michelson <span dir="ltr"><<a href="mailto:mmichelson@digium.com" target="_blank">mmichelson@digium.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><div><div class="h5">
<div><br>
<br>
On 07/21/2015 11:23 PM, Kelvin Chua wrote:<br>
</div>
<blockquote 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 href="http://0.0.0.0:2223" target="_blank">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>Kelvin Chua</div>
</div>
</div>
</blockquote>
<br></div></div>
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.<span class="HOEnZb"><font color="#888888"><br>
Mark Michelson<br>
</font></span></div>
<br>--<br>
_____________________________________________________________________<br>
-- Bandwidth and Colocation Provided by <a href="http://www.api-digital.com" rel="noreferrer" target="_blank">http://www.api-digital.com</a> --<br>
<br>
asterisk-dev mailing list<br>
To UNSUBSCRIBE or update options visit:<br>
<a href="http://lists.digium.com/mailman/listinfo/asterisk-dev" rel="noreferrer" target="_blank">http://lists.digium.com/mailman/listinfo/asterisk-dev</a><br></blockquote></div><br></div>