stunagent.c 18.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2007 Nokia Corporation. All rights reserved.
 *  Contact: Rémi Denis-Courmont
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Rémi Denis-Courmont, Nokia
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "stunmessage.h"
Youness Alaoui's avatar
Youness Alaoui committed
41
#include "stunagent.h"
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <string.h>
#include <stdlib.h>


static bool stun_agent_is_unknown (StunAgent *agent, uint16_t type);
static unsigned stun_agent_find_unknowns (StunAgent *agent,
    const StunMessage * msg, uint16_t *list, unsigned max);

void stun_agent_init (StunAgent *agent, const uint16_t *known_attributes,
    StunCompatibility compatibility, uint32_t usage_flags)
{
  int i;

  agent->known_attributes = (uint16_t *) known_attributes;
  agent->compatibility = compatibility;
  agent->usage_flags = usage_flags;

  for (i = 0; i < STUN_AGENT_MAX_SAVED_IDS; i++) {
    agent->sent_ids[i].valid = FALSE;
  }
}


66
67
68
69
70
71
72
73
bool stun_agent_default_validater (StunAgent *agent,
    StunMessage *message, uint8_t *username, uint16_t username_len,
    uint8_t **password, size_t *password_len, void *user_data)
{
  stun_validater_data* val = (stun_validater_data*) user_data;
  int i;

  for (i = 0; val && val[i].username ; i++) {
74
75
76
77
78
79
    stun_debug ("Comparing username '");
    stun_debug_bytes (username, username_len);
    stun_debug ("' (%d) with '", username_len);
    stun_debug_bytes (val[i].username, val[i].username_len);
    stun_debug ("' (%d) : %d\n",
        val[i].username_len, memcmp (username, val[i].username, username_len));
80
    if (username_len == val[i].username_len &&
81
82
83
        memcmp (username, val[i].username, username_len) == 0) {
      *password = (uint8_t *) val[i].password;
      *password_len = val[i].password_len;
84
      stun_debug ("Found valid username, returning password : '%s'\n", *password);
85
      return TRUE;
86
87
88
    }
  }

89
  return FALSE;
90
91
92

}

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
StunValidationStatus stun_agent_validate (StunAgent *agent, StunMessage *msg,
    const uint8_t *buffer, size_t buffer_len,
    StunMessageIntegrityValidate validater, void * validater_data)
{
  stun_transid_t msg_id;
  uint32_t fpr;
  uint32_t crc32;
  int len;
  uint8_t *username = NULL;
  uint16_t username_len;
  uint8_t *key = NULL;
  size_t key_len;
  uint8_t *hash;
  uint8_t sha[20];
  uint16_t hlen;
  int sent_id_idx = -1;
109
  uint16_t unknown;
110
  int error_code;
111
112
113
  int ignore_credentials = 0;
  uint8_t long_term_key[16];
  bool long_term_key_valid = FALSE;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

  len = stun_message_validate_buffer_length (buffer, buffer_len);
  if (len == STUN_MESSAGE_BUFFER_INVALID) {
    return STUN_VALIDATION_NOT_STUN;
  } else if (len == STUN_MESSAGE_BUFFER_INCOMPLETE) {
    return STUN_VALIDATION_INCOMPLETE_STUN;
  } else if (len != (int) buffer_len) {
    return STUN_VALIDATION_NOT_STUN;
  }

  msg->buffer = (uint8_t *) buffer;
  msg->buffer_len = buffer_len;
  msg->agent = agent;
  msg->key = NULL;
  msg->key_len = 0;
129
  msg->long_term_valid = FALSE;
130
131

  /* TODO: reject it or not ? */
132
  if (agent->compatibility == STUN_COMPATIBILITY_RFC5389 &&
133
134
135
136
137
      !stun_has_cookie (msg)) {
      stun_debug ("STUN demux error: no cookie!\n");
      return STUN_VALIDATION_BAD_REQUEST;
  }

138
  if (agent->compatibility == STUN_COMPATIBILITY_RFC5389 &&
139
140
141
142
143
144
145
146
      agent->usage_flags & STUN_AGENT_USAGE_USE_FINGERPRINT) {
    /* Looks for FINGERPRINT */
    if (stun_message_find32 (msg, STUN_ATTRIBUTE_FINGERPRINT, &fpr) != 0) {
      stun_debug ("STUN demux error: no FINGERPRINT attribute!\n");
      return STUN_VALIDATION_BAD_REQUEST;
    }

    /* Checks FINGERPRINT */
147
148
    crc32 = stun_fingerprint (msg->buffer, stun_message_length (msg));
    fpr = ntohl (fpr);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    if (fpr != crc32) {
      stun_debug ("STUN demux error: bad fingerprint: 0x%08x,"
          " expected: 0x%08x!\n", fpr, crc32);
      return STUN_VALIDATION_BAD_REQUEST;
    }

    stun_debug ("STUN demux: OK!\n");
  }

  if (stun_message_get_class (msg) == STUN_RESPONSE ||
      stun_message_get_class (msg) == STUN_ERROR) {
    stun_message_id (msg, msg_id);
    for (sent_id_idx = 0; sent_id_idx < STUN_AGENT_MAX_SAVED_IDS; sent_id_idx++) {
      if (agent->sent_ids[sent_id_idx].valid == TRUE &&
          agent->sent_ids[sent_id_idx].method == stun_message_get_method (msg) &&
          memcmp (msg_id, agent->sent_ids[sent_id_idx].id,
              sizeof(stun_transid_t)) == 0) {

        key = agent->sent_ids[sent_id_idx].key;
        key_len = agent->sent_ids[sent_id_idx].key_len;
169
170
171
        memcpy (long_term_key, agent->sent_ids[sent_id_idx].long_term_key,
            sizeof(long_term_key));
        long_term_key_valid = agent->sent_ids[sent_id_idx].long_term_valid;
172
173
174
175
176
177
178
179
        break;
      }
    }
    if (sent_id_idx == STUN_AGENT_MAX_SAVED_IDS) {
      return STUN_VALIDATION_UNMATCHED_RESPONSE;
    }
  }

180
181
182
183
  ignore_credentials =
      (agent->usage_flags & STUN_AGENT_USAGE_IGNORE_CREDENTIALS) ||
      (stun_message_get_class (msg) == STUN_ERROR &&
       stun_message_find_error (msg, &error_code) == 0 &&
184
185
186
       (error_code == 400 || error_code == 401)) ||
      (stun_message_get_class (msg) == STUN_INDICATION &&
       (agent->usage_flags & STUN_AGENT_USAGE_NO_INDICATION_AUTH));
187
188

  if (key == NULL &&
189
      ignore_credentials == 0 &&
190
191
      (stun_message_get_class (msg) == STUN_REQUEST ||
       stun_message_get_class (msg) == STUN_INDICATION) &&
192
      (((agent->usage_flags & STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS) &&
193
       (!stun_message_has_attribute (msg, STUN_ATTRIBUTE_USERNAME) ||
194
        !stun_message_has_attribute (msg, STUN_ATTRIBUTE_MESSAGE_INTEGRITY))) ||
195
196
      ((agent->usage_flags & STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS) &&
        stun_message_get_class (msg) == STUN_REQUEST &&
197
198
199
200
201
202
203
204
        (!stun_message_has_attribute (msg, STUN_ATTRIBUTE_USERNAME) ||
         !stun_message_has_attribute (msg, STUN_ATTRIBUTE_MESSAGE_INTEGRITY) ||
         !stun_message_has_attribute (msg, STUN_ATTRIBUTE_NONCE) ||
         !stun_message_has_attribute (msg, STUN_ATTRIBUTE_REALM))) ||
       ((agent->usage_flags & STUN_AGENT_USAGE_IGNORE_CREDENTIALS) == 0 &&
         stun_message_has_attribute (msg, STUN_ATTRIBUTE_USERNAME) &&
         !stun_message_has_attribute (msg, STUN_ATTRIBUTE_MESSAGE_INTEGRITY)))) {
        return STUN_VALIDATION_UNAUTHORIZED_BAD_REQUEST;
205
206
  }

207
  if (stun_message_has_attribute (msg, STUN_ATTRIBUTE_MESSAGE_INTEGRITY) &&
208
      ((key == NULL && ignore_credentials == 0) ||
209
          (agent->usage_flags & STUN_AGENT_USAGE_FORCE_VALIDATER))) {
210
    username_len = 0;
211
212
    username = (uint8_t *) stun_message_find (msg, STUN_ATTRIBUTE_USERNAME,
        &username_len);
Youness Alaoui's avatar
Youness Alaoui committed
213
214
215
216
    if (validater == NULL ||
        validater (agent, msg, username, username_len,
            &key, &key_len, validater_data) == FALSE) {
      return STUN_VALIDATION_UNAUTHORIZED;
217
218
219
    }
  }

220
  if (ignore_credentials == 0 && key != NULL && key_len > 0) {
221
222
    hash = (uint8_t *) stun_message_find (msg,
        STUN_ATTRIBUTE_MESSAGE_INTEGRITY, &hlen);
223

224
225
226
    if (hash) {
      /* We must give the size from start to the end of the attribute
         because you might have a FINGERPRINT attribute after it... */
227
228
229
230
231
232
233
      if (agent->usage_flags & STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS) {
        uint8_t *realm = NULL;
        uint8_t *username = NULL;
        uint16_t realm_len;
        uint16_t username_len;
        uint8_t md5[16];

234
235
236
237
238
239
240
241
242
243
244
245
        if (long_term_key_valid) {
          memcpy (md5, long_term_key, sizeof (md5));
        } else {
          realm = (uint8_t *) stun_message_find (msg,  STUN_ATTRIBUTE_REALM, &realm_len);
          username = (uint8_t *) stun_message_find (msg,
              STUN_ATTRIBUTE_USERNAME, &username_len);
          if (username == NULL || realm == NULL) {
            return STUN_VALIDATION_UNAUTHORIZED;
          }
          stun_hash_creds (realm, realm_len,
              username,  username_len,
              key, key_len, md5);
246
        }
247
248
249

        memcpy (msg->long_term_key, md5, sizeof(md5));
        msg->long_term_valid = TRUE;
250
251
252
253
254
255
256
257

        stun_sha1 (msg->buffer, hash + 20 - msg->buffer, sha, md5, sizeof(md5),
            agent->compatibility == STUN_COMPATIBILITY_RFC3489 ? TRUE : FALSE);
      } else {
        stun_sha1 (msg->buffer, hash + 20 - msg->buffer, sha, key, key_len,
            agent->compatibility == STUN_COMPATIBILITY_RFC3489 ? TRUE : FALSE);
      }

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
      stun_debug (" Message HMAC-SHA1 fingerprint:");
      stun_debug ("\nkey     : ");
      stun_debug_bytes (key, key_len);
      stun_debug ("\n  expected: ");
      stun_debug_bytes (sha, sizeof (sha));
      stun_debug ("\n  received: ");
      stun_debug_bytes (hash, sizeof (sha));
      stun_debug ("\n");

      if (memcmp (sha, hash, sizeof (sha)))  {
        stun_debug ("STUN auth error: SHA1 fingerprint mismatch!\n");
        return STUN_VALIDATION_UNAUTHORIZED;
      }

      stun_debug ("STUN auth: OK!\n");
      msg->key = key;
      msg->key_len = key_len;
    } else if (!(stun_message_get_class (msg) == STUN_ERROR &&
        stun_message_find_error (msg, &error_code) == 0 &&
        (error_code == 400 || error_code == 401))) {
      stun_debug ("STUN auth error: No message integrity attribute!\n");
279
280
281
282
      return STUN_VALIDATION_UNAUTHORIZED;
    }
  }

283

284
285
286
  if (sent_id_idx != -1 && sent_id_idx < STUN_AGENT_MAX_SAVED_IDS) {
    agent->sent_ids[sent_id_idx].valid = FALSE;
  }
287
288
289
290
291
292
293

  if (stun_agent_find_unknowns (agent, msg, &unknown, 1) > 0) {
    if (stun_message_get_class (msg) == STUN_REQUEST)
      return STUN_VALIDATION_UNKNOWN_REQUEST_ATTRIBUTE;
    else
      return STUN_VALIDATION_UNKNOWN_ATTRIBUTE;
  }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  return STUN_VALIDATION_SUCCESS;

}


