Decode JSON TRP messages, then send to main thread.
[trust_router.git] / include / trp_internal.h
index 0cf6f05..13e2d65 100644 (file)
@@ -1,82 +1,75 @@
 #ifndef TRP_INTERNAL_H
 #define TRP_INTERNAL_H
 
+#include <pthread.h>
 #include <talloc.h>
 
 #include <gsscon.h>
 #include <trust_router/tr_dh.h>
-
-#define TRP_PORT 12310
-#define TRP_METRIC_INFINITY 0xFFFF
-
-typedef enum trp_rc {
-  TRP_SUCCESS=0,
-  TRP_ERROR, /* generic error */
-  TRP_NOPARSE, /* parse error */
-  TRP_NOMEM, /* allocation error */
-} TRP_RC;
-
-/*** Messages ***/
-typedef enum trp_msg_type {
-  TRP_MSG_TYPE_UNKNOWN=0, /* conveniently, JSON parser returns 0 if a non-integer number is specified */
-  TRP_MSG_TYPE_UPDATE,
-  TRP_MSG_TYPE_ROUTE_REQ
-} TRP_MSG_TYPE;
-
-typedef struct trp_msg {
-  TRP_MSG_TYPE type;
-  void *body;
-} TRP_MSG;
-
-typedef struct trp_msg_body_update TRP_MSG_BODY_UPDATE;
-struct trp_msg_body_update {
-  TRP_MSG_BODY_UPDATE *next;
-  TR_NAME *community;
+#include <tr_mq.h>
+#include <tr_msg.h>
+#include <trust_router/trp.h>
+
+/* info records */
+/* TRP update record types */
+typedef struct trp_inforec_route {
+  TR_NAME *comm;
   TR_NAME *realm;
   TR_NAME *trust_router;
   unsigned int metric;
   unsigned int interval;
-};
+} TRP_INFOREC_ROUTE;
 
-typedef struct trp_msg_body_route_req {
-  TR_NAME *community;
-  TR_NAME *realm;
-} TRP_MSG_BODY_ROUTE_REQ;
+/* TODO: define struct trp_msg_info_community */
 
-TRP_MSG_TYPE trp_msg_type_from_string(const char *s);
-const char *trp_msg_type_to_string(TRP_MSG_TYPE msgtype);
+typedef union trp_inforec_data {
+  TRP_INFOREC_ROUTE *route;
+  /* TRP_INFOREC_COMM *comm; */
+} TRP_INFOREC_DATA;
 
-TRP_MSG *trp_msg_new(TALLOC_CTX *mem_ctx);
-void trp_msg_destroy(TRP_MSG *msg);
+struct trp_inforec {
+  TRP_INFOREC *next;
+  TRP_INFOREC_TYPE type;
+  TRP_INFOREC_DATA data; /* contains pointer to one of the record types */
+};
 
+struct trp_update {
+  TRP_INFOREC *records;
+};
 
-typedef struct trps_instance TRPS_INSTANCE;
+struct trp_req {
+  TR_NAME *comm;
+  TR_NAME *realm;
+};
 
-/* REMOVE THIS!! --jennifer, 2016-06-13 */
-typedef TRP_MSG TRP_REQ;
-typedef TRP_MSG TRP_RESP;
 
+typedef struct trps_instance TRPS_INSTANCE;
 
-typedef int (TRPS_REQ_FUNC)(TRPS_INSTANCE *, TRP_REQ *, TRP_RESP *, void *);
-typedef void (TRPS_RESP_FUNC)(TRPS_INSTANCE *, TRP_REQ *, TRP_RESP *, void *);
-typedef int (trps_auth_func)(gss_name_t client_name, TR_NAME *display_name, void *cookie);
 
+typedef enum trp_connection_status {
+  TRP_CONNECTION_DOWN=0,
+  TRP_CONNECTION_UP,
+} TRP_CONNECTION_STATUS;
 
-/* encapsulate a gss context and its connection file handle */
-typedef struct trps_connection {
-  int conn;
+typedef struct trp_connection TRP_CONNECTION;
+struct trp_connection {
+  TRP_CONNECTION *next;
+  pthread_t *thread; /* thread servicing this connection */
+  int fd;
+  TR_NAME *gssname;
   gss_ctx_id_t *gssctx;
-} TRPS_CONNECTION;
+  TRP_CONNECTION_STATUS status;
+  pthread_mutex_t status_mutex;
+};
 
