The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
authlistener.c
Go to the documentation of this file.
1 
23 #include <projectcommon.h>
24 #include <memory.h>
25 #include <glib.h>
26 #include <frame.h>
27 #include <frameset.h>
28 #define IS_LISTENER_SUBCLASS
29 #include <authlistener.h>
36 
38 
43 #define ONESEC 1000000
44 
46 FSTATIC gboolean
48 {
49  AuthListener* aself = CASTTOCLASS(AuthListener, self);
50  AuthListenerAction action;
51 
52  if (aself->authenticator && !aself->authenticator(fs)) {
53  char * addr_s = addr->baseclass.toString(&addr->baseclass);
54  g_warning("%s.%d: Received unauthorized command [%d] from address %s"
55  " sender key id: %s, sender identity: %s"
56  , __FUNCTION__, __LINE__, fs->fstype, addr_s
59  FREE(addr_s); addr_s=NULL;
60  goto returnout;
61  }
62 
63  action = g_hash_table_lookup(aself->actionmap, GINT_TO_POINTER((int)fs->fstype));
64 
65  if (NULL == action) {
66  g_warning("%s received FrameSet of unrecognized type %u", __FUNCTION__, fs->fstype);
67  goto returnout;
68  }
69  action(aself, fs, addr);
70 
71 
72 
73 returnout:
74  if (aself->autoack) {
75  DUMP3("_authlistener_got_frameset: AutoACKing FrameSet", &fs->baseclass, NULL);
76  self->transport->_netio->ackmessage(self->transport->_netio, addr, fs);
77  }
78  UNREF(fs);
79  return TRUE;
80 }
81 FSTATIC void
83 {
84  AuthListener* self = CASTTOCLASS(AuthListener, lself);
85  GHashTableIter iter;
86  gpointer key;
87  gpointer value;
88  if (lself->transport) {
89  lself->dissociate(lself);
90  }
91  g_hash_table_iter_init(&iter, self->actionmap);
92  while (g_hash_table_iter_next(&iter, &key, &value)) {
93  guint16 fstype = GPOINTER_TO_INT(key);
94  transport->addListener(transport, fstype, lself);
95  }
96  lself->transport = transport;
97 }
98 FSTATIC void
100 {
101  AuthListener* self = CASTTOCLASS(AuthListener, lself);
102  GHashTableIter iter;
103  gpointer key;
104  gpointer value;
105  if (!lself->transport) {
106  return;
107  }
108  g_hash_table_iter_init(&iter, self->actionmap);
109  while (g_hash_table_iter_next(&iter, &key, &value)) {
110  guint16 fstype = GPOINTER_TO_INT(key);
111  lself->transport->addListener(lself->transport, fstype, NULL);
112  }
113  lself->transport = NULL;
114 }
115 
117 FSTATIC void
119 {
120  AuthListener* self = CASTTOCLASS(AuthListener, aself);
121  self->baseclass.dissociate(&self->baseclass);
122  if (self->actionmap) {
123  g_hash_table_destroy(self->actionmap);
124  self->actionmap = NULL;
125  }
126  _listener_finalize(aself);
127 }
128 
129 
133 authlistener_new(gsize objsize,
136  gboolean autoack,
137  gboolean(*authenticator)(const FrameSet*fs))
138 {
139  AuthListener * newlistener;
141  if (objsize < sizeof(AuthListener)) {
142  objsize = sizeof(AuthListener);
143  }
144  newlistener = NEWSUBCLASS(AuthListener, listener_new(config, objsize));
145  if (newlistener != NULL) {
146  GHashTable* hash;
151  hash = g_hash_table_new(NULL, NULL);
152  for (; map->action; ++map) {
153  g_hash_table_insert(hash, GINT_TO_POINTER(map->framesettype), map->action);
154  }
155  newlistener->actionmap = hash;
156  newlistener->autoack = autoack;
157  newlistener->authenticator = authenticator;
158  }
159  return newlistener;
160 }
Listener baseclass
Definition: authlistener.h:42
AssimObj baseclass
Definition: frameset.h:47
int framesettype
FrameSet class type
Definition: authlistener.h:52
Structure associating FrameSet class types with actions to perform when they're received.
Definition: authlistener.h:51
Listener * listener_new(ConfigContext *config, gsize objsize)
Construct a new Listener - setting up GSource and timeout data structures for it. ...
Definition: listener.c:82
Implements minimal client-oriented Frame and Frameset capabilities.
FSTATIC void _authlistener_finalize(AssimObj *self)
Finalize a Listener.
Definition: authlistener.c:118
AssimObj baseclass
Definition: netaddr.h:44
void(* AuthListenerAction)(AuthListener *, FrameSet *, NetAddr *)
Definition: authlistener.h:49
WINEXPORT const char * frameset_sender_identity(const FrameSet *self)
Return the identity of this Frameset's CryptFrame sender – NULL if None or Unknown.
Definition: frameset.c:432
#define FSTATIC
Definition: projectcommon.h:31
FSTATIC void _authlistener_dissociate(Listener *self)
Definition: authlistener.c:99
guint16 fstype
Type of frameset.
Definition: frameset.h:52
#define BINDDEBUG(Cclass)
BINDDEBUG is for telling the class system where the debug variable for this class is - put it in the ...
Definition: proj_classes.h:82
#define __FUNCTION__
#define DUMP3(prefix, obj, suffix)
Definition: proj_classes.h:97
FSTATIC gboolean _authlistener_got_frameset(Listener *self, FrameSet *, NetAddr *)
Function called when a FrameSet class arrived from the given NetAddr class.
Definition: authlistener.c:47
This is the AuthListener class. object - which (authorizes and) obeys packets from the Authority...
Definition: authlistener.h:41
gboolean autoack
Definition: authlistener.h:44
#define FREE(m)
Our interface to free.
Definition: projectcommon.h:29
Implements basic Frame class.
GHashTable * actionmap
Definition: authlistener.h:43
Project common header file.
WINEXPORT const char * frameset_sender_key_id(const FrameSet *self)
Return the key_id of this Frameset's CryptFrame sender – NULL if None.
Definition: frameset.c:415
NetGSource * transport
Definition: listener.h:44
FSTATIC void _authlistener_associate(Listener *self, NetGSource *transport)
Definition: authlistener.c:82
void(* addListener)(NetGSource *, guint16, Listener *)
Register a new listener.
Definition: netgsource.h:55
AuthListenerAction action
What to do when we get it.
Definition: authlistener.h:53
void(* _finalize)(AssimObj *)
Free object (private)
Definition: assimobj.h:55
The NetAddr class class represents a general network address - whether IP, MAC, or any other type of ...
Definition: netaddr.h:43
gboolean(* got_frameset)(Listener *self, FrameSet *fs, NetAddr *na)
called when a FrameSet arrives
Definition: listener.h:45
gboolean(* authenticator)(const FrameSet *fs)
Definition: authlistener.h:45
FrameSet class - used for collecting Frames when not on the wire, and for marshalling/demarshalling t...
Definition: frameset.h:46
gchar *(* toString)(gconstpointer)
Produce malloc-ed string representation.
Definition: assimobj.h:58
struct _AuthListener AuthListener
Definition: authlistener.h:31
void(* dissociate)(Listener *self)
Dissociate us from our source.
Definition: listener.h:51
#define DEBUGDECLARATIONS
Definition: proj_classes.h:79
AssimObj baseclass
Definition: listener.h:42
void(* associate)(Listener *self, NetGSource *source)
NetGSource class to associate with
Definition: listener.h:49
Defines Listener interfaces for packets coming from the Collective Authority.
#define CASTTOCLASS(Cclass, obj)
Safely cast 'obj' to C-class 'class' - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
#define UNREF(obj)
Definition: assimobj.h:35
The NetGSource class objects integrate NetIO class objects into the g_main_loop paradigm.
Definition: netgsource.h:43
#define NEWSUBCLASS(Cclass, obj)
Definition: proj_classes.h:67
AuthListener * authlistener_new(gsize objsize, ObeyFrameSetTypeMap *map, ConfigContext *config, gboolean autoack, gboolean(*authenticator)(const FrameSet *fs))
Construct a new Listener - setting up GSource and timeout data structures for it. ...
Definition: authlistener.c:133
This is the Listener class. object - which generically listens for packets.
Definition: listener.h:41
ReliableUDP * transport
Definition: pinger.c:70
void _listener_finalize(AssimObj *self)
Finalize a Listener.
Definition: listener.c:53