Kannel: Open Source WAP and SMS gateway  svn-r5335
radius_pdu.c File Reference
#include <string.h>
#include "radius_pdu.h"
#include "radius_pdu.def"
#include "radius_attributes.def"

Go to the source code of this file.

Macros

#define MIN_RADIUS_PDU_LEN   20
 
#define MAX_RADIUS_PDU_LEN   4095
 
#define INTEGER(name, octets)
 
#define OCTETS(name, field_giving_octets)   p->name = NULL;
 
#define PDU(name, id, fields)
 
#define ATTR(attr, type, string, min, max)
 
#define UNASSIGNED(attr)
 
#define ATTRIBUTES(fields)   pdu->attr = dict_create(20, (void (*)(void *))octstr_destroy);
 
#define INTEGER(name, octets)   p->name = 0;
 
#define OCTETS(name, field_giving_octets)   octstr_destroy(p->name);
 
#define PDU(name, id, fields)   case id: { struct name *p = &pdu->u.name; fields } break;
 
#define ATTR(attr, type, string, min, max)
 
#define UNASSIGNED(attr)
 
#define ATTRIBUTES(fields)   dict_destroy(pdu->attr);
 
#define ATTR(atype, type, string, pmin, pmax)
 
#define UNASSIGNED(attr)
 
#define ATTRIBUTES(fields)
 
#define INTEGER(name, octets)   append_encoded_integer(os, p->name, octets);
 
#define OCTETS(name, field_giving_octets)   octstr_append(os, p->name);
 
#define PDU(name, id, fields)
 
#define ATTR(atype, type, string, pmin, pmax)
 
#define UNASSIGNED(attr)
 
#define ATTRIBUTES(fields)
 
#define INTEGER(name, octets)
 
#define OCTETS(name, field_giving_octets)   p->name = parse_get_octets(context, field_giving_octets);
 
#define PDU(name, id, fields)
 
#define UNASSIGNED(attr)
 
#define ATTR(atype, type, string, pmin, pmax)
 
#define ATTRIBUTES(fields)
 
#define INTEGER(name, octets)   debug("radius", 0, " %s: %lu = 0x%08lx", #name, p->name, p->name);
 
#define OCTETS(name, field_giving_octets)   octstr_dump_short(p->name, 2, #name);
 
#define PDU(name, id, fields)
 

Functions

static unsigned long decode_integer (Octstr *os, long pos, int octets)
 
static void append_encoded_integer (Octstr *os, unsigned long u, long octets)
 
RADIUS_PDUradius_pdu_create (int type, RADIUS_PDU *req)
 
void radius_pdu_destroy (RADIUS_PDU *pdu)
 
static Octstrradius_attr_pack (RADIUS_PDU *pdu)
 
Octstrradius_pdu_pack (RADIUS_PDU *pdu)
 
static Octstrradius_type_convert (int type, Octstr *value)
 
static void radius_attr_unpack (ParseContext **context, RADIUS_PDU **pdu)
 
RADIUS_PDUradius_pdu_unpack (Octstr *data_without_len)
 
int radius_authenticate_pdu (RADIUS_PDU *pdu, Octstr **data, Octstr *secret)
 
static void radius_attr_dump (RADIUS_PDU *pdu)
 
void radius_pdu_dump (RADIUS_PDU *pdu)
 
Octstrradius_get_attribute (RADIUS_PDU *pdu, Octstr *attribute)
 

Macro Definition Documentation

◆ ATTR [1/5]

#define ATTR (   attr,
  type,
  string,
  min,
  max 
)

◆ ATTR [2/5]

#define ATTR (   attr,
  type,
  string,
  min,
  max 
)

◆ ATTR [3/5]

#define ATTR (   atype,
  type,
  string,
  pmin,
  pmax 
)
Value:
{ \
Octstr *attr_strg = octstr_create(string); \
Octstr *attr_val = dict_get(p->attr, attr_str); \
if (attr_str != NULL) { \
int attr_len = octstr_len(attr_val) + 2; \
octstr_format_append(os, "%02X", atype); \
octstr_append_data(os, (char*) &attr_len, 2); \
radius_type_append(&os, type, pmin, pmax, attr_val); \
} \
octstr_destroy(attr_str); \
}
int type
Definition: smsc_cimd2.c:215
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
#define octstr_create(cstr)
Definition: octstr.h:125
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342

