Simple Client TCP for FRDM

Dependencies:   EthernetInterface-FRDM mbed-rtos mbed

Fork of CyaSSL-EchoClient by wolf SSL

Committer:
wolfSSL
Date:
Sat Feb 07 11:37:42 2015 +0000
Revision:
4:3cd5b93387c7
Parent:
2:e28e0426c0b0
Child:
5:b74ac4acd827
code Cleaned

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:0c584b87ea42 1 #include "mbed.h"
wolfSSL 0:0c584b87ea42 2 #include "EthernetInterface.h"
wolfSSL 0:0c584b87ea42 3
wolfSSL 1:ac91b4f8d818 4 /*** SSL #include <cyassl/ssl.h> ***/
wolfSSL 0:0c584b87ea42 5
wolfSSL 1:ac91b4f8d818 6 const int PORT = 80 /*** SSL 443 end SSL ***/ ;
wolfSSL 0:0c584b87ea42 7
wolfSSL 1:ac91b4f8d818 8 #define err_sys(m) { puts(m) ; }
wolfSSL 0:0c584b87ea42 9
wolfSSL 0:0c584b87ea42 10 TCPSocketConnection socket;
wolfSSL 0:0c584b87ea42 11
wolfSSL 1:ac91b4f8d818 12 /*** SSL
wolfSSL 0:0c584b87ea42 13 static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx)
wolfSSL 0:0c584b87ea42 14 {
wolfSSL 0:0c584b87ea42 15 int n ;
wolfSSL 0:0c584b87ea42 16 int i ;
wolfSSL 0:0c584b87ea42 17 #define RECV_RETRY 3
wolfSSL 0:0c584b87ea42 18 for(i=0; i<RECV_RETRY; i++) {
wolfSSL 0:0c584b87ea42 19 n = socket.receive(buf, sz) ;
wolfSSL 0:0c584b87ea42 20 if(n >= 0)return n ;
wolfSSL 0:0c584b87ea42 21 }
wolfSSL 0:0c584b87ea42 22 return n ;
wolfSSL 0:0c584b87ea42 23 }
wolfSSL 0:0c584b87ea42 24
wolfSSL 0:0c584b87ea42 25 static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx)
wolfSSL 0:0c584b87ea42 26 {
wolfSSL 0:0c584b87ea42 27 int n ;
wolfSSL 0:0c584b87ea42 28
wolfSSL 0:0c584b87ea42 29 n = socket.send(buf, sz);
wolfSSL 0:0c584b87ea42 30 if(n > 0) {
wolfSSL 0:0c584b87ea42 31 return n ;
wolfSSL 4:3cd5b93387c7 32 }
wolfSSL 0:0c584b87ea42 33 return n ;
wolfSSL 0:0c584b87ea42 34 }
wolfSSL 1:ac91b4f8d818 35 end SSL ***/
wolfSSL 0:0c584b87ea42 36
wolfSSL 1:ac91b4f8d818 37 EthernetInterface eth;
wolfSSL 1:ac91b4f8d818 38
wolfSSL 1:ac91b4f8d818 39 void net_main(void const *av)
wolfSSL 0:0c584b87ea42 40 {
wolfSSL 1:ac91b4f8d818 41 char server_ip[20] ;
wolfSSL 1:ac91b4f8d818 42 /*** SSL
wolfSSL 0:0c584b87ea42 43 CYASSL_CTX* ctx = 0;
wolfSSL 0:0c584b87ea42 44 CYASSL* ssl = 0;
wolfSSL 0:0c584b87ea42 45
wolfSSL 0:0c584b87ea42 46 CYASSL_METHOD* method = CyaTLSv1_2_client_method();
wolfSSL 1:ac91b4f8d818 47 end SSL ***/
wolfSSL 0:0c584b87ea42 48
wolfSSL 0:0c584b87ea42 49 /* Initialize CyaSSL Context */
wolfSSL 1:ac91b4f8d818 50 /*** SSL
wolfSSL 0:0c584b87ea42 51 ctx = CyaSSL_CTX_new(method);
wolfSSL 0:0c584b87ea42 52 if (ctx == NULL)
wolfSSL 0:0c584b87ea42 53 err_sys("unable to get ctx");
wolfSSL 0:0c584b87ea42 54 CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
wolfSSL 0:0c584b87ea42 55 CyaSSL_SetIORecv(ctx, SocketReceive) ;
wolfSSL 0:0c584b87ea42 56 CyaSSL_SetIOSend(ctx, SocketSend) ;
wolfSSL 1:ac91b4f8d818 57 end SSL ***/
wolfSSL 0:0c584b87ea42 58
wolfSSL 0:0c584b87ea42 59 socket.set_blocking(false, 300) ;
wolfSSL 1:ac91b4f8d818 60 printf("Server IP: ") ;
wolfSSL 1:ac91b4f8d818 61 scanf("%s", server_ip) ;
wolfSSL 1:ac91b4f8d818 62 puts(server_ip) ;
wolfSSL 1:ac91b4f8d818 63
wolfSSL 1:ac91b4f8d818 64 while (socket.connect(server_ip, PORT) < 0) {
wolfSSL 1:ac91b4f8d818 65 printf("Unable to connect to (%s) on port (%d)\n", server_ip, PORT);
wolfSSL 0:0c584b87ea42 66 wait(1);
wolfSSL 0:0c584b87ea42 67 }
wolfSSL 0:0c584b87ea42 68 printf("TCP Connected\n") ;
wolfSSL 0:0c584b87ea42 69
wolfSSL 1:ac91b4f8d818 70 /*** SSL
wolfSSL 0:0c584b87ea42 71 ssl = CyaSSL_new(ctx);
wolfSSL 0:0c584b87ea42 72 if (ssl == NULL)
wolfSSL 0:0c584b87ea42 73 err_sys("unable to get SSL object");
wolfSSL 0:0c584b87ea42 74 if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
wolfSSL 0:0c584b87ea42 75 int err = CyaSSL_get_error(ssl, 0);
wolfSSL 0:0c584b87ea42 76 printf("err = %d, %s\n", err,
wolfSSL 1:ac91b4f8d818 77 CyaSSL_ERR_error_string(err, "\n"));
wolfSSL 0:0c584b87ea42 78 err_sys("SSL Connection Error");
wolfSSL 0:0c584b87ea42 79 }
wolfSSL 0:0c584b87ea42 80 printf("SSL Connected\n") ;
wolfSSL 1:ac91b4f8d818 81 ***/
wolfSSL 0:0c584b87ea42 82
wolfSSL 2:e28e0426c0b0 83 char msg[] = "GET / HTTP/1.0\r\n\r\n" ;
wolfSSL 0:0c584b87ea42 84
wolfSSL 1:ac91b4f8d818 85 if (/*** SSL CyaSSL_write(ssl, end SSL***/
wolfSSL 1:ac91b4f8d818 86 socket.send(msg, sizeof(msg)-1) != (sizeof(msg)-1))
wolfSSL 0:0c584b87ea42 87 err_sys("CyaSSL_write failed");
wolfSSL 2:e28e0426c0b0 88 printf("Client Request: \n%s\n",msg) ;
wolfSSL 0:0c584b87ea42 89 char buf[1024];
wolfSSL 0:0c584b87ea42 90 int n ;
wolfSSL 1:ac91b4f8d818 91 puts("Server Response:\n") ;
wolfSSL 1:ac91b4f8d818 92 do {
wolfSSL 1:ac91b4f8d818 93 n = /*** SSL CyaSSL_read(ssl, end SSL ***/
wolfSSL 1:ac91b4f8d818 94 socket.receive(buf, sizeof(buf)-1);
wolfSSL 4:3cd5b93387c7 95 if (n > 0) {
wolfSSL 1:ac91b4f8d818 96 buf[n] = 0;
wolfSSL 1:ac91b4f8d818 97 printf("%s", buf);
wolfSSL 1:ac91b4f8d818 98 } else
wolfSSL 4:3cd5b93387c7 99 break ;
wolfSSL 1:ac91b4f8d818 100 } while(n > 0) ;
wolfSSL 1:ac91b4f8d818 101 puts("=== === === ===") ;
wolfSSL 1:ac91b4f8d818 102 /*** SSL CyaSSL_free(ssl) ; ***/
wolfSSL 1:ac91b4f8d818 103 socket.close();
wolfSSL 1:ac91b4f8d818 104 /*** SSL CyaSSL_CTX_free(ctx) ; ***/
wolfSSL 1:ac91b4f8d818 105 eth.disconnect();
wolfSSL 1:ac91b4f8d818 106 }
wolfSSL 1:ac91b4f8d818 107
wolfSSL 0:0c584b87ea42 108
wolfSSL 1:ac91b4f8d818 109 main()
wolfSSL 1:ac91b4f8d818 110 {
wolfSSL 1:ac91b4f8d818 111
wolfSSL 1:ac91b4f8d818 112 printf("===== Simple TCP Client ========\n") ;
wolfSSL 1:ac91b4f8d818 113 /* CyaSSL_Debugging_ON() ; */
wolfSSL 0:0c584b87ea42 114
wolfSSL 1:ac91b4f8d818 115 eth.init(); //Use DHCP
wolfSSL 1:ac91b4f8d818 116 eth.connect();
wolfSSL 1:ac91b4f8d818 117 printf("Client IP: %s\n", eth.getIPAddress());
wolfSSL 1:ac91b4f8d818 118
wolfSSL 1:ac91b4f8d818 119 #define STACK_SIZE 12000
wolfSSL 1:ac91b4f8d818 120 Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE);
wolfSSL 1:ac91b4f8d818 121
wolfSSL 1:ac91b4f8d818 122 while (true) {
wolfSSL 1:ac91b4f8d818 123 Thread::wait(1000);
wolfSSL 1:ac91b4f8d818 124 }
wolfSSL 1:ac91b4f8d818 125 }