Commit 31755980 authored by frtabu's avatar frtabu

Initial implem web interface for telnet server

parent a981291b
......@@ -3178,3 +3178,4 @@ ADD_CUSTOM_TARGET(oarf
)
include (${OPENAIR_DIR}/common/utils/telnetsrv/telnetsrv_CMakeLists.txt)
include (${OPENAIR_DIR}/common/utils/websrv/websrv_CMakeLists.txt)
......@@ -901,3 +901,7 @@ int telnetsrv_getfarray(loader_shlibfunc_t **farray) {
(*farray)[1].fptr=(int (*)(void) )poll_telnetcmdq;
return ( 2);
}
/* for webserver interface, needs access to some telnet server paramaters */
telnetsrv_params_t *get_telnetsrv_params(void) {
return &telnetparams;
}
......@@ -155,4 +155,7 @@ void set_sched(pthread_t tid, int pid,int priority);
void set_affinity(pthread_t tid, int pid, int coreid);
extern int get_phybsize(void);
#endif
#ifdef WEBSERVERCODE
extern telnetsrv_params_t *get_telnetsrv_params(void);
#endif
#endif
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file common/utils/websrv/websrv.c
* \brief: implementation of web API
* \author Francois TABURET
* \date 2022
* \version 0.1
* \company NOKIA BellLabs France
* \email: francois.taburet@nokia-bell-labs.com
* \note
* \warning
*/
#include <jansson.h>
#include <ulfius.h>
#include "common/config/config_userapi.h"
#include "common/utils/LOG/log.h"
#include "common/utils/websrv/websrv.h"
#include "executables/softmodem-common.h"
#define WEBSERVERCODE
#include "common/utils/telnetsrv/telnetsrv.h"
static websrv_params_t websrvparams;
paramdef_t websrvoptions[] = {
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* configuration parameters for telnet utility */
/* optname helpstr paramflags XXXptr defXXXval type numelt */
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
{"listenaddr", "<listen ip address>\n", 0, uptr:&websrvparams.listenaddr, defstrval:"0.0.0.0", TYPE_IPV4ADDR, 0 },
{"listenport", "<local port>\n", 0, uptr:&(websrvparams.listenport), defuintval:8090, TYPE_UINT, 0 },
{"priority", "<scheduling policy (0-99)\n", 0, iptr:&websrvparams.priority, defuintval:0, TYPE_INT, 0 },
{"debug", "<debug level>\n", 0, uptr:&websrvparams.dbglvl, defuintval:0, TYPE_UINT, 0 },
};
int websrv_callback_default (const struct _u_request * request, struct _u_response * response, void * user_data) {
return U_CALLBACK_CONTINUE;
}
int websrv_callback_get_softmodeminfo(const struct _u_request * request, struct _u_response * response, void * user_data) {
char *cfgfile=CONFIG_GETCONFFILE ;
char *execfunc=get_softmodem_function(NULL);
telnetsrv_params_t *telnetparams;
telnetparams = get_telnetsrv_params();
json_t * body;
json_t *status = json_pack("{s[{ss}{ss}]}","display_status", "Config file", cfgfile, "Executable function", execfunc);
if (status==NULL) {
LOG_E(UTIL,"websrv cannot encode status info\n");
}
char cmdnames[8192]="?";
for (int i=0; telnetparams->CmdParsers[i].var != NULL && telnetparams->CmdParsers[i].cmd != NULL; i++) {
// client_printf(" module %i = %s:\n",i,telnetparams.CmdParsers[i].module);
// for(j=0; telnetparams.CmdParsers[i].var[j].varvalptr != NULL ; j++) {
// client_printf(" %s [get set] %s <value>\n",
// telnetparams.CmdParsers[i].module, telnetparams.CmdParsers[i].var[j].varname);
// }
// for(j=0; telnetparams.CmdParsers[i].cmd[j].cmdfunc != NULL ; j++) {
// client_printf(" %s %s %s\n",
// telnetparams.CmdParsers[i].module,telnetparams.CmdParsers[i].cmd[j].cmdname,
// telnetparams.CmdParsers[i].cmd[j].helpstr);
// }
}
json_t *cmds = json_pack("{s[s]}","menu_cmds", cmdnames);
if (cmds==NULL) {
LOG_E(UTIL,"websrv cannot encode cmds\n");
}
body=json_pack("{s[oo]}","main_oai softmodem",status,cmds);
if (body==NULL) {
LOG_E(UTIL,"websrv cannot encode body response\n");
}
int us=ulfius_add_header_to_response(response,"content-type" ,"application/json");
if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_add_header_to_response)");
LOG_E(UTIL,"websrv cannot set response header type ulfius error %d \n",us);
}
us=ulfius_set_json_body_response(response, 200, body);
if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_set_json_body_response)");
LOG_E(UTIL,"websrv cannot set body response ulfius error %d \n",us);
}
// ulfius_set_string_body_response(response, 200, cfgfile);
return U_CALLBACK_CONTINUE;
}
void* websrv_autoinit() {
int ret;
memset(&websrvparams,0,sizeof(websrvparams));
config_get( websrvoptions,sizeof(websrvoptions)/sizeof(paramdef_t),"websrv");
// Set the framework port number
websrvparams.instance = malloc(sizeof(struct _u_instance));
if (ulfius_init_instance(websrvparams.instance, websrvparams.listenport, NULL, NULL) != U_OK) {
LOG_W(UTIL, "Error,cannot init websrv\n");
free(websrvparams.instance);
return(NULL);
}
u_map_put(websrvparams.instance->default_headers, "Access-Control-Allow-Origin", "*");
// Maximum body size sent by the client is 1 Kb
websrvparams.instance->max_post_body_size = 1024;
// Endpoint list declaration
ulfius_add_endpoint_by_val(websrvparams.instance, "GET", "oaisoftmodem", NULL, 0, &websrv_callback_get_softmodeminfo, NULL);
//ulfius_add_endpoint_by_val(&instance, "GET", "softmodem", "", 0, &callback_get_empty_response, NULL);
// ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/multiple/:multiple/:multiple/:not_multiple", 0, &callback_all_test_foo, NULL);
// ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, NULL, 0, &callback_post_test, NULL);
// ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 1");
// ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 2");
// ulfius_add_endpoint_by_val(&instance, "PUT", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 3");
// ulfius_add_endpoint_by_val(&instance, "DELETE", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 4");
// ulfius_add_endpoint_by_val(&instance, "GET", PREFIXCOOKIE, "/:lang/:extra", 0, &callback_get_cookietest, NULL);
// default_endpoint declaration
ulfius_set_default_endpoint(websrvparams.instance, &websrv_callback_default, NULL);
// Start the framework
// if (argc == 4 && o_strcmp("-secure", argv[1]) == 0) {
// If command-line options are -secure <key_file> <cert_file>, then open an https connection
// char * key_pem = read_file(argv[2]), * cert_pem = read_file(argv[3]);
// ret = ulfius_start_secure_framework(&instance, key_pem, cert_pem);
// o_free(key_pem);
// o_free(cert_pem);
// } else {
// Open an http connection
ret = ulfius_start_framework(websrvparams.instance);
// }
if (ret == U_OK) {
LOG_I(UTIL, "Web server started on port %d", websrvparams.instance->port);
} else {
LOG_W(UTIL,"Error starting web server on port %d\n",websrvparams.instance->port);
}
return websrvparams.instance;
}
void websrv_end(void *webinst) {
ulfius_stop_framework((struct _u_instance *)webinst);
ulfius_clean_instance((struct _u_instance *)webinst);
return;
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file common/utils/websrv/websrv.h
* \brief: implementation of web API
* \author Francois TABURET
* \date 2022
* \version 0.1
* \company NOKIA BellLabs France
* \email: francois.taburet@nokia-bell-labs.com
* \note
* \warning
*/
#ifndef WEBSRV_H
#define WEBSRV_H
#define WEBSRV_MODNAME "websrv"
#define WEBSRV_PORT 8090
/* websrv_params_t is an internal structure storing all the current parameters and */
/* global variables used by the web server */
typedef struct {
struct _u_instance *instance; // ulfius (web server) instance
unsigned int dbglvl; // debug level of the server
int priority; // server running priority
unsigned int listenport; // ip port the telnet server is listening on
unsigned int listenaddr; // ip address the telnet server is listening on
unsigned int listenstdin; // enable command input from stdin
} websrv_params_t;
#endif
set(WEBSRVROOT ${OPENAIR_DIR}/common/utils/websrv )
set(WEBSRV_SOURCE
${WEBSRVROOT}/websrv.c
)
add_library(websrv MODULE ${WEBSRV_SOURCE} )
target_link_libraries(websrv PRIVATE ulfius jansson)
install(TARGETS websrv DESTINATION bin)
if (EXISTS "${OPENAIR_BUILD_DIR}/ran_build/build" AND IS_DIRECTORY "${OPENAIR_BUILD_DIR}/ran_build/build")
install(TARGETS websrv DESTINATION ${OPENAIR_BUILD_DIR}/ran_build/build)
endif (EXISTS "${OPENAIR_BUILD_DIR}/ran_build/build" AND IS_DIRECTORY "${OPENAIR_BUILD_DIR}/ran_build/build")
......@@ -88,6 +88,7 @@ void get_common_options(uint32_t execmask) {
uint32_t online_log_messages=0;
uint32_t glog_level=0 ;
uint32_t start_telnetsrv = 0, start_telnetclt = 0;
uint32_t start_websrv = 0;
uint32_t noS1 = 0, nokrnmod = 1, nonbiot = 0;
uint32_t rfsim = 0, basicsim = 0, do_forms = 0;
char *logmem_filename = NULL;
......@@ -117,6 +118,10 @@ void get_common_options(uint32_t execmask) {
set_softmodem_optmask(SOFTMODEM_TELNETCLT_BIT);
}
if (start_telnetsrv) {
load_module_shlib("websrv",NULL,0,NULL);
}
if (logmem_filename != NULL && strlen(logmem_filename) > 0) {
log_mem_filename = &logmem_filename[0];
log_mem_flag = 1;
......
......@@ -170,6 +170,7 @@ extern int usrp_tx_thread;
#define CONFIG_HLP_FLOG "Enable online log \n"
#define CONFIG_HLP_LOGL "Set the global log level, valid options: (4:trace, 3:debug, 2:info, 1:warn, (0:error))\n"
#define CONFIG_HLP_TELN "Start embedded telnet server \n"
#define CONFIG_HLP_WEB "Start embedded web server \n"
#define CONFIG_FLOG_OPT "R"
#define CONFIG_LOGL_OPT "g"
/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
......@@ -180,6 +181,7 @@ extern int usrp_tx_thread;
{CONFIG_FLOG_OPT , CONFIG_HLP_FLOG, 0, uptr:&online_log_messages, defintval:1, TYPE_INT, 0}, \
{CONFIG_LOGL_OPT , CONFIG_HLP_LOGL, 0, uptr:&glog_level, defintval:0, TYPE_UINT, 0}, \
{"telnetsrv", CONFIG_HLP_TELN, PARAMFLAG_BOOL, uptr:&start_telnetsrv, defintval:0, TYPE_UINT, 0}, \
{"websrv", CONFIG_HLP_WEB, PARAMFLAG_BOOL, uptr:&start_websrv, defintval:0, TYPE_UINT, 0}, \
{"log-mem", NULL, 0, strptr:(char **)&logmem_filename, defstrval:NULL, TYPE_STRING, 0}, \
{"telnetclt", NULL, 0, uptr:&start_telnetclt, defstrval:NULL, TYPE_UINT, 0}, \
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment