summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/third_party/nICEr/src/stun/stun_codec.c
blob: ae748a667b36c0ba08dd01dc6b4d20438abd9633 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
/*
Copyright (c) 2007, Adobe Systems, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <errno.h>
#include <csi_platform.h>

#ifdef WIN32
#include <winsock2.h>
#include <stdlib.h>
#include <io.h>
#include <time.h>
#else   /* UNIX */
#include <string.h>
#endif  /* end UNIX */
#include <assert.h>
#include <stddef.h>

#include "nr_api.h"
#include "stun.h"
#include "byteorder.h"
#include "r_crc32.h"
#include "nr_crypto.h"

#define NR_STUN_IPV4_FAMILY  0x01
#define NR_STUN_IPV6_FAMILY  0x02

#define SKIP_ATTRIBUTE_DECODE -1

static int nr_stun_find_attr_info(UINT2 type, nr_stun_attr_info **info);

static int nr_stun_fix_attribute_ordering(nr_stun_message *msg);

static int nr_stun_encode_htons(UINT2 data, size_t buflen, UCHAR *buf, size_t *offset);
static int nr_stun_encode_htonl(UINT4 data, size_t buflen, UCHAR *buf, size_t *offset);
static int nr_stun_encode_htonll(UINT8 data, size_t buflen, UCHAR *buf, size_t *offset);
static int nr_stun_encode(UCHAR *data, size_t length, size_t buflen, UCHAR *buf, size_t *offset);

static int nr_stun_decode_htons(UCHAR *buf, size_t buflen, size_t *offset, UINT2 *data);
static int nr_stun_decode_htonl(UCHAR *buf, size_t buflen, size_t *offset, UINT4 *data);
static int nr_stun_decode_htonll(UCHAR *buf, size_t buflen, size_t *offset, UINT8 *data);
static int nr_stun_decode(size_t length, UCHAR *buf, size_t buflen, size_t *offset, UCHAR *data);

static int nr_stun_attr_string_illegal(nr_stun_attr_info *attr_info, size_t len, void *data, size_t max_bytes, size_t max_chars);

static int nr_stun_attr_error_code_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data);
static int nr_stun_attr_nonce_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data);
static int nr_stun_attr_realm_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data);
static int nr_stun_attr_server_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data);
static int nr_stun_attr_username_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data);
static int
nr_stun_attr_codec_fingerprint_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data);


int
nr_stun_encode_htons(UINT2 data, size_t buflen, UCHAR *buf, size_t *offset)
{
   UINT2 d = htons(data);

   if (*offset + sizeof(d) >= buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %zd >= %d", *offset, sizeof(d), buflen);
      return R_BAD_DATA;
   }

   memcpy(&buf[*offset], &d, sizeof(d));
   *offset += sizeof(d);

   return 0;
}

int
nr_stun_encode_htonl(UINT4 data, size_t buflen, UCHAR *buf, size_t *offset)
{
   UINT4 d = htonl(data);

   if (*offset + sizeof(d) > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %zd > %d", *offset, sizeof(d), buflen);
      return R_BAD_DATA;
   }

   memcpy(&buf[*offset], &d, sizeof(d));
   *offset += sizeof(d);

   return 0;
}

int
nr_stun_encode_htonll(UINT8 data, size_t buflen, UCHAR *buf, size_t *offset)
{
   UINT8 d = nr_htonll(data);

   if (*offset + sizeof(d) > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %zd > %d", *offset, sizeof(d), buflen);
      return R_BAD_DATA;
   }

   memcpy(&buf[*offset], &d, sizeof(d));
   *offset += sizeof(d);

   return 0;
}

int
nr_stun_encode(UCHAR *data, size_t length, size_t buflen, UCHAR *buf, size_t *offset)
{
   if (*offset + length > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %d > %d", *offset, length, buflen);
      return R_BAD_DATA;
   }

   memcpy(&buf[*offset], data, length);
   *offset += length;

   return 0;
}


int
nr_stun_decode_htons(UCHAR *buf, size_t buflen, size_t *offset, UINT2 *data)
{
   UINT2 d;

   if (*offset + sizeof(d) > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %zd > %d", *offset, sizeof(d), buflen);
      return R_BAD_DATA;
   }

   memcpy(&d, &buf[*offset], sizeof(d));
   *offset += sizeof(d);
   *data = htons(d);

   return 0;
}

int
nr_stun_decode_htonl(UCHAR *buf, size_t buflen, size_t *offset, UINT4 *data)
{
   UINT4 d;

   if (*offset + sizeof(d) > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %zd > %d", *offset, sizeof(d), buflen);
      return R_BAD_DATA;
   }

   memcpy(&d, &buf[*offset], sizeof(d));
   *offset += sizeof(d);
   *data = htonl(d);

   return 0;
}

int
nr_stun_decode_htonll(UCHAR *buf, size_t buflen, size_t *offset, UINT8 *data)
{
   UINT8 d;

   if (*offset + sizeof(d) > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %zd > %d", *offset, sizeof(d), buflen);
      return R_BAD_DATA;
   }

   memcpy(&d, &buf[*offset], sizeof(d));
   *offset += sizeof(d);
   *data = nr_htonll(d);

   return 0;
}

int
nr_stun_decode(size_t length, UCHAR *buf, size_t buflen, size_t *offset, UCHAR *data)
{
   if (*offset + length > buflen) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Attempted buffer overrun: %d + %d > %d", *offset, length, buflen);
      return R_BAD_DATA;
   }

   memcpy(data, &buf[*offset], length);
   *offset += length;

   return 0;
}

/**
 * The argument must be a non-null pointer to a zero-terminated string.
 *
 * If the argument is valid UTF-8, returns the number of code points in the
 * string excluding the zero-terminator.
 *
 * If the argument is invalid UTF-8, returns a lower bound for the number of
 * code points in the string. (If UTF-8 error handling was performed on the
 * string, new REPLACEMENT CHARACTER code points could be introduced in
 * a way that would increase the total number of code points compared to
 * what this function counts.)
 */
size_t
nr_count_utf8_code_points_without_validation(const char *s) {
    size_t nchars = 0;
    char c;
    while ((c = *s)) {
        if ((c & 0xC0) != 0x80) {
            ++nchars;
        }
        ++s;
    }
    return nchars;
}

int
nr_stun_attr_string_illegal(nr_stun_attr_info *attr_info, size_t len, void *data, size_t max_bytes, size_t max_chars)
{
    int _status;
    char *s = data;
    size_t nchars;

    if (len > max_bytes) {
        r_log(NR_LOG_STUN, LOG_WARNING, "%s is too large: %d bytes", attr_info->name, len);
        ABORT(R_FAILED);
    }

    nchars = nr_count_utf8_code_points_without_validation(s);
    if (nchars > max_chars) {
        r_log(NR_LOG_STUN, LOG_WARNING, "%s is too large: %zd characters", attr_info->name, nchars);
        ABORT(R_FAILED);
    }

    _status = 0;
  abort:
    return _status;
}

int
nr_stun_attr_error_code_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data)
{
    int r,_status;
    nr_stun_attr_error_code *ec = data;

    if (ec->number < 300 || ec->number > 699)
        ABORT(R_FAILED);

    if ((r=nr_stun_attr_string_illegal(attr_info, strlen(ec->reason), ec->reason, NR_STUN_MAX_ERROR_CODE_REASON_BYTES, NR_STUN_MAX_ERROR_CODE_REASON_CHARS)))
        ABORT(r);

    _status = 0;
  abort:
    return _status;
}

int
nr_stun_attr_nonce_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data)
{
    return nr_stun_attr_string_illegal(attr_info, attrlen, data, NR_STUN_MAX_NONCE_BYTES, NR_STUN_MAX_NONCE_CHARS);
}

int
nr_stun_attr_realm_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data)
{
    return nr_stun_attr_string_illegal(attr_info, attrlen, data, NR_STUN_MAX_REALM_BYTES, NR_STUN_MAX_REALM_CHARS);
}

int
nr_stun_attr_server_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data)
{
    return nr_stun_attr_string_illegal(attr_info, attrlen, data, NR_STUN_MAX_SERVER_BYTES, NR_STUN_MAX_SERVER_CHARS);
}

int
nr_stun_attr_username_illegal(nr_stun_attr_info *attr_info, size_t attrlen, void *data)
{
    return nr_stun_attr_string_illegal(attr_info, attrlen, data, NR_STUN_MAX_USERNAME_BYTES, -1);
}

static int
nr_stun_attr_codec_UCHAR_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %u", msg, attr_info->name, *(UCHAR*)data);
    return 0;
}

static int
nr_stun_attr_codec_UCHAR_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int start = offset;
    UINT4 tmp = *((UCHAR *)data);
    tmp <<= 24;

    if (nr_stun_encode_htons(attr_info->type    , buflen, buf, &offset)
     || nr_stun_encode_htons(sizeof(UINT4)      , buflen, buf, &offset)
     || nr_stun_encode_htonl(tmp                , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_UCHAR_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    UINT4 tmp;

    if (attrlen != sizeof(UINT4)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Integer is illegal size: %d", attrlen);
        return R_FAILED;
    }

    if (nr_stun_decode_htonl(buf, buflen, &offset, &tmp))
        return R_FAILED;

    *((UCHAR *)data) = (tmp >> 24) & 0xff;

    return 0;
}

nr_stun_attr_codec nr_stun_attr_codec_UCHAR = {
    "UCHAR",
    nr_stun_attr_codec_UCHAR_print,
    nr_stun_attr_codec_UCHAR_encode,
    nr_stun_attr_codec_UCHAR_decode
};

static int
nr_stun_attr_codec_UINT4_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %u", msg, attr_info->name, *(UINT4*)data);
    return 0;
}

static int
nr_stun_attr_codec_UINT4_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int start = offset;

    if (nr_stun_encode_htons(attr_info->type    , buflen, buf, &offset)
     || nr_stun_encode_htons(sizeof(UINT4)      , buflen, buf, &offset)
     || nr_stun_encode_htonl(*(UINT4*)data      , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_UINT4_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    if (attrlen != sizeof(UINT4)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Integer is illegal size: %d", attrlen);
        return R_FAILED;
    }

    if (nr_stun_decode_htonl(buf, buflen, &offset, (UINT4*)data))
        return R_FAILED;

    return 0;
}

nr_stun_attr_codec nr_stun_attr_codec_UINT4 = {
    "UINT4",
    nr_stun_attr_codec_UINT4_print,
    nr_stun_attr_codec_UINT4_encode,
    nr_stun_attr_codec_UINT4_decode
};

static int
nr_stun_attr_codec_UINT8_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %llu", msg, attr_info->name, *(UINT8*)data);
    return 0;
}

static int
nr_stun_attr_codec_UINT8_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int start = offset;

    if (nr_stun_encode_htons(attr_info->type   , buflen, buf, &offset)
     || nr_stun_encode_htons(sizeof(UINT8)     , buflen, buf, &offset)
     || nr_stun_encode_htonll(*(UINT8*)data    , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_UINT8_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    if (attrlen != sizeof(UINT8)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Integer is illegal size: %d", attrlen);
        return R_FAILED;
    }

    if (nr_stun_decode_htonll(buf, buflen, &offset, (UINT8*)data))
        return R_FAILED;

    return 0;
}

nr_stun_attr_codec nr_stun_attr_codec_UINT8 = {
    "UINT8",
    nr_stun_attr_codec_UINT8_print,
    nr_stun_attr_codec_UINT8_encode,
    nr_stun_attr_codec_UINT8_decode
};

static int
nr_stun_attr_codec_addr_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %s", msg, attr_info->name, ((nr_transport_addr*)data)->as_string);
    return 0;
}

static int
nr_stun_attr_codec_addr_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int r,_status;
    int start = offset;
    nr_transport_addr *addr = data;
    UCHAR pad = '\0';
    UCHAR family;

    if ((r=nr_stun_encode_htons(attr_info->type, buflen, buf, &offset)))
        ABORT(r);

    switch (addr->ip_version) {
    case NR_IPV4:
        family = NR_STUN_IPV4_FAMILY;
        if (nr_stun_encode_htons(8               , buflen, buf, &offset)
         || nr_stun_encode(&pad, 1               , buflen, buf, &offset)
         || nr_stun_encode(&family, 1            , buflen, buf, &offset)
         || nr_stun_encode_htons(ntohs(addr->u.addr4.sin_port), buflen, buf, &offset)
         || nr_stun_encode_htonl(ntohl(addr->u.addr4.sin_addr.s_addr), buflen, buf, &offset))
            ABORT(R_FAILED);
        break;

    case NR_IPV6:
        family = NR_STUN_IPV6_FAMILY;
        if (nr_stun_encode_htons(20              , buflen, buf, &offset)
         || nr_stun_encode(&pad, 1               , buflen, buf, &offset)
         || nr_stun_encode(&family, 1            , buflen, buf, &offset)
         || nr_stun_encode_htons(ntohs(addr->u.addr6.sin6_port), buflen, buf, &offset)
         || nr_stun_encode(addr->u.addr6.sin6_addr.s6_addr, 16, buflen, buf, &offset))
            ABORT(R_FAILED);
        break;

    default:
        assert(0);
        ABORT(R_INTERNAL);
        break;
    }

    *attrlen = offset - start;

    _status = 0;
  abort:
    return _status;
}