-/* a collection of the above */
-#define TRPS_CONNECTIONS_MAX 10
-typedef struct trps_connection_set {
-  TRPS_CONNECTION *conn[TRPS_CONNECTIONS_MAX];
-  unsigned int nconn;
-} TRPS_CONNECTION_SET;
+typedef TRP_RC (*TRPS_MSG_FUNC)(TRPS_INSTANCE *, TRP_CONNECTION *, TR_MSG *);
+typedef void (*TRP_RESP_FUNC)();
+/*typedef int (*TRP_AUTH_FUNC)(gss_name_t client_name, TR_NAME *display_name, void *cookie);*/
+typedef client_cb_fn TRP_AUTH_FUNC;
 
 /* TRP Client Instance Data */
 typedef struct trpc_instance {
+  TRP_CONNECTION *conn;
   DH *client_dh;                       /* Client's DH struct with priv and pub keys */
 } TRPC_INSTANCE;
 
@@ -84,38 +77,52 @@ typedef struct trpc_instance {
 struct trps_instance {
   char *hostname;
   unsigned int port;
-  TRPS_REQ_FUNC *req_handler;
-  trps_auth_func *auth_handler;
+  TRP_AUTH_FUNC auth_handler;
+  TRPS_MSG_FUNC msg_handler;
   void *cookie;
-  struct tr_rp_client *rp_gss;         /* Client matching GSS name, TBD -- FIX ME (??) */
-  TRPS_CONNECTION_SET *connections; /* active GSS connections */
+  TRP_CONNECTION *conn; /* connections to peers */
+  TR_MQ *mq;
 };
 
-typedef enum {
-  TRPS_ERR_OK=0, /* success */
-  TRPS_ERR_NOMEM, /* allocation problem */
-  TRPS_ERR_MAX_CONN, /* out of connections */
-  TRPS_ERR_UNKNOWN /* catch-all */
-} TRPS_ERR;
 
-/* prototypes */
-
-/* these should probably  be static? */
-TRPS_CONNECTION *trps_connection_new(TALLOC_CTX *mem_ctx);
-TRPS_CONNECTION_SET *trps_connection_set_new(TALLOC_CTX *mem_ctx);
-TRPS_ERR trps_connection_set_add(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *new_conn);
-TRPS_ERR trps_connection_set_del(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *conn);
-unsigned int trps_connection_set_len(TRPS_CONNECTION_SET *tcs);
-
-TRPC_INSTANCE *trpc_create (TALLOC_CTX *mem_ctx);
-void trpc_destroy (TRPC_INSTANCE *trpc);
+TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx);
+void trp_connection_free(TRP_CONNECTION *conn);
+void trp_connection_close(TRP_CONNECTION *conn);
+int trp_connection_get_fd(TRP_CONNECTION *conn);
+void trp_connection_set_fd(TRP_CONNECTION *conn, int fd);
+TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn);
+void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname);
+gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn);
+void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx);
+TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn);
+pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn);
+void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread);
+TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn);
+TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove);
+void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new);
+int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data);
+TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname);
+
+TRPC_INSTANCE *trpc_new (TALLOC_CTX *mem_ctx);
+void trpc_free (TRPC_INSTANCE *trpc);
 int trpc_open_connection (TRPC_INSTANCE *trpc, char *server, unsigned int port, gss_ctx_id_t *gssctx);
 int trpc_send_msg (TRPC_INSTANCE *trpc, int conn, gss_ctx_id_t gssctx, const char *msg_content,
                    int *resp_handler(), void *cookie);
 
-TRPS_INSTANCE *trps_create (TALLOC_CTX *mem_ctx);
-void trps_destroy (TRPS_INSTANCE *trps);
+TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx);
+void trps_free (TRPS_INSTANCE *trps);
 int trps_send_msg (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t gssctx, const char *msg_content);
 int trps_accept(TRPS_INSTANCE *trps, int listen);
-
+void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new);
+void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove);
+int trps_get_listener(TRPS_INSTANCE *trps,
+                      TRPS_MSG_FUNC msg_handler,
+                      TRP_AUTH_FUNC auth_handler,
+                      const char *hostname,
+                      unsigned int port,
+                      void *cookie);
+int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data);
+TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps);
+void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg);
+void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn);
 #endif /* TRP_INTERNAL_H */