coqos_rel.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * coqos_rel.c - Release MCSMA stream
  44. *
  45. *
  46. *
  47. * Contributor(s):
  48. * Bill Wike
  49. * Nathaniel Houghton
  50. * Charles Maier
  51. *
  52. *--------------------------------------------------------------------*/
  53. /*====================================================================*"
  54. * system header files;
  55. *--------------------------------------------------------------------*/
  56. #include <unistd.h>
  57. #include <stdlib.h>
  58. #include <stdint.h>
  59. #include <limits.h>
  60. /*====================================================================*
  61. * custom header files;
  62. *--------------------------------------------------------------------*/
  63. #include "../tools/getoptv.h"
  64. #include "../tools/putoptv.h"
  65. #include "../tools/memory.h"
  66. #include "../tools/number.h"
  67. #include "../tools/symbol.h"
  68. #include "../tools/types.h"
  69. #include "../tools/flags.h"
  70. #include "../tools/files.h"
  71. #include "../tools/error.h"
  72. #include "../plc/plc.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../tools/getoptv.c"
  78. #include "../tools/putoptv.c"
  79. #include "../tools/version.c"
  80. #include "../tools/hexdump.c"
  81. #include "../tools/hexencode.c"
  82. #include "../tools/bytespec.c"
  83. #include "../tools/ipv4spec.c"
  84. #include "../tools/ipv6spec.c"
  85. #include "../tools/hexdecode.c"
  86. #include "../tools/todigit.c"
  87. #include "../tools/typename.c"
  88. #include "../tools/error.c"
  89. #include "../tools/synonym.c"
  90. #endif
  91. #ifndef MAKEFILE
  92. #include "../plc/Request.c"
  93. #include "../plc/Confirm.c"
  94. #include "../plc/Failure.c"
  95. #include "../plc/Display.c"
  96. #include "../plc/ReadMME.c"
  97. #include "../plc/SendMME.c"
  98. #include "../plc/Devices.c"
  99. #include "../mme/EthernetHeader.c"
  100. #include "../mme/QualcommHeader.c"
  101. #include "../mme/UnwantedMessage.c"
  102. #endif
  103. #ifndef MAKEFILE
  104. #include "../ether/channel.c"
  105. #include "../ether/openchannel.c"
  106. #include "../ether/closechannel.c"
  107. #include "../ether/readpacket.c"
  108. #include "../ether/sendpacket.c"
  109. #endif
  110. #ifndef MAKEFILE
  111. #include "../mme/MMECode.c"
  112. #endif
  113. /*====================================================================*
  114. *
  115. * signed rel_conn (struct plc * plc, uint16_t CID);
  116. *
  117. *
  118. *
  119. * Contributor(s):
  120. * Bill Wike
  121. *
  122. *--------------------------------------------------------------------*/
  123. signed rel_conn (struct plc * plc, uint16_t CID)
  124. {
  125. struct channel * channel = (struct channel *)(plc->channel);
  126. struct message * message = (struct message *)(plc->message);
  127. #ifndef __GNUC__
  128. #pragma pack (push,1)
  129. #endif
  130. struct __packed vs_rel_conn_req
  131. {
  132. struct ethernet_hdr ethernet;
  133. struct qualcomm_hdr qualcomm;
  134. uint32_t REQ_ID;
  135. uint32_t RSVD;
  136. uint16_t CID;
  137. }
  138. * request = (struct vs_rel_conn_req *)(message);
  139. struct __packed vs_rel_conn_cnf
  140. {
  141. struct ethernet_hdr ethernet;
  142. struct qualcomm_hdr qualcomm;
  143. uint32_t REQ_ID;
  144. uint8_t MSTATUS;
  145. uint16_t ERR_REC_CODE;
  146. uint32_t RSVD;
  147. }
  148. * confirm = (struct vs_rel_conn_cnf *)(message);
  149. #ifndef __GNUC__
  150. #pragma pack (pop)
  151. #endif
  152. Request (plc, "Release COQOS connection");
  153. memset (message, 0, sizeof (* message));
  154. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  155. QualcommHeader (&request->qualcomm, 0, (VS_CONN_REL | MMTYPE_REQ));
  156. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  157. request->CID = CID;
  158. if (SendMME (plc) <= 0)
  159. {
  160. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  161. return (-1);
  162. }
  163. while (ReadMME (plc, 0, (VS_CONN_REL | MMTYPE_CNF)) > 0)
  164. {
  165. if (confirm->MSTATUS)
  166. {
  167. Failure (plc, PLC_WONTDOIT);
  168. return (-1);
  169. }
  170. Confirm (plc, "Released %04X", CID);
  171. }
  172. return (0);
  173. }
  174. /*====================================================================*
  175. *
  176. * int main (int argc, char const * argv[]);
  177. *
  178. *
  179. *--------------------------------------------------------------------*/
  180. int main (int argc, char const * argv [])
  181. {
  182. extern struct channel channel;
  183. static char const * optv [] =
  184. {
  185. "ei:qv",
  186. "cid",
  187. "CoQos Stream Utility",
  188. "e\tredirect stderr to stdout",
  189. #if defined (WINPCAP) || defined (LIBPCAP)
  190. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  191. #else
  192. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  193. #endif
  194. "q\tquiet mode",
  195. "v\tverbose mode",
  196. (char const *) (0)
  197. };
  198. #include "../plc/plc.c"
  199. uint16_t cid;
  200. signed c;
  201. if (getenv (PLCDEVICE))
  202. {
  203. #if defined (WINPCAP) || defined (LIBPCAP)
  204. channel.ifindex = atoi (getenv (PLCDEVICE));
  205. #else
  206. channel.ifname = strdup (getenv (PLCDEVICE));
  207. #endif
  208. }
  209. optind = 1;
  210. while ((c = getoptv (argc, argv, optv)) != -1)
  211. {
  212. switch (c)
  213. {
  214. case 'e':
  215. dup2 (STDOUT_FILENO, STDERR_FILENO);
  216. break;
  217. case 'i':
  218. #if defined (WINPCAP) || defined (LIBPCAP)
  219. channel.ifindex = atoi (optarg);
  220. #else
  221. channel.ifname = optarg;
  222. #endif
  223. break;
  224. case 'q':
  225. _setbits (channel.flags, CHANNEL_SILENCE);
  226. _setbits (plc.flags, PLC_SILENCE);
  227. break;
  228. case 'v':
  229. _setbits (channel.flags, CHANNEL_VERBOSE);
  230. _setbits (plc.flags, PLC_VERBOSE);
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. argc -= optind;
  237. argv += optind;
  238. if (!argc)
  239. {
  240. error (1, ECANCELED, "Missing CID");
  241. }
  242. if (!hexencode ((uint8_t *)(&cid), sizeof (cid), * argv++))
  243. {
  244. error (1, EINVAL, "CID=[%s]", *--argv);
  245. }
  246. cid = htons (cid);
  247. argc--;
  248. openchannel (&channel);
  249. if (!(plc.message = malloc (sizeof (* plc.message))))
  250. {
  251. error (1, errno, PLC_NOMEMORY);
  252. }
  253. if (!argc)
  254. {
  255. rel_conn (&plc, cid);
  256. }
  257. while ((argc) && (* argv))
  258. {
  259. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  260. {
  261. error (1, errno, PLC_BAD_MAC, * argv);
  262. }
  263. rel_conn (&plc, cid);
  264. argc--;
  265. argv++;
  266. }
  267. free (plc.message);
  268. closechannel (&channel);
  269. exit (0);
  270. }