ttynvt: Move socket function to seperate file
This commit is contained in:
+3
-1
@@ -2,7 +2,9 @@
|
||||
bin_PROGRAMS = ttynvt
|
||||
|
||||
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
|
||||
|
||||
ttynvt_CPPFLAGS = -I$(top_builddir) $(FUSE_CFLAGS) $(CFLAGS_WARN)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
@@ -7,7 +7,6 @@
|
||||
#include <cuse_lowlevel.h>
|
||||
#include <errno.h>
|
||||
#include <fuse_opt.h>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
@@ -21,13 +20,12 @@
|
||||
#include <unistd.h>
|
||||
#include <asm/ioctls.h>
|
||||
#include <asm/termbits.h>
|
||||
#include <linux/sockios.h>
|
||||
#include <linux/ppp-ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "nvt_log.h"
|
||||
#include "nvt_socket.h"
|
||||
#include "telnet.h"
|
||||
|
||||
#define NET_BUF_SIZE 1024
|
||||
@@ -293,44 +291,6 @@ static void *_read_net(void *arg)
|
||||
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)
|
||||
{
|
||||
ttynvt_t *tty = cctx;
|
||||
@@ -450,7 +410,6 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
|
||||
ttynvt_t *tty;
|
||||
char *slave_name;
|
||||
struct termios ios;
|
||||
int sockfd;
|
||||
int fd;
|
||||
int res;
|
||||
int n;
|
||||
@@ -467,14 +426,14 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
|
||||
return;
|
||||
}
|
||||
|
||||
sockfd = _srv_connect(ttynvt_param.host, ttynvt_param.port);
|
||||
if (sockfd < 0)
|
||||
fd = socket_create_client(ttynvt_param.host, ttynvt_param.port);
|
||||
if (fd < 0)
|
||||
{
|
||||
errno = EHOSTUNREACH;
|
||||
goto open_err;
|
||||
}
|
||||
|
||||
tty->fds[FD_NET].fd = sockfd;
|
||||
tty->fds[FD_NET].fd = fd;
|
||||
tty->fds[FD_NET].events = POLLIN;
|
||||
tty->net_cnt = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user