bool stun_agent_init_request (StunAgent *agent, StunMessage *msg,
    uint8_t *buffer, size_t buffer_len, stun_method_t m)
{
  bool ret;
  stun_transid_t id;

  msg->buffer = buffer;
  msg->buffer_len = buffer_len;
  msg->agent = agent;
  msg->key = NULL;
  msg->key_len = 0;
310
  msg->long_term_valid = FALSE;
311
312
313
314
315
316

  stun_make_transid (id);

  ret = stun_message_init (msg, STUN_REQUEST, m, id);

  if (ret) {
317
    if (agent->compatibility == STUN_COMPATIBILITY_RFC5389) {
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
      uint32_t cookie = htonl (STUN_MAGIC_COOKIE);
      memcpy (msg->buffer + STUN_MESSAGE_TRANS_ID_POS, &cookie, sizeof (cookie));
    }
  }

  return ret;
}


bool stun_agent_init_indication (StunAgent *agent, StunMessage *msg,
    uint8_t *buffer, size_t buffer_len, stun_method_t m)
{
  bool ret;
  stun_transid_t id;

  msg->buffer = buffer;
  msg->buffer_len = buffer_len;
  msg->agent = agent;
  msg->key = NULL;
  msg->key_len = 0;
338
  msg->long_term_valid = FALSE;
339
340
341
342
343

  stun_make_transid (id);
  ret = stun_message_init (msg, STUN_INDICATION, m, id);

  if (ret) {
344
    if (agent->compatibility == STUN_COMPATIBILITY_RFC5389) {
345
346
347
348
349
350
351
352
353
354
      uint32_t cookie = htonl (STUN_MAGIC_COOKIE);
      memcpy (msg->buffer + STUN_MESSAGE_TRANS_ID_POS, &cookie, sizeof (cookie));
    }
  }

  return ret;
}