static int
nr_stun_attr_codec_addr_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int _status;
    UCHAR pad;
    UCHAR family;
    UINT2 port;
    UINT4 addr4;
    struct in6_addr addr6;
    nr_transport_addr *result = data;

    if (nr_stun_decode(1, buf, buflen, &offset, &pad)
     || nr_stun_decode(1, buf, buflen, &offset, &family))
        ABORT(R_FAILED);

    switch (family) {
    case NR_STUN_IPV4_FAMILY:
        if (attrlen != 8) {
            r_log(NR_LOG_STUN, LOG_WARNING, "Illegal attribute length: %d", attrlen);
            ABORT(R_FAILED);
        }

        if (nr_stun_decode_htons(buf, buflen, &offset, &port)
         || nr_stun_decode_htonl(buf, buflen, &offset, &addr4))
            ABORT(R_FAILED);

        if (nr_ip4_port_to_transport_addr(addr4, port, IPPROTO_UDP, result))
            ABORT(R_FAILED);
        break;

    case NR_STUN_IPV6_FAMILY:
        if (attrlen != 20) {
            r_log(NR_LOG_STUN, LOG_WARNING, "Illegal attribute length: %d", attrlen);
            ABORT(R_FAILED);
        }

        if (nr_stun_decode_htons(buf, buflen, &offset, &port)
         || nr_stun_decode(16, buf, buflen, &offset, addr6.s6_addr))
            ABORT(R_FAILED);

        if (nr_ip6_port_to_transport_addr(&addr6, port, IPPROTO_UDP, result))
            ABORT(R_FAILED);
        break;

    default:
        r_log(NR_LOG_STUN, LOG_WARNING, "Illegal address family: %d", family);
        ABORT(R_FAILED);
        break;
    }

    _status = 0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_addr = {
    "addr",
    nr_stun_attr_codec_addr_print,
    nr_stun_attr_codec_addr_encode,
    nr_stun_attr_codec_addr_decode
};

static int
nr_stun_attr_codec_data_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    nr_stun_attr_data *d = data;
    r_dump(NR_LOG_STUN, LOG_DEBUG, attr_info->name, (char*)d->data, d->length);
    return 0;
}

static int
nr_stun_attr_codec_data_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    nr_stun_attr_data *d = data;
    int start = offset;

    if (nr_stun_encode_htons(attr_info->type     , buflen, buf, &offset)
     || nr_stun_encode_htons(d->length           , buflen, buf, &offset)
     || nr_stun_encode(d->data, d->length        , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_data_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int _status;
    nr_stun_attr_data *result = data;

    /* -1 because it is going to be null terminated just to be safe */
    if (attrlen >= (sizeof(result->data) - 1)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Too much data: %d bytes", attrlen);
        ABORT(R_FAILED);
    }

    if (nr_stun_decode(attrlen, buf, buflen, &offset, result->data))
        ABORT(R_FAILED);

    result->length = attrlen;
    result->data[attrlen] = '\0'; /* just to be nice */

    _status=0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_data = {
    "data",
    nr_stun_attr_codec_data_print,
    nr_stun_attr_codec_data_encode,
    nr_stun_attr_codec_data_decode
};

static int
nr_stun_attr_codec_error_code_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    nr_stun_attr_error_code *error_code = data;
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %d %s",
                          msg, attr_info->name, error_code->number,
                          error_code->reason);
    return 0;
}

static int
nr_stun_attr_codec_error_code_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    nr_stun_attr_error_code *error_code = data;
    int start = offset;
    int length = strlen(error_code->reason);
    UCHAR pad[2] = { 0 };
    UCHAR class = error_code->number / 100;
    UCHAR number = error_code->number % 100;

    if (nr_stun_encode_htons(attr_info->type   , buflen, buf, &offset)
     || nr_stun_encode_htons(4 + length        , buflen, buf, &offset)
     || nr_stun_encode(pad, 2                  , buflen, buf, &offset)
     || nr_stun_encode(&class, 1               , buflen, buf, &offset)
     || nr_stun_encode(&number, 1              , buflen, buf, &offset)
     || nr_stun_encode((UCHAR*)error_code->reason, length, buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_error_code_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int _status;
    nr_stun_attr_error_code *result = data;
    UCHAR pad[2];
    UCHAR class;
    UCHAR number;
    size_t size_reason;

    if (nr_stun_decode(2, buf, buflen, &offset, pad)
     || nr_stun_decode(1, buf, buflen, &offset, &class)
     || nr_stun_decode(1, buf, buflen, &offset, &number))
        ABORT(R_FAILED);

    result->number = (class * 100) + number;

    size_reason = attrlen - 4;

    /* -1 because the string will be null terminated */
    if (size_reason > (sizeof(result->reason) - 1)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Reason is too large, truncating");
        /* don't fail, but instead truncate the reason */
        size_reason = sizeof(result->reason) - 1;
    }

    if (nr_stun_decode(size_reason, buf, buflen, &offset, (UCHAR*)result->reason))
        ABORT(R_FAILED);
    result->reason[size_reason] = '\0';

    _status=0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_error_code = {
    "error_code",
    nr_stun_attr_codec_error_code_print,
    nr_stun_attr_codec_error_code_encode,
    nr_stun_attr_codec_error_code_decode
};

static int
nr_stun_attr_codec_fingerprint_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    nr_stun_attr_fingerprint *fingerprint = data;
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %08x", msg, attr_info->name, fingerprint->checksum);
    return 0;
}

static int
nr_stun_attr_codec_fingerprint_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    UINT4 checksum;
    nr_stun_attr_fingerprint *fingerprint = data;
    nr_stun_message_header *header = (nr_stun_message_header*)buf;

    /* the length must include the FINGERPRINT attribute when computing
     * the fingerprint */
    header->length = ntohs(header->length);
    header->length += 8;  /* Fingerprint */
    header->length = htons(header->length);

    if (r_crc32((char*)buf, offset, &checksum)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Unable to compute fingerprint");
        return R_FAILED;
    }

    fingerprint->checksum = checksum ^ 0x5354554e;

    r_log(NR_LOG_STUN, LOG_DEBUG, "Computed FINGERPRINT %08x", fingerprint->checksum);

    fingerprint->valid = 1;
    return nr_stun_attr_codec_UINT4.encode(attr_info, &fingerprint->checksum, offset, buflen, buf, attrlen);
}

