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