bool stun_agent_init_response (StunAgent *agent, StunMessage *msg,
355
    uint8_t *buffer, size_t buffer_len, const StunMessage *request)
356
357
358
359
360
361
362
363
364
365
366
{

  stun_transid_t id;

  if (stun_message_get_class (request) != STUN_REQUEST) {
    return FALSE;
  }

  msg->buffer = buffer;
  msg->buffer_len = buffer_len;
  msg->agent = agent;
367
368
  msg->key = request->key;
  msg->key_len = request->key_len;
369
  memmove (msg->long_term_key, request->long_term_key,
370
371
      sizeof(msg->long_term_key));
  msg->long_term_valid = request->long_term_valid;
372
373
374
375
376
377

  stun_message_id (request, id);

  if (stun_message_init (msg, STUN_RESPONSE,
          stun_message_get_method (request), id)) {

378
    if (agent->compatibility == STUN_COMPATIBILITY_RFC5389 &&
379
380
      agent->usage_flags & STUN_AGENT_USAGE_ADD_SOFTWARE) {
      stun_message_append_software (msg);
381
382
383
384
385
386
387
388
    }
    return TRUE;
  }
  return FALSE;
}


bool stun_agent_init_error (StunAgent *agent, StunMessage *msg,
389
    uint8_t *buffer, size_t buffer_len, const StunMessage *request,
390
391
392
393
394
395
396
397
398
399
400
    stun_error_t err)
{
  stun_transid_t id;

  if (stun_message_get_class (request) != STUN_REQUEST) {
    return FALSE;
  }

  msg->buffer = buffer;
  msg->buffer_len = buffer_len;
  msg->agent = agent;
401
402
  msg->key = request->key;
  msg->key_len = request->key_len;
403
  memmove (msg->long_term_key, request->long_term_key,
404
405
      sizeof(msg->long_term_key));
  msg->long_term_valid = request->long_term_valid;
406
407
408
409
410
411
412

  stun_message_id (request, id);


  if (stun_message_init (msg, STUN_ERROR,
          stun_message_get_method (request), id)) {

413
    if (agent->compatibility == STUN_COMPATIBILITY_RFC5389 &&
414
415
      agent->usage_flags & STUN_AGENT_USAGE_ADD_SOFTWARE) {
      stun_message_append_software (msg);
416
417
418
419
420
421
422
423
424
425
    }
    if (stun_message_append_error (msg, err) == 0) {
      return TRUE;
    }
  }
  return FALSE;
}


