Beginning of JSON parser for TRP messages (nonfunctional)
[trust_router.git] / include / trp_internal.h
1 #ifndef TRP_INTERNAL_H
2 #define TRP_INTERNAL_H
3
4 #include <talloc.h>
5
6 #include <gsscon.h>
7 #include <trust_router/tr_dh.h>
8
9 #define TRP_PORT 12310
10 #define TRP_METRIC_INFINITY 0xFFFF
11
12 typedef enum trp_rc {
13   TRP_SUCCESS=0,
14   TRP_ERROR, /* generic error */
15   TRP_NOPARSE, /* parse error */
16   TRP_NOMEM, /* allocation error */
17 } TRP_RC;
18
19 /*** Messages ***/
20 typedef enum trp_msg_type {
21   TRP_MSG_TYPE_UNKNOWN=0, /* conveniently, JSON parser returns 0 if a non-integer number is specified */
22   TRP_MSG_TYPE_UPDATE,
23   TRP_MSG_TYPE_ROUTE_REQ
24 } TRP_MSG_TYPE;
25
26 typedef struct trp_msg {
27   TRP_MSG_TYPE type;
28   void *body;
29 } TRP_MSG;
30
31 typedef struct trp_msg_body_update TRP_MSG_BODY_UPDATE;
32 struct trp_msg_body_update {
33   TRP_MSG_BODY_UPDATE *next;
34   TR_NAME *community;
35   TR_NAME *realm;
36   TR_NAME *trust_router;
37   unsigned int metric;
38   unsigned int interval;
39 };
40
41 typedef struct trp_msg_body_route_req {
42   TR_NAME *community;
43   TR_NAME *realm;
44 } TRP_MSG_BODY_ROUTE_REQ;
45
46 TRP_MSG_TYPE trp_msg_type_from_string(const char *s);
47 const char *trp_msg_type_to_string(TRP_MSG_TYPE msgtype);
48
49 TRP_MSG *trp_msg_new(TALLOC_CTX *mem_ctx);
50 void trp_msg_destroy(TRP_MSG *msg);
51
52
53 typedef struct trps_instance TRPS_INSTANCE;
54
55 /* REMOVE THIS!! --jennifer, 2016-06-13 */
56 typedef TRP_MSG TRP_REQ;
57 typedef TRP_MSG TRP_RESP;
58
59
60 typedef int (TRPS_REQ_FUNC)(TRPS_INSTANCE *, TRP_REQ *, TRP_RESP *, void *);
61 typedef void (TRPS_RESP_FUNC)(TRPS_INSTANCE *, TRP_REQ *, TRP_RESP *, void *);
62 typedef int (trps_auth_func)(gss_name_t client_name, TR_NAME *display_name, void *cookie);
63
64
65 /* encapsulate a gss context and its connection file handle */
66 typedef struct trps_connection {
67   int conn;
68   gss_ctx_id_t *gssctx;
69 } TRPS_CONNECTION;
70
71 /* a collection of the above */
72 #define TRPS_CONNECTIONS_MAX 10
73 typedef struct trps_connection_set {
74   TRPS_CONNECTION *conn[TRPS_CONNECTIONS_MAX];
75   unsigned int nconn;
76 } TRPS_CONNECTION_SET;
77
78 /* TRP Client Instance Data */
79 typedef struct trpc_instance {
80   DH *client_dh;                        /* Client's DH struct with priv and pub keys */
81 } TRPC_INSTANCE;
82
83 /* TRP Server Instance Data */
84 struct trps_instance {
85   char *hostname;
86   unsigned int port;
87   TRPS_REQ_FUNC *req_handler;
88   trps_auth_func *auth_handler;
89   void *cookie;
90   struct tr_rp_client *rp_gss;          /* Client matching GSS name, TBD -- FIX ME (??) */
91   TRPS_CONNECTION_SET *connections; /* active GSS connections */
92 };
93
94 typedef enum {
95   TRPS_ERR_OK=0, /* success */
96   TRPS_ERR_NOMEM, /* allocation problem */
97   TRPS_ERR_MAX_CONN, /* out of connections */
98   TRPS_ERR_UNKNOWN /* catch-all */
99 } TRPS_ERR;
100
101 /* prototypes */
102
103 /* these should probably  be static? */
104 TRPS_CONNECTION *trps_connection_new(TALLOC_CTX *mem_ctx);
105 TRPS_CONNECTION_SET *trps_connection_set_new(TALLOC_CTX *mem_ctx);
106 TRPS_ERR trps_connection_set_add(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *new_conn);
107 TRPS_ERR trps_connection_set_del(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *conn);
108 unsigned int trps_connection_set_len(TRPS_CONNECTION_SET *tcs);
109
110 TRPC_INSTANCE *trpc_create (TALLOC_CTX *mem_ctx);
111 void trpc_destroy (TRPC_INSTANCE *trpc);
112 int trpc_open_connection (TRPC_INSTANCE *trpc, char *server, unsigned int port, gss_ctx_id_t *gssctx);
113 int trpc_send_msg (TRPC_INSTANCE *trpc, int conn, gss_ctx_id_t gssctx, const char *msg_content,
114                    int *resp_handler(), void *cookie);
115
116 TRPS_INSTANCE *trps_create (TALLOC_CTX *mem_ctx);
117 void trps_destroy (TRPS_INSTANCE *trps);
118 int trps_send_msg (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t gssctx, const char *msg_content);
119 int trps_accept(TRPS_INSTANCE *trps, int listen);
120
121 #endif /* TRP_INTERNAL_H */