Move TRP messaging to tr_msg.c. Fix old bug.
[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 #include <trust_router/trp.h>
9
10 /* info records */
11 typedef enum trp_inforec_type {
12   TRP_INFOREC_TYPE_UNKNOWN=0, /* conveniently, JSON parser returns 0 if a non-integer number is specified */
13   TRP_INFOREC_TYPE_ROUTE,
14   TRP_INFOREC_TYPE_COMMUNITY, /* not yet implemented (2016-06-14) */
15 } TRP_INFOREC_TYPE;
16
17 /* TRP update record types */
18 typedef struct trp_inforec_route {
19   TR_NAME *comm;
20   TR_NAME *realm;
21   TR_NAME *trust_router;
22   unsigned int metric;
23   unsigned int interval;
24 } TRP_INFOREC_ROUTE;
25
26 /* TODO: define struct trp_msg_info_community */
27
28 typedef union trp_inforec_data {
29   TRP_INFOREC_ROUTE *route;
30   /* TRP_INFOREC_COMM *comm; */
31 } TRP_INFOREC_DATA;
32
33 typedef struct trp_inforec TRP_INFOREC;
34 struct trp_inforec {
35   TRP_INFOREC *next;
36   TRP_INFOREC_TYPE type;
37   TRP_INFOREC_DATA data; /* contains pointer to one of the record types */
38 };
39
40 struct trp_update {
41   TRP_INFOREC *records;
42 };
43
44 struct trp_req {
45   TR_NAME *comm;
46   TR_NAME *realm;
47 };
48
49 TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s);
50 const char *trp_inforec_type_to_string(TRP_INFOREC_TYPE msgtype);
51
52 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx);
53 void trp_upd_free(TRP_UPD *update);
54 TRP_REQ *trp_req_new(TALLOC_CTX *mem_ctx);
55 void trp_req_free(TRP_REQ *req);
56 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type);
57 void trp_inforec_free(TRP_INFOREC *rec);
58 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec);
59 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm);
60 TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec);
61 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm);
62 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec);
63 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router);
64 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec);
65 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric);
66 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec);
67 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval);
68
69
70 typedef struct trps_instance TRPS_INSTANCE;
71
72 typedef int (TRPS_REQ_FUNC)();
73 typedef void (TRPS_RESP_FUNC)();
74 typedef int (trps_auth_func)(gss_name_t client_name, TR_NAME *display_name, void *cookie);
75
76
77 /* encapsulate a gss context and its connection file handle */
78 typedef struct trps_connection {
79   int conn;
80   gss_ctx_id_t *gssctx;
81 } TRPS_CONNECTION;
82
83 /* a collection of the above */
84 #define TRPS_CONNECTIONS_MAX 10
85 typedef struct trps_connection_set {
86   TRPS_CONNECTION *conn[TRPS_CONNECTIONS_MAX];
87   unsigned int nconn;
88 } TRPS_CONNECTION_SET;
89
90 /* TRP Client Instance Data */
91 typedef struct trpc_instance {
92   DH *client_dh;                        /* Client's DH struct with priv and pub keys */
93 } TRPC_INSTANCE;
94
95 /* TRP Server Instance Data */
96 struct trps_instance {
97   char *hostname;
98   unsigned int port;
99   TRPS_REQ_FUNC *req_handler;
100   trps_auth_func *auth_handler;
101   void *cookie;
102   struct tr_rp_client *rp_gss;          /* Client matching GSS name, TBD -- FIX ME (??) */
103   TRPS_CONNECTION_SET *connections; /* active GSS connections */
104 };
105
106 typedef enum {
107   TRPS_ERR_OK=0, /* success */
108   TRPS_ERR_NOMEM, /* allocation problem */
109   TRPS_ERR_MAX_CONN, /* out of connections */
110   TRPS_ERR_UNKNOWN /* catch-all */
111 } TRPS_ERR;
112
113 /* prototypes */
114
115 /* these should probably  be static? */
116 TRPS_CONNECTION *trps_connection_new(TALLOC_CTX *mem_ctx);
117 TRPS_CONNECTION_SET *trps_connection_set_new(TALLOC_CTX *mem_ctx);
118 TRPS_ERR trps_connection_set_add(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *new_conn);
119 TRPS_ERR trps_connection_set_del(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *conn);
120 unsigned int trps_connection_set_len(TRPS_CONNECTION_SET *tcs);
121
122 TRPC_INSTANCE *trpc_create (TALLOC_CTX *mem_ctx);
123 void trpc_destroy (TRPC_INSTANCE *trpc);
124 int trpc_open_connection (TRPC_INSTANCE *trpc, char *server, unsigned int port, gss_ctx_id_t *gssctx);
125 int trpc_send_msg (TRPC_INSTANCE *trpc, int conn, gss_ctx_id_t gssctx, const char *msg_content,
126                    int *resp_handler(), void *cookie);
127
128 TRPS_INSTANCE *trps_create (TALLOC_CTX *mem_ctx);
129 void trps_destroy (TRPS_INSTANCE *trps);
130 int trps_send_msg (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t gssctx, const char *msg_content);
131 int trps_accept(TRPS_INSTANCE *trps, int listen);
132
133 #endif /* TRP_INTERNAL_H */