coqos_mod.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_mod.c - Modify MCMSA stream
  44. *
  45. *
  46. * Contributor(s):
  47. * Bill Wike
  48. * Nathaniel Houghton
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. /*====================================================================*"
  53. * system header files;
  54. *--------------------------------------------------------------------*/
  55. #include <unistd.h>
  56. #include <stdlib.h>
  57. #include <stdint.h>
  58. #include <limits.h>
  59. /*====================================================================*
  60. * custom header files;
  61. *--------------------------------------------------------------------*/
  62. #include "../tools/getoptv.h"
  63. #include "../tools/putoptv.h"
  64. #include "../tools/memory.h"
  65. #include "../tools/number.h"
  66. #include "../tools/symbol.h"
  67. #include "../tools/types.h"
  68. #include "../tools/flags.h"
  69. #include "../tools/files.h"
  70. #include "../tools/error.h"
  71. #include "../plc/plc.h"
  72. /*====================================================================*
  73. * custom source files;
  74. *--------------------------------------------------------------------*/
  75. #ifndef MAKEFILE
  76. #include "../tools/getoptv.c"
  77. #include "../tools/putoptv.c"
  78. #include "../tools/version.c"
  79. #include "../tools/hexdump.c"
  80. #include "../tools/hexencode.c"
  81. #include "../tools/bytespec.c"
  82. #include "../tools/ipv4spec.c"
  83. #include "../tools/ipv6spec.c"
  84. #include "../tools/hexdecode.c"
  85. #include "../tools/todigit.c"
  86. #include "../tools/typename.c"
  87. #include "../tools/error.c"
  88. #include "../tools/synonym.c"
  89. #include "../tools/lookup.c"
  90. #include "../tools/assist.c"
  91. #include "../tools/codelist.c"
  92. #endif
  93. #ifndef MAKEFILE
  94. #include "../plc/Request.c"
  95. #include "../plc/Confirm.c"
  96. #include "../plc/Display.c"
  97. #include "../plc/Failure.c"
  98. #include "../plc/ReadMME.c"
  99. #include "../plc/SendMME.c"
  100. #include "../plc/Devices.c"
  101. #include "../mme/EthernetHeader.c"
  102. #include "../mme/QualcommHeader.c"
  103. #include "../mme/UnwantedMessage.c"
  104. #endif
  105. #ifndef MAKEFILE
  106. #include "../ether/channel.c"
  107. #include "../ether/openchannel.c"
  108. #include "../ether/closechannel.c"
  109. #include "../ether/readpacket.c"
  110. #include "../ether/sendpacket.c"
  111. #endif
  112. #ifndef MAKEFILE
  113. #include "../mme/MMECode.c"
  114. #endif
  115. /*====================================================================*
  116. * constants;
  117. *--------------------------------------------------------------------*/
  118. #define ACTION "action"
  119. /*====================================================================*
  120. * program variables;
  121. *--------------------------------------------------------------------*/
  122. #define ACTION_SPND 0x00
  123. #define ACTION_RES 0x01
  124. static struct _code_ const actions [] =
  125. {
  126. {
  127. ACTION_SPND,
  128. "suspend"
  129. },
  130. {
  131. ACTION_RES,
  132. "resume"
  133. }
  134. };
  135. /*====================================================================*
  136. *
  137. * signed mod_conn (struct plc * plc, uint8_t TYPE, uint16_t CID);
  138. *
  139. *
  140. *
  141. * Contributor(s):
  142. * Bill Wike
  143. *
  144. *--------------------------------------------------------------------*/
  145. static signed mod_conn (struct plc * plc, uint8_t TYPE, uint16_t CID)
  146. {
  147. struct channel * channel = (struct channel *)(plc->channel);
  148. struct message * message = (struct message *)(plc->message);
  149. #ifndef __GNUC__
  150. #pragma pack (push,1)
  151. #endif
  152. struct __packed vs_mod_conn_req
  153. {
  154. struct ethernet_hdr ethernet;
  155. struct qualcomm_hdr qualcomm;
  156. uint32_t REQ_ID;
  157. uint32_t RSVD;
  158. uint16_t CID;
  159. uint8_t MOD_CTRL;
  160. }
  161. * request = (struct vs_mod_conn_req *)(message);
  162. struct __packed vs_mod_conn_cnf
  163. {
  164. struct ethernet_hdr ethernet;
  165. struct qualcomm_hdr qualcomm;
  166. uint32_t REQ_ID;
  167. uint8_t MSTATUS;
  168. uint16_t ERR_REC_CODE;
  169. uint32_t RSVD;
  170. uint16_t CID;
  171. }
  172. * confirm = (struct vs_mod_conn_cnf *)(message);
  173. #ifndef __GNUC__
  174. #pragma pack (pop)
  175. #endif
  176. Request (plc, "COQOS modify connection");
  177. memset (message, 0, sizeof (* message));
  178. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  179. QualcommHeader (&request->qualcomm, 0, (VS_CONN_MOD | MMTYPE_REQ));
  180. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  181. request->MOD_CTRL = TYPE;
  182. request->CID = CID;
  183. if (SendMME (plc) <= 0)
  184. {
  185. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  186. return (-1);
  187. }
  188. while (ReadMME (plc, 0, (VS_CONN_MOD | MMTYPE_CNF)) > 0)
  189. {
  190. if (confirm->MSTATUS)
  191. {
  192. Failure (plc, PLC_WONTDOIT);
  193. return (-1);
  194. }
  195. Confirm (plc, "%s connection %04X", (TYPE == ACTION_SPND)? "Suspended": "Resumed", CID);
  196. }
  197. return (0);
  198. }
  199. /*====================================================================*
  200. *
  201. * int main (int argc, char const * argv[]);
  202. *
  203. *
  204. *--------------------------------------------------------------------*/
  205. int main (int argc, char const * argv [])
  206. {
  207. extern struct channel channel;
  208. static char const * optv [] =
  209. {
  210. "ei:qv",
  211. "spnd,res cid",
  212. "CoQos Stream Utility",
  213. "e\tredirect stderr to stdout",
  214. #if defined (WINPCAP) || defined (LIBPCAP)
  215. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  216. #else
  217. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  218. #endif
  219. "q\tquiet mode",
  220. "v\tverbose mode",
  221. (char const *) (0)
  222. };
  223. #include "../plc/plc.c"
  224. uint16_t cid;
  225. uint8_t type;
  226. signed code;
  227. signed c;
  228. if (getenv (PLCDEVICE))
  229. {
  230. #if defined (WINPCAP) || defined (LIBPCAP)
  231. channel.ifindex = atoi (getenv (PLCDEVICE));
  232. #else
  233. channel.ifname = strdup (getenv (PLCDEVICE));
  234. #endif
  235. }
  236. optind = 1;
  237. while ((c = getoptv (argc, argv, optv)) != -1)
  238. {
  239. switch (c)
  240. {
  241. case 'e':
  242. dup2 (STDOUT_FILENO, STDERR_FILENO);
  243. break;
  244. case 'i':
  245. #if defined (WINPCAP) || defined (LIBPCAP)
  246. channel.ifindex = atoi (optarg);
  247. #else
  248. channel.ifname = optarg;
  249. #endif
  250. break;
  251. case 'q':
  252. _setbits (channel.flags, CHANNEL_SILENCE);
  253. _setbits (plc.flags, PLC_SILENCE);
  254. break;
  255. case 'v':
  256. _setbits (channel.flags, CHANNEL_VERBOSE);
  257. _setbits (plc.flags, PLC_VERBOSE);
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. argc -= optind;
  264. argv += optind;
  265. if ((code = lookup (* argv++, actions, SIZEOF (actions))) == -1)
  266. {
  267. assist (*--argv, ACTION, actions, SIZEOF (actions));
  268. }
  269. type = (uint8_t)(code);
  270. argc--;
  271. if (argc == 0)
  272. {
  273. error (1, ECANCELED, "Missing CID");
  274. }
  275. if (!hexencode ((uint8_t *)(&cid), sizeof (cid), * argv++))
  276. {
  277. error (1, errno, "Invalid CID");
  278. }
  279. cid = htons (cid);
  280. argc--;
  281. openchannel (&channel);
  282. if (!(plc.message = malloc (sizeof (* plc.message))))
  283. {
  284. error (1, errno, PLC_NOMEMORY);
  285. }
  286. if (!argc)
  287. {
  288. mod_conn (&plc, type, cid);
  289. }
  290. while ((argc) && (* argv))
  291. {
  292. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  293. {
  294. error (1, errno, PLC_BAD_MAC, * argv);
  295. }
  296. mod_conn (&plc, type, cid);
  297. argc--;
  298. argv++;
  299. }
  300. free (plc.message);
  301. closechannel (&channel);
  302. exit (0);
  303. }