Include sys/time.h to declare gettimeofday().
[openssl.git] / crypto / bio / bss_dgram.c
index c21a81dc9189202948fd5179c7084dce86f7945a..9d2b78d3a285a10d4ad7135f390c0c6eac5ce306 100644 (file)
 
 #include <stdio.h>
 #include <errno.h>
+#include <sys/time.h>
 #define USE_SOCKETS
 #include "cryptlib.h"
 
 #include <openssl/bio.h>
 
+#ifdef OPENSSL_SYS_WIN32
+#include <sys/timeb.h>
+#endif
+
 #define IP_MTU      14 /* linux is lame */
 
 #ifdef WATT32
@@ -104,6 +109,8 @@ typedef struct bio_dgram_data_st
        unsigned int connected;
        unsigned int _errno;
        unsigned int mtu;
+       struct timeval hstimeoutdiff;
+       struct timeval hstimeout;
        } bio_dgram_data;
 
 BIO_METHOD *BIO_s_datagram(void)
@@ -196,6 +203,30 @@ static int dgram_read(BIO *b, char *out, int outl)
                                BIO_set_retry_read(b);
                                data->_errno = get_last_socket_error();
                                }
+                       memset(&(data->hstimeout), 0, sizeof(struct timeval));
+                       }
+               else
+                       {
+                       if (data->hstimeout.tv_sec > 0 || data->hstimeout.tv_usec > 0)
+                               {
+                               struct timeval curtime;
+#ifdef OPENSSL_SYS_WIN32
+                               struct _timeb tb;
+                               _ftime(&tb);
+                               curtime.tv_sec = (long)tb.time;
+                               curtime.tv_usec = (long)tb.millitm * 1000;
+#else
+                               gettimeofday(&curtime, NULL);
+#endif
+
+                               if (curtime.tv_sec >= data->hstimeout.tv_sec &&
+                                       curtime.tv_usec >= data->hstimeout.tv_usec)
+                                       {
+                                       data->_errno = EAGAIN;
+                                       ret = -1;
+                                       memset(&(data->hstimeout), 0, sizeof(struct timeval));
+                                       }
+                               }
                        }
                }
        return(ret);
@@ -208,9 +239,13 @@ static int dgram_write(BIO *b, const char *in, int inl)
        clear_socket_error();
 
     if ( data->connected )
-        ret=send(b->num,in,inl,0);
+        ret=writesocket(b->num,in,inl);
     else
+#if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
+        ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
+#else
         ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
+#endif
 
        BIO_clear_retry_flags(b);
        if (ret <= 0)
@@ -341,30 +376,115 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
 
         memcpy(&(data->peer), to, sizeof(struct sockaddr));
         break;
+       case BIO_CTRL_DGRAM_SET_TIMEOUT:
+               if (num > 0)
+                       {
+#ifdef OPENSSL_SYS_WIN32
+                       struct _timeb tb;
+                       _ftime(&tb);
+                       data->hstimeout.tv_sec = (long)tb.time;
+                       data->hstimeout.tv_usec = (long)tb.millitm * 1000;
+#else
+                       gettimeofday(&(data->hstimeout), NULL);
+#endif
+                       data->hstimeout.tv_sec += data->hstimeoutdiff.tv_sec;
+                       data->hstimeout.tv_usec += data->hstimeoutdiff.tv_usec;
+                       if (data->hstimeout.tv_usec >= 1000000)
+                               {
+                               data->hstimeout.tv_sec++;
+                               data->hstimeout.tv_usec -= 1000000;
+                               }
+                       }
+               else
+                       {
+                       memset(&(data->hstimeout), 0, sizeof(struct timeval));
+                       }
+               break;
+#if defined(SO_RCVTIMEO)
        case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
+#ifdef OPENSSL_SYS_WINDOWS
+               {
+               struct timeval *tv = (struct timeval *)ptr;
+               int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
+               if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
+                       (void*)&timeout, sizeof(timeout)) < 0)
+                       { perror("setsockopt"); ret = -1; }
+               }
+#else
                if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
                        sizeof(struct timeval)) < 0)
                        { perror("setsockopt"); ret = -1; }
+#endif
+               memcpy(&(data->hstimeoutdiff), ptr, sizeof(struct timeval));
                break;
        case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
+#ifdef OPENSSL_SYS_WINDOWS
+               {
+               int timeout, sz = sizeof(timeout);
+               struct timeval *tv = (struct timeval *)ptr;
+               if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
+                       (void*)&timeout, &sz) < 0)
+                       { perror("getsockopt"); ret = -1; }
+               else
+                       {
+                       tv->tv_sec = timeout / 1000;
+                       tv->tv_usec = (timeout % 1000) * 1000;
+                       ret = sizeof(*tv);
+                       }
+               }
+#else
                if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
                        ptr, (void *)&ret) < 0)
                        { perror("getsockopt"); ret = -1; }
+#endif
                break;
+#endif
+#if defined(SO_SNDTIMEO)
        case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
+#ifdef OPENSSL_SYS_WINDOWS
+               {
+               struct timeval *tv = (struct timeval *)ptr;
+               int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
+               if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
+                       (void*)&timeout, sizeof(timeout)) < 0)
+                       { perror("setsockopt"); ret = -1; }
+               }
+#else
                if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
                        sizeof(struct timeval)) < 0)
                        { perror("setsockopt"); ret = -1; }
+#endif
                break;
        case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
+#ifdef OPENSSL_SYS_WINDOWS
+               {
+               int timeout, sz = sizeof(timeout);
+               struct timeval *tv = (struct timeval *)ptr;
+               if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
+                       (void*)&timeout, &sz) < 0)
+                       { perror("getsockopt"); ret = -1; }
+               else
+                       {
+                       tv->tv_sec = timeout / 1000;
+                       tv->tv_usec = (timeout % 1000) * 1000;
+                       ret = sizeof(*tv);
+                       }
+               }
+#else
                if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 
                        ptr, (void *)&ret) < 0)
                        { perror("getsockopt"); ret = -1; }
+#endif
                break;
+#endif
        case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
                /* fall-through */
        case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
+#ifdef OPENSSL_SYS_WINDOWS
+               if ( data->_errno == WSAETIMEDOUT)
+#else
                if ( data->_errno == EAGAIN)
+#endif
                        {
                        ret = 1;
                        data->_errno = 0;