Encode route_req messages.
[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_BADTYPE, /* typing error */
18   TRP_UNSUPPORTED, /* unsupported feature */
19 } TRP_RC;
20
21 /*** Messages ***/
22 typedef enum trp_msg_type {
23   TRP_MSG_TYPE_UNKNOWN=0, /* conveniently, JSON parser returns 0 if a non-integer number is specified */
24   TRP_MSG_TYPE_UPDATE,
25   TRP_MSG_TYPE_ROUTE_REQ
26 } TRP_MSG_TYPE;
27
28 /* info records */
29 typedef enum trp_msg_info_type {
30   TRP_MSG_INFO_TYPE_UNKNOWN=0, /* conveniently, JSON parser returns 0 if a non-integer number is specified */
31   TRP_MSG_INFO_TYPE_ROUTE,
32   TRP_MSG_INFO_TYPE_COMMUNITY, /* not yet implemented (2016-06-14) */
33 } TRP_MSG_INFO_TYPE;
34
35 typedef struct trp_msg {
36   TRP_MSG_TYPE type;
37   void *body;
38 } TRP_MSG;
39
40 /* update msg record types */
41 typedef struct trp_msg_info_route TRP_MSG_INFO_ROUTE;
42 struct trp_msg_info_route {
43   void *next;
44   TRP_MSG_INFO_TYPE type;
45   TR_NAME *comm;
46   TR_NAME *realm;
47   TR_NAME *trust_router;
48   unsigned int metric;
49   unsigned int interval;
50 };
51
52 /* TODO: define struct trp_msg_info_community */
53
54 typedef struct trp_route_update {
55   void *records;
56 } TRP_ROUTE_UPDATE;
57
58 typedef struct trp_route_req {
59   TR_NAME *comm;
60   TR_NAME *realm;
61 } TRP_ROUTE_REQ;
62
63 TRP_MSG_TYPE trp_msg_type_from_string(const char *s);
64 const char *trp_msg_type_to_string(TRP_MSG_TYPE msgtype);
65 TRP_MSG_INFO_TYPE trp_msg_info_type_from_string(const char *s);
66 const char *trp_msg_info_type_to_string(TRP_MSG_INFO_TYPE msgtype);
67
68 TRP_MSG *trp_msg_new(TALLOC_CTX *mem_ctx);
69 void trp_msg_destroy(TRP_MSG *msg);
70 void trp_msg_pprint(TRP_MSG *msg);
71 char *trp_encode_msg(TRP_MSG *msg);
72
73
74 typedef struct trps_instance TRPS_INSTANCE;
75
76 /* REMOVE THIS!! --jennifer, 2016-06-13 */
77 typedef TRP_MSG TRP_REQ;
78 typedef TRP_MSG TRP_RESP;
79
80
81 typedef int (TRPS_REQ_FUNC)(TRPS_INSTANCE *, TRP_REQ *, TRP_RESP *, void *);
82 typedef void (TRPS_RESP_FUNC)(TRPS_INSTANCE *, TRP_REQ *, TRP_RESP *, void *);
83 typedef int (trps_auth_func)(gss_name_t client_name, TR_NAME *display_name, void *cookie);
84
85
86 /* encapsulate a gss context and its connection file handle */
87 typedef struct trps_connection {
88   int conn;
89   gss_ctx_id_t *gssctx;
90 } TRPS_CONNECTION;
91
92 /* a collection of the above */
93 #define TRPS_CONNECTIONS_MAX 10
94 typedef struct trps_connection_set {
95   TRPS_CONNECTION *conn[TRPS_CONNECTIONS_MAX];
96   unsigned int nconn;
97 } TRPS_CONNECTION_SET;
98
99 /* TRP Client Instance Data */
100 typedef struct trpc_instance {
101   DH *client_dh;                        /* Client's DH struct with priv and pub keys */
102 } TRPC_INSTANCE;
103
104 /* TRP Server Instance Data */
105 struct trps_instance {
106   char *hostname;
107   unsigned int port;
108   TRPS_REQ_FUNC *req_handler;
109   trps_auth_func *auth_handler;
110   void *cookie;
111   struct tr_rp_client *rp_gss;          /* Client matching GSS name, TBD -- FIX ME (??) */
112   TRPS_CONNECTION_SET *connections; /* active GSS connections */
113 };
114
115 typedef enum {
116   TRPS_ERR_OK=0, /* success */
117   TRPS_ERR_NOMEM, /* allocation problem */
118   TRPS_ERR_MAX_CONN, /* out of connections */
119   TRPS_ERR_UNKNOWN /* catch-all */
120 } TRPS_ERR;
121
122 /* prototypes */
123
124 /* these should probably  be static? */
125 TRPS_CONNECTION *trps_connection_new(TALLOC_CTX *mem_ctx);
126 TRPS_CONNECTION_SET *trps_connection_set_new(TALLOC_CTX *mem_ctx);
127 TRPS_ERR trps_connection_set_add(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *new_conn);
128 TRPS_ERR trps_connection_set_del(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *conn);
129 unsigned int trps_connection_set_len(TRPS_CONNECTION_SET *tcs);
130
131 TRPC_INSTANCE *trpc_create (TALLOC_CTX *mem_ctx);
132 void trpc_destroy (TRPC_INSTANCE *trpc);
133 int trpc_open_connection (TRPC_INSTANCE *trpc, char *server, unsigned int port, gss_ctx_id_t *gssctx);
134 int trpc_send_msg (TRPC_INSTANCE *trpc, int conn, gss_ctx_id_t gssctx, const char *msg_content,
135                    int *resp_handler(), void *cookie);
136
137 TRPS_INSTANCE *trps_create (TALLOC_CTX *mem_ctx);
138 void trps_destroy (TRPS_INSTANCE *trps);
139 int trps_send_msg (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t gssctx, const char *msg_content);
140 int trps_accept(TRPS_INSTANCE *trps, int listen);
141
142 #endif /* TRP_INTERNAL_H */