ssl_access (http->https)

Dependencies:   CyaSSL EthernetInterface mbed-rtos mbed

Fork of ssl_access by shinichi satoh

Revision:
1:ac91b4f8d818
Parent:
0:0c584b87ea42
Child:
2:29a1370416cb
--- a/main.cpp	Sun May 25 06:47:15 2014 +0000
+++ b/main.cpp	Wed Oct 08 05:04:36 2014 +0000
@@ -1,15 +1,15 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
-#include <cyassl/ssl.h>
+/*** SSL #include <cyassl/ssl.h> ***/
 
-const char* ECHO_SERVER_ADDRESS = "192.168.137.1";
-const int ECHO_SERVER_PORT = 443;
+const int PORT = 80 /*** SSL 443 end SSL ***/ ;
 
-#define err_sys(m) { puts(m) ; return -1 ; }
+#define err_sys(m) { puts(m) ; }
 
 TCPSocketConnection socket;
 
+/*** SSL
 static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx)
 {
     int n ;
@@ -33,70 +33,99 @@
     } else  printf("SocketSend:%d/%d\n", n, sz);
     return n ;
 }
+end SSL ***/
 
-int main()
+EthernetInterface eth;
+
+void net_main(void const *av)
 {
+    char server_ip[20] ;
+    /*** SSL
     CYASSL_CTX*     ctx     = 0;
     CYASSL*         ssl     = 0;
 
     CYASSL_METHOD* method = CyaTLSv1_2_client_method();
-    EthernetInterface eth;
+    end SSL ***/
 
-    printf("===== ECHO CLIENT ========\n") ;
-    /* CyaSSL_Debugging_ON() ; */
 
-    eth.init(); //Use DHCP
-    eth.connect();
-    printf("Client IP: %s\n", eth.getIPAddress());
 
     /* Initialize CyaSSL Context */
+    /*** SSL
     ctx = CyaSSL_CTX_new(method);
     if (ctx == NULL)
         err_sys("unable to get ctx");
     CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
     CyaSSL_SetIORecv(ctx, SocketReceive) ;
     CyaSSL_SetIOSend(ctx, SocketSend) ;
+    end SSL ***/
 
     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") ;
 
+    /*** SSL
     ssl = CyaSSL_new(ctx);
     if (ssl == NULL)
         err_sys("unable to get SSL object");
     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 (/*** SSL   CyaSSL_write(ssl, end 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 = /*** SSL  CyaSSL_read(ssl, end 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("=== === === ===") ;
+    /*** SSL    CyaSSL_free(ssl) ; ***/
+    socket.close();
+    /*** SSL    CyaSSL_CTX_free(ctx) ; ***/
+    eth.disconnect();
+}
+
 
-    CyaSSL_free(ssl) ;
-    socket.close();
+main()
+{
+
+    printf("===== Simple TCP 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