size_t stun_agent_build_unknown_attributes_error (StunAgent *agent,
426
427
    StunMessage *msg, uint8_t *buffer, size_t buffer_len,
    const StunMessage *request)
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
{

  unsigned counter;
  uint16_t ids[STUN_AGENT_MAX_UNKNOWN_ATTRIBUTES];

  counter = stun_agent_find_unknowns (agent, request,
      ids, STUN_AGENT_MAX_UNKNOWN_ATTRIBUTES);

  if (stun_agent_init_error (agent, msg, buffer, buffer_len,
          request, STUN_ERROR_UNKNOWN_ATTRIBUTE) == FALSE) {
    return 0;
  }

  /* NOTE: Old RFC3489 compatibility:
   * When counter is odd, duplicate one value for 32-bits padding. */
  if (!stun_has_cookie (request) && (counter & 1))
    ids[counter++] = ids[0];

  if (stun_message_append_bytes (msg, STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES,
          ids, counter * 2) == 0) {
    return stun_agent_finish_message (agent, msg, request->key, request->key_len);
  }

  return 0;
}


size_t stun_agent_finish_message (StunAgent *agent, StunMessage *msg,
456
    const uint8_t *key, size_t key_len)
457
458
459
460
461
{
  uint8_t *ptr;
  uint32_t fpr;
  int i;
  stun_transid_t id;
462
  uint8_t md5[16];
463

464
465
466
467
468
  if (msg->key != NULL) {
    key = msg->key;
    key_len = msg->key_len;
  }

469
  if (key != NULL) {
470
    bool skip = FALSE;
471

472
473
474
    if (msg->long_term_valid) {
      memcpy (md5, msg->long_term_key, sizeof(msg->long_term_key));
    } else if (agent->usage_flags & STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS) {
475
476
477
478
479
480
481
482
483
      uint8_t *realm = NULL;
      uint8_t *username = NULL;
      uint16_t realm_len;
      uint16_t username_len;

      realm = (uint8_t *) stun_message_find (msg,  STUN_ATTRIBUTE_REALM, &realm_len);
      username = (uint8_t *) stun_message_find (msg,
          STUN_ATTRIBUTE_USERNAME, &username_len);
      if (username == NULL || realm == NULL) {
484
485
486
487
488
        skip = TRUE;
      } else {
        stun_hash_creds (realm, realm_len,
            username,  username_len,
            key, key_len, md5);
489
      }
490
491
      memcpy (msg->long_term_key, md5, sizeof(msg->long_term_key));
      msg->long_term_valid = TRUE;
492
    }
493

494
495
496
497
498
499
500
501
502
503
504
505
506
507
    /* If no realm/username and long term credentials,
       then don't send the message integrity */
    if (skip == FALSE) {
      ptr = stun_message_append (msg, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, 20);
      if (ptr == NULL) {
        return 0;
      }
      if (agent->usage_flags & STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS) {
        stun_sha1 (msg->buffer, stun_message_length (msg), ptr, md5, sizeof(md5),
            agent->compatibility == STUN_COMPATIBILITY_RFC3489 ? TRUE : FALSE);
      } else {
        stun_sha1 (msg->buffer, stun_message_length (msg), ptr, key, key_len,
            agent->compatibility == STUN_COMPATIBILITY_RFC3489 ? TRUE : FALSE);
      }
508

509
510
511
512
513
514
515
      stun_debug (" Message HMAC-SHA1 message integrity:"
          "\n  key     : ");
      stun_debug_bytes (key, key_len);
      stun_debug ("\n  sent    : ");
      stun_debug_bytes (ptr, 20);
      stun_debug ("\n");
    }
516
517
  }

518
  if (agent->compatibility == STUN_COMPATIBILITY_RFC5389 &&
519
      agent->usage_flags & STUN_AGENT_USAGE_USE_FINGERPRINT) {
520
521
522
523
524
    ptr = stun_message_append (msg, STUN_ATTRIBUTE_FINGERPRINT, 4);
    if (ptr == NULL) {
      return 0;
    }

525

526
527
    fpr = stun_fingerprint (msg->buffer, stun_message_length (msg));
    memcpy (ptr, &fpr, sizeof (fpr));
528
529
530
531

    stun_debug (" Message HMAC-SHA1 fingerprint: ");
    stun_debug_bytes (ptr, 4);
    stun_debug ("\n");
532
533
534
535
536
537
538
539
540
  }


  if (stun_message_get_class (msg) == STUN_REQUEST) {
    for (i = 0; i < STUN_AGENT_MAX_SAVED_IDS; i++) {
      if (agent->sent_ids[i].valid == FALSE) {
        stun_message_id (msg, id);
        memcpy (agent->sent_ids[i].id, id, sizeof(stun_transid_t));
        agent->sent_ids[i].method = stun_message_get_method (msg);
541
        agent->sent_ids[i].key = (uint8_t *) key;
542
        agent->sent_ids[i].key_len = key_len;
543
544
545
        memcpy (agent->sent_ids[i].long_term_key, msg->long_term_key,
            sizeof(msg->long_term_key));
        agent->sent_ids[i].long_term_valid = msg->long_term_valid;
546
        agent->sent_ids[i].valid = TRUE;
Youness Alaoui's avatar
Youness Alaoui committed
547
        break;
548
549
550
551
      }
    }
  }

552
  msg->key = (uint8_t *) key;
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
  msg->key_len = key_len;
  return stun_message_length (msg);

}