static int
nr_stun_attr_codec_fingerprint_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int r,_status;
    nr_stun_attr_fingerprint *fingerprint = data;
    nr_stun_message_header *header = (nr_stun_message_header*)buf;
    size_t length;
    UINT4 checksum;

    if ((r=nr_stun_attr_codec_UINT4.decode(attr_info, attrlen, buf, offset, buflen, &fingerprint->checksum)))
        ABORT(r);

    offset -= 4; /* rewind to before the length and type fields */

    /* the length must include the FINGERPRINT attribute when computing
     * the fingerprint */
    length = offset;  /* right before FINGERPRINT */
    length -= sizeof(*header); /* remove header length */
    length += 8;  /* add length of Fingerprint */
    header->length = htons(length);

    /* make sure FINGERPRINT is final attribute in message */
    if (length + sizeof(*header) != buflen) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Fingerprint is not final attribute in message");
        ABORT(R_FAILED);
    }

    if (r_crc32((char*)buf, offset, &checksum)) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Unable to compute fingerprint");
        ABORT(R_FAILED);
    }

    fingerprint->valid = (fingerprint->checksum == (checksum ^ 0x5354554e));

    r_log(NR_LOG_STUN, LOG_DEBUG, "Computed FINGERPRINT %08x", (checksum ^ 0x5354554e));
    if (! fingerprint->valid)
        r_log(NR_LOG_STUN, LOG_WARNING, "Invalid FINGERPRINT %08x", fingerprint->checksum);

    _status=0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_fingerprint = {
    "fingerprint",
    nr_stun_attr_codec_fingerprint_print,
    nr_stun_attr_codec_fingerprint_encode,
    nr_stun_attr_codec_fingerprint_decode
};

static int
nr_stun_attr_codec_flag_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: on", msg, attr_info->name);
    return 0;
}

static int
nr_stun_attr_codec_flag_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int start = offset;

    if (nr_stun_encode_htons(attr_info->type  , buflen, buf, &offset)
     || nr_stun_encode_htons(0                , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_flag_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    if (attrlen != 0) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Illegal flag length: %d", attrlen);
        return R_FAILED;
    }

    return 0;
}

nr_stun_attr_codec nr_stun_attr_codec_flag = {
    "flag",
    nr_stun_attr_codec_flag_print,
    nr_stun_attr_codec_flag_encode,
    nr_stun_attr_codec_flag_decode
};

static int
nr_stun_attr_codec_message_integrity_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    nr_stun_attr_message_integrity *integrity = data;
    r_dump(NR_LOG_STUN, LOG_DEBUG, attr_info->name, (char*)integrity->hash, sizeof(integrity->hash));
    return 0;
}

static int
nr_stun_compute_message_integrity(UCHAR *buf, int offset, UCHAR *password, int passwordlen, UCHAR *computedHMAC)
{
    int r,_status;
    UINT2 hold;
    UINT2 length;
    nr_stun_message_header *header;

    r_log(NR_LOG_STUN, LOG_DEBUG, "Computing MESSAGE-INTEGRITY");

    header = (nr_stun_message_header*)buf;
    hold = header->length;

    /* adjust the length of the message */
    length = offset;
    length -= sizeof(*header);
    length += 24; /* for MESSAGE-INTEGRITY attribute */
    header->length = htons(length);

    if ((r=nr_crypto_hmac_sha1((UCHAR*)password, passwordlen,
                               buf, offset, computedHMAC)))
        ABORT(r);

    r_dump(NR_LOG_STUN, LOG_DEBUG, "Computed MESSAGE-INTEGRITY ", (char*)computedHMAC, 20);

    _status=0;
 abort:
    header->length = hold;
    return _status;
}

static int
nr_stun_attr_codec_message_integrity_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int start = offset;
    nr_stun_attr_message_integrity *integrity = data;

    if (nr_stun_compute_message_integrity(buf, offset, integrity->password, integrity->passwordlen, integrity->hash))
        return R_FAILED;

    if (nr_stun_encode_htons(attr_info->type                         , buflen, buf, &offset)
     || nr_stun_encode_htons(sizeof(integrity->hash)                 , buflen, buf, &offset)
     || nr_stun_encode(integrity->hash, sizeof(integrity->hash)      , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_message_integrity_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int _status;
    int start;
    nr_stun_attr_message_integrity *result = data;
    UCHAR computedHMAC[20];

    result->valid = 0;

    if (attrlen != 20) {
        r_log(NR_LOG_STUN, LOG_WARNING, "%s must be 20 bytes, not %d", attr_info->name, attrlen);
        ABORT(R_FAILED);
    }

    start = offset - 4; /* rewind to before the length and type fields */
    if (start < 0)
        ABORT(R_INTERNAL);

    if (nr_stun_decode(attrlen, buf, buflen, &offset, result->hash))
        ABORT(R_FAILED);

    if (result->unknown_user) {
        result->valid = 0;
    }
    else {
        if (nr_stun_compute_message_integrity(buf, start, result->password, result->passwordlen, computedHMAC))
            ABORT(R_FAILED);

        assert(sizeof(computedHMAC) == sizeof(result->hash));

        result->valid = (memcmp(computedHMAC, result->hash, 20) == 0);
    }

   _status=0;
 abort:
   return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_message_integrity = {
    "message_integrity",
    nr_stun_attr_codec_message_integrity_print,
    nr_stun_attr_codec_message_integrity_encode,
    nr_stun_attr_codec_message_integrity_decode
};

static int
nr_stun_attr_codec_noop_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    return SKIP_ATTRIBUTE_DECODE;
}

nr_stun_attr_codec nr_stun_attr_codec_noop = {
    "NOOP",
    0,  /* ignore, never print these attributes */
    0,  /* ignore, never encode these attributes */
    nr_stun_attr_codec_noop_decode
};

static int
nr_stun_attr_codec_quoted_string_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %s",
                          msg, attr_info->name, (char*)data);
    return 0;
}

static int
nr_stun_attr_codec_quoted_string_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
//TODO: !nn! syntax check, conversion if not quoted already?
//We'll just restrict this in the API -- EKR
    return nr_stun_attr_codec_string.encode(attr_info, data, offset, buflen, buf, attrlen);
}