◆ ATTR [4/5]

#define ATTR (   atype,
  type,
  string,
  pmin,
  pmax 
)
Value:
if (atype == attr_type) { \
Octstr *tmp, *value; \
if ((attr_len-2) < pmin || (attr_len-2) > pmax) { \
error(0, "RADIUS: Attribute (%d) `%s' has invalid len %d, droppped.", \
attr_type, string, (attr_len-2)); \
continue; \
} \
attr_val = parse_get_octets(*context, attr_len - 2); \
tmp = octstr_format("RADIUS: Attribute (%d) `%s', len %d", \
attr_type, string, attr_len - 2); \
value = radius_type_convert(type, attr_val); \
octstr_destroy(attr_val); \
octstr_dump_short(value, 0, octstr_get_cstr(tmp)); \
octstr_destroy(tmp); \
attr_str = octstr_create(string); \
dict_put((*pdu)->attr, attr_str, value); \
octstr_destroy(attr_str); \
value = NULL; \
} else
Definition: parse.c:65
int type
Definition: smsc_cimd2.c:215
static Octstr * radius_type_convert(int type, Octstr *value)
Definition: radius_pdu.c:283
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * parse_get_octets(ParseContext *context, long length)
Definition: parse.c:230
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#define octstr_create(cstr)
Definition: octstr.h:125

◆ ATTR [5/5]

#define ATTR (   atype,
  type,
  string,
  pmin,
  pmax 
)
Value:
id = atype; \
key = octstr_create(string); \
val = dict_get(pdu->attr, key); \
if (val != NULL) \
octstr_dump_short(val, 2, #atype); \
octstr_destroy(key);
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
#define octstr_create(cstr)
Definition: octstr.h:125

◆ ATTRIBUTES [1/5]

#define ATTRIBUTES (   fields)    pdu->attr = dict_create(20, (void (*)(void *))octstr_destroy);

◆ ATTRIBUTES [2/5]

#define ATTRIBUTES (   fields)    dict_destroy(pdu->attr);

◆ ATTRIBUTES [3/5]

#define ATTRIBUTES (   fields)

◆ ATTRIBUTES [4/5]

#define ATTRIBUTES (   fields)
Value:
int attr_type, attr_len; \
Octstr *attr_val = NULL; \
Octstr *attr_str = NULL; \
attr_type = parse_get_char(*context); \
attr_len = parse_get_char(*context); \
fields \
{ \
debug("radius.unpack", 0, "RADIUS: Unknown attribute type (0x%03lx) " \
"len %d in PDU `%s'.", \
(long unsigned int)attr_type, attr_len, (*pdu)->type_name); \
parse_skip(*context, attr_len - 2); \
} \
}
int parse_get_char(ParseContext *context)
Definition: parse.c:218
Definition: parse.c:65
int parse_error(ParseContext *context)
Definition: parse.c:100
long parse_octets_left(ParseContext *context)
Definition: parse.c:159

◆ ATTRIBUTES [5/5]

#define ATTRIBUTES (   fields)
Value:
if (pdu->attr != NULL) { \
Octstr *key = NULL, *val = NULL; \
int id; \
fields \
}

◆ INTEGER [1/5]

#define INTEGER (   name,
  octets 
)
Value:
if (strcmp(#name, "code") == 0) p->name = type; \
else p->name = 0;
int type
Definition: smsc_cimd2.c:215
char * name
Definition: smsc_cimd2.c:212

◆ INTEGER [2/5]

#define INTEGER (   name,
  octets 
)    p->name = 0;

◆ INTEGER [3/5]

#define INTEGER (   name,
  octets 
)    append_encoded_integer(os, p->name, octets);

◆ INTEGER [4/5]

#define INTEGER (   name,
  octets 
)
Value:
pos = octstr_len(data_without_len) - parse_octets_left(context); \
p->name = decode_integer(data_without_len, pos, octets); \
parse_skip(context, octets);
Definition: parse.c:65
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
static unsigned long decode_integer(Octstr *os, long pos, int octets)
Definition: radius_pdu.c:76
long parse_octets_left(ParseContext *context)
Definition: parse.c:159

◆ INTEGER [5/5]

