socket: Move host:port parsing to socket function
This commit is contained in:
+16
-12
@@ -2,6 +2,8 @@
|
|||||||
* Socket interface API
|
* Socket interface API
|
||||||
*/
|
*/
|
||||||
#include <netdb.h> /* getaddrinfo() */
|
#include <netdb.h> /* getaddrinfo() */
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@@ -10,12 +12,22 @@
|
|||||||
#include "nvt_socket.h"
|
#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 hostent *server;
|
||||||
|
struct sockaddr_in serveraddr;
|
||||||
int sockfd, err;
|
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);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd < 0)
|
if (sockfd < 0)
|
||||||
{
|
{
|
||||||
@@ -23,24 +35,16 @@ int socket_create_client(const char *host, unsigned int port)
|
|||||||
return sockfd;
|
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));
|
bzero((char *)&serveraddr, sizeof(serveraddr));
|
||||||
serveraddr.sin_family = AF_INET;
|
serveraddr.sin_family = AF_INET;
|
||||||
bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr,
|
bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr,
|
||||||
server->h_length);
|
server->h_length);
|
||||||
serveraddr.sin_port = htons(port);
|
serveraddr.sin_port = htons(atoi(port));
|
||||||
|
|
||||||
err = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
|
err = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
|
||||||
if (err < 0)
|
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);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -4,6 +4,6 @@
|
|||||||
#ifndef NVT_SOCKET_H
|
#ifndef NVT_SOCKET_H
|
||||||
#define 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 */
|
#endif /* NVT_SOCKET_H */
|
||||||
|
|||||||
+6
-25
@@ -46,8 +46,6 @@ static struct ttynvt_param {
|
|||||||
int logstd;
|
int logstd;
|
||||||
const char *close_delay;
|
const char *close_delay;
|
||||||
/* Derived */
|
/* Derived */
|
||||||
char host[64];
|
|
||||||
unsigned int port;
|
|
||||||
unsigned int close_delay_us;
|
unsigned int close_delay_us;
|
||||||
/* Picked up */
|
/* Picked up */
|
||||||
int foreground;
|
int foreground;
|
||||||
@@ -416,8 +414,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
|
|||||||
|
|
||||||
pthread_mutex_lock(&access_lock);
|
pthread_mutex_lock(&access_lock);
|
||||||
|
|
||||||
nvt_log(LOG_INFO, "Connecting to server: %s:%d\n",
|
nvt_log(LOG_INFO, "Connecting to server: %s\n", ttynvt_param.server);
|
||||||
ttynvt_param.host, ttynvt_param.port);
|
|
||||||
|
|
||||||
tty = _ttynvt_ctx_create();
|
tty = _ttynvt_ctx_create();
|
||||||
if (!tty)
|
if (!tty)
|
||||||
@@ -426,7 +423,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = socket_create_client(ttynvt_param.host, ttynvt_param.port);
|
fd = socket_create_client(ttynvt_param.server);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
errno = EHOSTUNREACH;
|
errno = EHOSTUNREACH;
|
||||||
@@ -488,8 +485,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
|
|||||||
info->nonseekable = 1;
|
info->nonseekable = 1;
|
||||||
info->direct_io = 1;
|
info->direct_io = 1;
|
||||||
|
|
||||||
nvt_log(LOG_INFO, "Connected to server: %s:%d\n",
|
nvt_log(LOG_INFO, "Connected to server: %s\n", ttynvt_param.server);
|
||||||
ttynvt_param.host, ttynvt_param.port);
|
|
||||||
|
|
||||||
fuse_reply_open(req, info);
|
fuse_reply_open(req, info);
|
||||||
return;
|
return;
|
||||||
@@ -502,8 +498,8 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
|
|||||||
|
|
||||||
fuse_reply_err(req, errno);
|
fuse_reply_err(req, errno);
|
||||||
|
|
||||||
nvt_log(LOG_ERR, "Connection to server: %s:%d failed: %m\n",
|
nvt_log(LOG_ERR, "Connection to server: %s failed: %m\n",
|
||||||
ttynvt_param.host, ttynvt_param.port);
|
ttynvt_param.server);
|
||||||
|
|
||||||
pthread_mutex_unlock(&access_lock);
|
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);
|
fuse_reply_err(req, 0);
|
||||||
|
|
||||||
nvt_log(LOG_INFO, "Disconnected from server: %s:%d\n",
|
nvt_log(LOG_INFO, "Disconnected from server: %s\n", ttynvt_param.server);
|
||||||
ttynvt_param.host, ttynvt_param.port);
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&access_lock);
|
pthread_mutex_unlock(&access_lock);
|
||||||
}
|
}
|
||||||
@@ -1214,20 +1209,6 @@ int main(int argc, char *argv[])
|
|||||||
return 1;
|
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);
|
snprintf(dev_name, sizeof(dev_name), "DEVNAME=%s", ttynvt_param.dev_name);
|
||||||
|
|
||||||
if (ttynvt_param.close_delay)
|
if (ttynvt_param.close_delay)
|
||||||
|
|||||||
Reference in New Issue
Block a user