Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
8.0 KiB

  1. /*
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. sdpconn.cpp
  5. Abstract:
  6. Author:
  7. */
  8. #include "sdppch.h"
  9. #include "sdpconn.h"
  10. #include "sdpltran.h"
  11. // limited character strings
  12. const CHAR *LIMITED_NETWORK_TYPES[] = {INTERNET_STRING};
  13. const BYTE NUM_NETWORK_TYPES = sizeof(LIMITED_NETWORK_TYPES)/sizeof(CHAR *);
  14. const CHAR *LIMITED_ADDRESS_TYPES[] = {IP4_STRING};
  15. const BYTE NUM_ADDRESS_TYPES = sizeof(LIMITED_ADDRESS_TYPES)/sizeof(CHAR *);
  16. // line transition states
  17. enum CONNECTION_TRANSITION_STATES
  18. {
  19. CONNECTION_START,
  20. CONNECTION_NETWORK_TYPE,
  21. CONNECTION_ADDRESS_TYPE,
  22. CONNECTION_MCAST_ADDRESS,
  23. CONNECTION_UCAST_ADDRESS,
  24. CONNECTION_TTL,
  25. CONNECTION_TTL_NUM_ADDRESSES,
  26. CONNECTION_NUM_ADDRESSES
  27. };
  28. // table for connection line transitions
  29. const LINE_TRANSITION g_ConnectionStartTransitions[] = {
  30. {CHAR_BLANK, CONNECTION_NETWORK_TYPE }
  31. };
  32. const LINE_TRANSITION g_ConnectionNetworkTypeTransitions[] = {
  33. {CHAR_BLANK, CONNECTION_ADDRESS_TYPE }
  34. };
  35. const LINE_TRANSITION g_ConnectionAddressTypeTransitions[] = {
  36. {CHAR_BACK_SLASH, CONNECTION_MCAST_ADDRESS},
  37. {CHAR_NEWLINE, CONNECTION_UCAST_ADDRESS}
  38. };
  39. const LINE_TRANSITION g_ConnectionMcastAddressTransitions[] = {
  40. {CHAR_BACK_SLASH, CONNECTION_TTL_NUM_ADDRESSES},
  41. {CHAR_NEWLINE, CONNECTION_TTL }
  42. };
  43. /* no transitions */
  44. const LINE_TRANSITION *g_ConnectionUcastAddressTransitions = NULL;
  45. /* no transitions */
  46. const LINE_TRANSITION *g_ConnectionTtlTransitions = NULL;
  47. const LINE_TRANSITION g_ConnectionTtlNumAddressesTransitions[] ={
  48. {CHAR_NEWLINE, CONNECTION_NUM_ADDRESSES}
  49. };
  50. /* no transitions */
  51. const LINE_TRANSITION *g_ConnectionNumAddressesTransitions = NULL;
  52. LINE_TRANSITION_INFO g_ConnectionTransitionInfo[] = {
  53. LINE_TRANSITION_ENTRY(CONNECTION_START, g_ConnectionStartTransitions),
  54. LINE_TRANSITION_ENTRY(CONNECTION_NETWORK_TYPE, g_ConnectionNetworkTypeTransitions),
  55. LINE_TRANSITION_ENTRY(CONNECTION_ADDRESS_TYPE, g_ConnectionAddressTypeTransitions),
  56. LINE_TRANSITION_ENTRY(CONNECTION_MCAST_ADDRESS, g_ConnectionMcastAddressTransitions),
  57. LINE_TRANSITION_ENTRY(CONNECTION_UCAST_ADDRESS, g_ConnectionUcastAddressTransitions),
  58. LINE_TRANSITION_ENTRY(CONNECTION_TTL, g_ConnectionTtlTransitions),
  59. LINE_TRANSITION_ENTRY(CONNECTION_TTL_NUM_ADDRESSES, g_ConnectionTtlNumAddressesTransitions),
  60. LINE_TRANSITION_ENTRY(CONNECTION_NUM_ADDRESSES, g_ConnectionNumAddressesTransitions)
  61. };
  62. SDP_LINE_TRANSITION g_ConnectionTransition(
  63. g_ConnectionTransitionInfo,
  64. sizeof(g_ConnectionTransitionInfo)/sizeof(LINE_TRANSITION_INFO)
  65. );
  66. SDP_CONNECTION::SDP_CONNECTION(
  67. )
  68. : SDP_VALUE(SDP_INVALID_CONNECTION_FIELD, CONNECTION_STRING, &g_ConnectionTransition),
  69. m_NetworkType(LIMITED_NETWORK_TYPES, NUM_NETWORK_TYPES),
  70. m_AddressType(LIMITED_ADDRESS_TYPES, NUM_ADDRESS_TYPES)
  71. {
  72. m_NumAddresses.SetValue(1);
  73. }
  74. void
  75. SDP_CONNECTION::InternalReset(
  76. )
  77. {
  78. m_NetworkType.Reset();
  79. m_AddressType.Reset();
  80. m_StartAddress.Reset();
  81. m_Ttl.Reset();
  82. m_NumAddresses.Reset();
  83. }
  84. HRESULT
  85. SDP_CONNECTION::SetConnection(
  86. IN BSTR StartAddress,
  87. IN ULONG NumAddresses,
  88. IN BYTE Ttl
  89. )
  90. {
  91. // validate the address
  92. HRESULT ToReturn = m_StartAddress.SetAddress(StartAddress);
  93. if ( FAILED(ToReturn) )
  94. {
  95. return ToReturn;
  96. }
  97. // if multicast, valid number of addresses and ttl values are expected
  98. // set the num addresses and
  99. if ( m_StartAddress.IsMulticast() )
  100. {
  101. if ( (0 == NumAddresses) || (0 == Ttl) )
  102. {
  103. return E_INVALIDARG;
  104. }
  105. m_NumAddresses.SetValueAndFlag(NumAddresses);
  106. m_Ttl.SetValueAndFlag(Ttl);
  107. }
  108. else // invalidate the num addresses and ttl fields
  109. {
  110. m_NumAddresses.Reset();
  111. m_Ttl.Reset();
  112. }
  113. // if the network type is not valid, parse in the default IN network type
  114. if ( !m_NetworkType.IsValid() )
  115. {
  116. if ( !m_NetworkType.ParseToken("IN") )
  117. {
  118. return HRESULT_FROM_ERROR_CODE(GetLastError());
  119. }
  120. }
  121. // if the address type is not valid, parse in the default IP4 address type
  122. if ( !m_AddressType.IsValid() )
  123. {
  124. if ( !m_AddressType.ParseToken("IP4") )
  125. {
  126. return HRESULT_FROM_ERROR_CODE(GetLastError());
  127. }
  128. }
  129. // clear the field and separator arrays
  130. m_FieldArray.RemoveAll();
  131. m_SeparatorCharArray.RemoveAll();
  132. // fill in the field, separator char arrays with network/address type and
  133. // the address, if required
  134. if ( 0 == m_FieldArray.GetSize() )
  135. {
  136. try
  137. {
  138. m_FieldArray.SetAtGrow(0, &m_NetworkType);
  139. m_SeparatorCharArray.SetAtGrow(0, CHAR_BLANK);
  140. m_FieldArray.SetAtGrow(1, &m_AddressType);
  141. m_SeparatorCharArray.SetAtGrow(1, CHAR_BLANK);
  142. m_FieldArray.SetAtGrow(2, &m_StartAddress);
  143. }
  144. catch(...)
  145. {
  146. m_FieldArray.RemoveAll();
  147. m_SeparatorCharArray.RemoveAll();
  148. return E_OUTOFMEMORY;
  149. }
  150. }
  151. ASSERT(3 <= m_FieldArray.GetSize());
  152. // if multicast address, fill in the next three fields
  153. if ( m_StartAddress.IsMulticast() )
  154. {
  155. if ( 4 > m_FieldArray.GetSize() )
  156. {
  157. // ZoltanS bugfix: m_Ttl and m_NumAddresses were reversed below!
  158. try
  159. {
  160. m_SeparatorCharArray.SetAtGrow(2, CHAR_BACK_SLASH);
  161. m_FieldArray.SetAtGrow(3, &m_Ttl);
  162. m_SeparatorCharArray.SetAtGrow(3, CHAR_BACK_SLASH);
  163. m_FieldArray.SetAtGrow(4, &m_NumAddresses);
  164. m_SeparatorCharArray.SetAtGrow(4, CHAR_NEWLINE);
  165. }
  166. catch(...)
  167. {
  168. m_FieldArray.RemoveAll();
  169. m_SeparatorCharArray.RemoveAll();
  170. return E_OUTOFMEMORY;
  171. }
  172. }
  173. ASSERT(5 == m_FieldArray.GetSize());
  174. }
  175. else
  176. {
  177. // else, unicast, fill in the next field and remove from the next two
  178. try
  179. {
  180. m_SeparatorCharArray.SetAtGrow(2, CHAR_NEWLINE);
  181. }
  182. catch(...)
  183. {
  184. m_FieldArray.RemoveAll();
  185. m_SeparatorCharArray.RemoveAll();
  186. return E_OUTOFMEMORY;
  187. }
  188. if ( 4 <= m_FieldArray.GetSize() )
  189. {
  190. ASSERT(5 == m_FieldArray.GetSize());
  191. m_FieldArray.RemoveAt(3);
  192. m_SeparatorCharArray.RemoveAt(3);
  193. m_FieldArray.RemoveAt(4);
  194. m_SeparatorCharArray.RemoveAt(4);
  195. }
  196. ASSERT(3 == m_FieldArray.GetSize());
  197. }
  198. // if the address is not a multicast address, the other params are ignored
  199. return S_OK;
  200. }
  201. BOOL
  202. SDP_CONNECTION::GetField(
  203. OUT SDP_FIELD *&Field,
  204. OUT BOOL &AddToArray
  205. )
  206. {
  207. // add in all cases by default
  208. AddToArray = TRUE;
  209. switch(m_LineState)
  210. {
  211. case CONNECTION_NETWORK_TYPE:
  212. {
  213. Field = &m_NetworkType;
  214. }
  215. break;
  216. case CONNECTION_ADDRESS_TYPE:
  217. {
  218. Field = &m_AddressType;
  219. }
  220. break;
  221. case CONNECTION_MCAST_ADDRESS:
  222. {
  223. m_StartAddress.SetMulticast(TRUE);
  224. Field = &m_StartAddress;
  225. }
  226. break;
  227. case CONNECTION_UCAST_ADDRESS:
  228. {
  229. m_StartAddress.SetMulticast(FALSE);
  230. Field = &m_StartAddress;
  231. }
  232. break;
  233. case CONNECTION_TTL:
  234. case CONNECTION_TTL_NUM_ADDRESSES:
  235. {
  236. Field = &m_Ttl;
  237. }
  238. break;
  239. case CONNECTION_NUM_ADDRESSES:
  240. {
  241. Field = &m_NumAddresses;
  242. }
  243. break;
  244. default:
  245. {
  246. SetLastError(m_ErrorCode);
  247. return FALSE;
  248. }
  249. break;
  250. };
  251. return TRUE;
  252. }