static int
nr_stun_attr_codec_quoted_string_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
//TODO: !nn! I don't see any need to unquote this but we may
//find one later -- EKR
    return nr_stun_attr_codec_string.decode(attr_info, attrlen, buf, offset, buflen, data);
}

nr_stun_attr_codec nr_stun_attr_codec_quoted_string = {
    "quoted_string",
    nr_stun_attr_codec_quoted_string_print,
    nr_stun_attr_codec_quoted_string_encode,
    nr_stun_attr_codec_quoted_string_decode
};

static int
nr_stun_attr_codec_string_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %s",
                          msg, attr_info->name, (char*)data);
    return 0;
}

static int
nr_stun_attr_codec_string_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int start = offset;
    char *str = data;
    int length = strlen(str);

    if (nr_stun_encode_htons(attr_info->type  , buflen, buf, &offset)
     || nr_stun_encode_htons(length           , buflen, buf, &offset)
     || nr_stun_encode((UCHAR*)str, length    , buflen, buf, &offset))
        return R_FAILED;

    *attrlen = offset - start;

    return 0;
}

static int
nr_stun_attr_codec_string_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int _status;
    char *result = data;

    /* actual enforcement of the specific string size happens elsewhere */
    if (attrlen >= NR_STUN_MAX_STRING_SIZE) {
        r_log(NR_LOG_STUN, LOG_WARNING, "String is too large: %d bytes", attrlen);
        ABORT(R_FAILED);
    }

    if (nr_stun_decode(attrlen, buf, buflen, &offset, (UCHAR*)result))
        ABORT(R_FAILED);
    result[attrlen] = '\0'; /* just to be nice */

    if (strlen(result) != attrlen) {
        /* stund 0.96 sends a final null in the Server attribute, so
         * only error if the null appears anywhere else in a string */
        if (strlen(result) != attrlen-1) {
            r_log(NR_LOG_STUN, LOG_WARNING, "Error in string: %zd/%d", strlen(result), attrlen);
            ABORT(R_FAILED);
        }
    }

    _status = 0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_string = {
    "string",
    nr_stun_attr_codec_string_print,
    nr_stun_attr_codec_string_encode,
    nr_stun_attr_codec_string_decode
};

static int
nr_stun_attr_codec_unknown_attributes_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    nr_stun_attr_unknown_attributes *unknown_attributes = data;
    char type[9];
    char str[64 + (NR_STUN_MAX_UNKNOWN_ATTRIBUTES * sizeof(type))];
    int i;

    snprintf(str, sizeof(str), "%s %s:", msg, attr_info->name);
    for (i = 0; i < unknown_attributes->num_attributes; ++i) {
        snprintf(type, sizeof(type), "%s 0x%04x", ((i>0)?",":""), unknown_attributes->attribute[i]);
        strlcat(str, type, sizeof(str));
    }

    r_log(NR_LOG_STUN, LOG_DEBUG, "%s", str);
    return 0;
}

static int
nr_stun_attr_codec_unknown_attributes_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    int _status;
    int start = offset;
    nr_stun_attr_unknown_attributes *unknown_attributes = data;
    int length = (2 * unknown_attributes->num_attributes);
    int i;

    if (unknown_attributes->num_attributes > NR_STUN_MAX_UNKNOWN_ATTRIBUTES) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Too many UNKNOWN-ATTRIBUTES: %d", unknown_attributes->num_attributes);
        ABORT(R_FAILED);
    }

    if (nr_stun_encode_htons(attr_info->type  , buflen, buf, &offset)
     || nr_stun_encode_htons(length           , buflen, buf, &offset))
        ABORT(R_FAILED);

    for (i = 0; i < unknown_attributes->num_attributes; ++i) {
        if (nr_stun_encode_htons(unknown_attributes->attribute[i], buflen, buf, &offset))
            ABORT(R_FAILED);
    }

    *attrlen = offset - start;

    _status = 0;
  abort:
    return _status;
}

static int
nr_stun_attr_codec_unknown_attributes_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int _status;
    nr_stun_attr_unknown_attributes *unknown_attributes = data;
    int i;
    UINT2 *a;

    if ((attrlen % 4) != 0) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Attribute is illegal size: %d", attrlen);
        ABORT(R_REJECTED);
    }

    unknown_attributes->num_attributes = attrlen / 2;

    if (unknown_attributes->num_attributes > NR_STUN_MAX_UNKNOWN_ATTRIBUTES) {
        r_log(NR_LOG_STUN, LOG_WARNING, "Too many UNKNOWN-ATTRIBUTES: %d", unknown_attributes->num_attributes);
        ABORT(R_REJECTED);
    }

    for (i = 0; i < unknown_attributes->num_attributes; ++i) {
        a = &(unknown_attributes->attribute[i]);
        if (nr_stun_decode_htons(buf, buflen, &offset, a))
            return R_FAILED;
    }

    _status = 0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_unknown_attributes = {
    "unknown_attributes",
    nr_stun_attr_codec_unknown_attributes_print,
    nr_stun_attr_codec_unknown_attributes_encode,
    nr_stun_attr_codec_unknown_attributes_decode
};

static int
nr_stun_attr_codec_xor_mapped_address_print(nr_stun_attr_info *attr_info, char *msg, void *data)
{
    nr_stun_attr_xor_mapped_address *xor_mapped_address = data;
    r_log(NR_LOG_STUN, LOG_DEBUG, "%s %s: %s (unmasked) %s (masked)",
                          msg, attr_info->name,
                          xor_mapped_address->unmasked.as_string,
                          xor_mapped_address->masked.as_string);
    return 0;
}

static int
nr_stun_attr_codec_xor_mapped_address_encode(nr_stun_attr_info *attr_info, void *data, size_t offset, size_t buflen, UCHAR *buf, size_t *attrlen)
{
    nr_stun_attr_xor_mapped_address *xor_mapped_address = data;
    nr_stun_message_header *header = (nr_stun_message_header*)buf;
    UINT4 magic_cookie;

    r_log(NR_LOG_STUN, LOG_DEBUG, "Unmasked XOR-MAPPED-ADDRESS = %s", xor_mapped_address->unmasked.as_string);

    /* this needs to be the magic cookie in the header and not
     * the MAGIC_COOKIE constant because if we're talking to
     * older servers (that don't have a magic cookie) they use
     * message ID for this */
    magic_cookie = ntohl(header->magic_cookie);

    nr_stun_xor_mapped_address(magic_cookie, header->id, &xor_mapped_address->unmasked, &xor_mapped_address->masked);

    r_log(NR_LOG_STUN, LOG_DEBUG, "Masked XOR-MAPPED-ADDRESS = %s", xor_mapped_address->masked.as_string);

    if (nr_stun_attr_codec_addr.encode(attr_info, &xor_mapped_address->masked, offset, buflen, buf, attrlen))
        return R_FAILED;

    return 0;
}

