summaryrefslogtreecommitdiff
path: root/pjlib-util
diff options
context:
space:
mode:
authorRiza Sulistyo <riza@teluu.com>2013-04-19 06:05:06 +0000
committerRiza Sulistyo <riza@teluu.com>2013-04-19 06:05:06 +0000
commit5df88d5670b8860937acf820ec4483bf24def352 (patch)
tree73b3281a5499465920fa57cab590bb6478981ec9 /pjlib-util
parent2173d09f2e0a708f02af11f68f834425484d4e91 (diff)
Re #1643: Code restructure + modification to handle non blocking mode
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4476 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util')
-rw-r--r--pjlib-util/include/pjlib-util.h4
-rw-r--r--pjlib-util/include/pjlib-util/cli_imp.h2
-rw-r--r--pjlib-util/include/pjlib-util/cli_telnet.h19
-rw-r--r--pjlib-util/src/pjlib-util/cli_telnet.c74
4 files changed, 64 insertions, 35 deletions
diff --git a/pjlib-util/include/pjlib-util.h b/pjlib-util/include/pjlib-util.h
index b4e34656..b3daa0a3 100644
--- a/pjlib-util/include/pjlib-util.h
+++ b/pjlib-util/include/pjlib-util.h
@@ -63,7 +63,9 @@
/* HTTP */
#include <pjlib-util/http_client.h>
-/** CLI Telnet **/
+/** CLI **/
+#include <pjlib-util/cli.h>
+#include <pjlib-util/cli_console.h>
#include <pjlib-util/cli_telnet.h>
#endif /* __PJLIB_UTIL_H__ */
diff --git a/pjlib-util/include/pjlib-util/cli_imp.h b/pjlib-util/include/pjlib-util/cli_imp.h
index c79a8d3f..f52d2a96 100644
--- a/pjlib-util/include/pjlib-util/cli_imp.h
+++ b/pjlib-util/include/pjlib-util/cli_imp.h
@@ -47,7 +47,7 @@ PJ_BEGIN_DECL
* Default log level for telnet sessions.
*/
#ifndef PJ_CLI_TELNET_LOG_LEVEL
-# define PJ_CLI_TELNET_LOG_LEVEL 3
+# define PJ_CLI_TELNET_LOG_LEVEL 4
#endif
/**
diff --git a/pjlib-util/include/pjlib-util/cli_telnet.h b/pjlib-util/include/pjlib-util/cli_telnet.h
index f640de04..fff20895 100644
--- a/pjlib-util/include/pjlib-util/cli_telnet.h
+++ b/pjlib-util/include/pjlib-util/cli_telnet.h
@@ -50,15 +50,19 @@ typedef struct pj_cli_telnet_info
* The telnet's port number.
*/
pj_uint16_t port;
+
+ /* Internal buffer for IP address */
+ char buf_[32];
+
} pj_cli_telnet_info;
/**
* This specifies the callback called when telnet is started
*
- * @param telnet_info The telnet runtime information.
+ * @param status The status of telnet startup process.
*
*/
-typedef void (*pj_cli_telnet_on_started)(pj_cli_telnet_info *telnet_info);
+typedef void (*pj_cli_telnet_on_started)(pj_status_t status);
/**
* This structure contains various options to instantiate the telnet daemon.
@@ -142,6 +146,17 @@ PJ_DECL(pj_status_t) pj_cli_telnet_create(pj_cli_t *cli,
pj_cli_telnet_cfg *param,
pj_cli_front_end **p_fe);
+
+/**
+ * Retrieve cli telnet info.
+ *
+ * @param telnet_info The telnet runtime information.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pj_cli_telnet_get_info(pj_cli_front_end *fe,
+ pj_cli_telnet_info *info);
+
/**
* @}
*/
diff --git a/pjlib-util/src/pjlib-util/cli_telnet.c b/pjlib-util/src/pjlib-util/cli_telnet.c
index c3c19dfc..22ef0155 100644
--- a/pjlib-util/src/pjlib-util/cli_telnet.c
+++ b/pjlib-util/src/pjlib-util/cli_telnet.c
@@ -145,7 +145,7 @@ typedef enum cli_telnet_command
enum cli_telnet_options
{
TRANSMIT_BINARY = 0, /* Transmit Binary. */
- ECHO = 1, /* Echo. */
+ TERM_ECHO = 1, /* Echo. */
RECONNECT = 2, /* Reconnection. */
SUPPRESS_GA = 3, /* Suppress Go Aheah. */
MESSAGE_SIZE_NEGO = 4, /* Approx Message Size Negotiation. */
@@ -895,7 +895,7 @@ static void send_comp_arg(cli_telnet_sess *sess,
*/
static pj_bool_t handle_alfa_num(cli_telnet_sess *sess, unsigned char *data)
{
- if (is_local_option_state_ena(sess, ECHO)) {
+ if (is_local_option_state_ena(sess, TERM_ECHO)) {
if (recv_buf_right_len(sess->rcmd) > 0) {
/* Cursor is not at EOL, insert character */
unsigned char echo[5] = {0x1b, 0x5b, 0x31, 0x40, 0x00};
@@ -1061,7 +1061,7 @@ static pj_bool_t handle_return(cli_telnet_sess *sess)
NULL);
status = pj_cli_sess_exec(&sess->base, (char *)&sess->rcmd->rbuf,
- pool, &info);
+ pool, &info);
switch (status) {
case PJ_CLI_EINVARG:
@@ -1645,10 +1645,10 @@ static pj_bool_t telnet_fe_on_accept(pj_activesock_t *asock,
set_peer_option(sess, SUPPRESS_GA, PJ_TRUE);
set_peer_option(sess, STATUS, PJ_TRUE);
set_peer_option(sess, TIMING_MARK, PJ_TRUE);
- set_peer_option(sess, ECHO, PJ_TRUE);
+ set_peer_option(sess, TERM_ECHO, PJ_TRUE);
send_cmd_do(sess, SUPPRESS_GA);
- send_cmd_will(sess, ECHO);
+ send_cmd_will(sess, TERM_ECHO);
send_cmd_will(sess, STATUS);
send_cmd_will(sess, SUPPRESS_GA);
@@ -1727,7 +1727,9 @@ PJ_DEF(pj_status_t) pj_cli_telnet_create(pj_cli_t *cli,
goto on_exit;
/* Start telnet daemon */
- telnet_start(fe);
+ status = telnet_start(fe);
+ if (status != PJ_SUCCESS)
+ goto on_exit;
pj_cli_register_front_end(cli, &fe->base);
@@ -1769,8 +1771,9 @@ static pj_status_t telnet_start(cli_telnet_fe *fe)
status = pj_sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
&val, sizeof(val));
- if (status != PJ_SUCCESS)
- goto on_exit;
+ if (status != PJ_SUCCESS) {
+ PJ_LOG(3, (THIS_FILE, "Failed setting socket options"));
+ }
/* The loop is silly, but what else can we do? */
for (msec=MIN_WAIT_ON_TELNET_RESTART, restart_retry=0;
@@ -1833,30 +1836,13 @@ static pj_status_t telnet_start(cli_telnet_fe *fe)
goto on_exit;
}
- /** Fill telnet information and call pj_cli_telnet_on_started callback */
- if (fe->cfg.on_started) {
- char ip_addr[32];
- pj_cli_telnet_info telnet_info;
- pj_sockaddr hostip;
-
- telnet_info.ip_address.ptr = ip_addr;
- telnet_info.ip_address.slen = 0;
-
- status = pj_gethostip(pj_AF_INET(), &hostip);
- if (status != PJ_SUCCESS)
- goto on_exit;
-
- pj_strcpy2(&telnet_info.ip_address,
- pj_inet_ntoa(hostip.ipv4.sin_addr));
-
- telnet_info.port = fe->cfg.port;
-
- (*fe->cfg.on_started)(&telnet_info);
- }
-
return PJ_SUCCESS;
on_exit:
+ if (fe->cfg.on_started) {
+ (*fe->cfg.on_started)(status);
+ }
+
if (fe->asock)
pj_activesock_close(fe->asock);
else if (sock != PJ_INVALID_SOCKET)
@@ -1907,9 +1893,35 @@ static pj_status_t telnet_restart(cli_telnet_fe *fe)
/** Start Telnet **/
status = telnet_start(fe);
- if (status == PJ_SUCCESS)
+ if (status == PJ_SUCCESS) {
+ if (fe->cfg.on_started) {
+ (*fe->cfg.on_started)(status);
+ }
TRACE_((THIS_FILE, "Telnet Restarted"));
-
+ }
on_exit:
return status;
}
+
+PJ_DEF(pj_status_t) pj_cli_telnet_get_info(pj_cli_front_end *fe,
+ pj_cli_telnet_info *info)
+{
+ pj_sockaddr hostip;
+ pj_status_t status;
+ cli_telnet_fe *tfe = (cli_telnet_fe*) fe;
+
+ PJ_ASSERT_RETURN(fe && (fe->type == PJ_CLI_TELNET_FRONT_END) && info,
+ PJ_EINVAL);
+
+ pj_strset(&info->ip_address, info->buf_, 0);
+
+ status = pj_gethostip(pj_AF_INET(), &hostip);
+ if (status != PJ_SUCCESS)
+ return status;
+
+ pj_strcpy2(&info->ip_address, pj_inet_ntoa(hostip.ipv4.sin_addr));
+
+ info->port = tfe->cfg.port;
+
+ return PJ_SUCCESS;
+}