stun_msg.h 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJNATH_STUN_MSG_H__
  20. #define __PJNATH_STUN_MSG_H__
  21. /**
  22. * @file stun_msg.h
  23. * @brief STUN message components.
  24. */
  25. #include <pjnath/types.h>
  26. #include <pj/sock.h>
  27. PJ_BEGIN_DECL
  28. /* **************************************************************************/
  29. /**
  30. * @defgroup PJNATH_STUN_MSG STUN Message Representation and Parsing
  31. * @ingroup PJNATH_STUN_BASE
  32. * @brief Low-level representation and parsing of STUN messages.
  33. * @{
  34. */
  35. /**
  36. * STUN magic cookie.
  37. */
  38. #define PJ_STUN_MAGIC 0x2112A442
  39. /**
  40. * STUN method constants.
  41. */
  42. enum pj_stun_method_e
  43. {
  44. /**
  45. * STUN Binding method as defined by RFC 3489-bis.
  46. */
  47. PJ_STUN_BINDING_METHOD = 1,
  48. /**
  49. * STUN Shared Secret method as defined by RFC 3489-bis.
  50. */
  51. PJ_STUN_SHARED_SECRET_METHOD = 2,
  52. /**
  53. * STUN/TURN Allocate method as defined by draft-ietf-behave-turn
  54. */
  55. PJ_STUN_ALLOCATE_METHOD = 3,
  56. /**
  57. * STUN/TURN Refresh method as defined by draft-ietf-behave-turn
  58. */
  59. PJ_STUN_REFRESH_METHOD = 4,
  60. /**
  61. * STUN/TURN Send indication as defined by draft-ietf-behave-turn
  62. */
  63. PJ_STUN_SEND_METHOD = 6,
  64. /**
  65. * STUN/TURN Data indication as defined by draft-ietf-behave-turn
  66. */
  67. PJ_STUN_DATA_METHOD = 7,
  68. /**
  69. * STUN/TURN CreatePermission method as defined by draft-ietf-behave-turn
  70. */
  71. PJ_STUN_CREATE_PERM_METHOD = 8,
  72. /**
  73. * STUN/TURN ChannelBind as defined by draft-ietf-behave-turn
  74. */
  75. PJ_STUN_CHANNEL_BIND_METHOD = 9,
  76. /**
  77. * STUN/TURN Connect as defined by RFC 6062
  78. */
  79. PJ_STUN_CONNECT_METHOD = 10,
  80. /**
  81. * STUN/TURN ConnectionBind as defined by RFC 6062
  82. */
  83. PJ_STUN_CONNECTION_BIND_METHOD = 11,
  84. /**
  85. * STUN/TURN ConnectionAttempt as defined by RFC 6062
  86. */
  87. PJ_STUN_CONNECTION_ATTEMPT_METHOD = 12,
  88. /**
  89. * All known methods.
  90. */
  91. PJ_STUN_METHOD_MAX
  92. };
  93. /**
  94. * Retrieve the STUN method from the message-type field of the STUN
  95. * message.
  96. */
  97. #define PJ_STUN_GET_METHOD(msg_type) ((msg_type) & 0xFEEF)
  98. /**
  99. * STUN message classes constants.
  100. */
  101. enum pj_stun_msg_class_e
  102. {
  103. /**
  104. * This specifies that the message type is a STUN request message.
  105. */
  106. PJ_STUN_REQUEST_CLASS = 0,
  107. /**
  108. * This specifies that the message type is a STUN indication message.
  109. */
  110. PJ_STUN_INDICATION_CLASS = 1,
  111. /**
  112. * This specifies that the message type is a STUN successful response.
  113. */
  114. PJ_STUN_SUCCESS_CLASS = 2,
  115. /**
  116. * This specifies that the message type is a STUN error response.
  117. */
  118. PJ_STUN_ERROR_CLASS = 3
  119. };
  120. /**
  121. * Determine if the message type is a request.
  122. */
  123. #define PJ_STUN_IS_REQUEST(msg_type) (((msg_type) & 0x0110) == 0x0000)
  124. /**
  125. * Determine if the message type is a successful response.
  126. */
  127. #define PJ_STUN_IS_SUCCESS_RESPONSE(msg_type) (((msg_type) & 0x0110) == 0x0100)
  128. /**
  129. * The response bit in the message type.
  130. */
  131. #define PJ_STUN_SUCCESS_RESPONSE_BIT (0x0100)
  132. /**
  133. * Determine if the message type is an error response.
  134. */
  135. #define PJ_STUN_IS_ERROR_RESPONSE(msg_type) (((msg_type) & 0x0110) == 0x0110)
  136. /**
  137. * The error response bit in the message type.
  138. */
  139. #define PJ_STUN_ERROR_RESPONSE_BIT (0x0110)
  140. /**
  141. * Determine if the message type is a response.
  142. */
  143. #define PJ_STUN_IS_RESPONSE(msg_type) (((msg_type) & 0x0100) == 0x0100)
  144. /**
  145. * Determine if the message type is an indication message.
  146. */
  147. #define PJ_STUN_IS_INDICATION(msg_type) (((msg_type) & 0x0110) == 0x0010)
  148. /**
  149. * The error response bit in the message type.
  150. */
  151. #define PJ_STUN_INDICATION_BIT (0x0010)
  152. /**
  153. * This enumeration describes STUN message types.
  154. */
  155. typedef enum pj_stun_msg_type
  156. {
  157. /**
  158. * STUN BINDING request.
  159. */
  160. PJ_STUN_BINDING_REQUEST = 0x0001,
  161. /**
  162. * Successful response to STUN BINDING-REQUEST.
  163. */
  164. PJ_STUN_BINDING_RESPONSE = 0x0101,
  165. /**
  166. * Error response to STUN BINDING-REQUEST.
  167. */
  168. PJ_STUN_BINDING_ERROR_RESPONSE = 0x0111,
  169. /**
  170. * Binding Indication (ICE)
  171. */
  172. PJ_STUN_BINDING_INDICATION = 0x0011,
  173. /**
  174. * STUN SHARED-SECRET reqeust.
  175. */
  176. PJ_STUN_SHARED_SECRET_REQUEST = 0x0002,
  177. /**
  178. * Successful response to STUN SHARED-SECRET reqeust.
  179. */
  180. PJ_STUN_SHARED_SECRET_RESPONSE = 0x0102,
  181. /**
  182. * Error response to STUN SHARED-SECRET reqeust.
  183. */
  184. PJ_STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112,
  185. /**
  186. * STUN/TURN Allocate Request
  187. */
  188. PJ_STUN_ALLOCATE_REQUEST = 0x0003,
  189. /**
  190. * Successful response to STUN/TURN Allocate Request
  191. */
  192. PJ_STUN_ALLOCATE_RESPONSE = 0x0103,
  193. /**
  194. * Failure response to STUN/TURN Allocate Request
  195. */
  196. PJ_STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
  197. /**
  198. * STUN/TURN REFRESH Request
  199. */
  200. PJ_STUN_REFRESH_REQUEST = 0x0004,
  201. /**
  202. * Successful response to STUN REFRESH request
  203. */
  204. PJ_STUN_REFRESH_RESPONSE = 0x0104,
  205. /**
  206. * Error response to STUN REFRESH request.
  207. */
  208. PJ_STUN_REFRESH_ERROR_RESPONSE = 0x0114,
  209. /**
  210. * TURN Send indication
  211. */
  212. PJ_STUN_SEND_INDICATION = 0x0016,
  213. /**
  214. * TURN Data indication
  215. */
  216. PJ_STUN_DATA_INDICATION = 0x0017,
  217. /**
  218. * TURN CreatePermission request
  219. */
  220. PJ_STUN_CREATE_PERM_REQUEST = 0x0008,
  221. /**
  222. * TURN CreatePermission successful response.
  223. */
  224. PJ_STUN_CREATE_PERM_RESPONSE = 0x0108,
  225. /**
  226. * TURN CreatePermission failure response
  227. */
  228. PJ_STUN_CREATE_PERM_ERROR_RESPONSE = 0x0118,
  229. /**
  230. * STUN/TURN ChannelBind Request
  231. */
  232. PJ_STUN_CHANNEL_BIND_REQUEST = 0x0009,
  233. /**
  234. * Successful response to STUN ChannelBind request
  235. */
  236. PJ_STUN_CHANNEL_BIND_RESPONSE = 0x0109,
  237. /**
  238. * Error response to STUN ChannelBind request.
  239. */
  240. PJ_STUN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
  241. /**
  242. * STUN/TURN Connect Request
  243. */
  244. PJ_STUN_CONNECT_REQUEST = 0x000a,
  245. /**
  246. * STUN/TURN ConnectBind Request
  247. */
  248. PJ_STUN_CONNECTION_BIND_REQUEST = 0x000b,
  249. /**
  250. * TURN ConnectionAttempt indication
  251. */
  252. PJ_STUN_CONNECTION_ATTEMPT_INDICATION = 0x001c,
  253. } pj_stun_msg_type;
  254. /**
  255. * This enumeration describes STUN attribute types.
  256. */
  257. typedef enum pj_stun_attr_type
  258. {
  259. PJ_STUN_ATTR_MAPPED_ADDR = 0x0001,/**< MAPPED-ADDRESS. */
  260. PJ_STUN_ATTR_RESPONSE_ADDR = 0x0002,/**< RESPONSE-ADDRESS (deprcatd)*/
  261. PJ_STUN_ATTR_CHANGE_REQUEST = 0x0003,/**< CHANGE-REQUEST (deprecated)*/
  262. PJ_STUN_ATTR_SOURCE_ADDR = 0x0004,/**< SOURCE-ADDRESS (deprecated)*/
  263. PJ_STUN_ATTR_CHANGED_ADDR = 0x0005,/**< CHANGED-ADDRESS (deprecatd)*/
  264. PJ_STUN_ATTR_USERNAME = 0x0006,/**< USERNAME attribute. */
  265. PJ_STUN_ATTR_PASSWORD = 0x0007,/**< was PASSWORD attribute. */
  266. PJ_STUN_ATTR_MESSAGE_INTEGRITY = 0x0008,/**< MESSAGE-INTEGRITY. */
  267. PJ_STUN_ATTR_ERROR_CODE = 0x0009,/**< ERROR-CODE. */
  268. PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000A,/**< UNKNOWN-ATTRIBUTES. */
  269. PJ_STUN_ATTR_REFLECTED_FROM = 0x000B,/**< REFLECTED-FROM (deprecatd)*/
  270. PJ_STUN_ATTR_CHANNEL_NUMBER = 0x000C,/**< TURN CHANNEL-NUMBER */
  271. PJ_STUN_ATTR_LIFETIME = 0x000D,/**< TURN LIFETIME attr. */
  272. PJ_STUN_ATTR_MAGIC_COOKIE = 0x000F,/**< MAGIC-COOKIE attr (deprec)*/
  273. PJ_STUN_ATTR_BANDWIDTH = 0x0010,/**< TURN BANDWIDTH (deprec) */
  274. PJ_STUN_ATTR_XOR_PEER_ADDR = 0x0012,/**< TURN XOR-PEER-ADDRESS */
  275. PJ_STUN_ATTR_DATA = 0x0013,/**< DATA attribute. */
  276. PJ_STUN_ATTR_REALM = 0x0014,/**< REALM attribute. */
  277. PJ_STUN_ATTR_NONCE = 0x0015,/**< NONCE attribute. */
  278. PJ_STUN_ATTR_XOR_RELAYED_ADDR = 0x0016,/**< TURN XOR-RELAYED-ADDRESS */
  279. PJ_STUN_ATTR_REQ_ADDR_TYPE = 0x0017,/**< REQUESTED-ADDRESS-TYPE */
  280. PJ_STUN_ATTR_REQ_ADDR_FAMILY = 0x0017,/**< REQUESTED-ADDRESS-FAMILY */
  281. PJ_STUN_ATTR_EVEN_PORT = 0x0018,/**< TURN EVEN-PORT */
  282. PJ_STUN_ATTR_REQ_TRANSPORT = 0x0019,/**< TURN REQUESTED-TRANSPORT */
  283. PJ_STUN_ATTR_DONT_FRAGMENT = 0x001A,/**< TURN DONT-FRAGMENT */
  284. PJ_STUN_ATTR_XOR_MAPPED_ADDR = 0x0020,/**< XOR-MAPPED-ADDRESS */
  285. PJ_STUN_ATTR_TIMER_VAL = 0x0021,/**< TIMER-VAL attribute. */
  286. PJ_STUN_ATTR_RESERVATION_TOKEN = 0x0022,/**< TURN RESERVATION-TOKEN */
  287. PJ_STUN_ATTR_XOR_REFLECTED_FROM = 0x0023,/**< XOR-REFLECTED-FROM */
  288. PJ_STUN_ATTR_PRIORITY = 0x0024,/**< PRIORITY */
  289. PJ_STUN_ATTR_USE_CANDIDATE = 0x0025,/**< USE-CANDIDATE */
  290. PJ_STUN_ATTR_CONNECTION_ID = 0x002a,/**< CONNECTION-ID */
  291. PJ_STUN_ATTR_ICMP = 0x0030,/**< ICMP (TURN) */
  292. PJ_STUN_ATTR_END_MANDATORY_ATTR,
  293. PJ_STUN_ATTR_START_EXTENDED_ATTR= 0x8021,
  294. PJ_STUN_ATTR_SOFTWARE = 0x8022,/**< SOFTWARE attribute. */
  295. PJ_STUN_ATTR_ALTERNATE_SERVER = 0x8023,/**< ALTERNATE-SERVER. */
  296. PJ_STUN_ATTR_REFRESH_INTERVAL = 0x8024,/**< REFRESH-INTERVAL. */
  297. PJ_STUN_ATTR_FINGERPRINT = 0x8028,/**< FINGERPRINT attribute. */
  298. PJ_STUN_ATTR_ICE_CONTROLLED = 0x8029,/**< ICE-CCONTROLLED attribute.*/
  299. PJ_STUN_ATTR_ICE_CONTROLLING = 0x802a,/**< ICE-CCONTROLLING attribute*/
  300. PJ_STUN_ATTR_END_EXTENDED_ATTR
  301. } pj_stun_attr_type;
  302. /**
  303. * STUN error codes, which goes into STUN ERROR-CODE attribute.
  304. */
  305. typedef enum pj_stun_status
  306. {
  307. PJ_STUN_SC_TRY_ALTERNATE = 300, /**< Try Alternate */
  308. PJ_STUN_SC_BAD_REQUEST = 400, /**< Bad Request */
  309. PJ_STUN_SC_UNAUTHORIZED = 401, /**< Unauthorized */
  310. PJ_STUN_SC_FORBIDDEN = 403, /**< Forbidden (TURN) */
  311. PJ_STUN_SC_UNKNOWN_ATTRIBUTE = 420, /**< Unknown Attribute */
  312. #if 0
  313. /* These were obsolete in recent rfc3489bis */
  314. //PJ_STUN_SC_STALE_CREDENTIALS = 430, /**< Stale Credentials */
  315. //PJ_STUN_SC_INTEGRITY_CHECK_FAILURE= 431, /**< Integrity Chk Fail */
  316. //PJ_STUN_SC_MISSING_USERNAME = 432, /**< Missing Username */
  317. //PJ_STUN_SC_USE_TLS = 433, /**< Use TLS */
  318. //PJ_STUN_SC_MISSING_REALM = 434, /**< Missing Realm */
  319. //PJ_STUN_SC_MISSING_NONCE = 435, /**< Missing Nonce */
  320. //PJ_STUN_SC_UNKNOWN_USERNAME = 436, /**< Unknown Username */
  321. #endif
  322. PJ_STUN_SC_ALLOCATION_MISMATCH = 437, /**< TURN Alloc Mismatch */
  323. PJ_STUN_SC_STALE_NONCE = 438, /**< Stale Nonce */
  324. PJ_STUN_SC_TRANSITIONING = 439, /**< Transitioning. */
  325. PJ_STUN_SC_WRONG_CREDENTIALS = 441, /**< TURN Wrong Credentials */
  326. PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO = 442, /**< Unsupported Transport or
  327. Protocol (TURN) */
  328. PJ_STUN_SC_OPER_TCP_ONLY = 445, /**< Operation for TCP Only */
  329. PJ_STUN_SC_CONNECTION_FAILURE = 446, /**< Connection Failure */
  330. PJ_STUN_SC_CONNECTION_TIMEOUT = 447, /**< Connection Timeout */
  331. PJ_STUN_SC_ALLOCATION_QUOTA_REACHED = 486, /**< Allocation Quota Reached
  332. (TURN) */
  333. PJ_STUN_SC_ROLE_CONFLICT = 487, /**< Role Conflict */
  334. PJ_STUN_SC_SERVER_ERROR = 500, /**< Server Error */
  335. PJ_STUN_SC_INSUFFICIENT_CAPACITY = 508, /**< Insufficient Capacity
  336. (TURN) */
  337. PJ_STUN_SC_GLOBAL_FAILURE = 600 /**< Global Failure */
  338. } pj_stun_status;
  339. /**
  340. * This structure describes STUN message header. A STUN message has the
  341. * following format:
  342. *
  343. * \verbatim
  344. 0 1 2 3
  345. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  346. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  347. |0 0| STUN Message Type | Message Length |
  348. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  349. | Magic Cookie |
  350. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  351. |
  352. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  353. Transaction ID
  354. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  355. |
  356. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  357. \endverbatim
  358. */
  359. #pragma pack(1)
  360. typedef struct pj_stun_msg_hdr
  361. {
  362. /**
  363. * STUN message type, which the first two bits must be zeroes.
  364. */
  365. pj_uint16_t type;
  366. /**
  367. * The message length is the size, in bytes, of the message not
  368. * including the 20 byte STUN header.
  369. */
  370. pj_uint16_t length;
  371. /**
  372. * The magic cookie is a fixed value, 0x2112A442 (PJ_STUN_MAGIC constant).
  373. * In the previous version of this specification [15] this field was part
  374. * of the transaction ID.
  375. */
  376. pj_uint32_t magic;
  377. /**
  378. * The transaction ID is a 96 bit identifier. STUN transactions are
  379. * identified by their unique 96-bit transaction ID. For request/
  380. * response transactions, the transaction ID is chosen by the STUN
  381. * client and MUST be unique for each new STUN transaction generated by
  382. * that STUN client. The transaction ID MUST be uniformly and randomly
  383. * distributed between 0 and 2**96 - 1.
  384. */
  385. pj_uint8_t tsx_id[12];
  386. } pj_stun_msg_hdr;
  387. #pragma pack()
  388. /**
  389. * This structre describes STUN attribute header. Each attribute is
  390. * TLV encoded, with a 16 bit type, 16 bit length, and variable value.
  391. * Each STUN attribute ends on a 32 bit boundary:
  392. *
  393. * \verbatim
  394. 0 1 2 3
  395. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  396. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  397. | Type | Length |
  398. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  399. \endverbatim
  400. */
  401. #pragma pack(1)
  402. typedef struct pj_stun_attr_hdr
  403. {
  404. /**
  405. * STUN attribute type.
  406. */
  407. pj_uint16_t type;
  408. /**
  409. * The Length refers to the length of the actual useful content of the
  410. * Value portion of the attribute, measured in bytes. The value
  411. * in the Length field refers to the length of the Value part of the
  412. * attribute prior to padding - i.e., the useful content.
  413. */
  414. pj_uint16_t length;
  415. } pj_stun_attr_hdr;
  416. #pragma pack()
  417. /**
  418. * This structure describes STUN generic IP address attribute, used for
  419. * example to represent STUN MAPPED-ADDRESS attribute.
  420. *
  421. * The generic IP address attribute indicates the transport address.
  422. * It consists of an eight bit address family, and a sixteen bit port,
  423. * followed by a fixed length value representing the IP address. If the
  424. * address family is IPv4, the address is 32 bits, in network byte
  425. * order. If the address family is IPv6, the address is 128 bits in
  426. * network byte order.
  427. *
  428. * The format of the generic IP address attribute is:
  429. *
  430. * \verbatim
  431. 0 1 2 3
  432. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  433. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  434. |x x x x x x x x| Family | Port |
  435. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  436. | Address (variable)
  437. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  438. \endverbatim
  439. */
  440. typedef struct pj_stun_sockaddr_attr
  441. {
  442. /**
  443. * Standard STUN attribute header.
  444. */
  445. pj_stun_attr_hdr hdr;
  446. /**
  447. * Flag to indicate whether this attribute should be sent in XOR-ed
  448. * format, or has been received in XOR-ed format.
  449. */
  450. pj_bool_t xor_ed;
  451. /**
  452. * The socket address
  453. */
  454. pj_sockaddr sockaddr;
  455. } pj_stun_sockaddr_attr;
  456. /**
  457. * This structure represents a generic STUN attributes with no payload,
  458. * and it is used for example by ICE USE-CANDIDATE attribute.
  459. */
  460. typedef struct pj_stun_empty_attr
  461. {
  462. /**
  463. * Standard STUN attribute header.
  464. */
  465. pj_stun_attr_hdr hdr;
  466. } pj_stun_empty_attr;
  467. /**
  468. * This structure represents generic STUN string attributes, such as STUN
  469. * USERNAME, PASSWORD, SOFTWARE, REALM, and NONCE attributes.
  470. */
  471. typedef struct pj_stun_string_attr
  472. {
  473. /**
  474. * Standard STUN attribute header.
  475. */
  476. pj_stun_attr_hdr hdr;
  477. /**
  478. * The string value.
  479. */
  480. pj_str_t value;
  481. } pj_stun_string_attr;
  482. /**
  483. * This structure represents a generic STUN attributes with 32bit (unsigned)
  484. * integer value, such as STUN FINGERPRINT and REFRESH-INTERVAL attributes.
  485. */
  486. typedef struct pj_stun_uint_attr
  487. {
  488. /**
  489. * Standard STUN attribute header.
  490. */
  491. pj_stun_attr_hdr hdr;
  492. /**
  493. * The 32bit value, in host byte order.
  494. */
  495. pj_uint32_t value;
  496. } pj_stun_uint_attr;
  497. /**
  498. * This structure represents a generic STUN attributes with 64bit (unsigned)
  499. * integer value, such as ICE-CONTROLLED and ICE-CONTROLLING attributes.
  500. */
  501. typedef struct pj_stun_uint64_attr
  502. {
  503. /**
  504. * Standard STUN attribute header.
  505. */
  506. pj_stun_attr_hdr hdr;
  507. /**
  508. * The 64bit value, in host byte order, represented with pj_timestamp.
  509. */
  510. pj_timestamp value;
  511. } pj_stun_uint64_attr;
  512. /**
  513. * This structure represents generic STUN attributes to hold a raw binary
  514. * data.
  515. */
  516. typedef struct pj_stun_binary_attr
  517. {
  518. /**
  519. * Standard STUN attribute header.
  520. */
  521. pj_stun_attr_hdr hdr;
  522. /**
  523. * Special signature to indicate that this is a valid attribute even
  524. * though we don't have meta-data to describe this attribute.
  525. */
  526. pj_uint32_t magic;
  527. /**
  528. * Length of the data.
  529. */
  530. unsigned length;
  531. /**
  532. * The raw data.
  533. */
  534. pj_uint8_t *data;
  535. } pj_stun_binary_attr;
  536. /**
  537. * This structure describes STUN MESSAGE-INTEGRITY attribute.
  538. * The MESSAGE-INTEGRITY attribute contains an HMAC-SHA1 [10] of the
  539. * STUN message. The MESSAGE-INTEGRITY attribute can be present in any
  540. * STUN message type. Since it uses the SHA1 hash, the HMAC will be 20
  541. * bytes.
  542. */
  543. typedef struct pj_stun_msgint_attr
  544. {
  545. /**
  546. * Standard STUN attribute header.
  547. */
  548. pj_stun_attr_hdr hdr;
  549. /**
  550. * The 20 bytes hmac value.
  551. */
  552. pj_uint8_t hmac[20];
  553. } pj_stun_msgint_attr;
  554. /**
  555. * This structure describes STUN FINGERPRINT attribute. The FINGERPRINT
  556. * attribute can be present in all STUN messages. It is computed as
  557. * the CRC-32 of the STUN message up to (but excluding) the FINGERPRINT
  558. * attribute itself, xor-d with the 32 bit value 0x5354554e
  559. */
  560. typedef struct pj_stun_uint_attr pj_stun_fingerprint_attr;
  561. /**
  562. * This structure represents STUN ERROR-CODE attribute. The ERROR-CODE
  563. * attribute is present in the Binding Error Response and Shared Secret
  564. * Error Response. It is a numeric value in the range of 100 to 699
  565. * plus a textual reason phrase encoded in UTF-8
  566. *
  567. * \verbatim
  568. 0 1 2 3
  569. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  570. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  571. | 0 |Class| Number |
  572. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  573. | Reason Phrase (variable) ..
  574. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  575. \endverbatim
  576. */
  577. typedef struct pj_stun_errcode_attr
  578. {
  579. /**
  580. * Standard STUN attribute header.
  581. */
  582. pj_stun_attr_hdr hdr;
  583. /**
  584. * STUN error code.
  585. */
  586. int err_code;
  587. /**
  588. * The reason phrase.
  589. */
  590. pj_str_t reason;
  591. } pj_stun_errcode_attr;
  592. /**
  593. * This describes STUN REALM attribute.
  594. * The REALM attribute is present in requests and responses. It
  595. * contains text which meets the grammar for "realm" as described in RFC
  596. * 3261 [11], and will thus contain a quoted string (including the
  597. * quotes).
  598. */
  599. typedef struct pj_stun_string_attr pj_stun_realm_attr;
  600. /**
  601. * This describes STUN NONCE attribute.
  602. * The NONCE attribute is present in requests and in error responses.
  603. * It contains a sequence of qdtext or quoted-pair, which are defined in
  604. * RFC 3261 [11]. See RFC 2617 [7] for guidance on selection of nonce
  605. * values in a server.
  606. */
  607. typedef struct pj_stun_string_attr pj_stun_nonce_attr;
  608. /**
  609. * This describes STUN UNKNOWN-ATTRIBUTES attribute.
  610. * The UNKNOWN-ATTRIBUTES attribute is present only in an error response
  611. * when the response code in the ERROR-CODE attribute is 420.
  612. * The attribute contains a list of 16 bit values, each of which
  613. * represents an attribute type that was not understood by the server.
  614. * If the number of unknown attributes is an odd number, one of the
  615. * attributes MUST be repeated in the list, so that the total length of
  616. * the list is a multiple of 4 bytes.
  617. */
  618. typedef struct pj_stun_unknown_attr
  619. {
  620. /**
  621. * Standard STUN attribute header.
  622. */
  623. pj_stun_attr_hdr hdr;
  624. /**
  625. * Number of unknown attributes in the array.
  626. */
  627. unsigned attr_count;
  628. /**
  629. * Array of unknown attribute IDs.
  630. */
  631. pj_uint16_t attrs[PJ_STUN_MAX_ATTR];
  632. } pj_stun_unknown_attr;
  633. /**
  634. * This structure describes STUN MAPPED-ADDRESS attribute.
  635. * The MAPPED-ADDRESS attribute indicates the mapped transport address.
  636. */
  637. typedef struct pj_stun_sockaddr_attr pj_stun_mapped_addr_attr;
  638. /**
  639. * This describes STUN XOR-MAPPED-ADDRESS attribute (which has the same
  640. * format as STUN MAPPED-ADDRESS attribute).
  641. * The XOR-MAPPED-ADDRESS attribute is present in responses. It
  642. * provides the same information that would present in the MAPPED-
  643. * ADDRESS attribute but because the NAT's public IP address is
  644. * obfuscated through the XOR function, STUN messages are able to pass
  645. * through NATs which would otherwise interfere with STUN.
  646. */
  647. typedef struct pj_stun_sockaddr_attr pj_stun_xor_mapped_addr_attr;
  648. /**
  649. * This describes STUN SOFTWARE attribute.
  650. * The SOFTWARE attribute contains a textual description of the software
  651. * being used by the agent sending the message. It is used by clients
  652. * and servers. Its value SHOULD include manufacturer and version
  653. * number. */
  654. typedef struct pj_stun_string_attr pj_stun_software_attr;
  655. /**
  656. * This describes STUN ALTERNATE-SERVER attribute.
  657. * The alternate server represents an alternate transport address for a
  658. * different STUN server to try. It is encoded in the same way as
  659. * MAPPED-ADDRESS.
  660. */
  661. typedef struct pj_stun_sockaddr_attr pj_stun_alt_server_attr;
  662. /**
  663. * This describes STUN REFRESH-INTERVAL attribute.
  664. * The REFRESH-INTERVAL indicates the number of milliseconds that the
  665. * server suggests the client should use between refreshes of the NAT
  666. * bindings between the client and server.
  667. */
  668. typedef struct pj_stun_uint_attr pj_stun_refresh_interval_attr;
  669. /**
  670. * This structure describes STUN RESPONSE-ADDRESS attribute.
  671. * The RESPONSE-ADDRESS attribute indicates where the response to a
  672. * Binding Request should be sent. Its syntax is identical to MAPPED-
  673. * ADDRESS.
  674. *
  675. * Note that the usage of this attribute has been deprecated by the
  676. * RFC 3489-bis standard.
  677. */
  678. typedef struct pj_stun_sockaddr_attr pj_stun_response_addr_attr;
  679. /**
  680. * This structure describes STUN CHANGED-ADDRESS attribute.
  681. * The CHANGED-ADDRESS attribute indicates the IP address and port where
  682. * responses would have been sent from if the "change IP" and "change
  683. * port" flags had been set in the CHANGE-REQUEST attribute of the
  684. * Binding Request. The attribute is always present in a Binding
  685. * Response, independent of the value of the flags. Its syntax is
  686. * identical to MAPPED-ADDRESS.
  687. *
  688. * Note that the usage of this attribute has been deprecated by the
  689. * RFC 3489-bis standard.
  690. */
  691. typedef struct pj_stun_sockaddr_attr pj_stun_changed_addr_attr;
  692. /**
  693. * This structure describes STUN CHANGE-REQUEST attribute.
  694. * The CHANGE-REQUEST attribute is used by the client to request that
  695. * the server use a different address and/or port when sending the
  696. * response.
  697. *
  698. * Bit 29 of the value is the "change IP" flag. If true, it requests
  699. * the server to send the Binding Response with a different IP address
  700. * than the one the Binding Request was received on.
  701. *
  702. * Bit 30 of the value is the "change port" flag. If true, it requests
  703. * the server to send the Binding Response with a different port than
  704. * the one the Binding Request was received on.
  705. *
  706. * Note that the usage of this attribute has been deprecated by the
  707. * RFC 3489-bis standard.
  708. */
  709. typedef struct pj_stun_uint_attr pj_stun_change_request_attr;
  710. /**
  711. * This structure describes STUN SOURCE-ADDRESS attribute.
  712. * The SOURCE-ADDRESS attribute is present in Binding Responses. It
  713. * indicates the source IP address and port that the server is sending
  714. * the response from. Its syntax is identical to that of MAPPED-
  715. * ADDRESS.
  716. *
  717. * Note that the usage of this attribute has been deprecated by the
  718. * RFC 3489-bis standard.
  719. */
  720. typedef struct pj_stun_sockaddr_attr pj_stun_src_addr_attr;
  721. /**
  722. * This describes the STUN REFLECTED-FROM attribute.
  723. * The REFLECTED-FROM attribute is present only in Binding Responses,
  724. * when the Binding Request contained a RESPONSE-ADDRESS attribute. The
  725. * attribute contains the identity (in terms of IP address) of the
  726. * source where the request came from. Its purpose is to provide
  727. * traceability, so that a STUN server cannot be used as a reflector for
  728. * denial-of-service attacks.
  729. */
  730. typedef struct pj_stun_sockaddr_attr pj_stun_reflected_from_attr;
  731. /**
  732. * This describes STUN USERNAME attribute.
  733. * The USERNAME attribute is used for message integrity. It identifies
  734. * the shared secret used in the message integrity check. Consequently,
  735. * the USERNAME MUST be included in any request that contains the
  736. * MESSAGE-INTEGRITY attribute.
  737. */
  738. typedef struct pj_stun_string_attr pj_stun_username_attr;
  739. /**
  740. * This describes STUN PASSWORD attribute.
  741. * If the message type is Shared Secret Response it MUST include the
  742. * PASSWORD attribute.
  743. */
  744. typedef struct pj_stun_string_attr pj_stun_password_attr;
  745. /**
  746. * This describes TURN CHANNEL-NUMBER attribute. In this library,
  747. * this attribute is represented with 32bit integer. Application may
  748. * use #PJ_STUN_GET_CH_NB() and #PJ_STUN_SET_CH_NB() to extract/set
  749. * channel number value from the 32bit integral value.
  750. *
  751. * The CHANNEL-NUMBER attribute contains the number of the channel.
  752. * It is a 16-bit unsigned integer, followed by a two-octet RFFU field
  753. * which MUST be set to 0 on transmission and ignored on reception.
  754. \verbatim
  755. 0 1 2 3
  756. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  757. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  758. | Channel Number | RFFU |
  759. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  760. \endverbatim
  761. */
  762. typedef struct pj_stun_uint_attr pj_stun_channel_number_attr;
  763. /**
  764. * Get 16bit channel number from 32bit integral value.
  765. * Note that uint32 attributes are always stored in host byte order
  766. * after they have been parsed from the PDU, so no need to do ntohs()
  767. * here.
  768. */
  769. #define PJ_STUN_GET_CH_NB(u32) ((pj_uint16_t)(u32>>16))
  770. /**
  771. * Convert 16bit channel number into 32bit integral value.
  772. * Note that uint32 attributes will be converted to network byte order
  773. * when the attribute is written to packet, so no need to do htons()
  774. * here.
  775. */
  776. #define PJ_STUN_SET_CH_NB(chnum) (((pj_uint32_t)chnum) << 16)
  777. /**
  778. * This describes STUN LIFETIME attribute.
  779. * The lifetime attribute represents the duration for which the server
  780. * will maintain an allocation in the absence of data traffic either
  781. * from or to the client. It is a 32 bit value representing the number
  782. * of seconds remaining until expiration.
  783. */
  784. typedef struct pj_stun_uint_attr pj_stun_lifetime_attr;
  785. /**
  786. * This describes STUN BANDWIDTH attribute.
  787. * The bandwidth attribute represents the peak bandwidth, measured in
  788. * kbits per second, that the client expects to use on the binding. The
  789. * value represents the sum in the receive and send directions.
  790. */
  791. typedef struct pj_stun_uint_attr pj_stun_bandwidth_attr;
  792. /**
  793. * This describes the STUN XOR-PEER-ADDRESS attribute.
  794. * The XOR-PEER-ADDRESS specifies the address and port of the peer as seen
  795. * from the TURN server. It is encoded in the same way as XOR-MAPPED-
  796. * ADDRESS.
  797. */
  798. typedef struct pj_stun_sockaddr_attr pj_stun_xor_peer_addr_attr;
  799. /**
  800. * This describes the STUN DATA attribute.
  801. * The DATA attribute is present in Send Indications and Data
  802. * Indications. It contains raw payload data that is to be sent (in the
  803. * case of a Send Request) or was received (in the case of a Data
  804. * Indication)..
  805. */
  806. typedef struct pj_stun_binary_attr pj_stun_data_attr;
  807. /**
  808. * This describes the STUN XOR-RELAYED-ADDRESS attribute. The
  809. * XOR-RELAYED-ADDRESS is present in Allocate responses. It specifies the
  810. * address and port that the server allocated to the client. It is
  811. * encoded in the same way as XOR-MAPPED-ADDRESS.
  812. */
  813. typedef struct pj_stun_sockaddr_attr pj_stun_xor_relayed_addr_attr;
  814. /**
  815. * According to RFC 6156, this describes the REQUESTED-ADDRESS-FAMILY
  816. * attribute (formerly known as REQUESTED-ADDRESS-TYPE in the draft).
  817. * The REQUESTED-ADDRESS-FAMILY attribute is used by clients to request
  818. * the allocation of a specific address type from a server. The
  819. * following is the format of the REQUESTED-ADDRESS-FAMILY attribute.
  820. \verbatim
  821. 0 1 2 3
  822. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  823. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  824. | Family | Reserved |
  825. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  826. \endverbatim
  827. */
  828. typedef struct pj_stun_uint_attr pj_stun_req_addr_type_attr;
  829. /**
  830. * This describes the TURN REQUESTED-TRANSPORT attribute, encoded in
  831. * STUN generic integer attribute.
  832. *
  833. * This attribute allows the client to request that the port in the
  834. * relayed-transport-address be even, and (optionally) that the server
  835. * reserve the next-higher port number. The attribute is 8 bits long.
  836. * Its format is:
  837. \verbatim
  838. 0
  839. 0 1 2 3 4 5 6 7
  840. +-+-+-+-+-+-+-+-+
  841. |R| RFFU |
  842. +-+-+-+-+-+-+-+-+
  843. \endverbatim
  844. * The attribute contains a single 1-bit flag:
  845. *
  846. * R: If 1, the server is requested to reserve the next higher port
  847. * number (on the same IP address) for a subsequent allocation. If
  848. * 0, no such reservation is requested.
  849. *
  850. * The other 7 bits of the attribute must be set to zero on transmission
  851. * and ignored on reception.
  852. */
  853. typedef struct pj_stun_uint_attr pj_stun_even_port_attr;
  854. /**
  855. * This describes the TURN REQUESTED-TRANSPORT attribute, encoded in
  856. * STUN generic integer attribute.
  857. *
  858. * This attribute is used by the client to request a specific transport
  859. * protocol for the allocated transport address. It has the following
  860. * format:
  861. \verbatim
  862. 0 1 2 3
  863. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  864. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  865. | Protocol | RFFU |
  866. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  867. \endverbatim
  868. * The Protocol field specifies the desired protocol. The codepoints
  869. * used in this field are taken from those allowed in the Protocol field
  870. * in the IPv4 header and the NextHeader field in the IPv6 header
  871. * [Protocol-Numbers]. This specification only allows the use of
  872. * codepoint 17 (User Datagram Protocol).
  873. *
  874. * The RFFU field is set to zero on transmission and ignored on
  875. * receiption. It is reserved for future uses.
  876. */
  877. typedef struct pj_stun_uint_attr pj_stun_req_transport_attr;
  878. /**
  879. * Get protocol value from 32bit TURN REQUESTED-TRANSPORT attribute.
  880. */
  881. #define PJ_STUN_GET_RT_PROTO(u32) (u32 >> 24)
  882. /**
  883. * Convert protocol value to be placed in 32bit TURN REQUESTED-TRANSPORT
  884. * attribute.
  885. */
  886. #define PJ_STUN_SET_RT_PROTO(proto) (((pj_uint32_t)(proto)) << 24)
  887. /**
  888. * This describes the TURN DONT-FRAGMENT attribute.
  889. *
  890. * This attribute is used by the client to request that the server set
  891. * the DF (Don't Fragment) bit in the IP header when relaying the
  892. * application data onward to the peer. This attribute has no value
  893. * part and thus the attribute length field is 0.
  894. */
  895. typedef struct pj_stun_empty_attr pj_stun_dont_fragment_attr;
  896. /**
  897. * This describes the TURN RESERVATION-TOKEN attribute.
  898. * The RESERVATION-TOKEN attribute contains a token that uniquely
  899. * identifies a relayed transport address being held in reserve by the
  900. * server. The server includes this attribute in a success response to
  901. * tell the client about the token, and the client includes this
  902. * attribute in a subsequent Allocate request to request the server use
  903. * that relayed transport address for the allocation.
  904. *
  905. * The attribute value is a 64-bit-long field containing the token
  906. * value.
  907. */
  908. typedef struct pj_stun_uint64_attr pj_stun_res_token_attr;
  909. /**
  910. * This describes the XOR-REFLECTED-FROM attribute, as described by
  911. * draft-macdonald-behave-nat-behavior-discovery-00.
  912. * The XOR-REFLECTED-FROM attribute is used in place of the REFLECTED-
  913. * FROM attribute. It provides the same information, but because the
  914. * NAT's public address is obfuscated through the XOR function, It can
  915. * pass through a NAT that would otherwise attempt to translate it to
  916. * the private network address. XOR-REFLECTED-FROM has identical syntax
  917. * to XOR-MAPPED-ADDRESS.
  918. */
  919. typedef struct pj_stun_sockaddr_attr pj_stun_xor_reflected_from_attr;
  920. /**
  921. * This describes the PRIORITY attribute from draft-ietf-mmusic-ice-13.
  922. * The PRIORITY attribute indicates the priority that is to be
  923. * associated with a peer reflexive candidate, should one be discovered
  924. * by this check. It is a 32 bit unsigned integer, and has an attribute
  925. * type of 0x0024.
  926. */
  927. typedef struct pj_stun_uint_attr pj_stun_priority_attr;
  928. /**
  929. * This describes the USE-CANDIDATE attribute from draft-ietf-mmusic-ice-13.
  930. * The USE-CANDIDATE attribute indicates that the candidate pair
  931. * resulting from this check should be used for transmission of media.
  932. * The attribute has no content (the Length field of the attribute is
  933. * zero); it serves as a flag.
  934. */
  935. typedef struct pj_stun_empty_attr pj_stun_use_candidate_attr;
  936. /**
  937. * This describes the STUN TIMER-VAL attribute.
  938. * The TIMER-VAL attribute is used only in conjunction with the Set
  939. * Active Destination response. It conveys from the server, to the
  940. * client, the value of the timer used in the server state machine.
  941. */
  942. typedef struct pj_stun_uint_attr pj_stun_timer_val_attr;
  943. /**
  944. * This describes ICE-CONTROLLING attribute.
  945. */
  946. typedef struct pj_stun_uint64_attr pj_stun_ice_controlling_attr;
  947. /**
  948. * This describes ICE-CONTROLLED attribute.
  949. */
  950. typedef struct pj_stun_uint64_attr pj_stun_ice_controlled_attr;
  951. /**
  952. * This describes TURN ICMP attribute
  953. */
  954. typedef struct pj_stun_uint_attr pj_stun_icmp_attr;
  955. /**
  956. * This structure describes a parsed STUN message. All integral fields
  957. * in this structure (including IP addresses) will be in the host
  958. * byte order.
  959. */
  960. typedef struct pj_stun_msg
  961. {
  962. /**
  963. * STUN message header.
  964. */
  965. pj_stun_msg_hdr hdr;
  966. /**
  967. * Number of attributes in the STUN message.
  968. */
  969. unsigned attr_count;
  970. /**
  971. * Array of STUN attributes.
  972. */
  973. pj_stun_attr_hdr *attr[PJ_STUN_MAX_ATTR];
  974. } pj_stun_msg;
  975. /** STUN decoding options */
  976. enum pj_stun_decode_options
  977. {
  978. /**
  979. * Tell the decoder that the message was received from datagram
  980. * oriented transport (such as UDP).
  981. */
  982. PJ_STUN_IS_DATAGRAM = 1,
  983. /**
  984. * Tell pj_stun_msg_decode() to check the validity of the STUN
  985. * message by calling pj_stun_msg_check() before starting to
  986. * decode the packet.
  987. */
  988. PJ_STUN_CHECK_PACKET = 2,
  989. /**
  990. * This option current is only valid for #pj_stun_session_on_rx_pkt().
  991. * When specified, it tells the session NOT to authenticate the
  992. * message.
  993. */
  994. PJ_STUN_NO_AUTHENTICATE = 4,
  995. /**
  996. * Disable FINGERPRINT verification. This option can be used when calling
  997. * #pj_stun_msg_check() and #pj_stun_msg_decode() to disable the
  998. * verification of FINGERPRINT, for example when the STUN usage says when
  999. * FINGERPRINT mechanism shall not be used.
  1000. */
  1001. PJ_STUN_NO_FINGERPRINT_CHECK = 8
  1002. };
  1003. /**
  1004. * Get STUN message method name.
  1005. *
  1006. * @param msg_type The STUN message type (in host byte order)
  1007. *
  1008. * @return The STUN message method name string.
  1009. */
  1010. PJ_DECL(const char*) pj_stun_get_method_name(unsigned msg_type);
  1011. /**
  1012. * Get STUN message class name.
  1013. *
  1014. * @param msg_type The STUN message type (in host byte order)
  1015. *
  1016. * @return The STUN message class name string.
  1017. */
  1018. PJ_DECL(const char*) pj_stun_get_class_name(unsigned msg_type);
  1019. /**
  1020. * Get STUN attribute name.
  1021. *
  1022. * @return attr_type The STUN attribute type (in host byte order).
  1023. *
  1024. * @return The STUN attribute type name string.
  1025. */
  1026. PJ_DECL(const char*) pj_stun_get_attr_name(unsigned attr_type);
  1027. /**
  1028. * Get STUN standard reason phrase for the specified error code.
  1029. *
  1030. * @param err_code The STUN error code.
  1031. *
  1032. * @return The STUN error reason phrase.
  1033. */
  1034. PJ_DECL(pj_str_t) pj_stun_get_err_reason(int err_code);
  1035. /**
  1036. * Internal: set the padding character for string attribute.
  1037. * The default padding character is PJ_STUN_STRING_ATTR_PAD_CHR.
  1038. *
  1039. * @return The previous padding character.
  1040. */
  1041. PJ_DECL(int) pj_stun_set_padding_char(int chr);
  1042. /**
  1043. * Initialize a generic STUN message.
  1044. *
  1045. * @param msg The message structure to be initialized.
  1046. * @param msg_type The 14bit message type (see pj_stun_msg_type
  1047. * constants).
  1048. * @param magic Magic value to be put to the mesage; for requests,
  1049. * the value normally should be PJ_STUN_MAGIC.
  1050. * @param tsx_id Optional transaction ID, or NULL to let the
  1051. * function generates a random transaction ID.
  1052. *
  1053. * @return PJ_SUCCESS on success.
  1054. */
  1055. PJ_DECL(pj_status_t) pj_stun_msg_init(pj_stun_msg *msg,
  1056. unsigned msg_type,
  1057. pj_uint32_t magic,
  1058. const pj_uint8_t tsx_id[12]);
  1059. /**
  1060. * Create a generic STUN message.
  1061. *
  1062. * @param pool Pool to create the STUN message.
  1063. * @param msg_type The 14bit message type.
  1064. * @param magic Magic value to be put to the mesage; for requests,
  1065. * the value should be PJ_STUN_MAGIC.
  1066. * @param tsx_id Optional transaction ID, or NULL to let the
  1067. * function generates a random transaction ID.
  1068. * @param p_msg Pointer to receive the message.
  1069. *
  1070. * @return PJ_SUCCESS on success.
  1071. */
  1072. PJ_DECL(pj_status_t) pj_stun_msg_create(pj_pool_t *pool,
  1073. unsigned msg_type,
  1074. pj_uint32_t magic,
  1075. const pj_uint8_t tsx_id[12],
  1076. pj_stun_msg **p_msg);
  1077. /**
  1078. * Clone a STUN message with all of its attributes.
  1079. *
  1080. * @param pool Pool to allocate memory for the new message.
  1081. * @param msg The message to be cloned.
  1082. *
  1083. * @return The duplicate message.
  1084. */
  1085. PJ_DECL(pj_stun_msg*) pj_stun_msg_clone(pj_pool_t *pool,
  1086. const pj_stun_msg *msg);
  1087. /**
  1088. * Create STUN response message.
  1089. *
  1090. * @param pool Pool to create the mesage.
  1091. * @param req_msg The request message.
  1092. * @param err_code STUN error code. If this value is not zero,
  1093. * then error response will be created, otherwise
  1094. * successful response will be created.
  1095. * @param err_msg Optional error message to explain err_code.
  1096. * If this value is NULL and err_code is not zero,
  1097. * the error string will be taken from the default
  1098. * STUN error message.
  1099. * @param p_response Pointer to receive the response.
  1100. *
  1101. * @return PJ_SUCCESS on success, or the appropriate error.
  1102. */
  1103. PJ_DECL(pj_status_t) pj_stun_msg_create_response(pj_pool_t *pool,
  1104. const pj_stun_msg *req_msg,
  1105. unsigned err_code,
  1106. const pj_str_t *err_msg,
  1107. pj_stun_msg **p_response);
  1108. /**
  1109. * Add STUN attribute to STUN message.
  1110. *
  1111. * @param msg The STUN message.
  1112. * @param attr The STUN attribute to be added to the message.
  1113. *
  1114. * @return PJ_SUCCESS on success, or PJ_ETOOMANY if there are
  1115. * already too many attributes in the message.
  1116. */
  1117. PJ_DECL(pj_status_t) pj_stun_msg_add_attr(pj_stun_msg *msg,
  1118. pj_stun_attr_hdr *attr);
  1119. /**
  1120. * Print the STUN message structure to a packet buffer, ready to be
  1121. * sent to remote destination. This function will take care about
  1122. * calculating the MESSAGE-INTEGRITY digest as well as FINGERPRINT
  1123. * value, if these attributes are present in the message.
  1124. *
  1125. * If application wants to apply credential to the message, it MUST
  1126. * include a blank MESSAGE-INTEGRITY attribute in the message as the
  1127. * last attribute or the attribute before FINGERPRINT. This function will
  1128. * calculate the HMAC digest from the message using the supplied key in
  1129. * the parameter. The key should be set to the password if short term
  1130. * credential is used, or calculated from the MD5 hash of the realm,
  1131. * username, and password using #pj_stun_create_key() if long term
  1132. * credential is used.
  1133. *
  1134. * If FINGERPRINT attribute is present, this function will calculate
  1135. * the FINGERPRINT CRC attribute for the message. The FINGERPRINT MUST
  1136. * be added as the last attribute of the message.
  1137. *
  1138. * @param msg The STUN message to be printed. Upon return,
  1139. * some fields in the header (such as message
  1140. * length) will be updated.
  1141. * @param pkt_buf The buffer to be filled with the packet.
  1142. * @param buf_size Size of the buffer.
  1143. * @param options Options, which currently must be zero.
  1144. * @param key Authentication key to calculate MESSAGE-INTEGRITY
  1145. * value. Application can create this key by using
  1146. * #pj_stun_create_key() function.
  1147. * @param p_msg_len Upon return, it will be filed with the size of
  1148. * the packet in bytes, or negative value on error.
  1149. *
  1150. * @return PJ_SUCCESS on success or the appropriate error code.
  1151. */
  1152. PJ_DECL(pj_status_t) pj_stun_msg_encode(pj_stun_msg *msg,
  1153. pj_uint8_t *pkt_buf,
  1154. pj_size_t buf_size,
  1155. unsigned options,
  1156. const pj_str_t *key,
  1157. pj_size_t *p_msg_len);
  1158. /**
  1159. * Check that the PDU is potentially a valid STUN message. This function
  1160. * is useful when application needs to multiplex STUN packets with other
  1161. * application traffic. When this function returns PJ_SUCCESS, there is a
  1162. * big chance that the packet is a STUN packet.
  1163. *
  1164. * Note that we cannot be sure that the PDU is a really valid STUN message
  1165. * until we actually parse the PDU.
  1166. *
  1167. * @param pdu The packet buffer.
  1168. * @param pdu_len The length of the packet buffer.
  1169. * @param options Additional options to be applied in the checking,
  1170. * which can be taken from pj_stun_decode_options. One
  1171. * of the useful option is PJ_STUN_IS_DATAGRAM which
  1172. * means that the pdu represents a whole STUN packet.
  1173. *
  1174. * @return PJ_SUCCESS if the PDU is a potentially valid STUN
  1175. * message.
  1176. */
  1177. PJ_DECL(pj_status_t) pj_stun_msg_check(const pj_uint8_t *pdu,
  1178. pj_size_t pdu_len, unsigned options);
  1179. /**
  1180. * Decode incoming packet into STUN message.
  1181. *
  1182. * @param pool Pool to allocate the message.
  1183. * @param pdu The incoming packet to be parsed.
  1184. * @param pdu_len The length of the incoming packet.
  1185. * @param options Parsing flags, according to pj_stun_decode_options.
  1186. * @param p_msg Pointer to receive the parsed message.
  1187. * @param p_parsed_len Optional pointer to receive how many bytes have
  1188. * been parsed for the STUN message. This is useful
  1189. * when the packet is received over stream oriented
  1190. * transport.
  1191. * @param p_response Optional pointer to receive an instance of response
  1192. * message, if one can be created. If the packet being
  1193. * decoded is a request message, and it contains error,
  1194. * and a response can be created, then the STUN
  1195. * response message will be returned on this argument.
  1196. *
  1197. * @return PJ_SUCCESS if a STUN message has been successfully
  1198. * decoded.
  1199. */
  1200. PJ_DECL(pj_status_t) pj_stun_msg_decode(pj_pool_t *pool,
  1201. const pj_uint8_t *pdu,
  1202. pj_size_t pdu_len,
  1203. unsigned options,
  1204. pj_stun_msg **p_msg,
  1205. pj_size_t *p_parsed_len,
  1206. pj_stun_msg **p_response);
  1207. /**
  1208. * Dump STUN message to a printable string output.
  1209. *
  1210. * @param msg The STUN message
  1211. * @param buffer Buffer where the printable string output will
  1212. * be printed on.
  1213. * @param length Specify the maximum length of the buffer.
  1214. * @param printed_len Optional pointer, which on output will be filled
  1215. * up with the actual length of the output string.
  1216. *
  1217. * @return The message string output.
  1218. */
  1219. #if PJ_LOG_MAX_LEVEL > 0
  1220. PJ_DECL(char*) pj_stun_msg_dump(const pj_stun_msg *msg,
  1221. char *buffer,
  1222. unsigned length,
  1223. unsigned *printed_len);
  1224. #else
  1225. # define pj_stun_msg_dump(msg, buf, length, printed_len) ""
  1226. #endif
  1227. /**
  1228. * Find STUN attribute in the STUN message, starting from the specified
  1229. * index.
  1230. *
  1231. * @param msg The STUN message.
  1232. * @param attr_type The attribute type to be found, from pj_stun_attr_type.
  1233. * @param start_index The start index of the attribute in the message.
  1234. * Specify zero to start searching from the first
  1235. * attribute.
  1236. *
  1237. * @return The attribute instance, or NULL if it cannot be
  1238. * found.
  1239. */
  1240. PJ_DECL(pj_stun_attr_hdr*) pj_stun_msg_find_attr(const pj_stun_msg *msg,
  1241. int attr_type,
  1242. unsigned start_index);
  1243. /**
  1244. * Clone a STUN attribute.
  1245. *
  1246. * @param pool Pool to allocate memory.
  1247. * @param attr Attribute to clone.
  1248. *
  1249. * @return Duplicate attribute.
  1250. */
  1251. PJ_DECL(pj_stun_attr_hdr*) pj_stun_attr_clone(pj_pool_t *pool,
  1252. const pj_stun_attr_hdr *attr);
  1253. /**
  1254. * Initialize generic STUN IP address attribute. The \a addr_len and
  1255. * \a addr parameters specify whether the address is IPv4 or IPv4
  1256. * address.
  1257. *
  1258. * @param attr The socket address attribute to initialize.
  1259. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1260. * @param xor_ed If non-zero, the port and address will be XOR-ed
  1261. * with magic, to make the XOR-MAPPED-ADDRESS attribute.
  1262. * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure.
  1263. * @param addr_len Length of \a addr parameter.
  1264. *
  1265. * @return PJ_SUCCESS on success or the appropriate error code.
  1266. */
  1267. PJ_DECL(pj_status_t) pj_stun_sockaddr_attr_init(pj_stun_sockaddr_attr *attr,
  1268. int attr_type,
  1269. pj_bool_t xor_ed,
  1270. const pj_sockaddr_t *addr,
  1271. unsigned addr_len);
  1272. /**
  1273. * Create a generic STUN IP address attribute. The \a addr_len and
  1274. * \a addr parameters specify whether the address is IPv4 or IPv4
  1275. * address.
  1276. *
  1277. * @param pool The pool to allocate memory from.
  1278. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1279. * @param xor_ed If non-zero, the port and address will be XOR-ed
  1280. * with magic, to make the XOR-MAPPED-ADDRESS attribute.
  1281. * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure.
  1282. * @param addr_len Length of \a addr parameter.
  1283. * @param p_attr Pointer to receive the attribute.
  1284. *
  1285. * @return PJ_SUCCESS on success or the appropriate error code.
  1286. */
  1287. PJ_DECL(pj_status_t) pj_stun_sockaddr_attr_create(pj_pool_t *pool,
  1288. int attr_type,
  1289. pj_bool_t xor_ed,
  1290. const pj_sockaddr_t *addr,
  1291. unsigned addr_len,
  1292. pj_stun_sockaddr_attr **p_attr);
  1293. /**
  1294. * Create and add generic STUN IP address attribute to a STUN message.
  1295. * The \a addr_len and \a addr parameters specify whether the address is
  1296. * IPv4 or IPv4 address.
  1297. *
  1298. * @param pool The pool to allocate memory from.
  1299. * @param msg The STUN message.
  1300. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1301. * @param xor_ed If non-zero, the port and address will be XOR-ed
  1302. * with magic, to make the XOR-MAPPED-ADDRESS attribute.
  1303. * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure.
  1304. * @param addr_len Length of \a addr parameter.
  1305. *
  1306. * @return PJ_SUCCESS on success or the appropriate error code.
  1307. */
  1308. PJ_DECL(pj_status_t) pj_stun_msg_add_sockaddr_attr(pj_pool_t *pool,
  1309. pj_stun_msg *msg,
  1310. int attr_type,
  1311. pj_bool_t xor_ed,
  1312. const pj_sockaddr_t *addr,
  1313. unsigned addr_len);
  1314. /**
  1315. * Initialize a STUN generic string attribute.
  1316. *
  1317. * @param attr The string attribute to be initialized.
  1318. * @param pool Pool to duplicate the value into the attribute,
  1319. * if value is not NULL or empty.
  1320. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1321. * @param value The string value to be assigned to the attribute.
  1322. *
  1323. * @return PJ_SUCCESS on success or the appropriate error code.
  1324. */
  1325. PJ_DECL(pj_status_t) pj_stun_string_attr_init(pj_stun_string_attr *attr,
  1326. pj_pool_t *pool,
  1327. int attr_type,
  1328. const pj_str_t *value);
  1329. /**
  1330. * Create a STUN generic string attribute.
  1331. *
  1332. * @param pool The pool to allocate memory from.
  1333. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1334. * @param value The string value to be assigned to the attribute.
  1335. * @param p_attr Pointer to receive the attribute.
  1336. *
  1337. * @return PJ_SUCCESS on success or the appropriate error code.
  1338. */
  1339. PJ_DECL(pj_status_t) pj_stun_string_attr_create(pj_pool_t *pool,
  1340. int attr_type,
  1341. const pj_str_t *value,
  1342. pj_stun_string_attr **p_attr);
  1343. /**
  1344. * Create and add STUN generic string attribute to the message.
  1345. *
  1346. * @param pool The pool to allocate memory from.
  1347. * @param msg The STUN message.
  1348. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1349. * @param value The string value to be assigned to the attribute.
  1350. *
  1351. * @return PJ_SUCCESS on success or the appropriate error code.
  1352. */
  1353. PJ_DECL(pj_status_t) pj_stun_msg_add_string_attr(pj_pool_t *pool,
  1354. pj_stun_msg *msg,
  1355. int attr_type,
  1356. const pj_str_t *value);
  1357. /**
  1358. * Create a STUN generic 32bit value attribute.
  1359. *
  1360. * @param pool The pool to allocate memory from.
  1361. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1362. * @param value The 32bit value to be assigned to the attribute.
  1363. * @param p_attr Pointer to receive the attribute.
  1364. *
  1365. * @return PJ_SUCCESS on success or the appropriate error code.
  1366. */
  1367. PJ_DECL(pj_status_t) pj_stun_uint_attr_create(pj_pool_t *pool,
  1368. int attr_type,
  1369. pj_uint32_t value,
  1370. pj_stun_uint_attr **p_attr);
  1371. /**
  1372. * Create and add STUN generic 32bit value attribute to the message.
  1373. *
  1374. * @param pool The pool to allocate memory from.
  1375. * @param msg The STUN message
  1376. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1377. * @param value The 32bit value to be assigned to the attribute.
  1378. *
  1379. * @return PJ_SUCCESS on success or the appropriate error code.
  1380. */
  1381. PJ_DECL(pj_status_t) pj_stun_msg_add_uint_attr(pj_pool_t *pool,
  1382. pj_stun_msg *msg,
  1383. int attr_type,
  1384. pj_uint32_t value);
  1385. /**
  1386. * Create a STUN generic 64bit value attribute.
  1387. *
  1388. * @param pool Pool to allocate memory from.
  1389. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1390. * @param value Optional value to be assigned.
  1391. * @param p_attr Pointer to receive the attribute.
  1392. *
  1393. * @return PJ_SUCCESS on success or the appropriate error code.
  1394. */
  1395. PJ_DECL(pj_status_t) pj_stun_uint64_attr_create(pj_pool_t *pool,
  1396. int attr_type,
  1397. const pj_timestamp *value,
  1398. pj_stun_uint64_attr **p_attr);
  1399. /**
  1400. * Create and add STUN generic 64bit value attribute to the message.
  1401. *
  1402. * @param pool The pool to allocate memory from.
  1403. * @param msg The STUN message
  1404. * @param attr_type Attribute type, from #pj_stun_attr_type.
  1405. * @param value The 64bit value to be assigned to the attribute.
  1406. *
  1407. * @return PJ_SUCCESS on success or the appropriate error code.
  1408. */
  1409. PJ_DECL(pj_status_t) pj_stun_msg_add_uint64_attr(pj_pool_t *pool,
  1410. pj_stun_msg *msg,
  1411. int attr_type,
  1412. const pj_timestamp *value);
  1413. /**
  1414. * Create a STUN MESSAGE-INTEGRITY attribute.
  1415. *
  1416. * @param pool The pool to allocate memory from.
  1417. * @param p_attr Pointer to receive the attribute.
  1418. *
  1419. * @return PJ_SUCCESS on success or the appropriate error code.
  1420. */
  1421. PJ_DECL(pj_status_t) pj_stun_msgint_attr_create(pj_pool_t *pool,
  1422. pj_stun_msgint_attr **p_attr);
  1423. /**
  1424. * Create and add STUN MESSAGE-INTEGRITY attribute.
  1425. *
  1426. * @param pool The pool to allocate memory from.
  1427. * @param msg The STUN message
  1428. *
  1429. * @return PJ_SUCCESS on success or the appropriate error code.
  1430. */
  1431. PJ_DECL(pj_status_t) pj_stun_msg_add_msgint_attr(pj_pool_t *pool,
  1432. pj_stun_msg *msg);
  1433. /**
  1434. * Create a STUN ERROR-CODE attribute.
  1435. *
  1436. * @param pool The pool to allocate memory from.
  1437. * @param err_code STUN error code.
  1438. * @param err_reason Optional STUN error reason. If NULL is given, the
  1439. * standard error reason will be given.
  1440. * @param p_attr Pointer to receive the attribute.
  1441. *
  1442. * @return PJ_SUCCESS on success or the appropriate error code.
  1443. */
  1444. PJ_DECL(pj_status_t) pj_stun_errcode_attr_create(pj_pool_t *pool,
  1445. int err_code,
  1446. const pj_str_t *err_reason,
  1447. pj_stun_errcode_attr **p_attr);
  1448. /**
  1449. * Create and add STUN ERROR-CODE attribute to the message.
  1450. *
  1451. * @param pool The pool to allocate memory from.
  1452. * @param msg The STUN mesage.
  1453. * @param err_code STUN error code.
  1454. * @param err_reason Optional STUN error reason. If NULL is given, the
  1455. * standard error reason will be given.
  1456. *
  1457. * @return PJ_SUCCESS on success or the appropriate error code.
  1458. */
  1459. PJ_DECL(pj_status_t) pj_stun_msg_add_errcode_attr(pj_pool_t *pool,
  1460. pj_stun_msg *msg,
  1461. int err_code,
  1462. const pj_str_t *err_reason);
  1463. /**
  1464. * Create instance of STUN UNKNOWN-ATTRIBUTES attribute and copy the
  1465. * unknown attribute array to the attribute.
  1466. *
  1467. * @param pool The pool to allocate memory from.
  1468. * @param attr_cnt Number of attributes in the array (can be zero).
  1469. * @param attr Optional array of attributes.
  1470. * @param p_attr Pointer to receive the attribute.
  1471. *
  1472. * @return PJ_SUCCESS on success or the appropriate error code.
  1473. */
  1474. PJ_DECL(pj_status_t) pj_stun_unknown_attr_create(pj_pool_t *pool,
  1475. unsigned attr_cnt,
  1476. const pj_uint16_t attr[],
  1477. pj_stun_unknown_attr **p_attr);
  1478. /**
  1479. * Create and add STUN UNKNOWN-ATTRIBUTES attribute to the message.
  1480. *
  1481. * @param pool The pool to allocate memory from.
  1482. * @param msg The STUN message.
  1483. * @param attr_cnt Number of attributes in the array (can be zero).
  1484. * @param attr Optional array of attribute types.
  1485. *
  1486. * @return PJ_SUCCESS on success or the appropriate error code.
  1487. */
  1488. PJ_DECL(pj_status_t) pj_stun_msg_add_unknown_attr(pj_pool_t *pool,
  1489. pj_stun_msg *msg,
  1490. unsigned attr_cnt,
  1491. const pj_uint16_t attr[]);
  1492. /**
  1493. * Initialize STUN binary attribute.
  1494. *
  1495. * @param attr The attribute to be initialized.
  1496. * @param pool Pool to copy data, if the data and length are set.
  1497. * @param attr_type The attribute type, from #pj_stun_attr_type.
  1498. * @param data Data to be coped to the attribute, or NULL
  1499. * if no data to be copied now.
  1500. * @param length Length of data, or zero if no data is to be
  1501. * copied now.
  1502. *
  1503. * @return PJ_SUCCESS on success or the appropriate error code.
  1504. */
  1505. PJ_DECL(pj_status_t) pj_stun_binary_attr_init(pj_stun_binary_attr *attr,
  1506. pj_pool_t *pool,
  1507. int attr_type,
  1508. const pj_uint8_t *data,
  1509. unsigned length);
  1510. /**
  1511. * Create STUN binary attribute.
  1512. *
  1513. * @param pool The pool to allocate memory from.
  1514. * @param attr_type The attribute type, from #pj_stun_attr_type.
  1515. * @param data Data to be coped to the attribute, or NULL
  1516. * if no data to be copied now.
  1517. * @param length Length of data, or zero if no data is to be
  1518. * copied now.
  1519. * @param p_attr Pointer to receive the attribute.
  1520. *
  1521. * @return PJ_SUCCESS on success or the appropriate error code.
  1522. */
  1523. PJ_DECL(pj_status_t) pj_stun_binary_attr_create(pj_pool_t *pool,
  1524. int attr_type,
  1525. const pj_uint8_t *data,
  1526. unsigned length,
  1527. pj_stun_binary_attr **p_attr);
  1528. /**
  1529. * Create STUN binary attribute and add the attribute to the message.
  1530. *
  1531. * @param pool The pool to allocate memory from.
  1532. * @param msg The STUN message.
  1533. * @param attr_type The attribute type, from #pj_stun_attr_type.
  1534. * @param data Data to be coped to the attribute, or NULL
  1535. * if no data to be copied now.
  1536. * @param length Length of data, or zero if no data is to be
  1537. * copied now.
  1538. *
  1539. * @return PJ_SUCCESS on success or the appropriate error code.
  1540. */
  1541. PJ_DECL(pj_status_t) pj_stun_msg_add_binary_attr(pj_pool_t *pool,
  1542. pj_stun_msg *msg,
  1543. int attr_type,
  1544. const pj_uint8_t *data,
  1545. unsigned length);
  1546. /**
  1547. * Create STUN empty attribute.
  1548. *
  1549. * @param pool The pool to allocate memory from.
  1550. * @param attr_type The attribute type, from #pj_stun_attr_type.
  1551. * @param p_attr Pointer to receive the attribute.
  1552. *
  1553. * @return PJ_SUCCESS on success or the appropriate error code.
  1554. */
  1555. PJ_DECL(pj_status_t) pj_stun_empty_attr_create(pj_pool_t *pool,
  1556. int attr_type,
  1557. pj_stun_empty_attr **p_attr);
  1558. /**
  1559. * Create STUN empty attribute and add the attribute to the message.
  1560. *
  1561. * @param pool The pool to allocate memory from.
  1562. * @param msg The STUN message.
  1563. * @param attr_type The attribute type, from #pj_stun_attr_type.
  1564. *
  1565. * @return PJ_SUCCESS on success or the appropriate error code.
  1566. */
  1567. PJ_DECL(pj_status_t) pj_stun_msg_add_empty_attr(pj_pool_t *pool,
  1568. pj_stun_msg *msg,
  1569. int attr_type);
  1570. /**
  1571. * @}
  1572. */
  1573. PJ_END_DECL
  1574. #endif /* __PJNATH_STUN_MSG_H__ */