[asterisk-dev] new module

Mark Michelson mmichelson at digium.com
Wed Jul 22 09:45:49 CDT 2015



On 07/21/2015 11:23 PM, Kelvin Chua wrote:
> i am building a proof of concept for a new module,
>
> my first step was to create a socket to listen to, accept connections,
> show remote IP, accept data and print it in asterisk console.
>
> so far i am able to load, unload, setup the socket with a few lines of 
> code.
> then i got stuck. any pointers?
>
> here is what i have so far:
>
> static struct ast_tcptls_session_args desc = {
>         .accept_fd = -1,
>         .master = AST_PTHREADT_NULL,
>         .tls_cfg = NULL,
>         .poll_timeout = 5000,   /* wake up every 5 seconds */
>         .periodic_fn = cleanup,
>         .name = "Control Module",
>         .accept_fn = ast_tcptls_server_root,    /* thread doing the 
> accept() */
>         .worker_fn = session_do,        /* thread handling the session */
> };
>
> static int load_module(void)
> {
>         struct ast_sockaddr bindaddr;
>         ast_sockaddr_parse(&bindaddr, "0.0.0.0:2223 
> <http://0.0.0.0:2223>", 0);
>         ast_sockaddr_copy(&desc.local_address, &bindaddr);
>         ast_tcptls_server_start(&desc);
>
>         return AST_MODULE_LOAD_SUCCESS;
> }
>
> question:
> what would be the general flow of session_do() in this scenario?
>
>
>
> Kelvin Chua

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.

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().

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.

Hopefully that points you in the correct direction.
Mark Michelson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20150722/9f44a276/attachment.html>


More information about the asterisk-dev mailing list