#define INTEGER (   name,
  octets 
)    debug("radius", 0, " %s: %lu = 0x%08lx", #name, p->name, p->name);

◆ MAX_RADIUS_PDU_LEN

#define MAX_RADIUS_PDU_LEN   4095

Definition at line 73 of file radius_pdu.c.

◆ MIN_RADIUS_PDU_LEN

#define MIN_RADIUS_PDU_LEN   20

Definition at line 72 of file radius_pdu.c.

◆ OCTETS [1/5]

#define OCTETS (   name,
  field_giving_octets 
)    p->name = NULL;

◆ OCTETS [2/5]

#define OCTETS (   name,
  field_giving_octets 
)    octstr_destroy(p->name);

◆ OCTETS [3/5]

#define OCTETS (   name,
  field_giving_octets 
)    octstr_append(os, p->name);

◆ OCTETS [4/5]

#define OCTETS (   name,
  field_giving_octets 
)    p->name = parse_get_octets(context, field_giving_octets);

◆ OCTETS [5/5]

#define OCTETS (   name,
  field_giving_octets 
)    octstr_dump_short(p->name, 2, #name);

◆ PDU [1/5]

#define PDU (   name,
  id,
  fields 
)
Value:
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
} break;
char * name
Definition: smsc_cimd2.c:212

◆ PDU [2/5]

#define PDU (   name,
  id,
  fields 
)    case id: { struct name *p = &pdu->u.name; fields } break;

◆ PDU [3/5]

#define PDU (   name,
  id,
  fields 
)
Value:
case id: { struct name *p = &pdu->u.name; fields; oos = radius_attr_pack(pdu); \
octstr_append(os, oos);octstr_destroy(oos); } break;
char * name
Definition: smsc_cimd2.c:212
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
static Octstr * radius_attr_pack(RADIUS_PDU *pdu)
Definition: radius_pdu.c:210

◆ PDU [4/5]

#define PDU (   name,
  id,
  fields 
)
Value:
case id: { struct name *p = &pdu->u.name; fields; \
radius_attr_unpack(&context, &pdu); } break;
Definition: parse.c:65
char * name
Definition: smsc_cimd2.c:212

◆ PDU [5/5]

#define PDU (   name,
  id,
  fields 
)
Value:
case id: { struct name *p = &pdu->u.name; fields; \
radius_attr_dump(pdu); } break;
char * name
Definition: smsc_cimd2.c:212

◆ UNASSIGNED [1/5]

#define UNASSIGNED (   attr)

◆ UNASSIGNED [2/5]

#define UNASSIGNED (   attr)

◆ UNASSIGNED [3/5]

#define UNASSIGNED (   attr)

◆ UNASSIGNED [4/5]

#define UNASSIGNED (   attr)
Value:
if (attr == attr_type) { \
error(0, "RADIUS: Attribute (%d) is unassigned and should not be used.", \
attr_type); \
continue; \
} else

◆ UNASSIGNED [5/5]

#define UNASSIGNED (   attr)

Function Documentation

◆ append_encoded_integer()

static void append_encoded_integer ( Octstr os,
unsigned long  u,
long  octets 
)
static

Definition at line 91 of file radius_pdu.c.

References octstr_append_char().

Referenced by radius_pdu_pack().

92 {
93  long i;
94 
95  for (i = 0; i < octets; ++i)
96  octstr_append_char(os, (u >> ((octets - i - 1) * 8)) & 0xFF);
97 }
void octstr_append_char(Octstr *ostr, int ch)
Definition: octstr.c:1517

◆ decode_integer()

static unsigned long decode_integer ( Octstr os,
long  pos,
int  octets 
)
static

Definition at line 76 of file radius_pdu.c.

References gw_assert(), octstr_get_char(), and octstr_len().

Referenced by radius_pdu_unpack(), and radius_type_convert().

