Blame view

3rdparty/jrtplib-3.11.2/src/rtpudpv4transmitter.cpp 45.9 KB
3d2ab595   Hu Chunming   支持gb28181
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
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
  /*
  
    This file is a part of JRTPLIB
    Copyright (c) 1999-2017 Jori Liesenborgs
  
    Contact: jori.liesenborgs@gmail.com
  
    This library was developed at the Expertise Centre for Digital Media
    (http://www.edm.uhasselt.be), a research center of the Hasselt University
    (http://www.uhasselt.be). The library is based upon work done for 
    my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
  
    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:
  
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
  
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
  
  */
  
  #include "rtpudpv4transmitter.h"
  #include "rtprawpacket.h"
  #include "rtpipv4address.h"
  #include "rtptimeutilities.h"
  #include "rtpdefines.h"
  #include "rtpstructs.h"
  #include "rtpsocketutilinternal.h"
  #include "rtpinternalutils.h"
  #include "rtpselect.h"
  #include <stdio.h>
  #include <assert.h>
  #include <vector>
  #ifdef RTPDEBUG
  	#include <iostream>
  #endif // RTPDEBUG
  
  #include <iostream>
  
  #include "rtpdebug.h"
  
  #define RTPUDPV4TRANS_MAXPACKSIZE							65535
  #define RTPUDPV4TRANS_IFREQBUFSIZE							8192
  
  #define RTPUDPV4TRANS_IS_MCASTADDR(x)							(((x)&0xF0000000) == 0xE0000000)
  
  #define RTPUDPV4TRANS_MCASTMEMBERSHIP(socket,type,mcastip,status)	{\
  										struct ip_mreq mreq;\
  										\
  										mreq.imr_multiaddr.s_addr = htonl(mcastip);\
  										mreq.imr_interface.s_addr = htonl(mcastifaceIP);\
  										status = setsockopt(socket,IPPROTO_IP,type,(const char *)&mreq,sizeof(struct ip_mreq));\
  									}
  #ifdef RTP_SUPPORT_THREAD
  	#define MAINMUTEX_LOCK 		{ if (threadsafe) mainmutex.Lock(); }
  	#define MAINMUTEX_UNLOCK	{ if (threadsafe) mainmutex.Unlock(); }
  	#define WAITMUTEX_LOCK		{ if (threadsafe) waitmutex.Lock(); }
  	#define WAITMUTEX_UNLOCK	{ if (threadsafe) waitmutex.Unlock(); }
  #else
  	#define MAINMUTEX_LOCK
  	#define MAINMUTEX_UNLOCK
  	#define WAITMUTEX_LOCK
  	#define WAITMUTEX_UNLOCK
  #endif // RTP_SUPPORT_THREAD
  
  #define CLOSESOCKETS do { \
  	if (closesocketswhendone) \
  	{\
  		if (rtpsock != rtcpsock) \
  			RTPCLOSE(rtcpsock); \
  		RTPCLOSE(rtpsock); \
  	} \
  } while(0)
  		
  
  namespace jrtplib
  {
  
  RTPUDPv4Transmitter::RTPUDPv4Transmitter(RTPMemoryManager *mgr) : RTPTransmitter(mgr),destinations(mgr,RTPMEM_TYPE_CLASS_DESTINATIONLISTHASHELEMENT),
  #ifdef RTP_SUPPORT_IPV4MULTICAST
  								  multicastgroups(mgr,RTPMEM_TYPE_CLASS_MULTICASTHASHELEMENT),
  #endif // RTP_SUPPORT_IPV4MULTICAST
  								  acceptignoreinfo(mgr,RTPMEM_TYPE_CLASS_ACCEPTIGNOREHASHELEMENT)
  {
  	created = false;
  	init = false;
  }
  
  RTPUDPv4Transmitter::~RTPUDPv4Transmitter()
  {
  	Destroy();
  }
  
  int RTPUDPv4Transmitter::Init(bool tsafe)
  {
  	if (init)
  		return ERR_RTP_UDPV4TRANS_ALREADYINIT;
  	
  #ifdef RTP_SUPPORT_THREAD
  	threadsafe = tsafe;
  	if (threadsafe)
  	{
  		int status;
  		
  		status = mainmutex.Init();
  		if (status < 0)
  			return ERR_RTP_UDPV4TRANS_CANTINITMUTEX;
  		status = waitmutex.Init();
  		if (status < 0)
  			return ERR_RTP_UDPV4TRANS_CANTINITMUTEX;
  	}
  #else
  	if (tsafe)
  		return ERR_RTP_NOTHREADSUPPORT;
  #endif // RTP_SUPPORT_THREAD
  
  	init = true;
  	return 0;
  }
  
  static int GetIPv4SocketPort(SocketType s, uint16_t *pPort)
  {
  	assert(pPort != 0);
  
  	struct sockaddr_in addr;
  	memset(&addr, 0, sizeof(struct sockaddr_in));
  
  	RTPSOCKLENTYPE size = sizeof(struct sockaddr_in);
  	if (getsockname(s,(struct sockaddr*)&addr,&size) != 0)
  		return ERR_RTP_UDPV4TRANS_CANTGETSOCKETPORT;
  
  	if (addr.sin_family != AF_INET)
  		return ERR_RTP_UDPV4TRANS_NOTANIPV4SOCKET;
  
  	uint16_t port = ntohs(addr.sin_port);
  	if (port == 0)
  		return ERR_RTP_UDPV4TRANS_SOCKETPORTNOTSET;
  	
  	int type = 0;
  	RTPSOCKLENTYPE length = sizeof(type);
  
  	if (getsockopt(s, SOL_SOCKET, SO_TYPE, (char*)&type, &length) != 0)
  		return ERR_RTP_UDPV4TRANS_CANTGETSOCKETTYPE;
  
  	if (type != SOCK_DGRAM)
  		return ERR_RTP_UDPV4TRANS_INVALIDSOCKETTYPE;
  
  	*pPort = port;
  	return 0;
  }
  
  int GetAutoSockets(uint32_t bindIP, bool allowOdd, bool rtcpMux,
                     SocketType *pRtpSock, SocketType *pRtcpSock, 
                     uint16_t *pRtpPort, uint16_t *pRtcpPort)
  {
  	const int maxAttempts = 1024;
  	int attempts = 0;
  	std::vector<SocketType> toClose;
  
  	while (attempts++ < maxAttempts)
  	{
  		SocketType sock = socket(PF_INET, SOCK_DGRAM, 0);
  		if (sock == RTPSOCKERR)
  		{
  			for (size_t i = 0 ; i < toClose.size() ; i++)
  				RTPCLOSE(toClose[i]);
  			return ERR_RTP_UDPV4TRANS_CANTCREATESOCKET;
  		}
  
  		// First we get an automatically chosen port
  
  		struct sockaddr_in addr;
  		memset(&addr,0,sizeof(struct sockaddr_in));
  
  		addr.sin_family = AF_INET;
  		addr.sin_port = 0;
  		addr.sin_addr.s_addr = htonl(bindIP);
  		if (bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) != 0)
  		{
  			RTPCLOSE(sock);
  			for (size_t i = 0 ; i < toClose.size() ; i++)
  				RTPCLOSE(toClose[i]);
  			return ERR_RTP_UDPV4TRANS_CANTGETVALIDSOCKET;
  		}
  
  		uint16_t basePort = 0;
  		int status = GetIPv4SocketPort(sock, &basePort);
  		if (status < 0)
  		{
  			RTPCLOSE(sock);
  			for (size_t i = 0 ; i < toClose.size() ; i++)
  				RTPCLOSE(toClose[i]);
  			return status;
  		}
  
  		if (rtcpMux) // only need one socket
  		{
  			if (basePort%2 == 0 || allowOdd)
  			{
  				*pRtpSock = sock;
  				*pRtcpSock = sock;
  				*pRtpPort = basePort;
  				*pRtcpPort = basePort;
  				for (size_t i = 0 ; i < toClose.size() ; i++)
  					RTPCLOSE(toClose[i]);
  
  				return 0;
  			}
  			else
  				toClose.push_back(sock);
  		}
  		else
  		{
  			SocketType sock2 = socket(PF_INET, SOCK_DGRAM, 0);
  			if (sock2 == RTPSOCKERR)
  			{
  				RTPCLOSE(sock);
  				for (size_t i = 0 ; i < toClose.size() ; i++)
  					RTPCLOSE(toClose[i]);
  				return ERR_RTP_UDPV4TRANS_CANTCREATESOCKET;
  			}
  
  			// Try the next port or the previous port
  			uint16_t secondPort = basePort;
  			bool possiblyValid = false;
  
  			if (basePort%2 == 0)
  			{
  				secondPort++;
  				possiblyValid = true;
  			}
  			else if (basePort > 1) // avoid landing on port 0
  			{
  				secondPort--;
  				possiblyValid = true;
  			}
  
  			if (possiblyValid)
  			{
  				memset(&addr,0,sizeof(struct sockaddr_in));
  
  				addr.sin_family = AF_INET;
  				addr.sin_port = htons(secondPort);
  				addr.sin_addr.s_addr = htonl(bindIP);
  				if (bind(sock2,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) == 0)
  				{
  					// In this case, we have two consecutive port numbers, the lower of
  					// which is even
  
  					if (basePort < secondPort)
  					{
  						*pRtpSock = sock;
  						*pRtcpSock = sock2;
  						*pRtpPort = basePort;
  						*pRtcpPort = secondPort;
  					}
  					else
  					{
  						*pRtpSock = sock2;
  						*pRtcpSock = sock;
  						*pRtpPort = secondPort;
  						*pRtcpPort = basePort;
  					}
  
  					for (size_t i = 0 ; i < toClose.size() ; i++)
  						RTPCLOSE(toClose[i]);
  
  					return 0;
  				}
  			}
  
  			toClose.push_back(sock);
  			toClose.push_back(sock2);
  		}
  	}
  
  	for (size_t i = 0 ; i < toClose.size() ; i++)
  		RTPCLOSE(toClose[i]);
  
  	return ERR_RTP_UDPV4TRANS_TOOMANYATTEMPTSCHOOSINGSOCKET;
  }
  
  int RTPUDPv4Transmitter::Create(size_t maximumpacketsize,const RTPTransmissionParams *transparams)
  {
  	const RTPUDPv4TransmissionParams *params,defaultparams;
  	struct sockaddr_in addr;
  	RTPSOCKLENTYPE size;
  	int status;
  
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  
  	if (created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_ALREADYCREATED;
  	}
  	
  	// Obtain transmission parameters
  	
  	if (transparams == 0)
  		params = &defaultparams;
  	else
  	{
  		if (transparams->GetTransmissionProtocol() != RTPTransmitter::IPv4UDPProto)
  		{
  			MAINMUTEX_UNLOCK
  			return ERR_RTP_UDPV4TRANS_ILLEGALPARAMETERS;
  		}
  		params = (const RTPUDPv4TransmissionParams *)transparams;
  	}
  
  	if (params->GetUseExistingSockets(rtpsock, rtcpsock))
  	{
  		closesocketswhendone = false;
  
  		// Determine the port numbers
  		int status = GetIPv4SocketPort(rtpsock, &m_rtpPort);
  		if (status < 0)
  		{
  			MAINMUTEX_UNLOCK
  			return status;
  		}
  		status = GetIPv4SocketPort(rtcpsock, &m_rtcpPort);
  		if (status < 0)
  		{
  			MAINMUTEX_UNLOCK
  			return status;
  		}
  	}
  	else
  	{
  		closesocketswhendone = true;
  
  		if (params->GetPortbase() == 0)
  		{
  			int status = GetAutoSockets(params->GetBindIP(), params->GetAllowOddPortbase(), params->GetRTCPMultiplexing(),
  			                            &rtpsock, &rtcpsock, &m_rtpPort, &m_rtcpPort);
  			if (status < 0)
  			{
  				MAINMUTEX_UNLOCK
  				return status;
  			}
  		}
  		else
  		{
  			// Check if portbase is even (if necessary)
  			if (!params->GetAllowOddPortbase() && params->GetPortbase()%2 != 0)
  			{
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_UDPV4TRANS_PORTBASENOTEVEN;
  			}
  
  			// create sockets
  			
  			rtpsock = socket(PF_INET,SOCK_DGRAM,0);
  			if (rtpsock == RTPSOCKERR)
  			{
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_UDPV4TRANS_CANTCREATESOCKET;
  			}
  
  			// If we're multiplexing, we're just going to set the RTCP socket to equal the RTP socket
  			if (params->GetRTCPMultiplexing())
  				rtcpsock = rtpsock;
  			else
  			{
  				rtcpsock = socket(PF_INET,SOCK_DGRAM,0);
  				if (rtcpsock == RTPSOCKERR)
  				{
  					RTPCLOSE(rtpsock);
  					MAINMUTEX_UNLOCK
  					return ERR_RTP_UDPV4TRANS_CANTCREATESOCKET;
  				}
  			}
  
  			// bind sockets
  
  			uint32_t bindIP = params->GetBindIP();
  			
  			m_rtpPort = params->GetPortbase();
  
  			memset(&addr,0,sizeof(struct sockaddr_in));
  			addr.sin_family = AF_INET;
  			addr.sin_port = htons(params->GetPortbase());
  			addr.sin_addr.s_addr = htonl(bindIP);
  			if (bind(rtpsock,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) != 0)
  			{
  				CLOSESOCKETS;
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_UDPV4TRANS_CANTBINDRTPSOCKET;
  			}
  
  			if (rtpsock != rtcpsock) // no need to bind same socket twice when multiplexing
  			{
  				uint16_t rtpport = params->GetPortbase();
  				uint16_t rtcpport = params->GetForcedRTCPPort();
  
  				if (rtcpport == 0)
  				{
  					rtcpport = rtpport;
  					if (rtcpport < 0xFFFF)
  						rtcpport++;
  				}
  
  				memset(&addr,0,sizeof(struct sockaddr_in));
  				addr.sin_family = AF_INET;
  				addr.sin_port = htons(rtcpport);
  				addr.sin_addr.s_addr = htonl(bindIP);
  				if (bind(rtcpsock,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) != 0)
  				{
  					CLOSESOCKETS;
  					MAINMUTEX_UNLOCK
  					return ERR_RTP_UDPV4TRANS_CANTBINDRTCPSOCKET;
  				}
  
  				m_rtcpPort = rtcpport;
  			}
  			else
  				m_rtcpPort = m_rtpPort;
  		}
  
  		// set socket buffer sizes
  		
  		size = params->GetRTPReceiveBuffer();
  		if (setsockopt(rtpsock,SOL_SOCKET,SO_RCVBUF,(const char *)&size,sizeof(int)) != 0)
  		{
  			CLOSESOCKETS;
  			MAINMUTEX_UNLOCK
  			return ERR_RTP_UDPV4TRANS_CANTSETRTPRECEIVEBUF;
  		}
  		size = params->GetRTPSendBuffer();
  		if (setsockopt(rtpsock,SOL_SOCKET,SO_SNDBUF,(const char *)&size,sizeof(int)) != 0)
  		{
  			CLOSESOCKETS;
  			MAINMUTEX_UNLOCK
  			return ERR_RTP_UDPV4TRANS_CANTSETRTPTRANSMITBUF;
  		}
  
  		if (rtpsock != rtcpsock) // no need to set RTCP flags when multiplexing
  		{
  			size = params->GetRTCPReceiveBuffer();
  			if (setsockopt(rtcpsock,SOL_SOCKET,SO_RCVBUF,(const char *)&size,sizeof(int)) != 0)
  			{
  				CLOSESOCKETS;
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_UDPV4TRANS_CANTSETRTCPRECEIVEBUF;
  			}
  			size = params->GetRTCPSendBuffer();
  			if (setsockopt(rtcpsock,SOL_SOCKET,SO_SNDBUF,(const char *)&size,sizeof(int)) != 0)
  			{
  				CLOSESOCKETS;
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_UDPV4TRANS_CANTSETRTCPTRANSMITBUF;
  			}
  		}
  	}
  
  	// Try to obtain local IP addresses
  
  	localIPs = params->GetLocalIPList();
  	if (localIPs.empty()) // User did not provide list of local IP addresses, calculate them
  	{
  		int status;
  		
  		if ((status = CreateLocalIPList()) < 0)
  		{
  			CLOSESOCKETS;
  			MAINMUTEX_UNLOCK
  			return status;
  		}
  #ifdef RTPDEBUG
  		std::cout << "Found these local IP addresses:" << std::endl;
  		
  		std::list<uint32_t>::const_iterator it;
  
  		for (it = localIPs.begin() ; it != localIPs.end() ; it++)
  		{
  			RTPIPv4Address a(*it);
  
  			std::cout << a.GetAddressString() << std::endl;
  		}
  #endif // RTPDEBUG
  	}
  
  #ifdef RTP_SUPPORT_IPV4MULTICAST
  	if (SetMulticastTTL(params->GetMulticastTTL()))
  		supportsmulticasting = true;
  	else
  		supportsmulticasting = false;
  #else // no multicast support enabled
  	supportsmulticasting = false;
  #endif // RTP_SUPPORT_IPV4MULTICAST
  
  	if (maximumpacketsize > RTPUDPV4TRANS_MAXPACKSIZE)
  	{
  		CLOSESOCKETS;
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_SPECIFIEDSIZETOOBIG;
  	}
  	
  	if (!params->GetCreatedAbortDescriptors())
  	{
  		if ((status = m_abortDesc.Init()) < 0)
  		{
  			CLOSESOCKETS;
  			MAINMUTEX_UNLOCK
  			return status;
  		}
  		m_pAbortDesc = &m_abortDesc;
  	}
  	else
  	{
  		m_pAbortDesc = params->GetCreatedAbortDescriptors();
  		if (!m_pAbortDesc->IsInitialized())
  		{
  			CLOSESOCKETS;
  			MAINMUTEX_UNLOCK
  			return ERR_RTP_ABORTDESC_NOTINIT;
  		}
  	}
  
  	maxpacksize = maximumpacketsize;
  	multicastTTL = params->GetMulticastTTL();
  	mcastifaceIP = params->GetMulticastInterfaceIP();
  	receivemode = RTPTransmitter::AcceptAll;
  
  	localhostname = 0;
  	localhostnamelength = 0;
  
  	waitingfordata = false;
  	created = true;
  	MAINMUTEX_UNLOCK 
  	return 0;
  }
  
  void RTPUDPv4Transmitter::Destroy()
  {
  	if (!init)
  		return;
  
  	MAINMUTEX_LOCK
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK;
  		return;
  	}
  
  	if (localhostname)
  	{
  		RTPDeleteByteArray(localhostname,GetMemoryManager());
  		localhostname = 0;
  		localhostnamelength = 0;
  	}
  	
  	CLOSESOCKETS;
  	destinations.Clear();
  #ifdef RTP_SUPPORT_IPV4MULTICAST
  	multicastgroups.Clear();
  #endif // RTP_SUPPORT_IPV4MULTICAST
  	FlushPackets();
  	ClearAcceptIgnoreInfo();
  	localIPs.clear();
  	created = false;
  	
  	if (waitingfordata)
  	{
  		m_pAbortDesc->SendAbortSignal();
  		m_abortDesc.Destroy(); // Doesn't do anything if not initialized
  		MAINMUTEX_UNLOCK
  		WAITMUTEX_LOCK // to make sure that the WaitForIncomingData function ended
  		WAITMUTEX_UNLOCK
  	}
  	else
  		m_abortDesc.Destroy(); // Doesn't do anything if not initialized
  
  	MAINMUTEX_UNLOCK
  }
  
  RTPTransmissionInfo *RTPUDPv4Transmitter::GetTransmissionInfo()
  {
  	if (!init)
  		return 0;
  
  	MAINMUTEX_LOCK
  	RTPTransmissionInfo *tinf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTPTRANSMISSIONINFO) RTPUDPv4TransmissionInfo(localIPs,rtpsock,rtcpsock,m_rtpPort,m_rtcpPort);
  	MAINMUTEX_UNLOCK
  	return tinf;
  }
  
  void RTPUDPv4Transmitter::DeleteTransmissionInfo(RTPTransmissionInfo *i)
  {
  	if (!init)
  		return;
  
  	RTPDelete(i, GetMemoryManager());
  }
  
  int RTPUDPv4Transmitter::GetLocalHostName(uint8_t *buffer,size_t *bufferlength)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	MAINMUTEX_LOCK
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  
  	if (localhostname == 0)
  	{
  		if (localIPs.empty())
  		{
  			MAINMUTEX_UNLOCK
  			return ERR_RTP_UDPV4TRANS_NOLOCALIPS;
  		}
  		
  		std::list<uint32_t>::const_iterator it;
  		std::list<std::string> hostnames;
  	
  		for (it = localIPs.begin() ; it != localIPs.end() ; it++)
  		{
  			bool founddouble = false;
  			bool foundentry = true;
  
  			while (!founddouble && foundentry)
  			{
  				struct hostent *he;
  				uint8_t addr[4];
  				uint32_t ip = (*it);
  		
  				addr[0] = (uint8_t)((ip>>24)&0xFF);
  				addr[1] = (uint8_t)((ip>>16)&0xFF);
  				addr[2] = (uint8_t)((ip>>8)&0xFF);
  				addr[3] = (uint8_t)(ip&0xFF);
  				he = gethostbyaddr((char *)addr,4,AF_INET);
  				if (he != 0)
  				{
  					std::string hname = std::string(he->h_name);
  					std::list<std::string>::const_iterator it;
  
  					for (it = hostnames.begin() ; !founddouble && it != hostnames.end() ; it++)
  						if ((*it) == hname)
  							founddouble = true;
  
  					if (!founddouble)
  						hostnames.push_back(hname);
  					
  					int i = 0;
  					while (!founddouble && he->h_aliases[i] != 0)
  					{
  						std::string hname = std::string(he->h_aliases[i]);
  					
  						for (it = hostnames.begin() ; !founddouble && it != hostnames.end() ; it++)
  							if ((*it) == hname)
  								founddouble = true;
  
  						if (!founddouble)
  						{
  							hostnames.push_back(hname);
  							i++;
  						}
  					}
  				}
  				else
  					foundentry = false;
  			}
  		}
  	
  		bool found  = false;
  		
  		if (!hostnames.empty())	// try to select the most appropriate hostname
  		{
  			std::list<std::string>::const_iterator it;
  		
  			hostnames.sort();
  			for (it = hostnames.begin() ; !found && it != hostnames.end() ; it++)
  			{
  				if ((*it).find('.') != std::string::npos)
  				{
  					found = true;
  					localhostnamelength = (*it).length();
  					localhostname = RTPNew(GetMemoryManager(),RTPMEM_TYPE_OTHER) uint8_t [localhostnamelength+1];
  					if (localhostname == 0)
  					{
  						MAINMUTEX_UNLOCK
  						return ERR_RTP_OUTOFMEM;
  					}
  					memcpy(localhostname,(*it).c_str(),localhostnamelength);
  					localhostname[localhostnamelength] = 0;
  				}
  			}
  		}
  	
  		if (!found) // use an IP address
  		{
  			uint32_t ip;
  			int len;
  			char str[16];
  			
  			it = localIPs.begin();
  			ip = (*it);
  			
  			RTP_SNPRINTF(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF));
  			len = strlen(str);
  	
  			localhostnamelength = len;
  			localhostname = RTPNew(GetMemoryManager(),RTPMEM_TYPE_OTHER) uint8_t [localhostnamelength + 1];
  			if (localhostname == 0)
  			{
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_OUTOFMEM;
  			}
  			memcpy(localhostname,str,localhostnamelength);
  			localhostname[localhostnamelength] = 0;
  		}
  	}
  	
  	if ((*bufferlength) < localhostnamelength)
  	{
  		*bufferlength = localhostnamelength; // tell the application the required size of the buffer
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_TRANS_BUFFERLENGTHTOOSMALL;
  	}
  
  	memcpy(buffer,localhostname,localhostnamelength);
  	*bufferlength = localhostnamelength;
  	
  	MAINMUTEX_UNLOCK
  	return 0;
  }
  
  bool RTPUDPv4Transmitter::ComesFromThisTransmitter(const RTPAddress *addr)
  {
  	if (!init)
  		return false;
  
  	if (addr == 0)
  		return false;
  	
  	MAINMUTEX_LOCK
  	
  	bool v;
  		
  	if (created && addr->GetAddressType() == RTPAddress::IPv4Address)
  	{	
  		const RTPIPv4Address *addr2 = (const RTPIPv4Address *)addr;
  		bool found = false;
  		std::list<uint32_t>::const_iterator it;
  	
  		it = localIPs.begin();
  		while (!found && it != localIPs.end())
  		{
  			if (addr2->GetIP() == *it)
  				found = true;
  			else
  				++it;
  		}
  	
  		if (!found)
  			v = false;
  		else
  		{
  			if (addr2->GetPort() == m_rtpPort || addr2->GetPort() == m_rtcpPort) // check for RTP port and RTCP port
  				v = true;
  			else 
  				v = false;
  		}
  	}
  	else
  		v = false;
  
  	MAINMUTEX_UNLOCK
  	return v;
  }
  
  int RTPUDPv4Transmitter::Poll()
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	int status;
  	
  	MAINMUTEX_LOCK
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	status = PollSocket(true); // poll RTP socket
  	if (rtpsock != rtcpsock) // no need to poll twice when multiplexing
  	{
  		if (status >= 0)
  			status = PollSocket(false); // poll RTCP socket
  	}
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  int RTPUDPv4Transmitter::WaitForIncomingData(const RTPTime &delay,bool *dataavailable)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (waitingfordata)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_ALREADYWAITING;
  	}
  	
  	SocketType abortSocket = m_pAbortDesc->GetAbortSocket();
  
  	SocketType socks[3] = { rtpsock, rtcpsock, abortSocket };
  	int8_t readflags[3] = { 0, 0, 0 };
  	const int idxRTP = 0;
  	const int idxRTCP = 1;
  	const int idxAbort = 2;
  	
  	waitingfordata = true;
  	
  	WAITMUTEX_LOCK
  	MAINMUTEX_UNLOCK
  
  	int status = RTPSelect(socks, readflags, 3, delay);
  	if (status < 0)
  	{
  		MAINMUTEX_LOCK
  		waitingfordata = false;
  		MAINMUTEX_UNLOCK
  		WAITMUTEX_UNLOCK
  		return status;
  	}
  	
  	MAINMUTEX_LOCK
  	waitingfordata = false;
  	if (!created) // destroy called
  	{
  		MAINMUTEX_UNLOCK;
  		WAITMUTEX_UNLOCK
  		return 0;
  	}
  		
  	// if aborted, read from abort buffer
  	if (readflags[idxAbort])
  		m_pAbortDesc->ReadSignallingByte();
  
  	if (dataavailable != 0)
  	{
  		if (readflags[idxRTP] || readflags[idxRTCP])
  			*dataavailable = true;
  		else
  			*dataavailable = false;
  	}	
  	
  	MAINMUTEX_UNLOCK
  	WAITMUTEX_UNLOCK
  	return 0;
  }
  
  int RTPUDPv4Transmitter::AbortWait()
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (!waitingfordata)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTWAITING;
  	}
  
  	m_pAbortDesc->SendAbortSignal();
  	
  	MAINMUTEX_UNLOCK
  	return 0;
  }
  
  int RTPUDPv4Transmitter::SendRTPData(const void *data,size_t len)	
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	MAINMUTEX_LOCK
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (len > maxpacksize)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_SPECIFIEDSIZETOOBIG;
  	}
  	
  	destinations.GotoFirstElement();
  	while (destinations.HasCurrentElement())
  	{
  		sendto(rtpsock,(const char *)data,len,0,(const struct sockaddr *)destinations.GetCurrentElement().GetRTPSockAddr(),sizeof(struct sockaddr_in));
  		destinations.GotoNextElement();
  	}
  	
  	MAINMUTEX_UNLOCK
  	return 0;
  }
  
  int RTPUDPv4Transmitter::SendRTCPData(const void *data,size_t len)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	MAINMUTEX_LOCK
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (len > maxpacksize)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_SPECIFIEDSIZETOOBIG;
  	}
  	
  	destinations.GotoFirstElement();
  	while (destinations.HasCurrentElement())
  	{
  		sendto(rtcpsock,(const char *)data,len,0,(const struct sockaddr *)destinations.GetCurrentElement().GetRTCPSockAddr(),sizeof(struct sockaddr_in));
  		destinations.GotoNextElement();
  	}
  	
  	MAINMUTEX_UNLOCK
  	return 0;
  }
  
  int RTPUDPv4Transmitter::AddDestination(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  
  	RTPIPv4Destination dest;
  	if (!RTPIPv4Destination::AddressToDestination(addr, dest))
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	
  	int status = destinations.AddElement(dest);
  
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  int RTPUDPv4Transmitter::DeleteDestination(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	RTPIPv4Destination dest;
  	if (!RTPIPv4Destination::AddressToDestination(addr, dest))
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	
  	int status = destinations.DeleteElement(dest);
  	
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  void RTPUDPv4Transmitter::ClearDestinations()
  {
  	if (!init)
  		return;
  	
  	MAINMUTEX_LOCK
  	if (created)
  		destinations.Clear();
  	MAINMUTEX_UNLOCK
  }
  
  bool RTPUDPv4Transmitter::SupportsMulticasting()
  {
  	if (!init)
  		return false;
  	
  	MAINMUTEX_LOCK
  	
  	bool v;
  		
  	if (!created)
  		v = false;
  	else
  		v = supportsmulticasting;
  
  	MAINMUTEX_UNLOCK
  	return v;
  }
  
  #ifdef RTP_SUPPORT_IPV4MULTICAST
  
  int RTPUDPv4Transmitter::JoinMulticastGroup(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	MAINMUTEX_LOCK
  	
  	int status;
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (addr.GetAddressType() != RTPAddress::IPv4Address)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	
  	const RTPIPv4Address &address = (const RTPIPv4Address &)addr;
  	uint32_t mcastIP = address.GetIP();
  	
  	if (!RTPUDPV4TRANS_IS_MCASTADDR(mcastIP))
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTAMULTICASTADDRESS;
  	}
  	
  	status = multicastgroups.AddElement(mcastIP);
  	if (status >= 0)
  	{
  		RTPUDPV4TRANS_MCASTMEMBERSHIP(rtpsock,IP_ADD_MEMBERSHIP,mcastIP,status);
  		if (status != 0)
  		{
  			multicastgroups.DeleteElement(mcastIP);
  			MAINMUTEX_UNLOCK
  			return ERR_RTP_UDPV4TRANS_COULDNTJOINMULTICASTGROUP;
  		}
  
  		if (rtpsock != rtcpsock) // no need to join multicast group twice when multiplexing
  		{
  			RTPUDPV4TRANS_MCASTMEMBERSHIP(rtcpsock,IP_ADD_MEMBERSHIP,mcastIP,status);
  			if (status != 0)
  			{
  				RTPUDPV4TRANS_MCASTMEMBERSHIP(rtpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
  				multicastgroups.DeleteElement(mcastIP);
  				MAINMUTEX_UNLOCK
  				return ERR_RTP_UDPV4TRANS_COULDNTJOINMULTICASTGROUP;
  			}
  		}
  	}
  	MAINMUTEX_UNLOCK	
  	return status;
  }
  
  int RTPUDPv4Transmitter::LeaveMulticastGroup(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	MAINMUTEX_LOCK
  	
  	int status;
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (addr.GetAddressType() != RTPAddress::IPv4Address)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	
  	const RTPIPv4Address &address = (const RTPIPv4Address &)addr;
  	uint32_t mcastIP = address.GetIP();
  	
  	if (!RTPUDPV4TRANS_IS_MCASTADDR(mcastIP))
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTAMULTICASTADDRESS;
  	}
  	
  	status = multicastgroups.DeleteElement(mcastIP);
  	if (status >= 0)
  	{	
  		RTPUDPV4TRANS_MCASTMEMBERSHIP(rtpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
  		if (rtpsock != rtcpsock) // no need to leave multicast group twice when multiplexing
  			RTPUDPV4TRANS_MCASTMEMBERSHIP(rtcpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
  
  		status = 0;
  	}
  	
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  void RTPUDPv4Transmitter::LeaveAllMulticastGroups()
  {
  	if (!init)
  		return;
  	
  	MAINMUTEX_LOCK
  	if (created)
  	{
  		multicastgroups.GotoFirstElement();
  		while (multicastgroups.HasCurrentElement())
  		{
  			uint32_t mcastIP;
  			int status = 0;
  
  			mcastIP = multicastgroups.GetCurrentElement();
  			
  			RTPUDPV4TRANS_MCASTMEMBERSHIP(rtpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
  			if (rtpsock != rtcpsock) // no need to leave multicast group twice when multiplexing
  				RTPUDPV4TRANS_MCASTMEMBERSHIP(rtcpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
  			JRTPLIB_UNUSED(status);
  
  			multicastgroups.GotoNextElement();
  		}
  		multicastgroups.Clear();
  	}
  	MAINMUTEX_UNLOCK
  }
  
  #else // no multicast support
  
  int RTPUDPv4Transmitter::JoinMulticastGroup(const RTPAddress &addr)
  {
  	return ERR_RTP_UDPV4TRANS_NOMULTICASTSUPPORT;
  }
  
  int RTPUDPv4Transmitter::LeaveMulticastGroup(const RTPAddress &addr)
  {
  	return ERR_RTP_UDPV4TRANS_NOMULTICASTSUPPORT;
  }
  
  void RTPUDPv4Transmitter::LeaveAllMulticastGroups()
  {
  }
  
  #endif // RTP_SUPPORT_IPV4MULTICAST
  
  int RTPUDPv4Transmitter::SetReceiveMode(RTPTransmitter::ReceiveMode m)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (m != receivemode)
  	{
  		receivemode = m;
  		acceptignoreinfo.Clear();
  	}
  	MAINMUTEX_UNLOCK
  	return 0;
  }
  
  int RTPUDPv4Transmitter::AddToIgnoreList(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  
  	MAINMUTEX_LOCK
  	
  	int status;
  
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (addr.GetAddressType() != RTPAddress::IPv4Address)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	if (receivemode != RTPTransmitter::IgnoreSome)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_DIFFERENTRECEIVEMODE;
  	}
  	
  	const RTPIPv4Address &address = (const RTPIPv4Address &)addr;
  	status = ProcessAddAcceptIgnoreEntry(address.GetIP(),address.GetPort());
  	
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  int RTPUDPv4Transmitter::DeleteFromIgnoreList(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	
  	int status;
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (addr.GetAddressType() != RTPAddress::IPv4Address)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	if (receivemode != RTPTransmitter::IgnoreSome)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_DIFFERENTRECEIVEMODE;
  	}
  	
  	const RTPIPv4Address &address = (const RTPIPv4Address &)addr;	
  	status = ProcessDeleteAcceptIgnoreEntry(address.GetIP(),address.GetPort());
  
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  void RTPUDPv4Transmitter::ClearIgnoreList()
  {
  	if (!init)
  		return;
  	
  	MAINMUTEX_LOCK
  	if (created && receivemode == RTPTransmitter::IgnoreSome)
  		ClearAcceptIgnoreInfo();
  	MAINMUTEX_UNLOCK
  }
  
  int RTPUDPv4Transmitter::AddToAcceptList(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	
  	int status;
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (addr.GetAddressType() != RTPAddress::IPv4Address)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	if (receivemode != RTPTransmitter::AcceptSome)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_DIFFERENTRECEIVEMODE;
  	}
  	
  	const RTPIPv4Address &address = (const RTPIPv4Address &)addr;
  	status = ProcessAddAcceptIgnoreEntry(address.GetIP(),address.GetPort());
  
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  int RTPUDPv4Transmitter::DeleteFromAcceptList(const RTPAddress &addr)
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	
  	int status;
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (addr.GetAddressType() != RTPAddress::IPv4Address)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_INVALIDADDRESSTYPE;
  	}
  	if (receivemode != RTPTransmitter::AcceptSome)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_DIFFERENTRECEIVEMODE;
  	}
  	
  	const RTPIPv4Address &address = (const RTPIPv4Address &)addr;
  	status = ProcessDeleteAcceptIgnoreEntry(address.GetIP(),address.GetPort());
  
  	MAINMUTEX_UNLOCK
  	return status;
  }
  
  void RTPUDPv4Transmitter::ClearAcceptList()
  {
  	if (!init)
  		return;
  	
  	MAINMUTEX_LOCK
  	if (created && receivemode == RTPTransmitter::AcceptSome)
  		ClearAcceptIgnoreInfo();
  	MAINMUTEX_UNLOCK
  }
  
  int RTPUDPv4Transmitter::SetMaximumPacketSize(size_t s)	
  {
  	if (!init)
  		return ERR_RTP_UDPV4TRANS_NOTINIT;
  	
  	MAINMUTEX_LOCK
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_NOTCREATED;
  	}
  	if (s > RTPUDPV4TRANS_MAXPACKSIZE)
  	{
  		MAINMUTEX_UNLOCK
  		return ERR_RTP_UDPV4TRANS_SPECIFIEDSIZETOOBIG;
  	}
  	maxpacksize = s;
  	MAINMUTEX_UNLOCK
  	return 0;
  }
  
  bool RTPUDPv4Transmitter::NewDataAvailable()
  {
  	if (!init)
  		return false;
  	
  	MAINMUTEX_LOCK
  	
  	bool v;
  		
  	if (!created)
  		v = false;
  	else
  	{
  		if (rawpacketlist.empty())
  			v = false;
  		else
  			v = true;
  	}
  	
  	MAINMUTEX_UNLOCK
  	return v;
  }
  
  RTPRawPacket *RTPUDPv4Transmitter::GetNextPacket()
  {
  	if (!init)
  		return 0;
  	
  	MAINMUTEX_LOCK
  	
  	RTPRawPacket *p;
  	
  	if (!created)
  	{
  		MAINMUTEX_UNLOCK
  		return 0;
  	}
  	if (rawpacketlist.empty())
  	{
  		MAINMUTEX_UNLOCK
  		return 0;
  	}
  
  	p = *(rawpacketlist.begin());
  	rawpacketlist.pop_front();
  
  	MAINMUTEX_UNLOCK
  	return p;
  }
  
  // Here the private functions start...
  
  #ifdef RTP_SUPPORT_IPV4MULTICAST
  bool RTPUDPv4Transmitter::SetMulticastTTL(uint8_t ttl)
  {
  	int ttl2,status;
  
  	ttl2 = (int)ttl;
  	status = setsockopt(rtpsock,IPPROTO_IP,IP_MULTICAST_TTL,(const char *)&ttl2,sizeof(int));
  	if (status != 0)
  		return false;
  
  	if (rtpsock != rtcpsock) // no need to set TTL twice when multiplexing
  	{
  		status = setsockopt(rtcpsock,IPPROTO_IP,IP_MULTICAST_TTL,(const char *)&ttl2,sizeof(int));
  		if (status != 0)
  			return false;
  	}
  	return true;
  }
  #endif // RTP_SUPPORT_IPV4MULTICAST
  
  void RTPUDPv4Transmitter::FlushPackets()
  {
  	std::list<RTPRawPacket*>::const_iterator it;
  
  	for (it = rawpacketlist.begin() ; it != rawpacketlist.end() ; ++it)
  		RTPDelete(*it,GetMemoryManager());
  	rawpacketlist.clear();
  }
  
  int RTPUDPv4Transmitter::PollSocket(bool rtp)
  {
  	RTPSOCKLENTYPE fromlen;
  	int recvlen;
  	char packetbuffer[RTPUDPV4TRANS_MAXPACKSIZE];
  #ifdef RTP_SOCKETTYPE_WINSOCK
  	SOCKET sock;
  	unsigned long len;
  #else 
  	size_t len;
  	int sock;
  #endif // RTP_SOCKETTYPE_WINSOCK
  	struct sockaddr_in srcaddr;
  	bool dataavailable;
  	
  	if (rtp)
  		sock = rtpsock;
  	else
  		sock = rtcpsock;
  	
  	do
  	{
  		len = 0;
  		RTPIOCTL(sock,FIONREAD,&len);
  
  		if (len <= 0) // make sure a packet of length zero is not queued
  		{
  			// An alternative workaround would be to just use non-blocking sockets.
  			// However, since the user does have access to the sockets and I do not
  			// know how this would affect anyone else's code, I chose to do it using
  			// an extra select call in case ioctl says the length is zero.
  			
  			int8_t isset = 0;
  			int status = RTPSelect(&sock, &isset, 1, RTPTime(0));
  			if (status < 0)
  				return status;
  
  			if (isset)
  				dataavailable = true;
  			else
  				dataavailable = false;
  		}
  		else
  			dataavailable = true;
  		
  		if (dataavailable)
  		{
  			RTPTime curtime = RTPTime::CurrentTime();
  			fromlen = sizeof(struct sockaddr_in);
  			recvlen = recvfrom(sock,packetbuffer,RTPUDPV4TRANS_MAXPACKSIZE,0,(struct sockaddr *)&srcaddr,&fromlen);
  			if (recvlen > 0)
  			{
  				bool acceptdata;
  
  				// got data, process it
  				if (receivemode == RTPTransmitter::AcceptAll)
  					acceptdata = true;
  				else
  					acceptdata = ShouldAcceptData(ntohl(srcaddr.sin_addr.s_addr),ntohs(srcaddr.sin_port));
  				
  				if (acceptdata)
  				{
  					RTPRawPacket *pack;
  					RTPIPv4Address *addr;
  					uint8_t *datacopy;
  
  					addr = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTPADDRESS) RTPIPv4Address(ntohl(srcaddr.sin_addr.s_addr),ntohs(srcaddr.sin_port));
  					if (addr == 0)
  						return ERR_RTP_OUTOFMEM;
  					datacopy = RTPNew(GetMemoryManager(),(rtp)?RTPMEM_TYPE_BUFFER_RECEIVEDRTPPACKET:RTPMEM_TYPE_BUFFER_RECEIVEDRTCPPACKET) uint8_t[recvlen];
  					if (datacopy == 0)
  					{
  						RTPDelete(addr,GetMemoryManager());
  						return ERR_RTP_OUTOFMEM;
  					}
  					memcpy(datacopy,packetbuffer,recvlen);
  					
  					bool isrtp = rtp;
  					if (rtpsock == rtcpsock) // check payload type when multiplexing
  					{
  						isrtp = true;
  
  						if ((size_t)recvlen > sizeof(RTCPCommonHeader))
  						{
  							RTCPCommonHeader *rtcpheader = (RTCPCommonHeader *)datacopy;
  							uint8_t packettype = rtcpheader->packettype;
  
      						if (packettype >= 200 && packettype <= 204)
  								isrtp = false;
  						}
  					}
  						
  					pack = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTPRAWPACKET) RTPRawPacket(datacopy,recvlen,addr,curtime,isrtp,GetMemoryManager());
  					if (pack == 0)
  					{
  						RTPDelete(addr,GetMemoryManager());
  						RTPDeleteByteArray(datacopy,GetMemoryManager());
  						return ERR_RTP_OUTOFMEM;
  					}
  					rawpacketlist.push_back(pack);	
  				}
  			}
  		}
  	} while (dataavailable);
  
  	return 0;
  }
  
  int RTPUDPv4Transmitter::ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port)
  {
  	acceptignoreinfo.GotoElement(ip);
  	if (acceptignoreinfo.HasCurrentElement()) // An entry for this IP address already exists
  	{
  		PortInfo *portinf = acceptignoreinfo.GetCurrentElement();
  		
  		if (port == 0) // select all ports
  		{
  			portinf->all = true;
  			portinf->portlist.clear();
  		}
  		else if (!portinf->all)
  		{
  			std::list<uint16_t>::const_iterator it,begin,end;
  
  			begin = portinf->portlist.begin();
  			end = portinf->portlist.end();
  			for (it = begin ; it != end ; it++)
  			{
  				if (*it == port) // already in list
  					return 0;
  			}
  			portinf->portlist.push_front(port);
  		}
  	}
  	else // got to create an entry for this IP address
  	{
  		PortInfo *portinf;
  		int status;
  		
  		portinf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_ACCEPTIGNOREPORTINFO) PortInfo();
  		if (port == 0) // select all ports
  			portinf->all = true;
  		else
  			portinf->portlist.push_front(port);
  		
  		status = acceptignoreinfo.AddElement(ip,portinf);
  		if (status < 0)
  		{
  			RTPDelete(portinf,GetMemoryManager());
  			return status;
  		}
  	}
  
  	return 0;
  }
  
  void RTPUDPv4Transmitter::ClearAcceptIgnoreInfo()
  {
  	acceptignoreinfo.GotoFirstElement();
  	while (acceptignoreinfo.HasCurrentElement())
  	{
  		PortInfo *inf;
  
  		inf = acceptignoreinfo.GetCurrentElement();
  		RTPDelete(inf,GetMemoryManager());
  		acceptignoreinfo.GotoNextElement();
  	}
  	acceptignoreinfo.Clear();
  }
  	
  int RTPUDPv4Transmitter::ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port)
  {
  	acceptignoreinfo.GotoElement(ip);
  	if (!acceptignoreinfo.HasCurrentElement())
  		return ERR_RTP_UDPV4TRANS_NOSUCHENTRY;
  	
  	PortInfo *inf;
  
  	inf = acceptignoreinfo.GetCurrentElement();
  	if (port == 0) // delete all entries
  	{
  		inf->all = false;
  		inf->portlist.clear();
  	}
  	else // a specific port was selected
  	{
  		if (inf->all) // currently, all ports are selected. Add the one to remove to the list
  		{
  			// we have to check if the list doesn't contain the port already
  			std::list<uint16_t>::const_iterator it,begin,end;
  
  			begin = inf->portlist.begin();
  			end = inf->portlist.end();
  			for (it = begin ; it != end ; it++)
  			{
  				if (*it == port) // already in list: this means we already deleted the entry
  					return ERR_RTP_UDPV4TRANS_NOSUCHENTRY;
  			}
  			inf->portlist.push_front(port);
  		}
  		else // check if we can find the port in the list
  		{
  			std::list<uint16_t>::iterator it,begin,end;
  			
  			begin = inf->portlist.begin();
  			end = inf->portlist.end();
  			for (it = begin ; it != end ; ++it)
  			{
  				if (*it == port) // found it!
  				{
  					inf->portlist.erase(it);
  					return 0;
  				}
  			}
  			// didn't find it
  			return ERR_RTP_UDPV4TRANS_NOSUCHENTRY;			
  		}
  	}
  	return 0;
  }
  
  bool RTPUDPv4Transmitter::ShouldAcceptData(uint32_t srcip,uint16_t srcport)
  {
  	if (receivemode == RTPTransmitter::AcceptSome)
  	{
  		PortInfo *inf;
  
  		acceptignoreinfo.GotoElement(srcip);
  		if (!acceptignoreinfo.HasCurrentElement())
  			return false;
  		
  		inf = acceptignoreinfo.GetCurrentElement();
  		if (!inf->all) // only accept the ones in the list
  		{
  			std::list<uint16_t>::const_iterator it,begin,end;
  
  			begin = inf->portlist.begin();
  			end = inf->portlist.end();
  			for (it = begin ; it != end ; it++)
  			{
  				if (*it == srcport)
  					return true;
  			}
  			return false;
  		}
  		else // accept all, except the ones in the list
  		{
  			std::list<uint16_t>::const_iterator it,begin,end;
  
  			begin = inf->portlist.begin();
  			end = inf->portlist.end();
  			for (it = begin ; it != end ; it++)
  			{
  				if (*it == srcport)
  					return false;
  			}
  			return true;
  		}
  	}
  	else // IgnoreSome
  	{
  		PortInfo *inf;
  
  		acceptignoreinfo.GotoElement(srcip);
  		if (!acceptignoreinfo.HasCurrentElement())
  			return true;
  		
  		inf = acceptignoreinfo.GetCurrentElement();
  		if (!inf->all) // ignore the ports in the list
  		{
  			std::list<uint16_t>::const_iterator it,begin,end;
  
  			begin = inf->portlist.begin();
  			end = inf->portlist.end();
  			for (it = begin ; it != end ; it++)
  			{
  				if (*it == srcport)
  					return false;
  			}
  			return true;
  		}
  		else // ignore all, except the ones in the list
  		{
  			std::list<uint16_t>::const_iterator it,begin,end;
  
  			begin = inf->portlist.begin();
  			end = inf->portlist.end();
  			for (it = begin ; it != end ; it++)
  			{
  				if (*it == srcport)
  					return true;
  			}
  			return false;
  		}
  	}
  	return true;
  }
  
  int RTPUDPv4Transmitter::CreateLocalIPList()
  {
  	 // first try to obtain the list from the network interface info
  
  	if (!GetLocalIPList_Interfaces())
  	{
  		// If this fails, we'll have to depend on DNS info
  		GetLocalIPList_DNS();
  	}
  	AddLoopbackAddress();
  	return 0;
  }
  
  #ifdef RTP_SOCKETTYPE_WINSOCK
  
  bool RTPUDPv4Transmitter::GetLocalIPList_Interfaces()
  {
  	unsigned char buffer[RTPUDPV4TRANS_IFREQBUFSIZE];
  	DWORD outputsize;
  	DWORD numaddresses,i;
  	SOCKET_ADDRESS_LIST *addrlist;
  
  	if (WSAIoctl(rtpsock,SIO_ADDRESS_LIST_QUERY,NULL,0,&buffer,RTPUDPV4TRANS_IFREQBUFSIZE,&outputsize,NULL,NULL))
  		return false;
  	
  	addrlist = (SOCKET_ADDRESS_LIST *)buffer;
  	numaddresses = addrlist->iAddressCount;
  	for (i = 0 ; i < numaddresses ; i++)
  	{
  		SOCKET_ADDRESS *sockaddr = &(addrlist->Address[i]);
  		if (sockaddr->iSockaddrLength == sizeof(struct sockaddr_in)) // IPv4 address
  		{
  			struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr->lpSockaddr;
  
  			localIPs.push_back(ntohl(addr->sin_addr.s_addr));
  		}
  	}
  
  	if (localIPs.empty())
  		return false;
  
  	return true;
  }
  
  #else // use either getifaddrs or ioctl
  
  #ifdef RTP_SUPPORT_IFADDRS
  
  bool RTPUDPv4Transmitter::GetLocalIPList_Interfaces()
  {
  	struct ifaddrs *addrs,*tmp;
  	
  	getifaddrs(&addrs);
  	tmp = addrs;
  	
  	while (tmp != 0)
  	{
  		if (tmp->ifa_addr != 0 && tmp->ifa_addr->sa_family == AF_INET)
  		{
  			struct sockaddr_in *inaddr = (struct sockaddr_in *)tmp->ifa_addr;
  			localIPs.push_back(ntohl(inaddr->sin_addr.s_addr));
  		}
  		tmp = tmp->ifa_next;
  	}
  	
  	freeifaddrs(addrs);
  	
  	if (localIPs.empty())
  		return false;
  	return true;
  }
  
  #else // user ioctl
  
  bool RTPUDPv4Transmitter::GetLocalIPList_Interfaces()
  {
  	int status;
  	char buffer[RTPUDPV4TRANS_IFREQBUFSIZE];
  	struct ifconf ifc;
  	struct ifreq *ifr;
  	struct sockaddr *sa;
  	char *startptr,*endptr;
  	int remlen;
  	
  	ifc.ifc_len = RTPUDPV4TRANS_IFREQBUFSIZE;
  	ifc.ifc_buf = buffer;
  	status = ioctl(rtpsock,SIOCGIFCONF,&ifc);
  	if (status < 0)
  		return false;
  	
  	startptr = (char *)ifc.ifc_req;
  	endptr = startptr + ifc.ifc_len;
  	remlen = ifc.ifc_len;
  	while((startptr < endptr) && remlen >= (int)sizeof(struct ifreq))
  	{
  		ifr = (struct ifreq *)startptr;
  		sa = &(ifr->ifr_addr);
  #ifdef RTP_HAVE_SOCKADDR_LEN
  		if (sa->sa_len <= sizeof(struct sockaddr))
  		{
  			if (sa->sa_len == sizeof(struct sockaddr_in) && sa->sa_family == PF_INET)
  			{
  				uint32_t ip;
  				struct sockaddr_in *addr = (struct sockaddr_in *)sa;
  				
  				ip = ntohl(addr->sin_addr.s_addr);
  				localIPs.push_back(ip);
  			}
  			remlen -= sizeof(struct ifreq);
  			startptr += sizeof(struct ifreq);
  		}
  		else
  		{
  			int l = sa->sa_len-sizeof(struct sockaddr)+sizeof(struct ifreq);
  			
  			remlen -= l;
  			startptr += l;
  		}
  #else // don't have sa_len in struct sockaddr
  		if (sa->sa_family == PF_INET)
  		{
  			uint32_t ip;
  			struct sockaddr_in *addr = (struct sockaddr_in *)sa;
  		
  			ip = ntohl(addr->sin_addr.s_addr);
  			localIPs.push_back(ip);
  		}
  		remlen -= sizeof(struct ifreq);
  		startptr += sizeof(struct ifreq);
  	
  #endif // RTP_HAVE_SOCKADDR_LEN
  	}
  
  	if (localIPs.empty())
  		return false;
  	return true;
  }
  
  #endif // RTP_SUPPORT_IFADDRS
  
  #endif // RTP_SOCKETTYPE_WINSOCK
  
  void RTPUDPv4Transmitter::GetLocalIPList_DNS()
  {
  	struct hostent *he;
  	char name[1024];
  	bool done;
  	int i,j;
  
  	gethostname(name,1023);
  	name[1023] = 0;
  	he = gethostbyname(name);
  	if (he == 0)
  		return;
  	
  	i = 0;
  	done = false;
  	while (!done)
  	{
  		if (he->h_addr_list[i] == NULL)
  			done = true;
  		else
  		{
  			uint32_t ip = 0;
  
  			for (j = 0 ; j < 4 ; j++)
  				ip |= ((uint32_t)((unsigned char)he->h_addr_list[i][j])<<((3-j)*8));
  			localIPs.push_back(ip);
  			i++;
  		}
  	}
  }
  
  void RTPUDPv4Transmitter::AddLoopbackAddress()
  {
  	uint32_t loopbackaddr = (((uint32_t)127)<<24)|((uint32_t)1);
  	std::list<uint32_t>::const_iterator it;
  	bool found = false;
  	
  	for (it = localIPs.begin() ; !found && it != localIPs.end() ; it++)
  	{
  		if (*it == loopbackaddr)
  			found = true;
  	}
  
  	if (!found)
  		localIPs.push_back(loopbackaddr);
  }
  
  #ifdef RTPDEBUG
  void RTPUDPv4Transmitter::Dump()
  {
  	if (!init)
  		std::cout << "Not initialized" << std::endl;
  	else
  	{
  		MAINMUTEX_LOCK
  	
  		if (!created)
  			std::cout << "Not created" << std::endl;
  		else
  		{
  			char str[16];
  			uint32_t ip;
  			std::list<uint32_t>::const_iterator it;
  			
  			std::cout << "RTP Port:                       " << m_rtpPort << std::endl;
  			std::cout << "RTCP Port:                      " << m_rtcpPort << std::endl;
  			std::cout << "RTP socket descriptor:          " << rtpsock << std::endl;
  			std::cout << "RTCP socket descriptor:         " << rtcpsock << std::endl;
  			ip = mcastifaceIP;
  			RTP_SNPRINTF(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF));
  			std::cout << "Multicast interface IP address: " << str << std::endl;
  			std::cout << "Local IP addresses:" << std::endl;
  			for (it = localIPs.begin() ; it != localIPs.end() ; it++)
  			{
  				ip = (*it);
  				RTP_SNPRINTF(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF));
  				std::cout << "    " << str << std::endl;
  			}
  			std::cout << "Multicast TTL:                  " << (int)multicastTTL << std::endl;
  			std::cout << "Receive mode:                   ";
  			switch (receivemode)
  			{
  			case RTPTransmitter::AcceptAll:
  				std::cout << "Accept all";
  				break;
  			case RTPTransmitter::AcceptSome:
  				std::cout << "Accept some";
  				break;
  			case RTPTransmitter::IgnoreSome:
  				std::cout << "Ignore some";
  			}
  			std::cout << std::endl;
  			if (receivemode != RTPTransmitter::AcceptAll)
  			{
  				acceptignoreinfo.GotoFirstElement();
  				while(acceptignoreinfo.HasCurrentElement())
  				{
  					ip = acceptignoreinfo.GetCurrentKey();
  					RTP_SNPRINTF(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF));
  					PortInfo *pinfo = acceptignoreinfo.GetCurrentElement();
  					std::cout << "    " << str << ": ";
  					if (pinfo->all)
  					{
  						std::cout << "All ports";
  						if (!pinfo->portlist.empty())
  							std::cout << ", except ";
  					}
  					
  					std::list<uint16_t>::const_iterator it;
  					
  					for (it = pinfo->portlist.begin() ; it != pinfo->portlist.end() ; )
  					{
  						std::cout << (*it);
  						it++;
  						if (it != pinfo->portlist.end())
  							std::cout << ", ";
  					}
  					std::cout << std::endl;
  				}
  			}
  			
  			std::cout << "Local host name:                ";
  			if (localhostname == 0)
  				std::cout << "Not set";
  			else
  				std::cout << localhostname;
  			std::cout << std::endl;
  
  			std::cout << "List of destinations:           ";
  			destinations.GotoFirstElement();
  			if (destinations.HasCurrentElement())
  			{
  				std::cout << std::endl;
  				do
  				{
  					std::cout << "    " << destinations.GetCurrentElement().GetDestinationString() << std::endl;
  					destinations.GotoNextElement();
  				} while (destinations.HasCurrentElement());
  			}
  			else
  				std::cout << "Empty" << std::endl;
  		
  			std::cout << "Supports multicasting:          " << ((supportsmulticasting)?"Yes":"No") << std::endl;
  #ifdef RTP_SUPPORT_IPV4MULTICAST
  			std::cout << "List of multicast groups:       ";
  			multicastgroups.GotoFirstElement();
  			if (multicastgroups.HasCurrentElement())
  			{
  				std::cout << std::endl;
  				do
  				{
  					ip = multicastgroups.GetCurrentElement();
  					RTP_SNPRINTF(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF));
  					std::cout << "    " << str << std::endl;
  					multicastgroups.GotoNextElement();
  				} while (multicastgroups.HasCurrentElement());
  			}
  			else
  				std::cout << "Empty" << std::endl;
  #endif // RTP_SUPPORT_IPV4MULTICAST
  			
  			std::cout << "Number of raw packets in queue: " << rawpacketlist.size() << std::endl;
  			std::cout << "Maximum allowed packet size:    " << maxpacksize << std::endl;
  		}
  		
  		MAINMUTEX_UNLOCK
  	}
  }
  #endif // RTPDEBUG
  
  } // end namespace