static int
nr_stun_attr_codec_xor_mapped_address_decode(nr_stun_attr_info *attr_info, size_t attrlen, UCHAR *buf, size_t offset, size_t buflen, void *data)
{
    int r,_status;
    nr_stun_attr_xor_mapped_address *xor_mapped_address = data;
    nr_stun_message_header *header = (nr_stun_message_header*)buf;
    UINT4 magic_cookie;

    if ((r=nr_stun_attr_codec_addr.decode(attr_info, attrlen, buf, offset, buflen, &xor_mapped_address->masked)))
        ABORT(r);

    r_log(NR_LOG_STUN, LOG_DEBUG, "Masked XOR-MAPPED-ADDRESS = %s", xor_mapped_address->masked.as_string);

    /* this needs to be the magic cookie in the header and not
     * the MAGIC_COOKIE constant because if we're talking to
     * older servers (that don't have a magic cookie) they use
     * message ID for this */
    magic_cookie = ntohl(header->magic_cookie);

    nr_stun_xor_mapped_address(magic_cookie, header->id, &xor_mapped_address->masked, &xor_mapped_address->unmasked);

    r_log(NR_LOG_STUN, LOG_DEBUG, "Unmasked XOR-MAPPED-ADDRESS = %s", xor_mapped_address->unmasked.as_string);

    _status = 0;
  abort:
    return _status;
}

nr_stun_attr_codec nr_stun_attr_codec_xor_mapped_address = {
    "xor_mapped_address",
    nr_stun_attr_codec_xor_mapped_address_print,
    nr_stun_attr_codec_xor_mapped_address_encode,
    nr_stun_attr_codec_xor_mapped_address_decode
};

nr_stun_attr_codec nr_stun_attr_codec_old_xor_mapped_address = {
    "xor_mapped_address",
    nr_stun_attr_codec_xor_mapped_address_print,
    0, /* never encode this type */
    nr_stun_attr_codec_xor_mapped_address_decode
};

nr_stun_attr_codec nr_stun_attr_codec_xor_peer_address = {
    "xor_peer_address",
    nr_stun_attr_codec_xor_mapped_address_print,
    nr_stun_attr_codec_xor_mapped_address_encode,
    nr_stun_attr_codec_xor_mapped_address_decode
};

#define NR_ADD_STUN_ATTRIBUTE(type, name, codec, illegal) \
 { (type), (name), &(codec), illegal },

#define NR_ADD_STUN_ATTRIBUTE_IGNORE(type, name) \
 { (type), (name), &nr_stun_attr_codec_noop, 0 },


static nr_stun_attr_info attrs[] = {
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_ALTERNATE_SERVER, "ALTERNATE-SERVER", nr_stun_attr_codec_addr, 0)
#ifdef USE_STUND_0_96
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_OLD_CHANGE_REQUEST, "CHANGE-REQUEST", nr_stun_attr_codec_UINT4, 0)
#endif
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_ERROR_CODE, "ERROR-CODE", nr_stun_attr_codec_error_code, nr_stun_attr_error_code_illegal)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_FINGERPRINT, "FINGERPRINT", nr_stun_attr_codec_fingerprint, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_MAPPED_ADDRESS, "MAPPED-ADDRESS", nr_stun_attr_codec_addr, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_MESSAGE_INTEGRITY, "MESSAGE-INTEGRITY", nr_stun_attr_codec_message_integrity, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_NONCE, "NONCE", nr_stun_attr_codec_quoted_string, nr_stun_attr_nonce_illegal)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_REALM, "REALM", nr_stun_attr_codec_quoted_string, nr_stun_attr_realm_illegal)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_SERVER, "SERVER", nr_stun_attr_codec_string, nr_stun_attr_server_illegal)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_UNKNOWN_ATTRIBUTES, "UNKNOWN-ATTRIBUTES", nr_stun_attr_codec_unknown_attributes, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_USERNAME, "USERNAME", nr_stun_attr_codec_string, nr_stun_attr_username_illegal)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_XOR_MAPPED_ADDRESS, "XOR-MAPPED-ADDRESS", nr_stun_attr_codec_xor_mapped_address, 0)

#ifdef USE_ICE
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_ICE_CONTROLLED, "ICE-CONTROLLED", nr_stun_attr_codec_UINT8, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_ICE_CONTROLLING, "ICE-CONTROLLING", nr_stun_attr_codec_UINT8, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_PRIORITY, "PRIORITY", nr_stun_attr_codec_UINT4, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_USE_CANDIDATE, "USE-CANDIDATE", nr_stun_attr_codec_flag, 0)
#endif

#ifdef USE_TURN
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_DATA, "DATA", nr_stun_attr_codec_data, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_LIFETIME, "LIFETIME", nr_stun_attr_codec_UINT4, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_XOR_RELAY_ADDRESS, "XOR-RELAY-ADDRESS", nr_stun_attr_codec_xor_mapped_address, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_XOR_PEER_ADDRESS, "XOR-PEER-ADDRESS", nr_stun_attr_codec_xor_peer_address, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_REQUESTED_TRANSPORT, "REQUESTED-TRANSPORT", nr_stun_attr_codec_UCHAR, 0)
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_BANDWIDTH, "BANDWIDTH", nr_stun_attr_codec_UINT4, 0)
#endif /* USE_TURN */

   /* for backwards compatibilty */
   NR_ADD_STUN_ATTRIBUTE(NR_STUN_ATTR_OLD_XOR_MAPPED_ADDRESS, "Old XOR-MAPPED-ADDRESS", nr_stun_attr_codec_old_xor_mapped_address, 0)
#ifdef USE_RFC_3489_BACKWARDS_COMPATIBLE
   NR_ADD_STUN_ATTRIBUTE_IGNORE(NR_STUN_ATTR_OLD_RESPONSE_ADDRESS, "RESPONSE-ADDRESS")
   NR_ADD_STUN_ATTRIBUTE_IGNORE(NR_STUN_ATTR_OLD_SOURCE_ADDRESS, "SOURCE-ADDRESS")
   NR_ADD_STUN_ATTRIBUTE_IGNORE(NR_STUN_ATTR_OLD_CHANGED_ADDRESS, "CHANGED-ADDRESS")
   NR_ADD_STUN_ATTRIBUTE_IGNORE(NR_STUN_ATTR_OLD_PASSWORD, "PASSWORD")
