]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_WolfSSL_Windows_Simulator/SecureTCPClientTask.c
Update FreeRTOS+ version number ready for version 9 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_WolfSSL_Windows_Simulator / SecureTCPClientTask.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 #pragma comment( lib, "ws2_32.lib" )\r
71 \r
72 /* Win32 includes. */\r
73 #include <WinSock2.h>\r
74 \r
75 /* wolfSSL includes. */\r
76 #include "wolfssl/ssl.h"\r
77 \r
78 /* Standard includes. */\r
79 #include <stdint.h>\r
80 #include <stdio.h>\r
81 \r
82 /* FreeRTOS includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* The wolfSSL context for the client. */\r
89 static WOLFSSL_CTX* xWolfSSL_ClientContext = NULL;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* See the comments at the top of main.c. */\r
94 void vSecureTCPClientTask( void *pvParameters )\r
95 {\r
96 SOCKET xClientSocket;\r
97 struct sockaddr_in xConnection;\r
98 WOLFSSL* xWolfSSL_Object;\r
99 WORD wVersionRequested;\r
100 WSADATA xWSAData;\r
101 char cString[ 50 ];\r
102 BaseType_t lReturned;\r
103 uint32_t ulCount = 0UL;\r
104 \r
105         /* Remove compiler warning about unused parameters. */\r
106         ( void ) pvParameters;\r
107 \r
108         /* Prepare to use WinSock. */\r
109         wVersionRequested = MAKEWORD( 2, 2 );\r
110         configASSERT( WSAStartup( wVersionRequested, &xWSAData ) == 0 );\r
111 \r
112         /* Set family and port for client socket. */\r
113         memset( ( void * ) &xConnection, 0x00, sizeof( struct sockaddr_in ) );\r
114         xConnection.sin_family = AF_INET;\r
115         xConnection.sin_addr.s_addr = inet_addr("127.0.0.1");\r
116         xConnection.sin_port = htons( configTCP_PORT_NUMBER );\r
117 \r
118     /* Attempt to create a context that uses the TLS 1.2 server protocol. */\r
119     xWolfSSL_ClientContext = wolfSSL_CTX_new( wolfTLSv1_2_client_method() );\r
120         configASSERT( xWolfSSL_ClientContext );\r
121 \r
122     /* Load the CA certificate. */\r
123     lReturned = wolfSSL_CTX_load_verify_locations( xWolfSSL_ClientContext, "ca-cert.pem", 0 );\r
124         configASSERT( lReturned == SSL_SUCCESS );\r
125 \r
126         for( ;; )\r
127         {\r
128                 /* Create the socket. */\r
129                 xClientSocket = socket( AF_INET, SOCK_STREAM, 0 );\r
130                 configASSERT( xClientSocket != INVALID_SOCKET );\r
131 \r
132                 /* Connect to the secure server. */\r
133                 if( connect( xClientSocket, ( SOCKADDR * ) &xConnection, sizeof( xConnection ) ) == 0 )\r
134                 {\r
135                         /* The connect was successful.  Create a wolfSSL object to associate\r
136                         with this connection. */\r
137                         xWolfSSL_Object = wolfSSL_new( xWolfSSL_ClientContext );\r
138 \r
139                         if( xWolfSSL_Object != NULL )\r
140                         {\r
141                                 /* Associate the created wolfSSL object with the connected\r
142                                 socket. */\r
143                                 lReturned = wolfSSL_set_fd( xWolfSSL_Object, xClientSocket );\r
144                                 configASSERT( lReturned == SSL_SUCCESS );\r
145 \r
146                                 /* The count is used to differentiate between messages sent to\r
147                                 the server, and to break out of the do while loop below. */\r
148                                 ulCount = 0UL;\r
149 \r
150                                 do\r
151                                 {\r
152                                         /* Create the string that is sent to the secure server. */\r
153                                         sprintf( cString, "Message number %lu\r\n", ulCount );\r
154 \r
155                                         /* The next line is the secure equivalent of the standard\r
156                                         sockets call:\r
157                                         lReturned = send( xClientSocket, cString, strlen( cString ) + 1, 0 ); */\r
158                                         lReturned = wolfSSL_write( xWolfSSL_Object, cString, strlen( cString ) + 1 );\r
159 \r
160 \r
161                                         /* Short delay to prevent the messages streaming up the\r
162                                         console too quickly. */\r
163                                         vTaskDelay( 50 );\r
164                                         ulCount++;\r
165 \r
166                                 } while( ( lReturned != SOCKET_ERROR ) && ( ulCount < 10UL ) );\r
167                         }\r
168 \r
169                         wolfSSL_free( xWolfSSL_Object );\r
170                         closesocket( xClientSocket );\r
171 \r
172                         /* Delay for a short time before starting over. */\r
173                         vTaskDelay( 250 );\r
174                 }\r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r