automake build system
[mech_eap.orig] / src / eap_server / eap_server_methods.c
1 /*
2  * EAP server method registration
3  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eap_i.h"
19 #include "eap_methods.h"
20
21
22 static struct eap_method *eap_methods;
23
24
25 /**
26  * eap_server_get_eap_method - Get EAP method based on type number
27  * @vendor: EAP Vendor-Id (0 = IETF)
28  * @method: EAP type number
29  * Returns: Pointer to EAP method or %NULL if not found
30  */
31 const struct eap_method * eap_server_get_eap_method(int vendor, EapType method)
32 {
33         struct eap_method *m;
34         for (m = eap_methods; m; m = m->next) {
35                 if (m->vendor == vendor && m->method == method)
36                         return m;
37         }
38         return NULL;
39 }
40
41
42 /**
43  * eap_server_get_type - Get EAP type for the given EAP method name
44  * @name: EAP method name, e.g., TLS
45  * @vendor: Buffer for returning EAP Vendor-Id
46  * Returns: EAP method type or %EAP_TYPE_NONE if not found
47  *
48  * This function maps EAP type names into EAP type numbers based on the list of
49  * EAP methods included in the build.
50  */
51 EapType eap_server_get_type(const char *name, int *vendor)
52 {
53         struct eap_method *m;
54         for (m = eap_methods; m; m = m->next) {
55                 if (os_strcmp(m->name, name) == 0) {
56                         *vendor = m->vendor;
57                         return m->method;
58                 }
59         }
60         *vendor = EAP_VENDOR_IETF;
61         return EAP_TYPE_NONE;
62 }
63
64
65 /**
66  * eap_server_method_alloc - Allocate EAP server method structure
67  * @version: Version of the EAP server method interface (set to
68  * EAP_SERVER_METHOD_INTERFACE_VERSION)
69  * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
70  * @method: EAP type number (EAP_TYPE_*)
71  * @name: Name of the method (e.g., "TLS")
72  * Returns: Allocated EAP method structure or %NULL on failure
73  *
74  * The returned structure should be freed with eap_server_method_free() when it
75  * is not needed anymore.
76  */
77 struct eap_method * eap_server_method_alloc(int version, int vendor,
78                                             EapType method, const char *name)
79 {
80         struct eap_method *eap;
81         eap = os_zalloc(sizeof(*eap));
82         if (eap == NULL)
83                 return NULL;
84         eap->version = version;
85         eap->vendor = vendor;
86         eap->method = method;
87         eap->name = name;
88         return eap;
89 }
90
91
92 /**
93  * eap_server_method_free - Free EAP server method structure
94  * @method: Method structure allocated with eap_server_method_alloc()
95  */
96 void eap_server_method_free(struct eap_method *method)
97 {
98         os_free(method);
99 }
100
101
102 /**
103  * eap_server_method_register - Register an EAP server method
104  * @method: EAP method to register
105  * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
106  * has already been registered
107  *
108  * Each EAP server method needs to call this function to register itself as a
109  * supported EAP method.
110  */
111 int eap_server_method_register(struct eap_method *method)
112 {
113         struct eap_method *m, *last = NULL;
114
115         if (method == NULL || method->name == NULL ||
116             method->version != EAP_SERVER_METHOD_INTERFACE_VERSION)
117                 return -1;
118
119         for (m = eap_methods; m; m = m->next) {
120                 if ((m->vendor == method->vendor &&
121                      m->method == method->method) ||
122                     os_strcmp(m->name, method->name) == 0)
123                         return -2;
124                 last = m;
125         }
126
127         if (last)
128                 last->next = method;
129         else
130                 eap_methods = method;
131
132         return 0;
133 }
134
135
136 /**
137  * eap_server_unregister_methods - Unregister EAP server methods
138  *
139  * This function is called at program termination to unregister all EAP server
140  * methods.
141  */
142 void eap_server_unregister_methods(void)
143 {
144         struct eap_method *m;
145
146         while (eap_methods) {
147                 m = eap_methods;
148                 eap_methods = eap_methods->next;
149
150                 if (m->free)
151                         m->free(m);
152                 else
153                         eap_server_method_free(m);
154         }
155 }
156
157
158 /**
159  * eap_server_get_name - Get EAP method name for the given EAP type
160  * @vendor: EAP Vendor-Id (0 = IETF)
161  * @type: EAP method type
162  * Returns: EAP method name, e.g., TLS, or %NULL if not found
163  *
164  * This function maps EAP type numbers into EAP type names based on the list of
165  * EAP methods included in the build.
166  */
167 const char * eap_server_get_name(int vendor, EapType type)
168 {
169         struct eap_method *m;
170         for (m = eap_methods; m; m = m->next) {
171                 if (m->vendor == vendor && m->method == type)
172                         return m->name;
173         }
174         return NULL;
175 }