static bool stun_agent_is_unknown (StunAgent *agent, uint16_t type)
{

  uint16_t *known_attr = agent->known_attributes;

  while(*known_attr != 0) {
    if (*known_attr == type) {
      return FALSE;
    }
    known_attr++;
  }

  return TRUE;

}


static unsigned
stun_agent_find_unknowns (StunAgent *agent, const StunMessage * msg,
    uint16_t *list, unsigned max)
{
  unsigned count = 0;
  uint16_t len = stun_message_length (msg);
  size_t offset = 0;

  offset = STUN_MESSAGE_ATTRIBUTES_POS;

  while ((offset < len) && (count < max))
  {
    size_t alen = stun_getw (msg->buffer + offset + STUN_ATTRIBUTE_TYPE_LEN);
    uint16_t atype = stun_getw (msg->buffer + offset);

    offset += STUN_ATTRIBUTE_VALUE_POS + stun_align (alen);

    if (!stun_optional (atype) && stun_agent_is_unknown (agent, atype))
    {
      stun_debug ("STUN unknown: attribute 0x%04x(%u bytes)\n",
           (unsigned)atype, (unsigned)alen);
      list[count++] = htons (atype);
    }
  }

  stun_debug ("STUN unknown: %u mandatory attribute(s)!\n", count);
  return count;
}