PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mqx/cyassl_client/Sources/main.c

https://github.com/andersmalm/cyassl
C | 239 lines | 166 code | 47 blank | 26 comment | 31 complexity | 33f560523866e14822e8d5c1a6b30035 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * main.c
  3. */
  4. #include "main.h"
  5. #include "util.h"
  6. #if !BSPCFG_ENABLE_IO_SUBSYSTEM
  7. #error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined \
  8. non-zero in user_config.h. Please recompile BSP with this option.
  9. #endif
  10. #ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
  11. #error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. \
  12. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in \
  13. user_config.h and recompile BSP with this option.
  14. #endif
  15. #if defined BSP_SDCARD_ESDHC_CHANNEL
  16. #if ! BSPCFG_ENABLE_ESDHC
  17. #error This application requires BSPCFG_ENABLE_ESDHC defined non-zero in \
  18. user_config.h. Please recompile libraries with this option.
  19. #endif
  20. #elif defined BSP_SDCARD_SDHC_CHANNEL
  21. #if ! BSPCFG_ENABLE_SDHC
  22. #error This application requires BSPCFG_ENABLE_SDHC defined non-zero in \
  23. user_config.h. Please recompile libraries with this option.
  24. #endif
  25. #endif
  26. #if defined (BSP_SDCARD_SPI_CHANNEL)
  27. #define SDCARD_COM_CHANNEL BSP_SDCARD_SPI_CHANNEL
  28. #elif defined (BSP_SDCARD_ESDHC_CHANNEL)
  29. #define SDCARD_COM_CHANNEL BSP_SDCARD_ESDHC_CHANNEL
  30. #elif defined (BSP_SDCARD_SDHC_CHANNEL)
  31. #define SDCARD_COM_CHANNEL BSP_SDCARD_SDHC_CHANNEL
  32. #else
  33. #error "SDCARD low level communication device not defined!"
  34. #endif
  35. TASK_TEMPLATE_STRUCT MQX_template_list[] =
  36. {
  37. /* Task number, Entry point, Stack, Pri, String, Auto? */
  38. {MAIN_TASK, Main_task, 20000, 9, "main", MQX_AUTO_START_TASK},
  39. {0, 0, 0, 0, 0, 0, }
  40. };
  41. /*TASK*-----------------------------------------------------
  42. *
  43. * Task Name : Main_task
  44. * Comments :
  45. * This task sets up the SD card and Ethernet devices,
  46. * then starts the example CyaSSL client. The example
  47. * CyaSSL client connects to a server over SSL and sends
  48. * a simple HTTP GET message, then prints out the reply
  49. * from the server.
  50. *
  51. * To change the IP address and port of the server,
  52. * change the yasslIP and yasslPort variables in
  53. * client_test(). Note that yasslIP needs to be given
  54. * in hexadecimal.
  55. *
  56. *END*-----------------------------------------------------*/
  57. void Main_task(uint_32 initial_data)
  58. {
  59. int ret = 0;
  60. _mqx_int error_code, bytes;
  61. _mqx_uint param;
  62. _mqx_uint sz;
  63. MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partman_handle;
  64. MQX_FILE_PTR cert_file = NULL;
  65. char filesystem_name[] = "a:";
  66. char partman_name[] = "pm:";
  67. const char* fileName = "a:\certs\\client-key.der";
  68. printf("Starting client example... \n");
  69. ret = sdcard_open(&com_handle, &sdcard_handle, &partman_handle,
  70. &filesystem_handle, partman_name, filesystem_name);
  71. if (ret != 0) {
  72. printf("error: sdcard_open(), ret = %d\n", ret);
  73. _mqx_exit(1);
  74. }
  75. printf("SD card installed to %s\n", filesystem_name);
  76. setup_ethernet();
  77. client_test();
  78. ret = sdcard_close(&sdcard_handle, &partman_handle, &filesystem_handle,
  79. partman_name, filesystem_name);
  80. if (ret != 0) {
  81. printf("error: sdcard_close(), ret = %d\n", ret);
  82. _mqx_exit(1);
  83. }
  84. printf("SD card uninstalled.\n");
  85. _mqx_exit(0);
  86. }
  87. void setup_ethernet(void) {
  88. int error;
  89. _enet_handle ehandle; /* for Ethernet driver */
  90. _rtcs_if_handle ihandle;
  91. _enet_address address;
  92. error = RTCS_create();
  93. if (error) {
  94. err_sys("failed to create RTCS");
  95. }
  96. ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, address);
  97. /* Set up the Ethernet driver */
  98. error = ENET_initialize(BSP_DEFAULT_ENET_DEVICE, address, 0, &ehandle);
  99. if (error)
  100. err_sys("failed to initialize Ethernet driver");
  101. error = RTCS_if_add(ehandle, RTCS_IF_ENET, &ihandle);
  102. if (error)
  103. err_sys("failed to add interface for Ethernet");
  104. error = RTCS_if_bind(ihandle, ENET_IPADDR, ENET_IPMASK);
  105. if (error)
  106. err_sys("failed to bind interface for Ethernet");
  107. #ifdef GATE_IPADDR
  108. RTCS_gate_add(GATE_IPADDR, INADDR_ANY, INADDR_ANY);
  109. #endif
  110. printf("Ethernet device %d bound to %X\n", BSP_DEFAULT_ENET_DEVICE,
  111. ENET_IPADDR);
  112. }
  113. void client_test(void) {
  114. char msg[64];
  115. char reply[1024];
  116. int sockfd, input;
  117. int ret = 0, msgSz = 0;
  118. struct sockaddr_in servaddr;
  119. CYASSL_CTX* ctx;
  120. CYASSL* ssl;
  121. long yasslIP = 0xa9fea662; /* 169.254.166.98 */
  122. long yasslPort = 11111;
  123. CyaSSL_Debugging_ON();
  124. CyaSSL_Init();
  125. ctx = CyaSSL_CTX_new(CyaSSLv3_client_method());
  126. if (ctx == 0)
  127. err_sys("setting up ctx");
  128. ret = CyaSSL_CTX_use_certificate_file(ctx, clientCert, SSL_FILETYPE_PEM);
  129. if (ret != SSL_SUCCESS) {
  130. err_sys("can't load client cert file, check file");
  131. }
  132. ret = CyaSSL_CTX_use_PrivateKey_file(ctx, clientKey, SSL_FILETYPE_PEM);
  133. if (ret != SSL_SUCCESS) {
  134. err_sys("can't load client key file, check file");
  135. }
  136. ret = CyaSSL_CTX_load_verify_locations(ctx, caCert, 0);
  137. if (ret != SSL_SUCCESS) {
  138. err_sys("can't load CA cert file, check file");
  139. }
  140. /* create socket descriptor */
  141. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  142. if (sockfd == RTCS_SOCKET_ERROR) {
  143. err_sys("socket creation failed");
  144. } else {
  145. printf("socket created successfully\n");
  146. }
  147. /* Unlike most TCP/IP stacks, RTCS requires that sin_port and
  148. * sin_addr needs to be in Host Byte Order, not Network Byte Order.
  149. * This means we shouldn't use htons() when setting these values. */
  150. memset((char*)&servaddr, 0, sizeof(servaddr));
  151. servaddr.sin_family = AF_INET;
  152. servaddr.sin_port = yasslPort;
  153. servaddr.sin_addr.s_addr = yasslIP;
  154. ret = connect(sockfd, &servaddr, sizeof(servaddr));
  155. if (ret != RTCS_OK) {
  156. err_sys("connect() failed");
  157. } else {
  158. printf("Connected to %lx, port %d.\n", servaddr.sin_addr.s_addr,
  159. servaddr.sin_port);
  160. }
  161. if( (ssl = CyaSSL_new(ctx)) == NULL) {
  162. err_sys("CyaSSL_new failed");
  163. }
  164. CyaSSL_set_fd(ssl, sockfd);
  165. ret = CyaSSL_connect(ssl);
  166. if (ret != SSL_SUCCESS)
  167. err_sys("CyaSSL_connect failed");
  168. printf("CyaSSL_connect() ok, sending GET...\n");
  169. msgSz = 28;
  170. strncpy(msg, "GET /index.html HTTP/1.0\r\n\r\n", msgSz);
  171. if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
  172. err_sys("CyaSSL_write() failed");
  173. input = CyaSSL_read(ssl, reply, sizeof(reply));
  174. if (input > 0) {
  175. reply[input] = 0;
  176. printf("Server response: %s\n", reply);
  177. while(1) {
  178. input = CyaSSL_read(ssl, reply, sizeof(reply));
  179. if (input > 0) {
  180. reply[input] = 0;
  181. printf("%s\n", reply);
  182. } else {
  183. break;
  184. }
  185. }
  186. }
  187. CyaSSL_shutdown(ssl);
  188. CyaSSL_free(ssl);
  189. CyaSSL_CTX_free(ctx);
  190. CyaSSL_Cleanup();
  191. }
  192. /* EOF */