#endif /* USE_RFC_3489_BACKWARDS_COMPATIBLE */
};


int
nr_stun_find_attr_info(UINT2 type, nr_stun_attr_info **info)
{
    int _status;
    size_t i;

    *info = 0;
    for (i = 0; i < sizeof(attrs)/sizeof(*attrs); ++i) {
        if (type == attrs[i].type) {
            *info = &attrs[i];
            break;
        }
    }

    if (*info == 0)
        ABORT(R_NOT_FOUND);

    _status=0;
  abort:
    return(_status);
}

int
nr_stun_fix_attribute_ordering(nr_stun_message *msg)
{
    nr_stun_message_attribute *message_integrity;
    nr_stun_message_attribute *fingerprint;

    /* 2nd to the last */
    if (nr_stun_message_has_attribute(msg, NR_STUN_ATTR_MESSAGE_INTEGRITY, &message_integrity)) {
        TAILQ_REMOVE(&msg->attributes, message_integrity, entry);
        TAILQ_INSERT_TAIL(&msg->attributes, message_integrity, entry);
    }

    /* last */
    if (nr_stun_message_has_attribute(msg, NR_STUN_ATTR_FINGERPRINT, &fingerprint)) {
        TAILQ_REMOVE(&msg->attributes, fingerprint, entry);
        TAILQ_INSERT_TAIL(&msg->attributes, fingerprint, entry);
    }

    return 0;
}

// Since this sanity check is only a collection of assert statements and those
// assert statements are compiled out in non-debug builds, undef SANITY_CHECKS
// so we can avoid the warning that padding_bytes is never used in opt builds.
#ifdef NDEBUG
#undef SANITY_CHECKS
#endif

#ifdef SANITY_CHECKS
static void sanity_check_encoding_stuff(nr_stun_message *msg)
{
    nr_stun_message_attribute *attr = 0;
    int padding_bytes;
    int l;

    r_log(NR_LOG_STUN, LOG_DEBUG, "Starting to sanity check encoding");

    l = 0;
    TAILQ_FOREACH(attr, &msg->attributes, entry) {
        padding_bytes = 0;
        if ((attr->length % 4) != 0) {
            padding_bytes = 4 - (attr->length % 4);
        }
        assert(attr->length == (attr->encoding_length - (4 + padding_bytes)));
        assert(((void*)attr->encoding) == (msg->buffer + 20 + l));
        l += attr->encoding_length;
        assert((l % 4) == 0);
    }
    assert(l == msg->header.length);
}
#endif /* SANITY_CHECKS */


int
nr_stun_encode_message(nr_stun_message *msg)
{
    int r,_status;
    size_t length_offset;
    size_t length_offset_hold;
    nr_stun_attr_info *attr_info;
    nr_stun_message_attribute *attr;
    int padding_bytes;

    r_log(NR_LOG_STUN, LOG_DEBUG, "Encoding STUN message");

    nr_stun_fix_attribute_ordering(msg);

    msg->name = nr_stun_msg_type(msg->header.type);
    msg->length = 0;
    msg->header.length = 0;

    if ((r=nr_stun_encode_htons(msg->header.type, sizeof(msg->buffer), msg->buffer, &msg->length)))
        ABORT(r);
    if (msg->name)
        r_log(NR_LOG_STUN, LOG_DEBUG, "Encoded MsgType: %s", msg->name);
    else
        r_log(NR_LOG_STUN, LOG_DEBUG, "Encoded MsgType: 0x%03x", msg->header.type);

    /* grab the offset to be used later to re-write the header length field */
    length_offset_hold = msg->length;

    if ((r=nr_stun_encode_htons(msg->header.length, sizeof(msg->buffer), msg->buffer, &msg->length)))
        ABORT(r);

    if ((r=nr_stun_encode_htonl(msg->header.magic_cookie, sizeof(msg->buffer), msg->buffer, &msg->length)))
        ABORT(r);
    r_log(NR_LOG_STUN, LOG_DEBUG, "Encoded Cookie: %08x", msg->header.magic_cookie);

    if ((r=nr_stun_encode((UCHAR*)(&msg->header.id), sizeof(msg->header.id), sizeof(msg->buffer), msg->buffer, &msg->length)))
        ABORT(r);
    r_dump(NR_LOG_STUN, LOG_DEBUG, "Encoded ID", (void*)&msg->header.id, sizeof(msg->header.id));

    TAILQ_FOREACH(attr, &msg->attributes, entry) {
        if ((r=nr_stun_find_attr_info(attr->type, &attr_info))) {
            r_log(NR_LOG_STUN, LOG_WARNING, "Unrecognized attribute: 0x%04x", attr->type);
            ABORT(R_INTERNAL);
        }

        attr->name = attr_info->name;
        attr->type_name = attr_info->codec->name;
        attr->encoding = (nr_stun_encoded_attribute*)&msg->buffer[msg->length];

        if (attr_info->codec->encode != 0) {
            if ((r=attr_info->codec->encode(attr_info, &attr->u, msg->length, sizeof(msg->buffer), msg->buffer, &attr->encoding_length))) {
                r_log(NR_LOG_STUN, LOG_WARNING, "Unable to encode %s", attr_info->name);
                ABORT(r);
            }

            msg->length += attr->encoding_length;
            attr->length = attr->encoding_length - 4;  /* -4 for type and length fields */

            if (attr_info->illegal) {
                if ((r=attr_info->illegal(attr_info, attr->length, &attr->u)))
                    ABORT(r);
            }

            attr_info->codec->print(attr_info, "Encoded", &attr->u);

            if ((attr->length % 4) == 0) {
                padding_bytes = 0;
            }
            else {
                padding_bytes = 4 - (attr->length % 4);
                nr_stun_encode((UCHAR*)"\0\0\0\0", padding_bytes, sizeof(msg->buffer), msg->buffer, &msg->length);
                attr->encoding_length += padding_bytes;
            }

            msg->header.length += attr->encoding_length;
            length_offset = length_offset_hold;
            (void)nr_stun_encode_htons(msg->header.length, sizeof(msg->buffer), msg->buffer, &length_offset);
        }
        else {
            r_log(NR_LOG_STUN, LOG_WARNING, "Missing encode function for attribute: %s", attr_info->name);
        }
    }

    r_log(NR_LOG_STUN, LOG_DEBUG, "Encoded Length: %d", msg->header.length);

    assert(msg->length < NR_STUN_MAX_MESSAGE_SIZE);

#ifdef SANITY_CHECKS
    sanity_check_encoding_stuff(msg);
#endif /* SANITY_CHECKS */

    _status=0;
abort:
    return _status;
}

