Migrated eap-radius plugin to INIT/METHOD macros
authorMartin Willi <martin@revosec.ch>
Wed, 21 Jul 2010 07:15:32 +0000 (09:15 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 21 Jul 2010 15:09:27 +0000 (17:09 +0200)
src/libcharon/plugins/eap_radius/eap_radius.c
src/libcharon/plugins/eap_radius/eap_radius_plugin.c
src/libcharon/plugins/eap_radius/radius_client.c
src/libcharon/plugins/eap_radius/radius_message.c

index 4b1a879..3038e87 100644 (file)
@@ -145,10 +145,8 @@ static bool radius2ike(private_eap_radius_t *this,
        return FALSE;
 }
 
-/**
- * Implementation of eap_method_t.initiate
- */
-static status_t initiate(private_eap_radius_t *this, eap_payload_t **out)
+METHOD(eap_method_t, initiate, status_t,
+       private_eap_radius_t *this, eap_payload_t **out)
 {
        radius_message_t *request, *response;
        status_t status = FAILED;
@@ -218,11 +216,8 @@ static void process_class(private_eap_radius_t *this, radius_message_t *msg)
        enumerator->destroy(enumerator);
 }
 
-/**
- * Implementation of eap_method_t.process
- */
-static status_t process(private_eap_radius_t *this,
-                                               eap_payload_t *in, eap_payload_t **out)
+METHOD(eap_method_t, process, status_t,
+       private_eap_radius_t *this, eap_payload_t *in, eap_payload_t **out)
 {
        radius_message_t *request, *response;
        status_t status = FAILED;
@@ -274,19 +269,15 @@ static status_t process(private_eap_radius_t *this,
        return status;
 }
 
-/**
- * Implementation of eap_method_t.get_type.
- */
-static eap_type_t get_type(private_eap_radius_t *this, u_int32_t *vendor)
+METHOD(eap_method_t, get_type, eap_type_t,
+       private_eap_radius_t *this, u_int32_t *vendor)
 {
        *vendor = this->vendor;
        return this->type;
 }
 
-/**
- * Implementation of eap_method_t.get_msk.
- */
-static status_t get_msk(private_eap_radius_t *this, chunk_t *msk)
+METHOD(eap_method_t, get_msk, status_t,
+       private_eap_radius_t *this, chunk_t *msk)
 {
        if (this->msk.ptr)
        {
@@ -296,10 +287,8 @@ static status_t get_msk(private_eap_radius_t *this, chunk_t *msk)
        return FAILED;
 }
 
-/**
- * Implementation of eap_method_t.is_mutual.
- */
-static bool is_mutual(private_eap_radius_t *this)
+METHOD(eap_method_t, is_mutual, bool,
+       private_eap_radius_t *this)
 {
        switch (this->type)
        {
@@ -311,10 +300,8 @@ static bool is_mutual(private_eap_radius_t *this)
        }
 }
 
-/**
- * Implementation of eap_method_t.destroy.
- */
-static void destroy(private_eap_radius_t *this)
+METHOD(eap_method_t, destroy, void,
+       private_eap_radius_t *this)
 {
        this->peer->destroy(this->peer);
        this->server->destroy(this->server);
@@ -328,15 +315,26 @@ static void destroy(private_eap_radius_t *this)
  */
 eap_radius_t *eap_radius_create(identification_t *server, identification_t *peer)
 {
-       private_eap_radius_t *this = malloc_thing(private_eap_radius_t);
-
-       this->public.eap_method_interface.initiate = (status_t(*)(eap_method_t*,eap_payload_t**))initiate;
-       this->public.eap_method_interface.process = (status_t(*)(eap_method_t*,eap_payload_t*,eap_payload_t**))process;
-       this->public.eap_method_interface.get_type = (eap_type_t(*)(eap_method_t*,u_int32_t*))get_type;
-       this->public.eap_method_interface.is_mutual = (bool(*)(eap_method_t*))is_mutual;
-       this->public.eap_method_interface.get_msk = (status_t(*)(eap_method_t*,chunk_t*))get_msk;
-       this->public.eap_method_interface.destroy = (void(*)(eap_method_t*))destroy;
-
+       private_eap_radius_t *this;
+
+       INIT(this,
+               .public.eap_method_interface = {
+                       .initiate = _initiate,
+                       .process = _process,
+                       .get_type = _get_type,
+                       .is_mutual = _is_mutual,
+                       .get_msk = _get_msk,
+                       .destroy = _destroy,
+               },
+               /* initially EAP_RADIUS, but is set to the method selected by RADIUS */
+               .type = EAP_RADIUS,
+               .eap_start = lib->settings->get_bool(lib->settings,
+                                                               "charon.plugins.eap-radius.eap_start", FALSE),
+               .id_prefix = lib->settings->get_str(lib->settings,
+                                                               "charon.plugins.eap-radius.id_prefix", ""),
+               .class_group = lib->settings->get_bool(lib->settings,
+                                                               "charon.plugins.eap-radius.class_group", FALSE),
+       );
        this->client = radius_client_create();
        if (!this->client)
        {
@@ -345,16 +343,6 @@ eap_radius_t *eap_radius_create(identification_t *server, identification_t *peer
        }
        this->peer = peer->clone(peer);
        this->server = server->clone(server);
-       /* initially EAP_RADIUS, but is set to the method selected by RADIUS */
-       this->type = EAP_RADIUS;
-       this->vendor = 0;
-       this->msk = chunk_empty;
-       this->eap_start = lib->settings->get_bool(lib->settings,
-                                                               "charon.plugins.eap-radius.eap_start", FALSE);
-       this->id_prefix = lib->settings->get_str(lib->settings,
-                                                               "charon.plugins.eap-radius.id_prefix", "");
-       this->class_group = lib->settings->get_bool(lib->settings,
-                                                               "charon.plugins.eap-radius.class_group", FALSE);
        return &this->public;
 }
 
index 7d2788c..e194077 100644 (file)
 
 #include <daemon.h>
 
-/**
- * Implementation of plugin_t.destroy
- */
-static void destroy(eap_radius_plugin_t *this)
+METHOD(plugin_t, destroy, void,
+       eap_radius_plugin_t *this)
 {
        charon->eap->remove_method(charon->eap, (eap_constructor_t)eap_radius_create);
        radius_client_cleanup();
@@ -43,8 +41,9 @@ plugin_t *eap_radius_plugin_create()
                return NULL;
        }
 
-       this = malloc_thing(eap_radius_plugin_t);
-       this->plugin.destroy = (void(*)(plugin_t*))destroy;
+       INIT(this,
+               .plugin.destroy = _destroy,
+       );
 
        charon->eap->add_method(charon->eap, EAP_RADIUS, 0,
                                                        EAP_SERVER, (eap_constructor_t)eap_radius_create);
index 1d1f217..0727bc0 100644 (file)
@@ -261,11 +261,8 @@ static void save_state(private_radius_client_t *this, radius_message_t *msg)
        chunk_free(&this->state);
 }
 
-/**
- * Implementation of radius_client_t.request
- */
-static radius_message_t* request(private_radius_client_t *this,
-                                                                radius_message_t *req)
+METHOD(radius_client_t, request, radius_message_t*,
+       private_radius_client_t *this, radius_message_t *req)
 {
        char virtual[] = {0x00,0x00,0x00,0x05};
        entry_t *socket;
@@ -419,11 +416,9 @@ static chunk_t decrypt_mppe_key(private_radius_client_t *this, u_int16_t salt,
        return chunk_clone(chunk_create(P.ptr + 1, *P.ptr));
 }
 
-/**
- * Implementation of radius_client_t.decrypt_msk
- */
-static chunk_t decrypt_msk(private_radius_client_t *this,
-                                                  radius_message_t *response, radius_message_t *request)
+METHOD(radius_client_t, decrypt_msk, chunk_t,
+       private_radius_client_t *this, radius_message_t *response,
+       radius_message_t *request)
 {
        struct {
                u_int32_t id;
@@ -468,10 +463,8 @@ static chunk_t decrypt_msk(private_radius_client_t *this,
        return chunk_empty;
 }
 
-/**
- * Implementation of radius_client_t.destroy.
- */
-static void destroy(private_radius_client_t *this)
+METHOD(radius_client_t, destroy, void,
+       private_radius_client_t *this)
 {
        free(this->state.ptr);
        free(this);
@@ -482,13 +475,15 @@ static void destroy(private_radius_client_t *this)
  */
 radius_client_t *radius_client_create()
 {
-       private_radius_client_t *this = malloc_thing(private_radius_client_t);
-
-       this->public.request = (radius_message_t*(*)(radius_client_t*, radius_message_t *msg))request;
-       this->public.decrypt_msk = (chunk_t(*)(radius_client_t*, radius_message_t *, radius_message_t *))decrypt_msk;
-       this->public.destroy = (void(*)(radius_client_t*))destroy;
-
-       this->state = chunk_empty;
+       private_radius_client_t *this;
+
+       INIT(this,
+               .public = {
+                       .request = _request,
+                       .decrypt_msk = _decrypt_msk,
+                       .destroy = _destroy,
+               },
+       );
 
        return &this->public;
 }
index 11a1d8d..23a29b7 100644 (file)
@@ -215,13 +215,8 @@ typedef struct {
        int left;
 } attribute_enumerator_t;
 
-
-/**
- * Implementation of attribute_enumerator_t.enumerate
- */
-static bool attribute_enumerate(attribute_enumerator_t *this,
-                                                               int *type, chunk_t *data)
-
+METHOD(enumerator_t, attribute_enumerate, bool,
+       attribute_enumerator_t *this, int *type, chunk_t *data)
 {
        if (this->left == 0)
        {
@@ -241,10 +236,8 @@ static bool attribute_enumerate(attribute_enumerator_t *this,
        return TRUE;
 }
 
-/**
- * Implementation of radius_message_t.create_enumerator
- */
-static enumerator_t* create_enumerator(private_radius_message_t *this)
+METHOD(radius_message_t, create_enumerator, enumerator_t*,
+       private_radius_message_t *this)
 {
        attribute_enumerator_t *e;
 
@@ -252,20 +245,19 @@ static enumerator_t* create_enumerator(private_radius_message_t *this)
        {
                return enumerator_create_empty();
        }
-
-       e = malloc_thing(attribute_enumerator_t);
-       e->public.enumerate = (void*)attribute_enumerate;
-       e->public.destroy = (void*)free;
-       e->next = (rattr_t*)this->msg->attributes;
-       e->left = ntohs(this->msg->length) - sizeof(rmsg_t);
+       INIT(e,
+               .public = {
+                       .enumerate = (void*)_attribute_enumerate,
+                       .destroy = (void*)free,
+               },
+               .next = (rattr_t*)this->msg->attributes,
+               .left = ntohs(this->msg->length) - sizeof(rmsg_t),
+       );
        return &e->public;
 }
 
-/**
- * Implementation of radius_message_t.add
- */
-static void add(private_radius_message_t *this, radius_attribute_type_t type,
-                               chunk_t data)
+METHOD(radius_message_t, add, void,
+       private_radius_message_t *this, radius_attribute_type_t type, chunk_t data)
 {
        rattr_t *attribute;
 
@@ -279,10 +271,8 @@ static void add(private_radius_message_t *this, radius_attribute_type_t type,
        this->msg->length = htons(ntohs(this->msg->length) + attribute->length);
 }
 
-/**
- * Implementation of radius_message_t.sign
- */
-static void sign(private_radius_message_t *this, rng_t *rng, signer_t *signer)
+METHOD(radius_message_t, sign, void,
+       private_radius_message_t *this, rng_t *rng, signer_t *signer)
 {
        char buf[HASH_SIZE_MD5];
 
@@ -297,11 +287,9 @@ static void sign(private_radius_message_t *this, rng_t *rng, signer_t *signer)
                                ((u_char*)this->msg) + ntohs(this->msg->length) - HASH_SIZE_MD5);
 }
 
-/**
- * Implementation of radius_message_t.verify
- */
-static bool verify(private_radius_message_t *this, u_int8_t *req_auth,
-                                  chunk_t secret, hasher_t *hasher, signer_t *signer)
+METHOD(radius_message_t, verify, bool,
+       private_radius_message_t *this, u_int8_t *req_auth, chunk_t secret,
+       hasher_t *hasher, signer_t *signer)
 {
        char buf[HASH_SIZE_MD5], res_auth[HASH_SIZE_MD5];
        enumerator_t *enumerator;
@@ -369,51 +357,39 @@ static bool verify(private_radius_message_t *this, u_int8_t *req_auth,
        return TRUE;
 }
 
-/**
- * Implementation of radius_message_t.get_code
- */
-static radius_message_code_t get_code(private_radius_message_t *this)
+METHOD(radius_message_t, get_code, radius_message_code_t,
+       private_radius_message_t *this)
 {
        return this->msg->code;
 }
 
-/**
- * Implementation of radius_message_t.get_identifier
- */
-static u_int8_t get_identifier(private_radius_message_t *this)
+METHOD(radius_message_t, get_identifier, u_int8_t,
+       private_radius_message_t *this)
 {
        return this->msg->identifier;
 }
 
-/**
- * Implementation of radius_message_t.set_identifier
- */
-static void set_identifier(private_radius_message_t *this, u_int8_t identifier)
+METHOD(radius_message_t, set_identifier, void,
+       private_radius_message_t *this, u_int8_t identifier)
 {
        this->msg->identifier = identifier;
 }
 
-/**
- * Implementation of radius_message_t.get_authenticator
- */
-static u_int8_t* get_authenticator(private_radius_message_t *this)
+METHOD(radius_message_t, get_authenticator, u_int8_t*,
+       private_radius_message_t *this)
 {
        return this->msg->authenticator;
 }
 
 
-/**
- * Implementation of radius_message_t.get_encoding
- */
-static chunk_t get_encoding(private_radius_message_t *this)
+METHOD(radius_message_t, get_encoding, chunk_t,
+       private_radius_message_t *this)
 {
        return chunk_create((u_char*)this->msg, ntohs(this->msg->length));
 }
 
-/**
- * Implementation of radius_message_t.destroy.
- */
-static void destroy(private_radius_message_t *this)
+METHOD(radius_message_t, destroy, void,
+       private_radius_message_t *this)
 {
        free(this->msg);
        free(this);
@@ -424,18 +400,22 @@ static void destroy(private_radius_message_t *this)
  */
 static private_radius_message_t *radius_message_create()
 {
-       private_radius_message_t *this = malloc_thing(private_radius_message_t);
-
-       this->public.create_enumerator = (enumerator_t*(*)(radius_message_t*))create_enumerator;
-       this->public.add = (void(*)(radius_message_t*, radius_attribute_type_t,chunk_t))add;
-       this->public.get_code = (radius_message_code_t(*)(radius_message_t*))get_code;
-       this->public.get_identifier = (u_int8_t(*)(radius_message_t*))get_identifier;
-       this->public.set_identifier = (void(*)(radius_message_t*, u_int8_t identifier))set_identifier;
-       this->public.get_authenticator = (u_int8_t*(*)(radius_message_t*))get_authenticator;
-       this->public.get_encoding = (chunk_t(*)(radius_message_t*))get_encoding;
-       this->public.sign = (void(*)(radius_message_t*, rng_t *rng, signer_t *signer))sign;
-       this->public.verify = (bool(*)(radius_message_t*, u_int8_t *req_auth, chunk_t secret, hasher_t *hasher, signer_t *signer))verify;
-       this->public.destroy = (void(*)(radius_message_t*))destroy;
+       private_radius_message_t *this;
+
+       INIT(this,
+               .public = {
+                       .create_enumerator = _create_enumerator,
+                       .add = _add,
+                       .get_code = _get_code,
+                       .get_identifier = _get_identifier,
+                       .set_identifier = _set_identifier,
+                       .get_authenticator = _get_authenticator,
+                       .get_encoding = _get_encoding,
+                       .sign = _sign,
+                       .verify = _verify,
+                       .destroy = _destroy,
+               },
+       );
 
        return this;
 }
@@ -447,10 +427,11 @@ radius_message_t *radius_message_create_request()
 {
        private_radius_message_t *this = radius_message_create();
 
-       this->msg = malloc_thing(rmsg_t);
-       this->msg->code = RMC_ACCESS_REQUEST;
-       this->msg->identifier = 0;
-       this->msg->length = htons(sizeof(rmsg_t));
+       INIT(this->msg,
+               .code = RMC_ACCESS_REQUEST,
+               .identifier = 0,
+               .length = htons(sizeof(rmsg_t)),
+       );
 
        return &this->public;
 }