Use accessor functions for TRP objects.
[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_UPD *trp_upd_new(TALLOC_CTX *mem_ctx);
50 void trp_upd_free(TRP_UPD *update);
51 TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd);
52 void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec);
53 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type);
54 void trp_inforec_free(TRP_INFOREC *rec);
55 TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec);
56 void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec);
57 TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec);
58 void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type);
59 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec);
60 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm);
61 TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec);
62 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm);
63 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec);
64 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router);
65 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec);
66 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric);
67 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec);
68 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval);
69 TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s);
70 const char *trp_inforec_type_to_string(TRP_INFOREC_TYPE msgtype);
71
72
73 TRP_REQ *trp_req_new(TALLOC_CTX *mem_ctx);
74 void trp_req_free(TRP_REQ *req);
75 TR_NAME *trp_req_get_comm(TRP_REQ *req);
76 void trp_req_set_comm(TRP_REQ *req, TR_NAME *comm);
77 TR_NAME *trp_req_get_realm(TRP_REQ *req);
78 void trp_req_set_realm(TRP_REQ *req, TR_NAME *realm);
79
80 typedef struct trps_instance TRPS_INSTANCE;
81
82 typedef int (TRPS_REQ_FUNC)();
83 typedef void (TRPS_RESP_FUNC)();
84 typedef int (trps_auth_func)(gss_name_t client_name, TR_NAME *display_name, void *cookie);
85
86
87 /* encapsulate a gss context and its connection file handle */
88 typedef struct trps_connection {
89   int conn;
90   gss_ctx_id_t *gssctx;
91 } TRPS_CONNECTION;
92
93 /* a collection of the above */
94 #define TRPS_CONNECTIONS_MAX 10
95 typedef struct trps_connection_set {
96   TRPS_CONNECTION *conn[TRPS_CONNECTIONS_MAX];
97   unsigned int nconn;
98 } TRPS_CONNECTION_SET;
99
100 /* TRP Client Instance Data */
101 typedef struct trpc_instance {
102   DH *client_dh;                        /* Client's DH struct with priv and pub keys */
103 } TRPC_INSTANCE;
104
105 /* TRP Server Instance Data */
106 struct trps_instance {
107   char *hostname;
108   unsigned int port;
109   TRPS_REQ_FUNC *req_handler;
110   trps_auth_func *auth_handler;
111   void *cookie;
112   struct tr_rp_client *rp_gss;          /* Client matching GSS name, TBD -- FIX ME (??) */
113   TRPS_CONNECTION_SET *connections; /* active GSS connections */
114 };
115
116 typedef enum {
117   TRPS_ERR_OK=0, /* success */
118   TRPS_ERR_NOMEM, /* allocation problem */
119   TRPS_ERR_MAX_CONN, /* out of connections */
120   TRPS_ERR_UNKNOWN /* catch-all */
121 } TRPS_ERR;
122
123 /* prototypes */
124
125 /* these should probably  be static? */
126 TRPS_CONNECTION *trps_connection_new(TALLOC_CTX *mem_ctx);
127 TRPS_CONNECTION_SET *trps_connection_set_new(TALLOC_CTX *mem_ctx);
128 TRPS_ERR trps_connection_set_add(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *new_conn);
129 TRPS_ERR trps_connection_set_del(TRPS_CONNECTION_SET *tcs, TRPS_CONNECTION *conn);
130 unsigned int trps_connection_set_len(TRPS_CONNECTION_SET *tcs);
131
132 TRPC_INSTANCE *trpc_create (TALLOC_CTX *mem_ctx);
133 void trpc_destroy (TRPC_INSTANCE *trpc);
134 int trpc_open_connection (TRPC_INSTANCE *trpc, char *server, unsigned int port, gss_ctx_id_t *gssctx);
135 int trpc_send_msg (TRPC_INSTANCE *trpc, int conn, gss_ctx_id_t gssctx, const char *msg_content,
136                    int *resp_handler(), void *cookie);
137
138 TRPS_INSTANCE *trps_create (TALLOC_CTX *mem_ctx);
139 void trps_destroy (TRPS_INSTANCE *trps);
140 int trps_send_msg (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t gssctx, const char *msg_content);
141 int trps_accept(TRPS_INSTANCE *trps, int listen);
142
143 #endif /* TRP_INTERNAL_H */