~ubuntu-branches/debian/squeeze/polarssl/squeeze

« back to all changes in this revision

Viewing changes to library/net.c

  • Committer: Package Import Robot
  • Author(s): Roland Stigge
  • Date: 2013-10-16 20:04:47 UTC
  • mfrom: (1.3.1) (3.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20131016200447-x0hfc8ysrgkjyji2
Tags: 1.2.9-1~deb6u1
* New upstream release
  - Fixes CVE-2013-5914 CVE-2013-5915 (Closes: #725359)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  TCP networking functions
3
3
 *
4
 
 *  Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
 
4
 *  Copyright (C) 2006-2010, Brainspark B.V.
 
5
 *
 
6
 *  This file is part of PolarSSL (http://www.polarssl.org)
 
7
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
 
8
 *
5
9
 *  All rights reserved.
6
10
 *
7
 
 *  Joined copyright on original XySSL code with: Christophe Devine
8
 
 *
9
11
 *  This program is free software; you can redistribute it and/or modify
10
12
 *  it under the terms of the GNU General Public License as published by
11
13
 *  the Free Software Foundation; either version 2 of the License, or
27
29
 
28
30
#include "polarssl/net.h"
29
31
 
30
 
#if defined(WIN32) || defined(_WIN32_WCE)
 
32
#if defined(_WIN32) || defined(_WIN32_WCE)
31
33
 
32
34
#include <winsock2.h>
33
35
#include <windows.h>
38
40
#pragma comment( lib, "ws2_32.lib" )
39
41
#endif
40
42
 
41
 
#define read(fd,buf,len)        recv(fd,buf,len,0)
42
 
#define write(fd,buf,len)       send(fd,buf,len,0)
 
43
#define read(fd,buf,len)        recv(fd,(char*)buf,(int) len,0)
 
44
#define write(fd,buf,len)       send(fd,(char*)buf,(int) len,0)
43
45
#define close(fd)               closesocket(fd)
44
46
 
45
47
static int wsa_init_done = 0;
57
59
#include <netdb.h>
58
60
#include <errno.h>
59
61
 
60
 
#if defined(__FreeBSD__)
 
62
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) ||  \
 
63
    defined(__DragonflyBSD__)
61
64
#include <sys/endian.h>
62
65
#elif defined(__APPLE__)
63
66
#include <machine/endian.h>
 
67
#elif defined(sun)
 
68
#include <sys/isa_defs.h>
64
69
#else
65
70
#include <endian.h>
66
71
#endif
67
72
 
68
73
#endif
69
74
 
70
 
#include <string.h>
71
75
#include <stdlib.h>
72
76
#include <stdio.h>
73
77
#include <time.h>
74
78
 
 
79
#ifdef _MSC_VER
 
80
#include <basetsd.h>
 
81
typedef UINT32 uint32_t;
 
82
#else
 
83
#include <inttypes.h>
 
84
#endif
 
85
 
75
86
/*
76
87
 * htons() is not always available.
77
88
 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
79
90
 */
80
91
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
81
92
#define POLARSSL_HTONS(n) (n)
 
93
#define POLARSSL_HTONL(n) (n)
82
94
#else
83
 
#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
 
95
#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF      ) << 8 ) | \
 
96
                           (((unsigned short)(n) & 0xFF00    ) >> 8 ))
 
97
#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF      ) << 24) | \
 
98
                           (((unsigned long )(n) & 0xFF00    ) << 8 ) | \
 
99
                           (((unsigned long )(n) & 0xFF0000  ) >> 8 ) | \
 
100
                           (((unsigned long )(n) & 0xFF000000) >> 24))
84
101
#endif
85
102
 
86
103
unsigned short net_htons(unsigned short n);
 
104
unsigned long  net_htonl(unsigned long  n);
87
105
#define net_htons(n) POLARSSL_HTONS(n)
 
106
#define net_htonl(n) POLARSSL_HTONL(n)
88
107
 
89
108
/*
90
109
 * Initiate a TCP connection with host:port
91
110
 */
92
 
int net_connect( int *fd, char *host, int port )
 
111
int net_connect( int *fd, const char *host, int port )
93
112
{
94
113
    struct sockaddr_in server_addr;
95
114
    struct hostent *server_host;
96
115
 
97
 
#if defined(WIN32) || defined(_WIN32_WCE)
 
116
#if defined(_WIN32) || defined(_WIN32_WCE)
98
117
    WSADATA wsaData;
99
118
 
100
119
    if( wsa_init_done == 0 )
134
153
/*
135
154
 * Create a listening socket on bind_ip:port
136
155
 */
137
 
int net_bind( int *fd, char *bind_ip, int port )
 
156
int net_bind( int *fd, const char *bind_ip, int port )
138
157
{
139
158
    int n, c[4];
140
159
    struct sockaddr_in server_addr;
141
160
 
142
 
#if defined(WIN32) || defined(_WIN32_WCE)
 
161
#if defined(_WIN32) || defined(_WIN32_WCE)
143
162
    WSADATA wsaData;
144
163
 
145
164
    if( wsa_init_done == 0 )
160
179
    setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
161
180
                (const char *) &n, sizeof( n ) );
162
181
 
163
 
    server_addr.sin_addr.s_addr = INADDR_ANY;
 
182
    server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
164
183
    server_addr.sin_family      = AF_INET;
165
184
    server_addr.sin_port        = net_htons( port );
166
185
 
174
193
                break;
175
194
 
176
195
        if( n == 4 )
177
 
            server_addr.sin_addr.s_addr =
178
 
                ( (unsigned long) c[0] << 24 ) |
179
 
                ( (unsigned long) c[1] << 16 ) |
180
 
                ( (unsigned long) c[2] <<  8 ) |
181
 
                ( (unsigned long) c[3]       );
 
196
            server_addr.sin_addr.s_addr = net_htonl(
 
197
                ( (uint32_t) c[0] << 24 ) |
 
198
                ( (uint32_t) c[1] << 16 ) |
 
199
                ( (uint32_t) c[2] <<  8 ) |
 
200
                ( (uint32_t) c[3]       ) );
182
201
    }
