wolf SSL / Mbed 2 deprecated SimpleClient-SSL-FRDM

Dependencies:   CyaSSL EthernetInterface-FRDM mbed-rtos mbed

Fork of CyaSSL-EchoClient by wolf SSL

Revision:
1:0b78d1071fee
Parent:
0:0c584b87ea42
Child:
2:dc88e0c4270e
diff -r 0c584b87ea42 -r 0b78d1071fee main.cpp
--- a/main.cpp	Sun May 25 06:47:15 2014 +0000
+++ b/main.cpp	Wed Oct 08 05:04:09 2014 +0000
@@ -3,10 +3,9 @@
 
 #include <cyassl/ssl.h>
 
-const char* ECHO_SERVER_ADDRESS = "192.168.137.1";
-const int ECHO_SERVER_PORT = 443;
+const int PORT = 443 ;
 
-#define err_sys(m) { puts(m) ; return -1 ; }
+#define err_sys(m) { puts(m) ; }
 
 TCPSocketConnection socket;
 
@@ -34,20 +33,15 @@
     return n ;
 }
 
-int main()
+EthernetInterface eth;
+
+void net_main(void const *av)
 {
+    char server_ip[20] ;
     CYASSL_CTX*     ctx     = 0;
     CYASSL*         ssl     = 0;
 
     CYASSL_METHOD* method = CyaTLSv1_2_client_method();
-    EthernetInterface eth;
-
-    printf("===== ECHO CLIENT ========\n") ;
-    /* CyaSSL_Debugging_ON() ; */
-
-    eth.init(); //Use DHCP
-    eth.connect();
-    printf("Client IP: %s\n", eth.getIPAddress());
 
     /* Initialize CyaSSL Context */
     ctx = CyaSSL_CTX_new(method);
@@ -58,8 +52,11 @@
     CyaSSL_SetIOSend(ctx, SocketSend) ;
 
     socket.set_blocking(false, 300) ;
-    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+    printf("Server IP: ") ;
+    scanf("%s", server_ip) ; 
+    puts(server_ip) ;
+    while (socket.connect(server_ip, PORT) < 0) {
+        printf("Unable to connect to (%s) on port (%d)\n", server_ip, PORT);
         wait(1);
     }
     printf("TCP Connected\n") ;
@@ -70,33 +67,54 @@
     if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
         int  err = CyaSSL_get_error(ssl, 0);
         printf("err = %d, %s\n", err,
-               CyaSSL_ERR_error_string(err, "\n" /*buffer*/));
+               CyaSSL_ERR_error_string(err, "\n"));
         err_sys("SSL Connection Error");
     }
     printf("SSL Connected\n") ;
 
-    const char msg[] = "GET / HTTP/1.0\r\n\r\n" ;
+    char msg[] = "GET / HTTP/1.0\r\nConnection: Close\r\n\r\n" ;
     // const char msg[] = "Hello World\r\n" ;
 
-    if (CyaSSL_write(ssl, msg, sizeof(msg)-1) != (sizeof(msg)-1))
+    if (CyaSSL_write(ssl, 
+        /* socket.send(*/ msg, sizeof(msg)-1) != (sizeof(msg)-1))
         err_sys("CyaSSL_write failed");
 
     char buf[1024];
     int n ;
+    puts("Server Response:\n") ;
+    do {
+        n = CyaSSL_read(ssl, 
+            /* socket.receive(*/ buf, sizeof(buf)-1);
+        if (n >= 0) {
 
-    n = CyaSSL_read(ssl, buf, sizeof(buf)-1);
-    if (n > 0) {
-        puts("Server Response") ;
-        buf[n] = 0;
-        puts(buf);
-        puts("=== === === ===") ;
-    } else
-        err_sys("CyaSSL_read failed");
+            buf[n] = 0;
+            //printf("%d,", n) ;
+            printf("%s", buf);
+        } else
+            err_sys("CyaSSL_read failed");
+    } while(n > 0) ;
+    puts("=== === === ===") ;
+    CyaSSL_free(ssl) ; 
+    socket.close();
+    CyaSSL_CTX_free(ctx) ; 
+    eth.disconnect();
+}
+
 
-    CyaSSL_free(ssl) ;
-    socket.close();
+main()
+{
+
+    printf("===== Simple SSL CLIENT ========\n") ;
+    /* CyaSSL_Debugging_ON() ; */
 
-    CyaSSL_CTX_free(ctx) ;
-    eth.disconnect();
-    while(true) {}
-}
+    eth.init(); //Use DHCP
+    eth.connect();
+    printf("Client IP: %s\n", eth.getIPAddress());
+
+#define STACK_SIZE 12000
+    Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE);
+
+    while (true) {
+        Thread::wait(1000);
+    }
+}
\ No newline at end of file