Subversion Repositories Kolibri OS

Rev

Rev 8774 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *  TCP/IP or UDP/IP networking functions
  3.  *
  4.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5.  *  SPDX-License-Identifier: GPL-2.0
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  *  This file is part of mbed TLS (https://tls.mbed.org)
  22.  */
  23.  
  24. /* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
  25.  * be set before config.h, which pulls in glibc's features.h indirectly.
  26.  * Harmless on other platforms. */
  27. #define _POSIX_C_SOURCE 200112L
  28.  
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "mbedtls/config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34.  
  35. #if defined(MBEDTLS_NET_C)
  36.  
  37. #if defined(MBEDTLS_PLATFORM_C)
  38. #include "mbedtls/platform.h"
  39. #else
  40. #include <stdlib.h>
  41. #endif
  42.  
  43. #include "mbedtls/net_sockets.h"
  44.  
  45. #include <string.h>
  46.  
  47. #include <sys/socket.h>
  48. #include <clayer/network.h>
  49. /*
  50. #include <netinet/in.h>
  51. #include <arpa/inet.h>
  52. #include <sys/time.h>
  53. #include <unistd.h>
  54. #include <signal.h>
  55. #include <fcntl.h>
  56. #include <netdb.h>
  57. #include <errno.h>
  58. */
  59.  
  60. #define IS_EINTR( ret ) ( ( ret ) == EINTR )
  61.  
  62. #include <stdio.h>
  63. //#include <time.h>
  64. #include <stdint.h>
  65.  
  66. /*
  67.  * Prepare for using the sockets interface
  68.  */
  69. static int net_prepare( void )
  70. {
  71.         //load_network_obj();
  72.     return( 0 );
  73. }
  74.  
  75.  
  76. /*
  77.  * Initialize a context
  78.  */
  79. void mbedtls_net_init( mbedtls_net_context *ctx )
  80. {
  81.     //printf("snprintf=%p\n", printf);
  82.     ctx->fd = -1;
  83. }
  84.  
  85. /*
  86.  * Initiate a TCP connection with host:port and the given protocol
  87.  */
  88. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
  89.                          const char *port, int proto )
  90. {
  91.     int ret;
  92.     struct addrinfo hints, *addr_list, *cur;
  93.  
  94.     if( ( ret = net_prepare() ) != 0 )
  95.         return( ret );
  96.  
  97.     /* Do name resolution with both IPv6 and IPv4 */
  98.     memset( &hints, 0, sizeof( hints ) );
  99.     hints.ai_family = AF_UNSPEC;
  100.     hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  101.     hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  102.  
  103.     if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
  104.         return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
  105.  
  106.     /* Try the sockaddrs until a connection succeeds */
  107.     ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  108.     for( cur = addr_list; cur != NULL; cur = cur->ai_next )
  109.     {
  110.         ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
  111.                             cur->ai_protocol );
  112.         if( ctx->fd < 0 )
  113.         {
  114.             ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  115.             continue;
  116.         }
  117.  
  118.         if( connect( ctx->fd, cur->ai_addr, (int) cur->ai_addrlen ) == 0 )
  119.         {
  120.             ret = 0;
  121.             break;
  122.         }
  123.  
  124.         close( ctx->fd );
  125.         ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  126.     }
  127.  
  128.     freeaddrinfo( addr_list );
  129.  
  130.     return( ret );
  131. }
  132.  
  133. /**************************/
  134.  
  135. /*
  136.  * Read at most 'len' characters
  137.  */
  138. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
  139. {
  140.     int ret;
  141.     int fd = ((mbedtls_net_context *) ctx)->fd;
  142.  
  143.     if( fd < 0 )
  144.         return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  145.  
  146.     ret = (int) recv( fd, (char*)( buf ), (int)( len ), 0 );
  147.  
  148.     if( ret < 0 )
  149.     {
  150.         /*if( net_would_block( ctx ) != 0 )
  151.             return( MBEDTLS_ERR_SSL_WANT_READ );
  152.  
  153.  
  154.         if( errno == EPIPE || errno == ECONNRESET )
  155.             return( MBEDTLS_ERR_NET_CONN_RESET );
  156.  
  157.         if( errno == EINTR )
  158.             return( MBEDTLS_ERR_SSL_WANT_READ );
  159.             */
  160.  
  161.         return( MBEDTLS_ERR_NET_RECV_FAILED );
  162.     }
  163.  
  164.     return( ret );
  165. }
  166.  
  167. /*******/
  168.  
  169. /*
  170.  * Write at most 'len' characters
  171.  */
  172. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
  173. {
  174.     int ret;
  175.     int fd = ((mbedtls_net_context *) ctx)->fd;
  176.  
  177.     if( fd < 0 )
  178.         return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  179.  
  180.     ret = (int) send( fd, (char*)( buf ), (int)( len ), 0 );
  181.  
  182.     if( ret < 0 )
  183.     {
  184.         /*if( net_would_block( ctx ) != 0 )
  185.             return( MBEDTLS_ERR_SSL_WANT_WRITE );
  186.  
  187.  
  188.         if( errno == EPIPE || errno == ECONNRESET )
  189.             return( MBEDTLS_ERR_NET_CONN_RESET );
  190.  
  191.         if( errno == EINTR )
  192.             return( MBEDTLS_ERR_SSL_WANT_WRITE );
  193.             */
  194.  
  195.         return( MBEDTLS_ERR_NET_SEND_FAILED );
  196.     }
  197.  
  198.     return( ret );
  199. }
  200.  
  201. /*
  202.  * Gracefully close the connection
  203.  */
  204. void mbedtls_net_free( mbedtls_net_context *ctx )
  205. {
  206.     if( ctx->fd == -1 )
  207.         return;
  208.  
  209.     //shutdown( ctx->fd, 2 );
  210.     close( ctx->fd );
  211.  
  212.     ctx->fd = -1;
  213. }
  214.  
  215. #endif /* MBEDTLS_NET_C */
  216.