--- a/src/nexus.c Sat Feb 28 19:58:49 2009 +0200
+++ b/src/nexus.c Sat Feb 28 20:21:53 2009 +0200
@@ -1,12 +1,10 @@
#include <stdlib.h>
-#include <unistd.h>
+#include <stdbool.h>
+#include <err.h>
#include <stdio.h>
-#include <string.h>
-#include <event.h>
-#include <assert.h>
+#include <getopt.h>
-#include <err.h>
#include <event2/event.h>
#include "sock.h"
@@ -16,16 +14,64 @@
#define CONNECT_SERV "6697"
#define LINE_LENGTH 512
-void on_line (const char *line, void *arg) {
+static struct option options[] = {
+ {"help", 0, NULL, 'h' },
+ {"hostname", 1, NULL, 'H' },
+ {"port", 1, NULL, 'P' },
+ {"ssl", 0, NULL, 'S' },
+ {0, 0, 0, 0 },
+};
+
+void usage (const char *exe)
+{
+ printf("Usage: %s [OPTIONS]\n", exe);
+ printf("\n");
+ printf(" --help / -h display this message\n");
+ printf(" --hostname / -H HOST set hostname to connect to\n");
+ printf(" --port / -P PORT set service port to connect to\n");
+ printf(" --ssl / -S use SSL\n");
+}
+
+void on_line (const char *line, void *arg)
+{
printf("<<< %s\n", line);
}
-int main (int argc, char **argv) {
+int main (int argc, char **argv)
+{
+ int opt, option_index;
struct event_base *ev_base;
struct sock_stream *sock;
struct line_proto *lp;
struct error_info _err;
+ const char *hostname = CONNECT_HOST, *portname = CONNECT_SERV;
+ bool ssl = 0;
+
+ // parse options
+ while ((opt = getopt_long(argc, argv, "hH:P:S", options, &option_index)) != -1) {
+ switch (opt) {
+ case 'h':
+ usage(argv[0]);
+ return EXIT_SUCCESS;
+
+ case 'H':
+ hostname = optarg;
+ break;
+
+ case 'P':
+ portname = optarg;
+ break;
+
+ case 'S':
+ ssl = true;
+
+ case '?':
+ usage(argv[0]);
+ return EXIT_FAILURE;
+ }
+ }
+
// initialize libevent
if ((ev_base = event_base_new()) == NULL)
err(1, "event_base_new");
@@ -35,8 +81,15 @@
errx(1, "sock_init: %s", error_msg(&_err));
// over-simplified connect
- if (sock_ssl_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
- errx(1, "sock_gnutls_connect: %s", error_msg(&_err));
+ if (ssl) {
+ if (sock_ssl_connect(&sock, hostname, portname, &_err))
+ errx(1, "sock_ssl_connect: %s", error_msg(&_err));
+
+ } else {
+ if (sock_tcp_connect(&sock, hostname, portname, &_err))
+ errx(1, "sock_tcp_connect: %s", error_msg(&_err));
+
+ }
// line protocol, with safety margin for buffer
if (line_proto_create(&lp, sock, LINE_LENGTH * 2, on_line, NULL, &_err))
--- a/src/sock.h Sat Feb 28 19:58:49 2009 +0200
+++ b/src/sock.h Sat Feb 28 20:21:53 2009 +0200
@@ -48,7 +48,7 @@
* XXX: blocking
* XXX: doesn't do any certificate verification.
*/
-err_t sock_gnutls_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);
+err_t sock_ssl_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);
/*
* The generic read/write API for stream sockets. These are mostly identical to the equivalent read/write syscalls, but