77 {
78  unsigned long u;
79  int i;
80 
81  gw_assert(octstr_len(os) >= pos + octets);
82 
83  u = 0;
84  for (i = 0; i < octets; ++i)
85  u = (u << 8) | octstr_get_char(os, pos + i);
86 
87  return u;
88 }
gw_assert(wtls_machine->packet_to_send !=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406

◆ radius_attr_dump()

static void radius_attr_dump ( RADIUS_PDU pdu)
static

Definition at line 459 of file radius_pdu.c.

460 {
461  #define UNASSIGNED(attr)
462  #define ATTR(atype, type, string, pmin, pmax) \
463  id = atype; \
464  key = octstr_create(string); \
465  val = dict_get(pdu->attr, key); \
466  if (val != NULL) \
467  octstr_dump_short(val, 2, #atype); \
468  octstr_destroy(key);
469  #define ATTRIBUTES(fields) \
470  if (pdu->attr != NULL) { \
471  Octstr *key = NULL, *val = NULL; \
472  int id; \
473  fields \
474  }
475  #include "radius_attributes.def"
476 }

◆ radius_attr_pack()

static Octstr* radius_attr_pack ( RADIUS_PDU pdu)
static

Definition at line 210 of file radius_pdu.c.

References gw_assert(), and octstr_create.

211 {
212  Octstr *os;
213 
214  os = octstr_create("");
215 
216  gw_assert(pdu != NULL);
217 
218  #define ATTR(atype, type, string, pmin, pmax) \
219  { \
220  Octstr *attr_strg = octstr_create(string); \
221  Octstr *attr_val = dict_get(p->attr, attr_str); \
222  if (attr_str != NULL) { \
223  int attr_len = octstr_len(attr_val) + 2; \
224  octstr_format_append(os, "%02X", atype); \
225  octstr_append_data(os, (char*) &attr_len, 2); \
226  radius_type_append(&os, type, pmin, pmax, attr_val); \
227  } \
228  octstr_destroy(attr_str); \
229  }
230  #define UNASSIGNED(attr)
231  #define ATTRIBUTES(fields)
232  #include "radius_attributes.def"
233 
234  return os;
235 }
gw_assert(wtls_machine->packet_to_send !=NULL)
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118

◆ radius_attr_unpack()

static void radius_attr_unpack ( ParseContext **  context,
RADIUS_PDU **  pdu 
)
static

Definition at line 314 of file radius_pdu.c.

315 {
316  #define ATTR(atype, type, string, pmin, pmax) \
317  if (atype == attr_type) { \
318  Octstr *tmp, *value; \
319  if ((attr_len-2) < pmin || (attr_len-2) > pmax) { \
320  error(0, "RADIUS: Attribute (%d) `%s' has invalid len %d, droppped.", \
321  attr_type, string, (attr_len-2)); \
322  continue; \
323  } \
324  attr_val = parse_get_octets(*context, attr_len - 2); \
325  tmp = octstr_format("RADIUS: Attribute (%d) `%s', len %d", \
326  attr_type, string, attr_len - 2); \
327  value = radius_type_convert(type, attr_val); \
328  octstr_destroy(attr_val); \
329  octstr_dump_short(value, 0, octstr_get_cstr(tmp)); \
330  octstr_destroy(tmp); \
331  attr_str = octstr_create(string); \
332  dict_put((*pdu)->attr, attr_str, value); \
333  octstr_destroy(attr_str); \
334  value = NULL; \
335  } else
336  #define UNASSIGNED(attr) \
337  if (attr == attr_type) { \
338  error(0, "RADIUS: Attribute (%d) is unassigned and should not be used.", \
339  attr_type); \
340  continue; \
341  } else
342  #define ATTRIBUTES(fields) \
343  while (parse_octets_left(*context) > 0 && !parse_error(*context)) { \
344  int attr_type, attr_len; \
345  Octstr *attr_val = NULL; \
346  Octstr *attr_str = NULL; \
347  attr_type = parse_get_char(*context); \
348  attr_len = parse_get_char(*context); \
349  fields \
350  { \
351  debug("radius.unpack", 0, "RADIUS: Unknown attribute type (0x%03lx) " \
352  "len %d in PDU `%s'.", \
353  (long unsigned int)attr_type, attr_len, (*pdu)->type_name); \
354  parse_skip(*context, attr_len - 2); \
355  } \
356  }
357  #include "radius_attributes.def"
358 }

◆ radius_authenticate_pdu()

int radius_authenticate_pdu ( RADIUS_PDU pdu,
Octstr **  data,
Octstr secret 
)

Definition at line 416 of file radius_pdu.c.

References md5(), octstr_append(), octstr_append_data(), octstr_compare(), octstr_copy, octstr_delete(), octstr_destroy(), octstr_duplicate, octstr_insert(), octstr_len(), RADIUS_PDU::type, and RADIUS_PDU::u.

Referenced by main(), proxy_thread(), and server().

417 {
418  int rc = 0;
419  Octstr *stream;
420  Octstr *attributes;
421  Octstr *digest;
422 
423  stream = attributes = digest = NULL;
424 
425  /* first extract attributes from raw data, where
426  * the first 20 octets are code, idendifier, length
427  * and authenticator value as described in RFC2866, sec. 3 */
428  if (octstr_len(*data) > 20)
429  attributes = octstr_copy(*data, 20, octstr_len(*data)-20);
430 
431  switch (pdu->type) {
432  case 0x04: /* Accounting-Request, see RFC2866, page 6 */
433  stream = octstr_copy(*data, 0, 4);
434  octstr_append_data(stream, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
435  octstr_append(stream, attributes);
436  octstr_append(stream, secret);
437  digest = md5(stream);
438  rc = octstr_compare(pdu->u.Accounting_Request.authenticator,
439  digest) == 0 ? 1 : 0;
440  break;
441  case 0x05: /* Accounting-Response, create Response authenticator */
442  stream = octstr_duplicate(*data);
443  octstr_append(stream, secret);
444  digest = md5(stream);
445  octstr_delete(*data, 4, 16);
446  octstr_insert(*data, digest, 4);
447  break;
448  default:
449  break;
450  }
451 
452  octstr_destroy(attributes);
453  octstr_destroy(stream);
454  octstr_destroy(digest);
455 
456  return rc;
457 }
Octstr * md5(Octstr *data)
Definition: md5.c:387
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1497
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
union RADIUS_PDU::@72 u
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos)
Definition: octstr.c:1303
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871

◆ radius_get_attribute()

Octstr* radius_get_attribute ( RADIUS_PDU pdu,
Octstr attribute 
)

Definition at line 498 of file radius_pdu.c.

References RADIUS_PDU::attr, dict_get(), and gw_assert().

499 {
500  gw_assert(pdu != NULL);
501 
502  if (pdu->attr == NULL)
503  return NULL;
504 
505  return dict_get(pdu->attr, attribute);
506 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Dict * attr
Definition: radius_pdu.h:93
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286

◆ radius_pdu_create()

RADIUS_PDU* radius_pdu_create ( int  type,
RADIUS_PDU req 
)

Definition at line 123 of file radius_pdu.c.

References error(), RADIUS_PDU::type, and type.

Referenced by main(), proxy_thread(), radius_pdu_unpack(), and server().

124 {
125  RADIUS_PDU *pdu;
126 
127  pdu = gw_malloc(sizeof(*pdu));
128  pdu->type = type;
129 
130  switch (type) {
131  #define INTEGER(name, octets) \
132  if (strcmp(#name, "code") == 0) p->name = type; \
133  else p->name = 0;
134  #define OCTETS(name, field_giving_octets) p->name = NULL;
135  #define PDU(name, id, fields) \
136  case id: { \
137  struct name *p = &pdu->u.name; \
138  pdu->type_name = #name; \
139  fields \
140  } break;
141  #include "radius_pdu.def"
142  default:
143  error(0, "Unknown RADIUS_PDU type, internal error.");
144  gw_free(pdu);
145  return NULL;
146  }
147  #define ATTR(attr, type, string, min, max)
148  #define UNASSIGNED(attr)
149  #define ATTRIBUTES(fields) \
150  pdu->attr = dict_create(20, (void (*)(void *))octstr_destroy);
151  #include "radius_attributes.def"
152 
153  return pdu;
154 }
void error(int err, const char *fmt,...)
Definition: log.c:648
int type
Definition: smsc_cimd2.c:215

◆ radius_pdu_destroy()

void radius_pdu_destroy ( RADIUS_PDU pdu)

Definition at line 156 of file radius_pdu.c.

References error(), and RADIUS_PDU::type.

Referenced by main(), proxy_thread(), and server().

157 {
158  if (pdu == NULL)
159  return;
160 
161  switch (pdu->type) {
162  #define INTEGER(name, octets) p->name = 0;
163  #define OCTETS(name, field_giving_octets) octstr_destroy(p->name);
164  #define PDU(name, id, fields) \
165  case id: { struct name *p = &pdu->u.name; fields } break;
166  #include "radius_pdu.def"
167  default:
168  error(0, "Unknown RADIUS_PDU type, internal error while destroying.");
169  }
170 
171  #define ATTR(attr, type, string, min, max)
172  #define UNASSIGNED(attr)
173  #define ATTRIBUTES(fields) dict_destroy(pdu->attr);
174  #include "radius_attributes.def"
175 
176  gw_free(pdu);
177 }
void error(int err, const char *fmt,...)
Definition: log.c:648

◆ radius_pdu_dump()

void radius_pdu_dump ( RADIUS_PDU pdu)

Definition at line 478 of file radius_pdu.c.

References debug(), and error().

479 {
480  debug("radius", 0, "RADIUS PDU %p dump:", (void *) pdu);
481  debug("radius", 0, " type_name: %s", pdu->type_name);
482  switch (pdu->type) {
483  #define INTEGER(name, octets) \
484  debug("radius", 0, " %s: %lu = 0x%08lx", #name, p->name, p->name);
485  #define OCTETS(name, field_giving_octets) \
486  octstr_dump_short(p->name, 2, #name);
487  #define PDU(name, id, fields) \
488  case id: { struct name *p = &pdu->u.name; fields; \
489  radius_attr_dump(pdu); } break;
490  #include "radius_pdu.def"
491  default:
492  error(0, "Unknown RADIUS_PDU type, internal error.");
493  break;
494  }
495  debug("radius", 0, "RADIUS PDU dump ends.");
496 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
const char * type_name
Definition: radius_pdu.h:92

◆ radius_pdu_pack()

Octstr* radius_pdu_pack ( RADIUS_PDU pdu)

Definition at line 237 of file radius_pdu.c.

References append_encoded_integer(), error(), gw_assert(), octstr_create, octstr_delete(), octstr_destroy(), octstr_insert(), octstr_len(), and RADIUS_PDU::type.

Referenced by main(), proxy_thread(), and server().

238 {
239  Octstr *os,*oos;
240  Octstr *temp;
241 
242  os = octstr_create("");
243 
244  gw_assert(pdu != NULL);
245 
246  /*
247  switch (pdu->type) {
248  #define INTEGER(name, octets) p = *(&p);
249  #define NULTERMINATED(name, max_octets) p = *(&p);
250  #define OCTETS(name, field_giving_octets) \
251  p->field_giving_octets = octstr_len(p->name);
252  #define PDU(name, id, fields) \
253  case id: { struct name *p = &pdu->u.name; fields } break;
254  #include "radius_pdu.def"
255  default:
256  error(0, "Unknown RADIUS_PDU type, internal error while packing.");
257  }
258  */
259 
260  switch (pdu->type) {
261  #define INTEGER(name, octets) \
262  append_encoded_integer(os, p->name, octets);
263  #define OCTETS(name, field_giving_octets) \
264  octstr_append(os, p->name);
265  #define PDU(name, id, fields) \
266  case id: { struct name *p = &pdu->u.name; fields; oos = radius_attr_pack(pdu); \
267  octstr_append(os, oos);octstr_destroy(oos); } break;
268  #include "radius_pdu.def"
269  default:
270  error(0, "Unknown RADIUS_PDU type, internal error while packing.");
271  }
272 
273  /* now set PDU length */
274  temp = octstr_create("");
275  append_encoded_integer(temp, octstr_len(os), 2);
276  octstr_delete(os, 2, 2);
277  octstr_insert(os, temp, 2);
278  octstr_destroy(temp);
279 
280  return os;
281 }
void error(int err, const char *fmt,...)
Definition: log.c:648
gw_assert(wtls_machine->packet_to_send !=NULL)
void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos)
Definition: octstr.c:1303
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
static void append_encoded_integer(Octstr *os, unsigned long u, long octets)
Definition: radius_pdu.c:91
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118

◆ radius_pdu_unpack()

RADIUS_PDU* radius_pdu_unpack ( Octstr data_without_len)

Definition at line 360 of file radius_pdu.c.

References debug(), decode_integer(), error(), octstr_destroy(), octstr_dump_short(), octstr_len(), parse_context_create(), parse_context_destroy(), parse_get_char(), parse_get_octets(), parse_skip(), radius_pdu_create(), and type.

Referenced by main(), proxy_thread(), and server().

361 {
362  RADIUS_PDU *pdu;
363  int type;
364  long len, pos;
366  Octstr *authenticator;
367 
368  len = octstr_len(data_without_len);
369 
370  if (len < 20) {
371  error(0, "RADIUS: PDU was too short (%ld bytes).",
372  octstr_len(data_without_len));
373  return NULL;
374  }
375 
376  context = parse_context_create(data_without_len);
377 
380  pdu = radius_pdu_create(type, NULL);
381  if (pdu == NULL)
382  return NULL;
383 
384  len = decode_integer(data_without_len, 2, 2) - 19;
385  parse_skip(context, 2);
386  debug("radius", 0, "RADIUS: Attributes len is %ld", len);
387 
388  authenticator = parse_get_octets(context, 16);
389  octstr_dump_short(authenticator, 0, "RADIUS: Authenticator (md5) is:");
390 
391  /* skipping back to context start for macro magic */
393  context = parse_context_create(data_without_len);
394 
395  switch (type) {
396  #define INTEGER(name, octets) \
397  pos = octstr_len(data_without_len) - parse_octets_left(context); \
398  p->name = decode_integer(data_without_len, pos, octets); \
399  parse_skip(context, octets);
400  #define OCTETS(name, field_giving_octets) \
401  p->name = parse_get_octets(context, field_giving_octets);
402  #define PDU(name, id, fields) \
403  case id: { struct name *p = &pdu->u.name; fields; \
404  radius_attr_unpack(&context, &pdu); } break;
405  #include "radius_pdu.def"
406  default:
407  error(0, "Unknown RADIUS_PDU type, internal error while unpacking.");
408  }
409 
411  octstr_destroy(authenticator);
412 
413  return pdu;
414 }
void error(int err, const char *fmt,...)
Definition: log.c:648
int parse_get_char(ParseContext *context)
Definition: parse.c:218
Definition: parse.c:65
int type
Definition: smsc_cimd2.c:215
void octstr_dump_short(Octstr *ostr, int level, const char *name)
Definition: octstr.c:2144
Octstr * parse_get_octets(ParseContext *context, long length)
Definition: parse.c:230
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
int parse_skip(ParseContext *context, long count)
Definition: parse.c:166
void parse_context_destroy(ParseContext *context)
Definition: parse.c:88
RADIUS_PDU * radius_pdu_create(int type, RADIUS_PDU *req)
Definition: radius_pdu.c:123
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
static unsigned long decode_integer(Octstr *os, long pos, int octets)
Definition: radius_pdu.c:76
ParseContext * parse_context_create(Octstr *str)
Definition: parse.c:74

◆ radius_type_convert()

static Octstr* radius_type_convert ( int  type,
Octstr value 
)
static

Definition at line 283 of file radius_pdu.c.

References decode_integer(), octstr_append(), octstr_append_cstr(), octstr_create, octstr_destroy(), octstr_format(), octstr_get_char(), octstr_get_cstr, panic, t_int, t_ipaddr, t_string, and type.

284 {
285  Octstr *ret = NULL;
286  int i;
287 
288  switch (type) {
289  case t_int:
290  ret = octstr_format("%ld", decode_integer(value, 0, 4));
291  break;
292  case t_string:
293  ret = octstr_format("%s", octstr_get_cstr(value));
294  break;
295  case t_ipaddr:
296  ret = octstr_create("");
297  for (i = 0; i < 4; i++) {
298  int c = octstr_get_char(value, i);
299  Octstr *b = octstr_format("%d", c);
300  octstr_append(ret, b);
301  if (i < 3)
302  octstr_append_cstr(ret, ".");
303  octstr_destroy(b);
304  }
305  break;
306  default:
307  panic(0, "RADIUS: Attribute type %d does not exist.", type);
308  break;
309  }
310 
311  return ret;
312 }
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
int type
Definition: smsc_cimd2.c:215
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1511
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static unsigned long decode_integer(Octstr *os, long pos, int octets)
Definition: radius_pdu.c:76
#define panic
Definition: log.h:87
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.