e5b9df757b299296ed9a01b7377605f591fd8f8f
[trust_router.git] / trp / trp_req.c
1 #include <jansson.h>
2 #include <talloc.h>
3
4 #include <trust_router/tr_name.h>
5 #include <trp_internal.h>
6 #include <tr_debug.h>
7
8 static int trp_req_destructor(void *object)
9 {
10   TRP_REQ *req=talloc_get_type_abort(object, TRP_REQ);
11   
12   /* clean up TR_NAME data, which are not managed by talloc */
13   if (req->comm != NULL)
14     tr_free_name(req->comm);
15
16   if (req->realm != NULL)
17     tr_free_name(req->realm);
18
19   if (req->peer != NULL)
20     tr_free_name(req->peer);
21
22   return 0;
23 }
24
25 TRP_REQ *trp_req_new(TALLOC_CTX *mem_ctx)
26 {
27   TRP_REQ *new_req=talloc(mem_ctx, TRP_REQ);
28
29   if (new_req != NULL) {
30     new_req->comm=NULL;
31     new_req->realm=NULL;
32     new_req->peer=NULL;
33   }
34
35   talloc_set_destructor((void *)new_req, trp_req_destructor);
36   return new_req;
37 }
38
39 void trp_req_free(TRP_REQ *req)
40 {
41   if (req!=NULL)
42     talloc_free(req);
43 }
44
45 TR_NAME *trp_req_get_comm(TRP_REQ *req)
46 {
47   if (req!=NULL)
48     return req->comm;
49   else
50     return NULL;
51 }
52
53 void trp_req_set_comm(TRP_REQ *req, TR_NAME *comm)
54 {
55   if (req)
56     req->comm=comm;
57 }
58
59 TR_NAME *trp_req_get_realm(TRP_REQ *req)
60 {
61   if (req!=NULL)
62     return req->realm;
63   else
64     return NULL;
65 }
66
67
68 void trp_req_set_realm(TRP_REQ *req, TR_NAME *realm)
69 {
70   if (req)
71     req->realm=realm;
72 }
73
74 TR_NAME *trp_req_get_peer(TRP_REQ *req)
75 {
76   if (req!=NULL)
77     return req->peer;
78   else
79     return NULL;
80 }
81
82
83 void trp_req_set_peer(TRP_REQ *req, TR_NAME *peer)
84 {
85   if (req)
86     req->peer=peer;
87 }
88
89 /* Defines what we use as a wildcard for realm or community name.
90  * Must not be a valid name for either of those. Currently, we
91  * use the empty string. */
92 static int trp_req_name_is_wildcard(TR_NAME *name)
93 {
94   return (name!=NULL) && (name->len==0) && (name->buf!=NULL) && (name->buf[0]='\0');
95 }
96
97 int trp_req_is_wildcard(TRP_REQ *req)
98 {
99   return (req!=NULL) && trp_req_name_is_wildcard(req->comm) && trp_req_name_is_wildcard(req->realm);
100 }