Merge remote-tracking branch 'mahmoudb/telnet-IPV6-support' into integration_2026_w09 (!3940)
IPV6 support for telnet socket binding Now, the listening telnet socket is only bound to the dedicated interface IPs in config file, both for V4 and V6 Closes #834
This commit is contained in:
@@ -57,6 +57,10 @@
|
|||||||
#include "common/utils/threadPool/notified_fifo.h"
|
#include "common/utils/threadPool/notified_fifo.h"
|
||||||
#include <readline/history.h>
|
#include <readline/history.h>
|
||||||
#include "common/oai_version.h"
|
#include "common/oai_version.h"
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
|
||||||
#include "telnetsrv_phycmd.h"
|
#include "telnetsrv_phycmd.h"
|
||||||
@@ -79,7 +83,7 @@ paramdef_t telnetoptions[] = {
|
|||||||
/* configuration parameters for telnet utility */
|
/* configuration parameters for telnet utility */
|
||||||
/* optname helpstr paramflags XXXptr defXXXval type numelt */
|
/* optname helpstr paramflags XXXptr defXXXval type numelt */
|
||||||
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
|
||||||
{"listenaddr", "<listen ip address>\n", 0, .uptr = &telnetparams.listenaddr, .defstrval = "0.0.0.0", TYPE_IPV4ADDR, 0},
|
{"listenaddr", "<listen ip address>\n", 0, .strptr = &telnetparams.listenaddr, .defstrval = "0.0.0.0", TYPE_STRING, 0},
|
||||||
{"listenport", "<local port>\n", 0, .uptr = &telnetparams.listenport, .defuintval = 9090, TYPE_UINT, 0},
|
{"listenport", "<local port>\n", 0, .uptr = &telnetparams.listenport, .defuintval = 9090, TYPE_UINT, 0},
|
||||||
{"listenstdin", "enable input from stdin\n", PARAMFLAG_BOOL, .uptr = &telnetparams.listenstdin, .defuintval = 0, TYPE_UINT, 0},
|
{"listenstdin", "enable input from stdin\n", PARAMFLAG_BOOL, .uptr = &telnetparams.listenstdin, .defuintval = 0, TYPE_UINT, 0},
|
||||||
{"priority", "<scheduling policy (0-99)\n", 0, .iptr = &telnetparams.priority, .defuintval = 0, TYPE_INT, 0},
|
{"priority", "<scheduling policy (0-99)\n", 0, .iptr = &telnetparams.priority, .defuintval = 0, TYPE_INT, 0},
|
||||||
@@ -626,7 +630,6 @@ int process_command(char *buf, int iteration)
|
|||||||
|
|
||||||
void run_telnetsrv(void) {
|
void run_telnetsrv(void) {
|
||||||
int sock;
|
int sock;
|
||||||
struct sockaddr_in name;
|
|
||||||
char buf[TELNET_MAX_MSGLENGTH];
|
char buf[TELNET_MAX_MSGLENGTH];
|
||||||
struct sockaddr cli_addr;
|
struct sockaddr cli_addr;
|
||||||
unsigned int cli_len = sizeof(cli_addr);
|
unsigned int cli_len = sizeof(cli_addr);
|
||||||
@@ -636,23 +639,42 @@ void run_telnetsrv(void) {
|
|||||||
char prompt[sizeof(TELNET_PROMPT_PREFIX)+10];
|
char prompt[sizeof(TELNET_PROMPT_PREFIX)+10];
|
||||||
pthread_setname_np(pthread_self(), "telnet");
|
pthread_setname_np(pthread_self(), "telnet");
|
||||||
set_sched(pthread_self(),0,telnetparams.priority);
|
set_sched(pthread_self(),0,telnetparams.priority);
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
|
|
||||||
if (sock < 0)
|
struct addrinfo hints = {
|
||||||
fprintf(stderr,"[TELNETSRV] Error %s on socket call\n",strerror(errno));
|
.ai_family = AF_UNSPEC,
|
||||||
|
.ai_socktype = SOCK_STREAM,
|
||||||
|
.ai_flags = AI_NUMERICHOST,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct addrinfo *res;
|
||||||
|
sock = -1;
|
||||||
|
char port_str[6];
|
||||||
|
snprintf(port_str, sizeof(port_str), "%u", telnetparams.listenport);
|
||||||
|
|
||||||
|
int ret = getaddrinfo(telnetparams.listenaddr, port_str, &hints, &res);
|
||||||
|
if (ret != 0) {
|
||||||
|
fprintf(stderr, "[TELNETSRV] Invalid listenaddr '%s': %s\n",
|
||||||
|
telnetparams.listenaddr, gai_strerror(ret));
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (struct addrinfo *p = res; p != NULL; p = p-> ai_next){
|
||||||
|
|
||||||
|
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||||
|
if (sock < 0) continue;
|
||||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
|
||||||
name.sin_family = AF_INET;
|
if (bind(sock, p->ai_addr, p->ai_addrlen) == 0)
|
||||||
|
break;
|
||||||
|
close(sock);
|
||||||
|
sock = -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (telnetparams.listenaddr == 0)
|
freeaddrinfo(res);
|
||||||
name.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
else
|
|
||||||
name.sin_addr.s_addr = telnetparams.listenaddr;
|
|
||||||
|
|
||||||
name.sin_port = htons((unsigned short)(telnetparams.listenport));
|
if (sock < 0) {
|
||||||
|
fprintf(stderr, "Failed to bind socket\n");
|
||||||
if(bind(sock, (void *) &name, sizeof(name)))
|
exit(-1);
|
||||||
fprintf(stderr,"[TELNETSRV] Error %s on bind call\n",strerror(errno));
|
}
|
||||||
|
|
||||||
if(listen(sock, 1) == -1)
|
if(listen(sock, 1) == -1)
|
||||||
fprintf(stderr,"[TELNETSRV] Error %s on listen call\n",strerror(errno));
|
fprintf(stderr,"[TELNETSRV] Error %s on listen call\n",strerror(errno));
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ typedef struct {
|
|||||||
cmdparser_t CmdParsers[TELNET_MAXCMD]; // array of registered modules.
|
cmdparser_t CmdParsers[TELNET_MAXCMD]; // array of registered modules.
|
||||||
char msgbuff[TELNET_MAX_MSGLENGTH]; // internal buffer of the client_printf function which is used to print to the client terminal */
|
char msgbuff[TELNET_MAX_MSGLENGTH]; // internal buffer of the client_printf function which is used to print to the client terminal */
|
||||||
unsigned int listenport; // ip port the telnet server is listening on
|
unsigned int listenport; // ip port the telnet server is listening on
|
||||||
unsigned int listenaddr; // ip address the telnet server is listening on
|
char *listenaddr; // ip address the telnet server is listening on
|
||||||
unsigned int listenstdin; // enable command input from stdin
|
unsigned int listenstdin; // enable command input from stdin
|
||||||
unsigned int loopcount; // loop command param: number of loop iteration
|
unsigned int loopcount; // loop command param: number of loop iteration
|
||||||
unsigned int loopdelay; // loop command param: delay in ms between 2 iterations
|
unsigned int loopdelay; // loop command param: delay in ms between 2 iterations
|
||||||
|
|||||||
Reference in New Issue
Block a user