summaryrefslogtreecommitdiff
path: root/ecp/server/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'ecp/server/server.c')
-rw-r--r--ecp/server/server.c204
1 files changed, 204 insertions, 0 deletions
diff --git a/ecp/server/server.c b/ecp/server/server.c
new file mode 100644
index 0000000..bbe7c4e
--- /dev/null
+++ b/ecp/server/server.c
@@ -0,0 +1,204 @@
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+#include <ecp/core.h>
+#include <ecp/dir/dir.h>
+#include <ecp/vconn/vconn.h>
+
+#include "dir.h"
+#include "vlink.h"
+#include "ht.h"
+
+#include "server.h"
+
+static SRVConfig srv_config;
+
+SRVConfig *srv_get_config(void) {
+ return &srv_config;
+}
+
+static void usage(char *arg) {
+ fprintf(stderr, "Usage: %s <capabilities> <priv> <addr> [ <pub> <addr> ]\n", arg);
+ exit(1);
+}
+
+static void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
+ if (conn->type == ECP_CTYPE_VLINK) vlink_handle_err(conn, mtype, err);
+ LOG(LOG_ERR, "handle error ctype:%d mtype:%d err:%d\n", conn->type, mtype, err);
+}
+
+static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) {
+ ECPConnection *conn = NULL;
+
+ switch (type) {
+ case ECP_CTYPE_VCONN: {
+ ECPVConnInb *_conn;
+
+ _conn = malloc(sizeof(ECPVConnInb));
+ if (_conn) {
+ ecp_vconn_init_inb(_conn, sock);
+ conn = &_conn->b;
+ }
+ break;
+ }
+
+ case ECP_CTYPE_VLINK: {
+ conn = malloc(sizeof(ECPConnection));
+ if (conn) ecp_vlink_init(conn, sock);
+ break;
+ }
+
+ default: {
+ conn = malloc(sizeof(ECPConnection));
+ if (conn) ecp_conn_init(conn, sock, type);
+ break;
+ }
+ }
+
+ return conn;
+}
+
+static void conn_free(ECPConnection *conn) {
+ free(conn);
+}
+
+int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) {
+ int rv;
+
+ rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free);
+ if (rv) return rv;
+
+ rv = ecp_vconn_handler_init(ctx, vconn_handler);
+ if (rv) return rv;
+
+ rv = ecp_vlink_handler_init(ctx, vlink_handler);
+ if (rv) return rv;
+
+ return ECP_OK;
+}
+
+int main(int argc, char *argv[]) {
+ ecp_tr_addr_t addr;
+ ECPContext ctx;
+ ECPSocket sock;
+ ECPConnHandler vconn_handler;
+ ECPConnHandler vlink_handler;
+ char *endptr;
+ int fd;
+ int rv;
+
+ if ((argc < 4) || (argc > 6)) usage(argv[0]);
+
+ srv_config.capabilities = (uint16_t)strtol(argv[1], &endptr, 16);
+ if (endptr[0] != '\0') {
+ fprintf(stderr, "Bad capabilities\n");
+ exit(1);
+ }
+
+ if ((fd = open(argv[2], O_RDONLY)) < 0) {
+ fprintf(stderr, "Unable to open %s\n", argv[2]);
+ exit(1);
+ }
+ if (read(fd, &srv_config.key_perma.public, sizeof(ecp_ecdh_public_t)) != sizeof(ecp_ecdh_public_t)) {
+ close(fd);
+ fprintf(stderr, "Unable to read public key from %s\n", argv[2]);
+ exit(1);
+ }
+ if (read(fd, &srv_config.key_perma.private, sizeof(ecp_ecdh_private_t)) != sizeof(ecp_ecdh_private_t)) {
+ close(fd);
+ fprintf(stderr, "Unable to read private key from %s\n", argv[2]);
+ exit(1);
+ }
+ close(fd);
+ srv_config.key_perma.valid = 1;
+
+ rv = ecp_init(&ctx, &vconn_handler, &vlink_handler);
+ if (rv) {
+ fprintf(stderr, "ecp_init RV:%d\n", rv);
+ exit(1);
+ }
+ rv = ecp_sock_create(&sock, &ctx, &srv_config.key_perma);
+ if (rv) {
+ fprintf(stderr, "ecp_sock_create RV:%d\n", rv);
+ exit(1);
+ }
+
+
+ rv = ecp_addr_init(&addr, argv[3]);
+ if (rv) {
+ fprintf(stderr, "ecp_addr_init RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = ecp_sock_open(&sock, &addr);
+ if (rv) {
+ fprintf(stderr, "ecp_sock_open RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = dir_init(&ctx);
+ if (rv) {
+ fprintf(stderr, "dir_init RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = vlink_init(&ctx);
+ if (rv) {
+ fprintf(stderr, "vlink_init RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = dir_start_announce(&sock);
+ if (rv) {
+ fprintf(stderr, "dir_start_announce RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = vlink_start_open(&sock);
+ if (rv) {
+ fprintf(stderr, "vlink_start_open RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = vlink_start_keyx();
+ if (rv) {
+ fprintf(stderr, "vlink_start_keyx RV:%d\n", rv);
+ exit(1);
+ }
+
+ if (argc == 6) {
+ ECPNode node;
+ ecp_ecdh_public_t node_pub;
+ ecp_tr_addr_t node_addr;
+
+ if ((fd = open(argv[4], O_RDONLY)) < 0) {
+ fprintf(stderr, "Unable to open %s\n", argv[4]);
+ exit(1);
+ }
+ if (read(fd, &node_pub, sizeof(ecp_ecdh_public_t)) != sizeof(ecp_ecdh_public_t)) {
+ close(fd);
+ fprintf(stderr, "Unable to read public key from %s\n", argv[4]);
+ exit(1);
+ }
+ close(fd);
+
+ int rv;
+
+ ecp_node_init(&node, &node_pub, NULL);
+ rv = ecp_node_set_addr(&node, argv[5]);
+ if (rv) {
+ fprintf(stderr, "ecp_node_set_addr RV:%d\n", rv);
+ exit(1);
+ }
+
+ rv = dir_open_conn(&sock, &node);
+ if (rv) {
+ fprintf(stderr, "dir_open_conn RV:%d\n", rv);
+ exit(1);
+ }
+ }
+
+ ecp_receiver(&sock);
+} \ No newline at end of file