socket: Move host:port parsing to socket function

This commit is contained in:
Kim Woelders
2023-02-12 22:12:59 +01:00
parent a49f95a300
commit a978e86c82
3 changed files with 23 additions and 38 deletions
+16 -12
View File
@@ -2,6 +2,8 @@
* Socket interface API
*/
#include <netdb.h> /* getaddrinfo() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
@@ -10,12 +12,22 @@
#include "nvt_socket.h"
int socket_create_client(const char *host, unsigned int port)
int socket_create_client(const char *addr)
{
struct sockaddr_in serveraddr;
char host[64], port[64];
struct hostent *server;
struct sockaddr_in serveraddr;
int sockfd, err;
sscanf(addr, "%63[^:]:%63s", host, port);
server = gethostbyname(host);
if (server == NULL)
{
nvt_log(LOG_ERR, "Cannot resolve host name: %m\n");
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
@@ -23,24 +35,16 @@ int socket_create_client(const char *host, unsigned int port)
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);
serveraddr.sin_port = htons(atoi(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);
nvt_log(LOG_ERR, "Failed to connect to: %s: %m\n", addr);
close(sockfd);
return -1;
}
+1 -1
View File
@@ -4,6 +4,6 @@
#ifndef NVT_SOCKET_H
#define NVT_SOCKET_H
int socket_create_client(const char *host, unsigned int port);
int socket_create_client(const char *addr);
#endif /* NVT_SOCKET_H */
+6 -25
View File
@@ -46,8 +46,6 @@ static struct ttynvt_param {
int logstd;
const char *close_delay;
/* Derived */
char host[64];
unsigned int port;
unsigned int close_delay_us;
/* Picked up */
int foreground;
@@ -416,8 +414,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
pthread_mutex_lock(&access_lock);
nvt_log(LOG_INFO, "Connecting to server: %s:%d\n",
ttynvt_param.host, ttynvt_param.port);
nvt_log(LOG_INFO, "Connecting to server: %s\n", ttynvt_param.server);
tty = _ttynvt_ctx_create();
if (!tty)
@@ -426,7 +423,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
return;
}
fd = socket_create_client(ttynvt_param.host, ttynvt_param.port);
fd = socket_create_client(ttynvt_param.server);
if (fd < 0)
{
errno = EHOSTUNREACH;
@@ -488,8 +485,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
info->nonseekable = 1;
info->direct_io = 1;
nvt_log(LOG_INFO, "Connected to server: %s:%d\n",
ttynvt_param.host, ttynvt_param.port);
nvt_log(LOG_INFO, "Connected to server: %s\n", ttynvt_param.server);
fuse_reply_open(req, info);
return;
@@ -502,8 +498,8 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
fuse_reply_err(req, errno);
nvt_log(LOG_ERR, "Connection to server: %s:%d failed: %m\n",
ttynvt_param.host, ttynvt_param.port);
nvt_log(LOG_ERR, "Connection to server: %s failed: %m\n",
ttynvt_param.server);
pthread_mutex_unlock(&access_lock);
}
@@ -1117,8 +1113,7 @@ static void ttynvt_release(fuse_req_t req, struct fuse_file_info *info)
fuse_reply_err(req, 0);
nvt_log(LOG_INFO, "Disconnected from server: %s:%d\n",
ttynvt_param.host, ttynvt_param.port);
nvt_log(LOG_INFO, "Disconnected from server: %s\n", ttynvt_param.server);
pthread_mutex_unlock(&access_lock);
}
@@ -1214,20 +1209,6 @@ int main(int argc, char *argv[])
return 1;
}
rc = sscanf(ttynvt_param.server, "%63[^:]:%u", ttynvt_param.host,
&ttynvt_param.port);
if (rc <= 1)
{
fprintf(stderr, "Bad server specification: '%s'\n",
ttynvt_param.server);
return 1;
}
if (ttynvt_param.port == 0)
{
fprintf(stderr, "Bad server port: %u\n", ttynvt_param.port);
return 1;
}
snprintf(dev_name, sizeof(dev_name), "DEVNAME=%s", ttynvt_param.dev_name);
if (ttynvt_param.close_delay)