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