mirror of
https://github.com/sys-fs/tubes
synced 2024-11-22 03:54:15 -05:00
Use syslog and getopt
This commit is contained in:
parent
de2010c262
commit
d9a38ceabb
6
tubes.1
6
tubes.1
@ -1,4 +1,4 @@
|
|||||||
.TH TUBES 1 tubes-1.3.0
|
.TH TUBES 1 tubes-1.4.0
|
||||||
.SH NAME
|
.SH NAME
|
||||||
tubes \- irc pipes
|
tubes \- irc pipes
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
@ -33,7 +33,7 @@ if SSL is also desired
|
|||||||
The incoming and outgoing FIFO buffers are stored in /tmp and are named for
|
The incoming and outgoing FIFO buffers are stored in /tmp and are named for
|
||||||
the server tubes is connected to with .in or .out appended to the end.
|
the server tubes is connected to with .in or .out appended to the end.
|
||||||
~/.tubes.err is used to store an error string if tubes exits with -1.
|
~/.tubes.err is used to store an error string if tubes exits with -1.
|
||||||
.SH RETURN VALUE
|
.SH EXIT STATUS
|
||||||
Returns -1 on error, 0 on success.
|
Returns 1 on error, 0 on success.
|
||||||
.SH AUTHOR
|
.SH AUTHOR
|
||||||
Copyright \(co 2016 Thomas Mannay <audiobarrier@openmailbox.org>.
|
Copyright \(co 2016 Thomas Mannay <audiobarrier@openmailbox.org>.
|
||||||
|
134
tubes.c
134
tubes.c
@ -12,54 +12,55 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <syslog.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define PING_TIMEOUT 240
|
#define PING_TIMEOUT 240
|
||||||
|
|
||||||
|
char *argv0;
|
||||||
static SSL_CTX *ctx;
|
static SSL_CTX *ctx;
|
||||||
static SSL *ssl;
|
static SSL *ssl;
|
||||||
static char *host = "chat.freenode.net";
|
static char *host = "chat.freenode.net";
|
||||||
static int port = 6667;
|
static char port[6] = "6667";
|
||||||
static FILE *log;
|
|
||||||
static short use_ssl = 0;
|
static short use_ssl = 0;
|
||||||
static unsigned int last_response;
|
static unsigned int last_response;
|
||||||
|
|
||||||
static FILE *
|
static int
|
||||||
slog(char *file)
|
tube(char *direction)
|
||||||
{
|
{
|
||||||
const char *home = getenv("HOME");
|
int fd;
|
||||||
char path[100];
|
char buf[512];
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
snprintf(path, 100, "%s/%s", home, file);
|
snprintf(buf, sizeof(buf), "/tmp/%s.%s", host, direction);
|
||||||
mknod(path, 0 | 0666, 0);
|
unlink(buf);
|
||||||
fp = fopen(path, "w");
|
mkfifo(buf, 0660);
|
||||||
|
if ((fd = open(buf, O_RDWR)) < 0)
|
||||||
|
perror("tube");
|
||||||
|
|
||||||
return fp;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dial(char *host, int port)
|
dial(char *host, char *port)
|
||||||
{
|
{
|
||||||
int sockfd, err;
|
int sockfd, err;
|
||||||
struct addrinfo hints, *serv;
|
struct addrinfo hints, *serv;
|
||||||
|
|
||||||
char tmp[8];
|
|
||||||
snprintf(tmp, 8, "%d", port);
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
if ((err = getaddrinfo(host, tmp, &hints, &serv)) != 0) {
|
if ((err = getaddrinfo(host, port, &hints, &serv)) != 0) {
|
||||||
fprintf(log, "getaddrinfo: %s\n", gai_strerror(err));
|
fprintf(stderr, "dial: %s\n", gai_strerror(err));
|
||||||
return -1;
|
return -1;
|
||||||
} else if ((sockfd = socket(serv->ai_family, serv->ai_socktype,
|
}
|
||||||
serv->ai_protocol)) == -1) {
|
if ((sockfd = socket(serv->ai_family, serv->ai_socktype,
|
||||||
fprintf(log, "tubes: error on socket()\n");
|
serv->ai_protocol)) < 0) {
|
||||||
|
perror("dial");
|
||||||
return -1;
|
return -1;
|
||||||
} else if (connect(sockfd, serv->ai_addr, serv->ai_addrlen) == -1) {
|
}
|
||||||
fprintf(log, "tubes: error on connect().\n");
|
if (connect(sockfd, serv->ai_addr, serv->ai_addrlen) < 0) {
|
||||||
|
perror("dial");
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -84,7 +85,7 @@ sslify(int *sockfd)
|
|||||||
SSL_set_connect_state(ssl);
|
SSL_set_connect_state(ssl);
|
||||||
|
|
||||||
if ((r = SSL_connect(ssl)) < 1) {
|
if ((r = SSL_connect(ssl)) < 1) {
|
||||||
fprintf(log, "sslify: %s\n", strerror(SSL_get_error(ssl, r)));
|
fprintf(stderr, "sslify: %s\n", strerror(SSL_get_error(ssl, r)));
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -102,57 +103,38 @@ main(int argc, char **argv)
|
|||||||
char buf[512];
|
char buf[512];
|
||||||
int i, r, status;
|
int i, r, status;
|
||||||
|
|
||||||
for (i = 1; i < argc; i++) {
|
while ((i = getopt(argc, argv, "Shp:")) != -1) {
|
||||||
r = argv[i][1];
|
switch (i) {
|
||||||
if (argv[i][0] != '-' || argv[i][2])
|
|
||||||
r = -1;
|
|
||||||
switch (r) {
|
|
||||||
case 'S':
|
case 'S':
|
||||||
use_ssl = 1;
|
use_ssl = 1;
|
||||||
port = 6697;
|
strncpy(port, "6697", sizeof(port));
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
if (++i < argc)
|
host = optarg;
|
||||||
host = argv[i];
|
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
if (++i < argc)
|
strncpy(port, optarg, sizeof(port));
|
||||||
port = atoi(argv[i]);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr,
|
fprintf(stderr, "usage: tubes [-S] [-h host] [-p port]\n");
|
||||||
"usage: tubes [-S] [-h host] [-p port]\n");
|
return 1;
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((log = slog(".tubes.err")) == NULL) {
|
if ((sockfd = dial(host, port)) < 0)
|
||||||
fprintf(stderr, "error on slog()");
|
return 1;
|
||||||
exit(-1);
|
if (use_ssl && sslify(&sockfd) < 0)
|
||||||
|
return 1;
|
||||||
|
if ((in = tube("in")) < 0)
|
||||||
|
return 1;
|
||||||
|
if ((out = tube("out")) < 0)
|
||||||
|
return 1;
|
||||||
|
if (daemon(0, 0) < 0) {
|
||||||
|
perror("main");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
if (daemon(0, 0) == -1) {
|
|
||||||
fprintf(log, "error on daemon()\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
if ((sockfd = dial(host, port)) == -1)
|
|
||||||
exit(-1);
|
|
||||||
if (use_ssl && sslify(&sockfd) == -1)
|
|
||||||
exit(-1);
|
|
||||||
|
|
||||||
snprintf(buf, 512, "/tmp/%s.in", host);
|
openlog(argv[0], LOG_PID, LOG_DAEMON);
|
||||||
unlink(buf);
|
|
||||||
mkfifo(buf, 0660);
|
|
||||||
if ((in = open(buf, O_RDWR | O_NONBLOCK)) < 0) {
|
|
||||||
fprintf(log, "in: error on open()\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
snprintf(buf, 512, "/tmp/%s.out", host);
|
|
||||||
unlink(buf);
|
|
||||||
mkfifo(buf, 0660);
|
|
||||||
if ((out = open(buf, O_RDWR)) < 0) {
|
|
||||||
fprintf(log, "out: error on open()\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
for (status = 0;;) {
|
for (status = 0;;) {
|
||||||
FD_ZERO(&rd);
|
FD_ZERO(&rd);
|
||||||
maxfd = (out >= sockfd) ? out : sockfd;
|
maxfd = (out >= sockfd) ? out : sockfd;
|
||||||
@ -165,22 +147,29 @@ main(int argc, char **argv)
|
|||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
fprintf(log, "error on select()\n");
|
syslog(LOG_ERR, strerror(errno));
|
||||||
status = -1;
|
status = 1;
|
||||||
break;
|
break;
|
||||||
} else if (r == 0 && last_response - time(NULL) >= PING_TIMEOUT) {
|
} else if (r == 0 && last_response - time(NULL) >= PING_TIMEOUT) {
|
||||||
fprintf(log, "ping timeout\n");
|
syslog(LOG_ERR, "ping timeout");
|
||||||
status = -1;
|
status = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (FD_ISSET(out, &rd))
|
if (FD_ISSET(out, &rd)) {
|
||||||
if ((i = read(out, buf, sizeof(buf))) > 0) {
|
if ((i = read(out, buf, sizeof(buf))) < 0) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
syslog(LOG_ERR, "broken pipe");
|
||||||
|
status = 1;
|
||||||
|
break;
|
||||||
|
} else if (i > 0) {
|
||||||
buf[i] = 0;
|
buf[i] = 0;
|
||||||
if (use_ssl)
|
if (use_ssl)
|
||||||
SSL_write(ssl, buf, strlen(buf));
|
SSL_write(ssl, buf, strlen(buf));
|
||||||
else
|
else
|
||||||
send(sockfd, buf, strlen(buf), 0);
|
send(sockfd, buf, strlen(buf), 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (FD_ISSET(sockfd, &rd)) {
|
if (FD_ISSET(sockfd, &rd)) {
|
||||||
if (use_ssl) {
|
if (use_ssl) {
|
||||||
do {
|
do {
|
||||||
@ -194,15 +183,15 @@ main(int argc, char **argv)
|
|||||||
i = recv(sockfd, buf, sizeof(buf), 0);
|
i = recv(sockfd, buf, sizeof(buf), 0);
|
||||||
if (i != -1) {
|
if (i != -1) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
fprintf(log, "connection closed\n");
|
syslog(LOG_NOTICE, "connection closed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
buf[i] = 0;
|
buf[i] = 0;
|
||||||
if (write(in, buf, strlen(buf)) < 0) {
|
if (write(in, buf, strlen(buf)) < 0) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
fprintf(log, "error on write()\n");
|
syslog(LOG_ERR, "broken pipe");
|
||||||
status = -1;
|
status = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
last_response = time(NULL);
|
last_response = time(NULL);
|
||||||
@ -213,7 +202,7 @@ main(int argc, char **argv)
|
|||||||
close(sockfd);
|
close(sockfd);
|
||||||
close(in);
|
close(in);
|
||||||
close(out);
|
close(out);
|
||||||
fclose(log);
|
closelog();
|
||||||
|
|
||||||
ERR_free_strings();
|
ERR_free_strings();
|
||||||
EVP_cleanup();
|
EVP_cleanup();
|
||||||
@ -225,5 +214,6 @@ main(int argc, char **argv)
|
|||||||
unlink(buf);
|
unlink(buf);
|
||||||
snprintf(buf, 512, "/tmp/%s.out", host);
|
snprintf(buf, 512, "/tmp/%s.out", host);
|
||||||
unlink(buf);
|
unlink(buf);
|
||||||
exit(status);
|
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user