Navigation


Changeset 922:c7bf1a7a4e90 in freeDiameter


Ignore:
Timestamp:
Feb 14, 2013, 11:43:36 PM (11 years ago)
Author:
Sebastien Decugis <sdecugis@freediameter.net>
Branch:
default
Phase:
public
Message:

Split the encoders/interpreters for the dictionary types into a different file for better reusability, add decoder/interpreter for Time type based on code from Thomas Klausner

Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • include/freeDiameter/libfdproto.h

    r907 r922  
    11641164};
    11651165
     1166/****
     1167 Callbacks defined in libfdproto/dictionary_functions.c file -- see that file for usage.
     1168 */
     1169
     1170/* Convert an Address type AVP into a struct sockaddr_storage */
     1171int fd_dictfct_Address_encode(void * data, union avp_value * avp_value);
     1172int fd_dictfct_Address_interpret(union avp_value * avp_value, void * interpreted);
     1173char * fd_dictfct_Address_dump(union avp_value * avp_value);
     1174
     1175/* Display the content of an AVP of type UTF8String in the log file */
     1176char * fd_dictfct_UTF8String_dump(union avp_value * avp_value);
     1177
     1178/* For Time AVPs, map with time_t value directly */
     1179int fd_dictfct_Time_encode(void * data, union avp_value * avp_value);
     1180int fd_dictfct_Time_interpret(union avp_value * avp_value, void * interpreted);
     1181char * fd_dictfct_Time_dump(union avp_value * avp_value);
     1182
     1183
     1184
     1185/****/
    11661186
    11671187/***
     
    11941214 
    11951215*/
    1196          
     1216
    11971217/*
    11981218 ***************************************************************************
  • libfdcore/dict_base_proto.c

    r838 r922  
    4545struct dictionary * fd_g_dict = NULL;
    4646
    47 /* The functions to encode and interpret the derived types defined in the base protocol */
    48 
    49 /* Address AVP <-> struct sockaddr_storage */
    50 static int Address_encode(void * data, union avp_value * avp_value)
    51 {
    52         sSS * ss = (sSS *) data;
    53         uint16_t AddressType = 0;
    54         size_t  size = 0;
    55         unsigned char * buf = NULL;
    56        
    57         TRACE_ENTRY("%p %p", data, avp_value);
    58         CHECK_PARAMS( data && avp_value  );
    59        
    60         switch (ss->ss_family) {
    61                 case AF_INET:
    62                         {
    63                                 /* We are encoding an IP address */
    64                                 sSA4 * sin = (sSA4 *)ss;
    65                                
    66                                 AddressType = 1;/* see http://www.iana.org/assignments/address-family-numbers/ */
    67                                 size = 6;       /* 2 for AddressType + 4 for data */
    68                                
    69                                 CHECK_MALLOC(  buf = malloc(size)  );
    70                                
    71                                 /* may not work because of alignment: *(uint32_t *)(buf+2) = htonl(sin->sin_addr.s_addr); */
    72                                 memcpy(buf + 2, &sin->sin_addr.s_addr, 4);
    73                         }
    74                         break;
    75                                
    76                 case AF_INET6:
    77                         {
    78                                 /* We are encoding an IPv6 address */
    79                                 sSA6 * sin6 = (sSA6 *)ss;
    80                                
    81                                 AddressType = 2;/* see http://www.iana.org/assignments/address-family-numbers/ */
    82                                 size = 18;      /* 2 for AddressType + 16 for data */
    83                                
    84                                 CHECK_MALLOC(  buf = malloc(size)  );
    85                                
    86                                 /* The order is already good here */
    87                                 memcpy(buf + 2, &sin6->sin6_addr.s6_addr, 16);
    88                         }
    89                         break;
    90                                
    91                 default:
    92                         CHECK_PARAMS( AddressType = 0 );
    93         }
    94        
    95         *(uint16_t *)buf = htons(AddressType);
    96 
    97         avp_value->os.len = size;
    98         avp_value->os.data = buf;
    99        
    100         return 0;
    101 }
    102 
    103 static int Address_interpret(union avp_value * avp_value, void * interpreted)
    104 {
    105         uint16_t AddressType = 0;
    106         unsigned char * buf;
    107        
    108         TRACE_ENTRY("%p %p", avp_value, interpreted);
    109        
    110         CHECK_PARAMS( avp_value && interpreted && (avp_value->os.len >= 2)  );
    111        
    112         AddressType = ntohs(*(uint16_t *)avp_value->os.data);
    113         buf = &avp_value->os.data[2];
    114        
    115         switch (AddressType) {
    116                 case 1 /* IP */:
    117                         {
    118                                 sSA4 * sin = (sSA4 *)interpreted;
    119                                
    120                                 CHECK_PARAMS(  avp_value->os.len == 6  );
    121                                
    122                                 sin->sin_family = AF_INET;
    123                                 /* sin->sin_addr.s_addr = ntohl( * (uint32_t *) buf); -- may not work because of bad alignment */
    124                                 memcpy(&sin->sin_addr.s_addr, buf, 4);
    125                         }
    126                         break;
    127                                
    128                 case 2 /* IP6 */:
    129                         {
    130                                 sSA6 * sin6 = (sSA6 *)interpreted;
    131                                
    132                                 CHECK_PARAMS(  avp_value->os.len == 18  );
    133                                
    134                                 sin6->sin6_family = AF_INET6;
    135                                 memcpy(&sin6->sin6_addr.s6_addr, buf, 16);
    136                         }
    137                         break;
    138                                
    139                 default:
    140                         CHECK_PARAMS( AddressType = 0 );
    141         }
    142        
    143         return 0;
    144 }
    145 
    146 /* Dump the content of an Address AVP */
    147 static char * Address_dump(union avp_value * avp_value)
    148 {
    149         char * ret;
    150         #define STR_LEN 1024
    151         union {
    152                 sSA     sa;
    153                 sSS     ss;
    154                 sSA4    sin;
    155                 sSA6    sin6;
    156         } s;
    157         uint16_t fam;
    158        
    159         memset(&s, 0, sizeof(s));
    160        
    161         CHECK_MALLOC_DO( ret = malloc(STR_LEN), return NULL );
    162        
    163         /* The first two octets represent the address family, http://www.iana.org/assignments/address-family-numbers/ */
    164         if (avp_value->os.len < 2) {
    165                 snprintf(ret, STR_LEN, "[invalid length: %zd]", avp_value->os.len);
    166                 return ret;
    167         }
    168        
    169         /* Following octets are the address in network byte order already */
    170         fam = avp_value->os.data[0] << 8 | avp_value->os.data[1];
    171         switch (fam) {
    172                 case 1:
    173                         /* IP */
    174                         s.sa.sa_family = AF_INET;
    175                         if (avp_value->os.len != 6) {
    176                                 snprintf(ret, STR_LEN, "[invalid IP length: %zd]", avp_value->os.len);
    177                                 return ret;
    178                         }
    179                         memcpy(&s.sin.sin_addr.s_addr, avp_value->os.data + 2, 4);
    180                         break;
    181                 case 2:
    182                         /* IP6 */
    183                         s.sa.sa_family = AF_INET6;
    184                         if (avp_value->os.len != 18) {
    185                                 snprintf(ret, STR_LEN, "[invalid IP6 length: %zd]", avp_value->os.len);
    186                                 return ret;
    187                         }
    188                         memcpy(&s.sin6.sin6_addr.s6_addr, avp_value->os.data + 2, 16);
    189                         break;
    190                 default:
    191                         snprintf(ret, STR_LEN, "[unsupported family: 0x%hx]", fam);
    192                         return ret;
    193         }
    194        
    195         {
    196                 int rc = getnameinfo(&s.sa, sSAlen(&s.sa), ret, STR_LEN, NULL, 0, NI_NUMERICHOST);
    197                 if (rc)
    198                         snprintf(ret, STR_LEN, "%s", (char *)gai_strerror(rc));
    199         }
    200        
    201         return ret;
    202 }
    203 
    204 static char * UTF8String_dump(union avp_value * avp_value)
    205 {
    206         return strndup((char *)avp_value->os.data, 42); /* avoid very long strings */
    207 }
    208 
    209 
    21047
    21148
     
    295132                                the content and format of the remaining octets.
    296133                        */
    297                         struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "Address"               , Address_interpret     , Address_encode,       Address_dump    };
     134                        struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "Address"               , fd_dictfct_Address_interpret  , fd_dictfct_Address_encode,    fd_dictfct_Address_dump };
    298135                        CHECK_dict_new( DICT_TYPE, &data , NULL, NULL);
    299136                }
     
    314151                                This procedure MUST be supported by all DIAMETER nodes.
    315152                        */
    316                         struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "Time"                  , NULL                  , NULL          , NULL          };
     153                        struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "Time"                  , fd_dictfct_Time_interpret     , fd_dictfct_Time_encode,       fd_dictfct_Time_dump            };
    317154                        CHECK_dict_new( DICT_TYPE, &data , NULL, NULL);
    318155                }
     
    352189                                octets, not characters.
    353190                        */
    354                         struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "UTF8String"            , NULL                  , NULL  , UTF8String_dump       };
     191                        struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "UTF8String"            , NULL                  , NULL  , fd_dictfct_UTF8String_dump    };
    355192                        CHECK_dict_new( DICT_TYPE, &data , NULL, NULL);
    356193                }
     
    381218                                Domain Name (IDNs).
    382219                        */
    383                         struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "DiameterIdentity"      , NULL                  , NULL          , UTF8String_dump       };
     220                        struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "DiameterIdentity"      , NULL                  , NULL          , fd_dictfct_UTF8String_dump    };
    384221                        CHECK_dict_new( DICT_TYPE, &data , NULL, NULL);
    385222                }
     
    436273                                aaa://host.example.com:1813;transport=udp;protocol=radius
    437274                        */
    438                         struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "DiameterURI"           , NULL                  , NULL          , UTF8String_dump       };
     275                        struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "DiameterURI"           , NULL                  , NULL          , fd_dictfct_UTF8String_dump    };
    439276                        CHECK_dict_new( DICT_TYPE, &data , NULL, NULL);
    440277                }
     
    498335                                infrastructure.
    499336                        */
    500                         struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "IPFilterRule"          , NULL                  , NULL          , UTF8String_dump       };
     337                        struct dict_type_data data = { AVP_TYPE_OCTETSTRING,    "IPFilterRule"          , NULL                  , NULL          , fd_dictfct_UTF8String_dump    };
    501338                        CHECK_dict_new( DICT_TYPE, &data , NULL, NULL);
    502339                }
  • libfdproto/CMakeLists.txt

    r836 r922  
    66        fdproto-internal.h
    77        dictionary.c
     8        dictionary_functions.c
    89        dispatch.c
    910        fifo.c
Note: See TracChangeset for help on using the changeset viewer.