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