183
202
 
184
203
    if( bind( *fd, (struct sockaddr *) &server_addr,
188
207
        return( POLARSSL_ERR_NET_BIND_FAILED );
189
208
    }
190
209
 
191
 
    if( listen( *fd, 10 ) != 0 )
 
210
    if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
192
211
    {
193
212
        close( *fd );
194
213
        return( POLARSSL_ERR_NET_LISTEN_FAILED );
202
221
 */
203
222
static int net_is_blocking( void )
204
223
{
205
 
#if defined(WIN32) || defined(_WIN32_WCE)
 
224
#if defined(_WIN32) || defined(_WIN32_WCE)
206
225
    return( WSAGetLastError() == WSAEWOULDBLOCK );
207
226
#else
208
227
    switch( errno )
226
245
{
227
246
    struct sockaddr_in client_addr;
228
247
 
229
 
#if defined(__socklen_t_defined)
 
248
#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) ||  \
 
249
    defined(_SOCKLEN_T_DECLARED)
230
250
    socklen_t n = (socklen_t) sizeof( client_addr );
231
251
#else
232
252
    int n = (int) sizeof( client_addr );
238
258
    if( *client_fd < 0 )
239
259
    {
240
260
        if( net_is_blocking() != 0 )
241
 
            return( POLARSSL_ERR_NET_TRY_AGAIN );
 
261
            return( POLARSSL_ERR_NET_WANT_READ );
242
262
 
243
263
        return( POLARSSL_ERR_NET_ACCEPT_FAILED );
244
264
    }
255
275
 */
256
276
int net_set_block( int fd )
257
277
{
258
 
#if defined(WIN32) || defined(_WIN32_WCE)
259
 
    long n = 0;
 
278
#if defined(_WIN32) || defined(_WIN32_WCE)
 
279
    u_long n = 0;
260
280
    return( ioctlsocket( fd, FIONBIO, &n ) );
261
281
#else
262
282
    return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
265
285
 
266
286
int net_set_nonblock( int fd )
267
287
{
268
 
#if defined(WIN32) || defined(_WIN32_WCE)
269
 
    long n = 1;
 
288
#if defined(_WIN32) || defined(_WIN32_WCE)
 
289
    u_long n = 1;
270
290
    return( ioctlsocket( fd, FIONBIO, &n ) );
271
291
#else
272
292
    return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
287
307
/*
288
308
 * Read at most 'len' characters
289
309
 */
290
 
int net_recv( void *ctx, unsigned char *buf, int len )
 
310
int net_recv( void *ctx, unsigned char *buf, size_t len )
291
311
292
312
    int ret = read( *((int *) ctx), buf, len );
293
313
 
294
 
    if( len > 0 && ret == 0 )
295
 
        return( POLARSSL_ERR_NET_CONN_RESET );
296
 
 
297
314
    if( ret < 0 )
298
315
    {
299
316
        if( net_is_blocking() != 0 )
300
 
            return( POLARSSL_ERR_NET_TRY_AGAIN );
 
317
            return( POLARSSL_ERR_NET_WANT_READ );
301
318
 
302
 
#if defined(WIN32) || defined(_WIN32_WCE)
 
319
#if defined(_WIN32) || defined(_WIN32_WCE)
303
320
        if( WSAGetLastError() == WSAECONNRESET )
304
321
            return( POLARSSL_ERR_NET_CONN_RESET );
305
322
#else
307
324
            return( POLARSSL_ERR_NET_CONN_RESET );
308
325
 
309
326
        if( errno == EINTR )
310
 
            return( POLARSSL_ERR_NET_TRY_AGAIN );
 
327
            return( POLARSSL_ERR_NET_WANT_READ );
311
328
#endif
312
329
 
313
330
        return( POLARSSL_ERR_NET_RECV_FAILED );
319
336
/*
320
337
 * Write at most 'len' characters
321
338
 */
322
 
int net_send( void *ctx, unsigned char *buf, int len )
 
339
int net_send( void *ctx, const unsigned char *buf, size_t len )
323
340
{
324
341
    int ret = write( *((int *) ctx), buf, len );
325
342
 
326
343
    if( ret < 0 )
327
344
    {
328
345
        if( net_is_blocking() != 0 )
329
 
            return( POLARSSL_ERR_NET_TRY_AGAIN );
 
346
            return( POLARSSL_ERR_NET_WANT_WRITE );
330
347
 
331
 
#if defined(WIN32) || defined(_WIN32_WCE)
 
348
#if defined(_WIN32) || defined(_WIN32_WCE)
332
349
        if( WSAGetLastError() == WSAECONNRESET )
333
350
            return( POLARSSL_ERR_NET_CONN_RESET );
334
351
#else
336
353
            return( POLARSSL_ERR_NET_CONN_RESET );
337
354
 
338
355
        if( errno == EINTR )
339
 
            return( POLARSSL_ERR_NET_TRY_AGAIN );
 
356
            return( POLARSSL_ERR_NET_WANT_WRITE );
340
357
#endif
341
358
 
342
359
        return( POLARSSL_ERR_NET_SEND_FAILED );