Subversion Repositories Kolibri OS

Rev

Go to most recent revision | 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 "kosnet/socket.h"
  48. #include "kosnet/network.h"
  49. /*#include <sys/socket.h>
  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.  * Initialize a context
  77.  */
  78. void mbedtls_net_init( mbedtls_net_context *ctx )
  79. {
  80.     ctx->fd = -1;
  81. }
  82.  
  83. /*
  84.  * Initiate a TCP connection with host:port and the given protocol
  85.  */
  86. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
  87.                          const char *port, int proto )
  88. {
  89.     int ret;
  90.     struct addrinfo hints, *addr_list, *cur;
  91.  
  92.     if( ( ret = net_prepare() ) != 0 )
  93.         return( ret );
  94.  
  95.     /* Do name resolution with both IPv6 and IPv4 */
  96.     memset( &hints, 0, sizeof( hints ) );
  97.     hints.ai_family = AF_UNSPEC;
  98.     hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  99.     hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  100.  
  101.     if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
  102.         return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
  103.  
  104.     /* Try the sockaddrs until a connection succeeds */
  105.     ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  106.     for( cur = addr_list; cur != NULL; cur = cur->ai_next )
  107.     {
  108.         ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
  109.                             cur->ai_protocol );
  110.         if( ctx->fd < 0 )
  111.         {
  112.             ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  113.             continue;
  114.         }
  115.  
  116.         if( connect( ctx->fd, cur->ai_addr, (int) cur->ai_addrlen ) == 0 )
  117.         {
  118.             ret = 0;
  119.             break;
  120.         }
  121.  
  122.         closesocket( ctx->fd );
  123.         ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  124.     }
  125.  
  126.     freeaddrinfo( addr_list );
  127.  
  128.     return( ret );
  129. }
  130.  
  131. /**************************/
  132.  
  133. /*
  134.  * Read at most 'len' characters
  135.  */
  136. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
  137. {
  138.     int ret;
  139.     int fd = ((mbedtls_net_context *) ctx)->fd;
  140.  
  141.     if( fd < 0 )
  142.         return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  143.  
  144.     ret = (int) recv( fd, (char*)( buf ), (int)( len ), 0 );
  145.  
  146.     if( ret < 0 )
  147.     {
  148.         /*if( net_would_block( ctx ) != 0 )
  149.             return( MBEDTLS_ERR_SSL_WANT_READ );
  150.  
  151.  
  152.         if( errno == EPIPE || errno == ECONNRESET )
  153.             return( MBEDTLS_ERR_NET_CONN_RESET );
  154.  
  155.         if( errno == EINTR )
  156.             return( MBEDTLS_ERR_SSL_WANT_READ );
  157.             */
  158.  
  159.         return( MBEDTLS_ERR_NET_RECV_FAILED );
  160.     }
  161.  
  162.     return( ret );
  163. }
  164.  
  165. /*******/
  166.  
  167. /*
  168.  * Write at most 'len' characters
  169.  */
  170. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
  171. {
  172.     int ret;
  173.     int fd = ((mbedtls_net_context *) ctx)->fd;
  174.  
  175.     if( fd < 0 )
  176.         return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  177.  
  178.     ret = (int) send( fd, (char*)( buf ), (int)( len ), 0 );
  179.  
  180.     if( ret < 0 )
  181.     {
  182.         /*if( net_would_block( ctx ) != 0 )
  183.             return( MBEDTLS_ERR_SSL_WANT_WRITE );
  184.  
  185.  
  186.         if( errno == EPIPE || errno == ECONNRESET )
  187.             return( MBEDTLS_ERR_NET_CONN_RESET );
  188.  
  189.         if( errno == EINTR )
  190.             return( MBEDTLS_ERR_SSL_WANT_WRITE );
  191.             */
  192.  
  193.         return( MBEDTLS_ERR_NET_SEND_FAILED );
  194.     }
  195.  
  196.     return( ret );
  197. }
  198.  
  199. /*
  200.  * Gracefully close the connection
  201.  */
  202. void mbedtls_net_free( mbedtls_net_context *ctx )
  203. {
  204.     if( ctx->fd == -1 )
  205.         return;
  206.  
  207.     //shutdown( ctx->fd, 2 );
  208.     closesocket( ctx->fd );
  209.  
  210.     ctx->fd = -1;
  211. }
  212.  
  213. #endif /* MBEDTLS_NET_C */
  214.