ttynvt: Move socket function to seperate file

This commit is contained in:
Kim Woelders
2023-02-12 19:04:54 +01:00
parent cc038383fe
commit ac58d1a470
4 changed files with 65 additions and 46 deletions
+3 -1
View File
@@ -2,7 +2,9 @@
bin_PROGRAMS = ttynvt bin_PROGRAMS = ttynvt
ttynvt_SOURCES = \ ttynvt_SOURCES = \
ttynvt.c nvt_log.c nvt_log.h \ ttynvt.c \
nvt_log.c nvt_log.h \
nvt_socket.c nvt_socket.h \
telnet_basic.c telnet_rfc2217.c telnet.h telnet_param.h telnet_basic.c telnet_rfc2217.c telnet.h telnet_param.h
ttynvt_CPPFLAGS = -I$(top_builddir) $(FUSE_CFLAGS) $(CFLAGS_WARN) ttynvt_CPPFLAGS = -I$(top_builddir) $(FUSE_CFLAGS) $(CFLAGS_WARN)
+49
View File
@@ -0,0 +1,49 @@
/*
* Socket interface API
*/
#include <netdb.h> /* getaddrinfo() */
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include "nvt_log.h"
#include "nvt_socket.h"
int socket_create_client(const char *host, unsigned int port)
{
struct sockaddr_in serveraddr;
struct hostent *server;
int sockfd, err;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
nvt_log(LOG_ERR, "Socket open failed: %m\n");
return sockfd;
}
server = gethostbyname(host);
if (server == NULL)
{
nvt_log(LOG_ERR, "Cannot resolve host name: %m\n");
close(sockfd);
return -1;
}
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr,
server->h_length);
serveraddr.sin_port = htons(port);
err = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (err < 0)
{
nvt_log(LOG_ERR, "Failed to connect to: %s:%u: %m\n", host, port);
close(sockfd);
return -1;
}
return sockfd;
}
+9
View File
@@ -0,0 +1,9 @@
/*
* Socket functions
*/
#ifndef NVT_SOCKET_H
#define NVT_SOCKET_H
int socket_create_client(const char *host, unsigned int port);
#endif /* NVT_SOCKET_H */
+4 -45
View File
@@ -7,7 +7,6 @@
#include <cuse_lowlevel.h> #include <cuse_lowlevel.h>
#include <errno.h> #include <errno.h>
#include <fuse_opt.h> #include <fuse_opt.h>
#include <netdb.h>
#include <poll.h> #include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
@@ -21,13 +20,12 @@
#include <unistd.h> #include <unistd.h>
#include <asm/ioctls.h> #include <asm/ioctls.h>
#include <asm/termbits.h> #include <asm/termbits.h>
#include <linux/sockios.h>
#include <linux/ppp-ioctl.h> #include <linux/ppp-ioctl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h>
#include "nvt_log.h" #include "nvt_log.h"
#include "nvt_socket.h"
#include "telnet.h" #include "telnet.h"
#define NET_BUF_SIZE 1024 #define NET_BUF_SIZE 1024
@@ -293,44 +291,6 @@ static void *_read_net(void *arg)
return 0; return 0;
} }
static int _srv_connect(const char *host, unsigned int port)
{
struct sockaddr_in serveraddr;
struct hostent *server;
int sockfd, err;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
nvt_log(LOG_ERR, "Socket open failed: %m\n");
return sockfd;
}
server = gethostbyname(host);
if (server == NULL)
{
nvt_log(LOG_ERR, "Cannot resolve host name: %m\n");
close(sockfd);
return -1;
}
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr,
server->h_length);
serveraddr.sin_port = htons(port);
err = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (err < 0)
{
nvt_log(LOG_ERR, "Failed to connect to: %s:%u: %m\n", host, port);
close(sockfd);
return -1;
}
return sockfd;
}
static int _srv_write(void *cctx, const void *buf, int len) static int _srv_write(void *cctx, const void *buf, int len)
{ {
ttynvt_t *tty = cctx; ttynvt_t *tty = cctx;
@@ -450,7 +410,6 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
ttynvt_t *tty; ttynvt_t *tty;
char *slave_name; char *slave_name;
struct termios ios; struct termios ios;
int sockfd;
int fd; int fd;
int res; int res;
int n; int n;
@@ -467,14 +426,14 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
return; return;
} }
sockfd = _srv_connect(ttynvt_param.host, ttynvt_param.port); fd = socket_create_client(ttynvt_param.host, ttynvt_param.port);
if (sockfd < 0) if (fd < 0)
{ {
errno = EHOSTUNREACH; errno = EHOSTUNREACH;
goto open_err; goto open_err;
} }
tty->fds[FD_NET].fd = sockfd; tty->fds[FD_NET].fd = fd;
tty->fds[FD_NET].events = POLLIN; tty->fds[FD_NET].events = POLLIN;
tty->net_cnt = 0; tty->net_cnt = 0;