LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_methods.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 50 54 92.6 %
Date: 2014-03-02 Functions: 7 7 100.0 %
Branches: 34 42 81.0 %

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

Generated by: LCOV version 1.9