int
nr_stun_decode_message(nr_stun_message *msg, int (*get_password)(void *arg, nr_stun_message *msg, Data **password), void *arg)
{
    int r,_status;
    int offset;
    int size;
    int padding_bytes;
    nr_stun_message_attribute *attr;
    nr_stun_attr_info *attr_info;
    Data *password;

    r_log(NR_LOG_STUN, LOG_DEBUG, "Parsing STUN message of %d bytes", msg->length);

    if (!TAILQ_EMPTY(&msg->attributes))
        ABORT(R_BAD_ARGS);

    if (sizeof(nr_stun_message_header) > msg->length) {
       r_log(NR_LOG_STUN, LOG_WARNING, "Message too small");
       ABORT(R_FAILED);
    }

    memcpy(&msg->header, msg->buffer, sizeof(msg->header));
    msg->header.type = ntohs(msg->header.type);
    msg->header.length = ntohs(msg->header.length);
    msg->header.magic_cookie = ntohl(msg->header.magic_cookie);

    msg->name = nr_stun_msg_type(msg->header.type);

    if (msg->name)
        r_log(NR_LOG_STUN, LOG_DEBUG, "Parsed MsgType: %s", msg->name);
    else
        r_log(NR_LOG_STUN, LOG_DEBUG, "Parsed MsgType: 0x%03x", msg->header.type);
    r_log(NR_LOG_STUN, LOG_DEBUG, "Parsed Length: %d", msg->header.length);
    r_log(NR_LOG_STUN, LOG_DEBUG, "Parsed Cookie: %08x", msg->header.magic_cookie);
    r_dump(NR_LOG_STUN, LOG_DEBUG, "Parsed ID", (void*)&msg->header.id, sizeof(msg->header.id));

    if (msg->header.length + sizeof(msg->header) != msg->length) {
       r_log(NR_LOG_STUN, LOG_WARNING, "Inconsistent message header length: %d/%d",
                                        msg->header.length, msg->length);
       ABORT(R_FAILED);
    }

    size = msg->header.length;

    if ((size % 4) != 0) {
       r_log(NR_LOG_STUN, LOG_WARNING, "Illegal message size: %d", msg->header.length);
       ABORT(R_FAILED);
    }

    offset = sizeof(msg->header);

    while (size > 0) {
        r_log(NR_LOG_STUN, LOG_DEBUG, "size = %d", size);

        if (size < 4) {
           r_log(NR_LOG_STUN, LOG_WARNING, "Illegal message length: %d", size);
           ABORT(R_FAILED);
        }

        if ((r=nr_stun_message_attribute_create(msg, &attr)))
            ABORT(R_NO_MEMORY);

        attr->encoding          = (nr_stun_encoded_attribute*)&msg->buffer[offset];
        attr->type              = ntohs(attr->encoding->type);
        attr->length            = ntohs(attr->encoding->length);
        attr->encoding_length   = attr->length + 4;

        if ((attr->length % 4) != 0) {
            padding_bytes = 4 - (attr->length % 4);
            attr->encoding_length += padding_bytes;
        }

        if ((attr->encoding_length) > (size_t)size) {
           r_log(NR_LOG_STUN, LOG_WARNING, "Attribute length larger than remaining message size: %d/%d", attr->encoding_length, size);
           ABORT(R_FAILED);
        }

        if ((r=nr_stun_find_attr_info(attr->type, &attr_info))) {
            if (attr->type <= 0x7FFF)
                ++msg->comprehension_required_unknown_attributes;
            else
                ++msg->comprehension_optional_unknown_attributes;
            r_log(NR_LOG_STUN, LOG_INFO, "Unrecognized attribute: 0x%04x", attr->type);
        }
        else {
            attr_info->name = attr_info->name;
            attr->type_name = attr_info->codec->name;

            if (attr->type == NR_STUN_ATTR_MESSAGE_INTEGRITY) {
                if (get_password && get_password(arg, msg, &password) == 0) {
                    if (password->len > sizeof(attr->u.message_integrity.password)) {
                        r_log(NR_LOG_STUN, LOG_WARNING, "Password too long: %d bytes", password->len);
                        ABORT(R_FAILED);
                    }

                    memcpy(attr->u.message_integrity.password, password->data, password->len);
                    attr->u.message_integrity.passwordlen = password->len;
                }
                else {
                    /* set to user "not found" */
                    attr->u.message_integrity.unknown_user = 1;
                }
            }
            else if (attr->type == NR_STUN_ATTR_OLD_XOR_MAPPED_ADDRESS) {
                attr->type = NR_STUN_ATTR_XOR_MAPPED_ADDRESS;
                r_log(NR_LOG_STUN, LOG_INFO, "Translating obsolete XOR-MAPPED-ADDRESS type");
            }

            if ((r=attr_info->codec->decode(attr_info, attr->length, msg->buffer, offset+4, msg->length, &attr->u))) {
                if (r == SKIP_ATTRIBUTE_DECODE) {
                    r_log(NR_LOG_STUN, LOG_INFO, "Skipping %s", attr_info->name);
                }
                else {
                    r_log(NR_LOG_STUN, LOG_WARNING, "Unable to parse %s", attr_info->name);
                }

                attr->invalid = 1;
            }
            else {
                attr_info->codec->print(attr_info, "Parsed", &attr->u);

#ifdef USE_STUN_PEDANTIC
                r_log(NR_LOG_STUN, LOG_DEBUG, "Before pedantic attr_info checks");
                if (attr_info->illegal) {
                    if ((r=attr_info->illegal(attr_info, attr->length, &attr->u))) {
                        r_log(NR_LOG_STUN, LOG_WARNING, "Failed pedantic attr_info checks");
                        ABORT(r);
                    }
                }
                r_log(NR_LOG_STUN, LOG_DEBUG, "After pedantic attr_info checks");
#endif /* USE_STUN_PEDANTIC */
            }
        }

        offset += attr->encoding_length;
        size -= attr->encoding_length;
    }

#ifdef SANITY_CHECKS
    sanity_check_encoding_stuff(msg);
#endif /* SANITY_CHECKS */

    _status=0;
  abort:
    return _status;
}