Decode JSON TRP messages, then send to main thread.
[trust_router.git] / include / trp_internal.h
1 #ifndef TRP_INTERNAL_H
2 #define TRP_INTERNAL_H
3
4 #include <pthread.h>
5 #include <talloc.h>
6
7 #include <gsscon.h>
8 #include <trust_router/tr_dh.h>
9 #include <tr_mq.h>
10 #include <tr_msg.h>
11 #include <trust_router/trp.h>
12
13 /* info records */
14 /* TRP update record types */
15 typedef struct trp_inforec_route {
16   TR_NAME *comm;
17   TR_NAME *realm;
18   TR_NAME *trust_router;
19   unsigned int metric;
20   unsigned int interval;
21 } TRP_INFOREC_ROUTE;
22
23 /* TODO: define struct trp_msg_info_community */
24
25 typedef union trp_inforec_data {
26   TRP_INFOREC_ROUTE *route;
27   /* TRP_INFOREC_COMM *comm; */
28 } TRP_INFOREC_DATA;
29
30 struct trp_inforec {
31   TRP_INFOREC *next;
32   TRP_INFOREC_TYPE type;
33   TRP_INFOREC_DATA data; /* contains pointer to one of the record types */
34 };
35
36 struct trp_update {
37   TRP_INFOREC *records;
38 };
39
40 struct trp_req {
41   TR_NAME *comm;
42   TR_NAME *realm;
43 };
44
45
46 typedef struct trps_instance TRPS_INSTANCE;
47
48
49 typedef enum trp_connection_status {
50   TRP_CONNECTION_DOWN=0,
51   TRP_CONNECTION_UP,
52 } TRP_CONNECTION_STATUS;
53
54 typedef struct trp_connection TRP_CONNECTION;
55 struct trp_connection {
56   TRP_CONNECTION *next;
57   pthread_t *thread; /* thread servicing this connection */
58   int fd;
59   TR_NAME *gssname;
60   gss_ctx_id_t *gssctx;
61   TRP_CONNECTION_STATUS status;
62   pthread_mutex_t status_mutex;
63 };
64
65 typedef TRP_RC (*TRPS_MSG_FUNC)(TRPS_INSTANCE *, TRP_CONNECTION *, TR_MSG *);
66 typedef void (*TRP_RESP_FUNC)();
67 /*typedef int (*TRP_AUTH_FUNC)(gss_name_t client_name, TR_NAME *display_name, void *cookie);*/
68 typedef client_cb_fn TRP_AUTH_FUNC;
69
70 /* TRP Client Instance Data */
71 typedef struct trpc_instance {
72   TRP_CONNECTION *conn;
73   DH *client_dh;                        /* Client's DH struct with priv and pub keys */
74 } TRPC_INSTANCE;
75
76 /* TRP Server Instance Data */
77 struct trps_instance {
78   char *hostname;
79   unsigned int port;
80   TRP_AUTH_FUNC auth_handler;
81   TRPS_MSG_FUNC msg_handler;
82   void *cookie;
83   TRP_CONNECTION *conn; /* connections to peers */
84   TR_MQ *mq;
85 };
86
87
88 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx);
89 void trp_connection_free(TRP_CONNECTION *conn);
90 void trp_connection_close(TRP_CONNECTION *conn);
91 int trp_connection_get_fd(TRP_CONNECTION *conn);
92 void trp_connection_set_fd(TRP_CONNECTION *conn, int fd);
93 TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn);
94 void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname);
95 gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn);
96 void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx);
97 TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn);
98 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn);
99 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread);
100 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn);
101 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove);
102 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new);
103 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data);
104 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname);
105
106 TRPC_INSTANCE *trpc_new (TALLOC_CTX *mem_ctx);
107 void trpc_free (TRPC_INSTANCE *trpc);
108 int trpc_open_connection (TRPC_INSTANCE *trpc, char *server, unsigned int port, gss_ctx_id_t *gssctx);
109 int trpc_send_msg (TRPC_INSTANCE *trpc, int conn, gss_ctx_id_t gssctx, const char *msg_content,
110                    int *resp_handler(), void *cookie);
111
112 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx);
113 void trps_free (TRPS_INSTANCE *trps);
114 int trps_send_msg (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t gssctx, const char *msg_content);
115 int trps_accept(TRPS_INSTANCE *trps, int listen);
116 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new);
117 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove);
118 int trps_get_listener(TRPS_INSTANCE *trps,
119                       TRPS_MSG_FUNC msg_handler,
120                       TRP_AUTH_FUNC auth_handler,
121                       const char *hostname,
122                       unsigned int port,
123                       void *cookie);
124 int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data);
125 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps);
126 void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg);
127 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn);
128 #endif /* TRP_INTERNAL_H */