Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
13263 lines (11892 sloc) 377 KB
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1997-2018. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* If we HAVE_SCTP_H and Solaris, we need to define the following in
order to get SCTP working:
*/
#if (defined(HAVE_SCTP_H) && defined(__sun) && defined(__SVR4))
#define SOLARIS10 1
/* WARNING: This is not quite correct, it may also be Solaris 11! */
#define _XPG4_2
#define __EXTENSIONS__
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>
#include <stdint.h>
#define IDENTITY(c) c
#define STRINGIFY_1(b) IDENTITY(#b)
#define STRINGIFY(a) STRINGIFY_1(a)
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef HAVE_NET_IF_DL_H
#include <net/if_dl.h>
#endif
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
#ifdef HAVE_NETPACKET_PACKET_H
#include <netpacket/packet.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#ifdef HAVE_SENDFILE
#if defined(__linux__) || (defined(__sun) && defined(__SVR4))
#include <sys/sendfile.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
/* Need to define __BSD_VISIBLE in order to expose prototype of sendfile */
#define __BSD_VISIBLE 1
#include <sys/socket.h>
#endif
#endif
#if defined(__APPLE__) && defined(__MACH__) && !defined(__DARWIN__)
#define __DARWIN__ 1
#endif
/* All platforms fail on malloc errors. */
#define FATAL_MALLOC
#include "erl_driver.h"
/* The IS_SOCKET_ERROR macro below is used for portability reasons. While
POSIX specifies that errors from socket-related system calls should be
indicated with a -1 return value, some users have experienced non-Windows
OS kernels that return negative values other than -1. While one can argue
that such kernels are technically broken, comparing against values less
than 0 covers their out-of-spec return values without imposing incorrect
semantics on systems that manage to correctly return -1 for errors, thus
increasing Erlang's portability.
*/
#ifdef __WIN32__
#define IS_SOCKET_ERROR(val) ((val) == SOCKET_ERROR)
#else
#define IS_SOCKET_ERROR(val) ((val) < 0)
#endif
#ifdef __WIN32__
#define LLU "%I64u"
#else
#define LLU "%llu"
#endif
typedef unsigned long long llu_t;
#ifndef INT16_MIN
#define INT16_MIN (-32768)
#endif
#ifndef INT16_MAX
#define INT16_MAX (32767)
#endif
#ifdef __WIN32__
#define STRNCASECMP strncasecmp
#define INCL_WINSOCK_API_TYPEDEFS 1
#ifndef WINDOWS_H_INCLUDES_WINSOCK2_H
#include <winsock2.h>
#endif
#include <windows.h>
#include <Ws2tcpip.h> /* NEED VC 6.0 or higher */
/* Visual studio 2008+: NTDDI_VERSION needs to be set for iphlpapi.h
to define the right structures. It needs to be set to WINXP (or LONGHORN)
for IPV6 to work and it's set lower by default, so we need to change it. */
#ifdef HAVE_SDKDDKVER_H
# include <sdkddkver.h>
# ifdef NTDDI_VERSION
# undef NTDDI_VERSION
# endif
# define NTDDI_VERSION NTDDI_WINXP
#endif
#include <iphlpapi.h>
#undef WANT_NONBLOCKING
#include "sys.h"
#undef EWOULDBLOCK
#undef ETIMEDOUT
#ifdef EINPROGRESS
#undef EINPROGRESS
#endif
#ifdef EALREADY
#undef EALREADY
#endif
#ifdef ENOTSOCK
#undef ENOTSOCK
#endif
#ifdef EDESTADDRREQ
#undef EDESTADDRREQ
#endif
#ifdef EMSGSIZE
#undef EMSGSIZE
#endif
#ifdef EPROTOTYPE
#undef EPROTOTYPE
#endif
#ifdef ENOPROTOOPT
#undef ENOPROTOOPT
#endif
#ifdef EPROTONOSUPPORT
#undef EPROTONOSUPPORT
#endif
#ifdef EOPNOTSUPP
#undef EOPNOTSUPP
#endif
#ifdef EAFNOSUPPORT
#undef EAFNOSUPPORT
#endif
#ifdef EADDRINUSE
#undef EADDRINUSE
#endif
#ifdef EADDRNOTAVAIL
#undef EADDRNOTAVAIL
#endif
#ifdef ENETDOWN
#undef ENETDOWN
#endif
#ifdef ENETUNREACH
#undef ENETUNREACH
#endif
#ifdef ENETRESET
#undef ENETRESET
#endif
#ifdef ECONNABORTED
#undef ECONNABORTED
#endif
#ifdef ECONNRESET
#undef ECONNRESET
#endif
#ifdef ENOBUFS
#undef ENOBUFS
#endif
#ifdef EISCONN
#undef EISCONN
#endif
#ifdef ENOTCONN
#undef ENOTCONN
#endif
#ifdef ECONNREFUSED
#undef ECONNREFUSED
#endif
#ifdef ELOOP
#undef ELOOP
#endif
#ifdef EHOSTUNREACH
#undef EHOSTUNREACH
#endif
#define HAVE_MULTICAST_SUPPORT
#define HAVE_UDP
#define ERRNO_BLOCK WSAEWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#define EALREADY WSAEALREADY
#define ENOTSOCK WSAENOTSOCK
#define EDESTADDRREQ WSAEDESTADDRREQ
#define EMSGSIZE WSAEMSGSIZE
#define EPROTOTYPE WSAEPROTOTYPE
#define ENOPROTOOPT WSAENOPROTOOPT
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
#define EOPNOTSUPP WSAEOPNOTSUPP
#define EPFNOSUPPORT WSAEPFNOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define EADDRINUSE WSAEADDRINUSE
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
#define ENETDOWN WSAENETDOWN
#define ENETUNREACH WSAENETUNREACH
#define ENETRESET WSAENETRESET
#define ECONNABORTED WSAECONNABORTED
#define ECONNRESET WSAECONNRESET
#define ENOBUFS WSAENOBUFS
#define EISCONN WSAEISCONN
#define ENOTCONN WSAENOTCONN
#define ESHUTDOWN WSAESHUTDOWN
#define ETOOMANYREFS WSAETOOMANYREFS
#define ETIMEDOUT WSAETIMEDOUT
#define ECONNREFUSED WSAECONNREFUSED
#define ELOOP WSAELOOP
#undef ENAMETOOLONG
#define ENAMETOOLONG WSAENAMETOOLONG
#define EHOSTDOWN WSAEHOSTDOWN
#define EHOSTUNREACH WSAEHOSTUNREACH
#undef ENOTEMPTY
#define ENOTEMPTY WSAENOTEMPTY
#define EPROCLIM WSAEPROCLIM
#define EUSERS WSAEUSERS
#define EDQUOT WSAEDQUOT
#define ESTALE WSAESTALE
#define EREMOTE WSAEREMOTE
#define INVALID_EVENT WSA_INVALID_EVENT
static BOOL (WINAPI *fpSetHandleInformation)(HANDLE,DWORD,DWORD);
#define sock_open(af, type, proto) \
make_noninheritable_handle(socket((af), (type), (proto)))
#define sock_close(s) closesocket((s))
#define sock_shutdown(s, how) shutdown((s), (how))
#define sock_accept(s, addr, len) \
make_noninheritable_handle(accept((s), (addr), (len)))
#define sock_connect(s, addr, len) connect((s), (addr), (len))
#define sock_listen(s, b) listen((s), (b))
#define sock_bind(s, addr, len) bind((s), (addr), (len))
#define sock_getopt(s,t,n,v,l) getsockopt((s),(t),(n),(v),(l))
#define sock_setopt(s,t,n,v,l) setsockopt((s),(t),(n),(v),(l))
#define sock_name(s, addr, len) getsockname((s), (addr), (len))
#define sock_peer(s, addr, len) getpeername((s), (addr), (len))
#define sock_ntohs(x) ntohs((x))
#define sock_ntohl(x) ntohl((x))
#define sock_htons(x) htons((x))
#define sock_htonl(x) htonl((x))
#define sock_send(s,buf,len,flag) send((s),(buf),(len),(flag))
#define sock_sendv(s, vec, size, np, flag) \
WSASend((s),(WSABUF*)(vec),(size),(np),(flag),NULL,NULL)
#define sock_recv(s,buf,len,flag) recv((s),(buf),(len),(flag))
#define sock_recvfrom(s,buf,blen,flag,addr,alen) \
recvfrom((s),(buf),(blen),(flag),(addr),(alen))
#define sock_sendto(s,buf,blen,flag,addr,alen) \
sendto((s),(buf),(blen),(flag),(addr),(alen))
#define sock_hostname(buf, len) gethostname((buf), (len))
#define sock_getservbyname(name,proto) getservbyname((name),(proto))
#define sock_getservbyport(port,proto) getservbyport((port),(proto))
#define sock_errno() WSAGetLastError()
#define sock_create_event(d) WSACreateEvent()
#define sock_close_event(e) WSACloseEvent(e)
#define sock_select(D, Flags, OnOff) winsock_event_select(D, Flags, OnOff)
#define SET_BLOCKING(s) ioctlsocket(s, FIONBIO, &zero_value)
#define SET_NONBLOCKING(s) ioctlsocket(s, FIONBIO, &one_value)
static unsigned long zero_value = 0;
static unsigned long one_value = 1;
#define TCP_SHUT_WR SD_SEND
#define TCP_SHUT_RD SD_RECEIVE
#define TCP_SHUT_RDWR SD_BOTH
#else /* !__WIN32__ */
#include <sys/time.h>
#ifdef NETDB_H_NEEDS_IN_H
#include <netinet/in.h>
#endif
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef DEF_INADDR_LOOPBACK_IN_RPC_TYPES_H
#include <rpc/types.h>
#endif
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/param.h>
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <net/if.h>
#ifdef HAVE_SCHED_H
#include <sched.h>
#endif
#ifdef HAVE_SETNS_H
#include <setns.h>
#endif
#define HAVE_UDP
/* SCTP support -- currently for UNIX platforms only: */
#undef HAVE_SCTP
#if defined(HAVE_SCTP_H)
#include <netinet/sctp.h>
/* SCTP Socket API Draft from version 11 on specifies that netinet/sctp.h must
explicitly define HAVE_SCTP in case when SCTP is supported, but Solaris 10
still apparently uses Draft 10, and does not define that symbol, so we have
to define it explicitly:
*/
#ifndef HAVE_SCTP
# define HAVE_SCTP
#endif
/* These changed in draft 11, so SOLARIS10 uses the old MSG_* */
#if ! HAVE_DECL_SCTP_UNORDERED
# define SCTP_UNORDERED MSG_UNORDERED
#endif
#if ! HAVE_DECL_SCTP_ADDR_OVER
# define SCTP_ADDR_OVER MSG_ADDR_OVER
#endif
#if ! HAVE_DECL_SCTP_ABORT
# define SCTP_ABORT MSG_ABORT
#endif
#if ! HAVE_DECL_SCTP_EOF
# define SCTP_EOF MSG_EOF
#endif
/* More Solaris 10 fixes: */
#if ! HAVE_DECL_SCTP_CLOSED && HAVE_DECL_SCTPS_IDLE
# define SCTP_CLOSED SCTPS_IDLE
# undef HAVE_DECL_SCTP_CLOSED
# define HAVE_DECL_SCTP_CLOSED 1
#endif
#if ! HAVE_DECL_SCTP_BOUND && HAVE_DECL_SCTPS_BOUND
# define SCTP_BOUND SCTPS_BOUND
# undef HAVE_DECL_SCTP_BOUND
# define HAVE_DECL_SCTP_BOUND 1
#endif
#if ! HAVE_DECL_SCTP_LISTEN && HAVE_DECL_SCTPS_LISTEN
# define SCTP_LISTEN SCTPS_LISTEN
# undef HAVE_DECL_SCTP_LISTEN
# define HAVE_DECL_SCTP_LISTEN 1
#endif
#if ! HAVE_DECL_SCTP_COOKIE_WAIT && HAVE_DECL_SCTPS_COOKIE_WAIT
# define SCTP_COOKIE_WAIT SCTPS_COOKIE_WAIT
# undef HAVE_DECL_SCTP_COOKIE_WAIT
# define HAVE_DECL_SCTP_COOKIE_WAIT 1
#endif
#if ! HAVE_DECL_SCTP_COOKIE_ECHOED && HAVE_DECL_SCTPS_COOKIE_ECHOED
# define SCTP_COOKIE_ECHOED SCTPS_COOKIE_ECHOED
# undef HAVE_DECL_SCTP_COOKIE_ECHOED
# define HAVE_DECL_SCTP_COOKIE_ECHOED 1
#endif
#if ! HAVE_DECL_SCTP_ESTABLISHED && HAVE_DECL_SCTPS_ESTABLISHED
# define SCTP_ESTABLISHED SCTPS_ESTABLISHED
# undef HAVE_DECL_SCTP_ESTABLISHED
# define HAVE_DECL_SCTP_ESTABLISHED 1
#endif
#if ! HAVE_DECL_SCTP_SHUTDOWN_PENDING && HAVE_DECL_SCTPS_SHUTDOWN_PENDING
# define SCTP_SHUTDOWN_PENDING SCTPS_SHUTDOWN_PENDING
# undef HAVE_DECL_SCTP_SHUTDOWN_PENDING
# define HAVE_DECL_SCTP_SHUTDOWN_PENDING 1
#endif
#if ! HAVE_DECL_SCTP_SHUTDOWN_SENT && HAVE_DECL_SCTPS_SHUTDOWN_SENT
# define SCTP_SHUTDOWN_SENT SCTPS_SHUTDOWN_SENT
# undef HAVE_DECL_SCTP_SHUTDOWN_SENT
# define HAVE_DECL_SCTP_SHUTDOWN_SENT 1
#endif
#if ! HAVE_DECL_SCTP_SHUTDOWN_RECEIVED && HAVE_DECL_SCTPS_SHUTDOWN_RECEIVED
# define SCTP_SHUTDOWN_RECEIVED SCTPS_SHUTDOWN_RECEIVED
# undef HAVE_DECL_SCTP_SHUTDOWN_RECEIVED
# define HAVE_DECL_SCTP_SHUTDOWN_RECEIVED 1
#endif
#if ! HAVE_DECL_SCTP_SHUTDOWN_ACK_SENT && HAVE_DECL_SCTPS_SHUTDOWN_ACK_SENT
# define SCTP_SHUTDOWN_ACK_SENT SCTPS_SHUTDOWN_ACK_SENT
# undef HAVE_DECL_SCTP_SHUTDOWN_ACK_SENT
# define HAVE_DECL_SCTP_SHUTDOWN_ACK_SENT 1
#endif
/* New spelling in lksctp 2.6.22 or maybe even earlier:
* adaption -> adaptation
*/
#if !defined(SCTP_ADAPTATION_LAYER) && defined (SCTP_ADAPTION_LAYER)
# define SCTP_ADAPTATION_LAYER SCTP_ADAPTION_LAYER
# define SCTP_ADAPTATION_INDICATION SCTP_ADAPTION_INDICATION
# define sctp_adaptation_event sctp_adaption_event
# define sctp_setadaptation sctp_setadaption
# define sn_adaptation_event sn_adaption_event
# define sai_adaptation_ind sai_adaption_ind
# define ssb_adaptation_ind ssb_adaption_ind
# define sctp_adaptation_layer_event sctp_adaption_layer_event
#endif
#if defined(__GNUC__) && defined(HAVE_SCTP_BINDX)
static typeof(sctp_bindx) *p_sctp_bindx = NULL;
#else
static int (*p_sctp_bindx)
(int sd, struct sockaddr *addrs, int addrcnt, int flags) = NULL;
#endif
#if defined(__GNUC__) && defined(HAVE_SCTP_PEELOFF)
static typeof(sctp_peeloff) *p_sctp_peeloff = NULL;
#else
static int (*p_sctp_peeloff)
(int sd, sctp_assoc_t assoc_id) = NULL;
#endif
#if defined(__GNUC__) && defined(HAVE_SCTP_GETLADDRS)
static typeof(sctp_getladdrs) *p_sctp_getladdrs = NULL;
#else
static int (*p_sctp_getladdrs)
(int sd, sctp_assoc_t assoc_id, struct sockaddr **ss) = NULL;
#endif
#if defined(__GNUC__) && defined(HAVE_SCTP_FREELADDRS)
static typeof(sctp_freeladdrs) *p_sctp_freeladdrs = NULL;
#else
static void (*p_sctp_freeladdrs)(struct sockaddr *addrs) = NULL;
#endif
#if defined(__GNUC__) && defined(HAVE_SCTP_GETPADDRS)
static typeof(sctp_getpaddrs) *p_sctp_getpaddrs = NULL;
#else
static int (*p_sctp_getpaddrs)
(int sd, sctp_assoc_t assoc_id, struct sockaddr **ss) = NULL;
#endif
#if defined(__GNUC__) && defined(HAVE_SCTP_FREEPADDRS)
static typeof(sctp_freepaddrs) *p_sctp_freepaddrs = NULL;
#else
static void (*p_sctp_freepaddrs)(struct sockaddr *addrs) = NULL;
#endif
#endif /* #if defined(HAVE_SCTP_H) */
#ifndef WANT_NONBLOCKING
#define WANT_NONBLOCKING
#endif
#include "sys.h"
/* #define INET_DRV_DEBUG 1 */
#ifdef INET_DRV_DEBUG
#define DEBUG 1
#undef DEBUGF
#define DEBUGF(X) printf X
#endif
#if !defined(HAVE_STRNCASECMP)
#define STRNCASECMP my_strncasecmp
static int my_strncasecmp(const char *s1, const char *s2, size_t n)
{
int i;
for (i=0;i<n-1 && s1[i] && s2[i] && toupper(s1[i]) == toupper(s2[i]);++i)
;
return (toupper(s1[i]) - toupper(s2[i]));
}
#else
#define STRNCASECMP strncasecmp
#endif
#define INVALID_SOCKET -1
#define INVALID_EVENT -1
#define SOCKET_ERROR -1
#define SOCKET int
#define HANDLE long int
#define FD_READ ERL_DRV_READ
#define FD_WRITE ERL_DRV_WRITE
#define FD_CLOSE 0
#define FD_CONNECT ERL_DRV_WRITE
#define FD_ACCEPT ERL_DRV_READ
#define sock_connect(s, addr, len) connect((s), (addr), (len))
#define sock_listen(s, b) listen((s), (b))
#define sock_bind(s, addr, len) bind((s), (addr), (len))
#define sock_getopt(s,t,n,v,l) getsockopt((s),(t),(n),(v),(l))
#define sock_setopt(s,t,n,v,l) setsockopt((s),(t),(n),(v),(l))
#define sock_name(s, addr, len) getsockname((s), (addr), (len))
#define sock_peer(s, addr, len) getpeername((s), (addr), (len))
#define sock_ntohs(x) ntohs((x))
#define sock_ntohl(x) ntohl((x))
#define sock_htons(x) htons((x))
#define sock_htonl(x) htonl((x))
#define sock_accept(s, addr, len) accept((s), (addr), (len))
#define sock_send(s,buf,len,flag) send((s),(buf),(len),(flag))
#define sock_sendto(s,buf,blen,flag,addr,alen) \
sendto((s),(buf),(blen),(flag),(addr),(alen))
#define sock_sendv(s, vec, size, np, flag) \
(*(np) = writev((s), (struct iovec*)(vec), (size)))
#define sock_sendmsg(s,msghdr,flag) sendmsg((s),(msghdr),(flag))
#define sock_open(af, type, proto) socket((af), (type), (proto))
#define sock_close(s) close((s))
#define sock_shutdown(s, how) shutdown((s), (how))
#define sock_hostname(buf, len) gethostname((buf), (len))
#define sock_getservbyname(name,proto) getservbyname((name), (proto))
#define sock_getservbyport(port,proto) getservbyport((port), (proto))
#define sock_recv(s,buf,len,flag) recv((s),(buf),(len),(flag))
#define sock_recvfrom(s,buf,blen,flag,addr,alen) \
recvfrom((s),(buf),(blen),(flag),(addr),(alen))
#define sock_recvmsg(s,msghdr,flag) recvmsg((s),(msghdr),(flag))
#define sock_errno() errno
#define sock_create_event(d) ((d)->s) /* return file descriptor */
#define sock_close_event(e) /* do nothing */
#define inet_driver_select(port, e, mode, on) \
driver_select(port, e, mode | (on?ERL_DRV_USE:0), on)
#define sock_select(d, flags, onoff) do { \
ASSERT(!(d)->is_ignored); \
(d)->event_mask = (onoff) ? \
((d)->event_mask | (flags)) : \
((d)->event_mask & ~(flags)); \
DEBUGF(("(%s / %d) sock_select(%ld): flags=%02X, onoff=%d, event_mask=%02lX\r\n", \
__FILE__, __LINE__, (long) (d)->port, (flags), (onoff), (unsigned long) (d)->event_mask)); \
inet_driver_select((d)->port, (ErlDrvEvent)(long)(d)->event, (flags), (onoff)); \
} while(0)
#define TCP_SHUT_WR SHUT_WR
#define TCP_SHUT_RD SHUT_RD
#define TCP_SHUT_RDWR SHUT_RDWR
#endif /* !__WIN32__ */
#ifdef HAVE_SOCKLEN_T
# define SOCKLEN_T socklen_t
#else
# define SOCKLEN_T size_t
#endif
#include "packet_parser.h"
#define get_int24(s) ((((unsigned char*) (s))[0] << 16) | \
(((unsigned char*) (s))[1] << 8) | \
(((unsigned char*) (s))[2]))
#define get_little_int32(s) ((((unsigned char*) (s))[3] << 24) | \
(((unsigned char*) (s))[2] << 16) | \
(((unsigned char*) (s))[1] << 8) | \
(((unsigned char*) (s))[0]))
#if defined(HAVE_SYS_UN_H) || defined(SO_BINDTODEVICE)
/* strnlen doesn't exist everywhere */
static size_t my_strnlen(const char *s, size_t maxlen)
{
size_t i = 0;
while (i < maxlen && s[i] != '\0')
i++;
return i;
}
#endif
#ifdef VALGRIND
# include <valgrind/memcheck.h>
#else
# define VALGRIND_MAKE_MEM_DEFINED(ptr,size)
#endif
#ifndef __WIN32__
/* Calculate CMSG_NXTHDR without having a struct msghdr*.
* CMSG_LEN only caters for alignment for start of data.
* To get how much to advance we need to use CMSG_SPACE
* on the payload length. To get the payload length we
* take the calculated cmsg->cmsg_len and subtract the
* header length. To get the header length we use
* the pointer difference from the cmsg start pointer
* to the CMSG_DATA(cmsg) pointer.
*
* Some platforms (seen on ppc Linux 2.6.29-3.ydl61.3)
* may return 0 as the cmsg_len if the cmsg is to be ignored.
*/
#define LEN_CMSG_DATA(cmsg) \
((cmsg)->cmsg_len < sizeof (struct cmsghdr) ? 0 : \
(cmsg)->cmsg_len - ((char*)CMSG_DATA(cmsg) - (char*)(cmsg)))
#define NXT_CMSG_HDR(cmsg) \
((struct cmsghdr*)(((char*)(cmsg)) + CMSG_SPACE(LEN_CMSG_DATA(cmsg))))
#endif
#if !defined(IPV6_PKTOPTIONS) && defined(IPV6_2292PKTOPTIONS)
#define IPV6_PKTOPTIONS IPV6_2292PKTOPTIONS
#endif
/*
Magic errno value used locally for return of {error, system_limit}
- the emulator definition of SYSTEM_LIMIT is not available here.
*/
#define INET_ERRNO_SYSTEM_LIMIT (15 << 8)
/*----------------------------------------------------------------------------
** Interface constants.
**
** This section must be "identical" to the corresponding inet_int.hrl
*/
/* general address encode/decode tag */
#define INET_AF_UNSPEC 0
#define INET_AF_INET 1
#define INET_AF_INET6 2
#define INET_AF_ANY 3 /* INADDR_ANY or IN6ADDR_ANY_INIT */
#define INET_AF_LOOPBACK 4 /* INADDR_LOOPBACK or IN6ADDR_LOOPBACK_INIT */
#define INET_AF_LOCAL 5
#define INET_AF_UNDEFINED 6 /* Unknown */
/* open and INET_REQ_GETTYPE enumeration */
#define INET_TYPE_STREAM 1
#define INET_TYPE_DGRAM 2
#define INET_TYPE_SEQPACKET 3
/* INET_LOPT_MODE options */
#define INET_MODE_LIST 0
#define INET_MODE_BINARY 1
/* INET_LOPT_DELIVER options */
#define INET_DELIVER_PORT 0
#define INET_DELIVER_TERM 1
/* INET_LOPT_ACTIVE options */
#define INET_PASSIVE 0 /* false */
#define INET_ACTIVE 1 /* true */
#define INET_ONCE 2 /* true; active once then passive */
#define INET_MULTI 3 /* true; active N then passive */
/* INET_REQ_GETSTATUS enumeration */
#define INET_F_OPEN 0x0001
/* INET_F_BOUND removed - renumber when there comes a bigger rewrite */
#define INET_F_ACTIVE 0x0004
#define INET_F_LISTEN 0x0008
#define INET_F_CON 0x0010
#define INET_F_ACC 0x0020
#define INET_F_LST 0x0040
#define INET_F_BUSY 0x0080
#define INET_F_MULTI_CLIENT 0x0100 /* Multiple clients for one descriptor, i.e. multi-accept */
/* One numberspace for *_REQ_* so if an e.g UDP request is issued
** for a TCP socket, the driver can protest.
*/
#define INET_REQ_OPEN 1
#define INET_REQ_CLOSE 2
#define INET_REQ_CONNECT 3
#define INET_REQ_PEER 4
#define INET_REQ_NAME 5
#define INET_REQ_BIND 6
#define INET_REQ_SETOPTS 7
#define INET_REQ_GETOPTS 8
/* #define INET_REQ_GETIX 9 NOT USED ANY MORE */
/* #define INET_REQ_GETIF 10 REPLACE BY NEW STUFF */
#define INET_REQ_GETSTAT 11
#define INET_REQ_GETHOSTNAME 12
#define INET_REQ_FDOPEN 13
#define INET_REQ_GETFD 14
#define INET_REQ_GETTYPE 15
#define INET_REQ_GETSTATUS 16
#define INET_REQ_GETSERVBYNAME 17
#define INET_REQ_GETSERVBYPORT 18
#define INET_REQ_SETNAME 19
#define INET_REQ_SETPEER 20
#define INET_REQ_GETIFLIST 21
#define INET_REQ_IFGET 22
#define INET_REQ_IFSET 23
#define INET_REQ_SUBSCRIBE 24
#define INET_REQ_GETIFADDRS 25
#define INET_REQ_ACCEPT 26
#define INET_REQ_LISTEN 27
#define INET_REQ_IGNOREFD 28
#define INET_REQ_GETLADDRS 29
#define INET_REQ_GETPADDRS 30
/* TCP requests */
/* #define TCP_REQ_ACCEPT 40 MOVED */
/* #define TCP_REQ_LISTEN 41 MERGED */
#define TCP_REQ_RECV 42
#define TCP_REQ_UNRECV 43
#define TCP_REQ_SHUTDOWN 44
#define TCP_REQ_SENDFILE 45
/* UDP and SCTP requests */
#define PACKET_REQ_RECV 60 /* Common for UDP and SCTP */
/* #define SCTP_REQ_LISTEN 61 MERGED Different from TCP; not for UDP */
#define SCTP_REQ_BINDX 62 /* Multi-home SCTP bind */
#define SCTP_REQ_PEELOFF 63
/* INET_REQ_SUBSCRIBE sub-requests */
#define INET_SUBS_EMPTY_OUT_Q 1
/* TCP additional flags */
#define TCP_ADDF_DELAY_SEND 1
#define TCP_ADDF_CLOSE_SENT 2 /* Close sent (active mode only) */
#define TCP_ADDF_DELAYED_CLOSE_RECV 4 /* If receive fails, report {error,closed} (passive mode) */
#define TCP_ADDF_DELAYED_CLOSE_SEND 8 /* If send fails, report {error,closed} (passive mode) */
#define TCP_ADDF_PENDING_SHUT_WR 16 /* Call shutdown(sock, SHUT_WR) when queue empties */
#define TCP_ADDF_PENDING_SHUT_RDWR 32 /* Call shutdown(sock, SHUT_RDWR) when queue empties */
#define TCP_ADDF_PENDING_SHUTDOWN \
(TCP_ADDF_PENDING_SHUT_WR | TCP_ADDF_PENDING_SHUT_RDWR)
#define TCP_ADDF_SHOW_ECONNRESET 64 /* Tell user about incoming RST */
#define TCP_ADDF_DELAYED_ECONNRESET 128 /* An ECONNRESET error occurred on send or shutdown */
#define TCP_ADDF_SHUTDOWN_WR_DONE 256 /* A shutdown(sock, SHUT_WR) or SHUT_RDWR was made */
#define TCP_ADDF_LINGER_ZERO 512 /* Discard driver queue on port close */
#define TCP_ADDF_SENDFILE 1024 /* Send from an fd instead of the driver queue */
/* *_REQ_* replies */
#define INET_REP_ERROR 0
#define INET_REP_OK 1
#define INET_REP 2
/* INET_REQ_SETOPTS and INET_REQ_GETOPTS options */
#define INET_OPT_REUSEADDR 0 /* enable/disable local address reuse */
#define INET_OPT_KEEPALIVE 1 /* enable/disable keep connections alive */
#define INET_OPT_DONTROUTE 2 /* enable/disable routing for messages */
#define INET_OPT_LINGER 3 /* linger on close if data is present */
#define INET_OPT_BROADCAST 4 /* enable/disable transmission of broadcast */
#define INET_OPT_OOBINLINE 5 /* enable/disable out-of-band data in band */
#define INET_OPT_SNDBUF 6 /* set send buffer size */
#define INET_OPT_RCVBUF 7 /* set receive buffer size */
#define INET_OPT_PRIORITY 8 /* set priority */
#define INET_OPT_TOS 9 /* Set type of service */
#define TCP_OPT_NODELAY 10 /* don't delay send to coalesce packets */
#define UDP_OPT_MULTICAST_IF 11 /* set/get IP multicast interface */
#define UDP_OPT_MULTICAST_TTL 12 /* set/get IP multicast timetolive */
#define UDP_OPT_MULTICAST_LOOP 13 /* set/get IP multicast loopback */
#define UDP_OPT_ADD_MEMBERSHIP 14 /* add an IP group membership */
#define UDP_OPT_DROP_MEMBERSHIP 15 /* drop an IP group membership */
#define INET_OPT_IPV6_V6ONLY 16 /* IPv6 only socket, no mapped v4 addrs */
/* LOPT is local options */
#define INET_LOPT_BUFFER 20 /* min buffer size hint */
#define INET_LOPT_HEADER 21 /* list header size */
#define INET_LOPT_ACTIVE 22 /* enable/disable active receive */
#define INET_LOPT_PACKET 23 /* packet header type (TCP) */
#define INET_LOPT_MODE 24 /* list or binary mode */
#define INET_LOPT_DELIVER 25 /* port or term delivery */
#define INET_LOPT_EXITONCLOSE 26 /* exit port on active close or not ! */
#define INET_LOPT_TCP_HIWTRMRK 27 /* set local high watermark */
#define INET_LOPT_TCP_LOWTRMRK 28 /* set local low watermark */
/* 29 unused */
#define INET_LOPT_TCP_SEND_TIMEOUT 30 /* set send timeout */
#define INET_LOPT_TCP_DELAY_SEND 31 /* Delay sends until next poll */
#define INET_LOPT_PACKET_SIZE 32 /* Max packet size */
#define INET_LOPT_UDP_READ_PACKETS 33 /* Number of packets to read */
#define INET_OPT_RAW 34 /* Raw socket options */
#define INET_LOPT_TCP_SEND_TIMEOUT_CLOSE 35 /* auto-close on send timeout or not */
#define INET_LOPT_MSGQ_HIWTRMRK 36 /* set local msgq high watermark */
#define INET_LOPT_MSGQ_LOWTRMRK 37 /* set local msgq low watermark */
#define INET_LOPT_NETNS 38 /* Network namespace pathname */
#define INET_LOPT_TCP_SHOW_ECONNRESET 39 /* tell user about incoming RST */
#define INET_LOPT_LINE_DELIM 40 /* Line delimiting char */
#define INET_OPT_TCLASS 41 /* IPv6 transport class */
#define INET_OPT_BIND_TO_DEVICE 42 /* get/set network device the socket is bound to */
#define INET_OPT_RECVTOS 43 /* IP_RECVTOS ancillary data */
#define INET_OPT_RECVTCLASS 44 /* IPV6_RECVTCLASS ancillary data */
#define INET_OPT_PKTOPTIONS 45 /* IP(V6)_PKTOPTIONS get ancillary data */
#define INET_OPT_TTL 46 /* IP_TTL */
#define INET_OPT_RECVTTL 47 /* IP_RECVTTL ancillary data */
#define TCP_OPT_NOPUSH 48 /* super-Nagle, aka TCP_CORK */
/* SCTP options: a separate range, from 100: */
#define SCTP_OPT_RTOINFO 100
#define SCTP_OPT_ASSOCINFO 101
#define SCTP_OPT_INITMSG 102
#define SCTP_OPT_AUTOCLOSE 103
#define SCTP_OPT_NODELAY 104
#define SCTP_OPT_DISABLE_FRAGMENTS 105
#define SCTP_OPT_I_WANT_MAPPED_V4_ADDR 106
#define SCTP_OPT_MAXSEG 107
#define SCTP_OPT_SET_PEER_PRIMARY_ADDR 108
#define SCTP_OPT_PRIMARY_ADDR 109
#define SCTP_OPT_ADAPTATION_LAYER 110
#define SCTP_OPT_PEER_ADDR_PARAMS 111
#define SCTP_OPT_DEFAULT_SEND_PARAM 112
#define SCTP_OPT_EVENTS 113
#define SCTP_OPT_DELAYED_ACK_TIME 114
#define SCTP_OPT_STATUS 115
#define SCTP_OPT_GET_PEER_ADDR_INFO 116
/* INET_REQ_IFGET and INET_REQ_IFSET options */
#define INET_IFOPT_ADDR 1
#define INET_IFOPT_BROADADDR 2
#define INET_IFOPT_DSTADDR 3
#define INET_IFOPT_MTU 4
#define INET_IFOPT_NETMASK 5
#define INET_IFOPT_FLAGS 6
#define INET_IFOPT_HWADDR 7
/* INET_REQ_GETSTAT enumeration */
#define INET_STAT_RECV_CNT 1
#define INET_STAT_RECV_MAX 2
#define INET_STAT_RECV_AVG 3
#define INET_STAT_RECV_DVI 4
#define INET_STAT_SEND_CNT 5
#define INET_STAT_SEND_MAX 6
#define INET_STAT_SEND_AVG 7
#define INET_STAT_SEND_PND 8
#define INET_STAT_RECV_OCT 9 /* received octets */
#define INET_STAT_SEND_OCT 10 /* sent octets */
/* INET_IFOPT_FLAGS enumeration */
#define INET_IFF_UP 0x0001
#define INET_IFF_BROADCAST 0x0002
#define INET_IFF_LOOPBACK 0x0004
#define INET_IFF_POINTTOPOINT 0x0008
#define INET_IFF_RUNNING 0x0010
#define INET_IFF_MULTICAST 0x0020
/* Complement flags for turning them off */
#define INET_IFF_DOWN 0x0100
#define INET_IFF_NBROADCAST 0x0200
/* #define INET_IFF_NLOOPBACK 0x0400 */
#define INET_IFF_NPOINTTOPOINT 0x0800
/* #define INET_IFF_NRUNNING 0x1000 */
/* #define INET_IFF_NMULTICAST 0x2000 */
/* Flags for "sctp_sndrcvinfo". Used in a bitmask -- must be powers of 2:
** INET_REQ_SETOPTS:SCTP_OPT_DEFAULT_SEND_PARAM
*/
#define SCTP_FLAG_UNORDERED (1 /* am_unordered */)
#define SCTP_FLAG_ADDR_OVER (2 /* am_addr_over */)
#define SCTP_FLAG_ABORT (4 /* am_abort */)
#define SCTP_FLAG_EOF (8 /* am_eof */)
#define SCTP_FLAG_SNDALL (16 /* am_sndall, NOT YET IMPLEMENTED */)
/* Flags for "sctp_set_opts" (actually for SCTP_OPT_PEER_ADDR_PARAMS).
** These flags are also used in a bitmask, so they must be powers of 2:
*/
#define SCTP_FLAG_HB_ENABLE (1 /* am_hb_enable */)
#define SCTP_FLAG_HB_DISABLE (2 /* am_hb_disable */)
#define SCTP_FLAG_HB_DEMAND (4 /* am_hb_demand */)
#define SCTP_FLAG_PMTUD_ENABLE (8 /* am_pmtud_enable */)
#define SCTP_FLAG_PMTUD_DISABLE (16 /* am_pmtud_disable */)
#define SCTP_FLAG_SACDELAY_ENABLE (32 /* am_sackdelay_enable */)
#define SCTP_FLAG_SACDELAY_DISABLE (64 /* am_sackdelay_disable */)
/* Flags for recv_cmsgflags */
#define INET_CMSG_RECVTOS (1 << 0) /* am_recvtos, am_tos */
#define INET_CMSG_RECVTCLASS (1 << 1) /* am_recvtclass, am_tclass */
#define INET_CMSG_RECVTTL (1 << 2) /* am_recvttl, am_ttl */
/*
** End of interface constants.
**--------------------------------------------------------------------------*/
#define INET_STATE_CLOSED (0)
#define INET_STATE_OPEN (INET_F_OPEN)
#define INET_STATE_CONNECTED (INET_STATE_OPEN | INET_F_ACTIVE)
#define INET_STATE_LISTENING (INET_STATE_OPEN | INET_F_LISTEN)
#define INET_STATE_CONNECTING (INET_STATE_OPEN | INET_F_CON)
#define INET_STATE_ACCEPTING (INET_STATE_LISTENING | INET_F_ACC)
#define INET_STATE_MULTI_ACCEPTING (INET_STATE_ACCEPTING | INET_F_MULTI_CLIENT)
#define IS_OPEN(d) \
(((d)->state & INET_F_OPEN) == INET_F_OPEN)
#define IS_CONNECTED(d) \
(((d)->state & INET_STATE_CONNECTED) == INET_STATE_CONNECTED)
#define IS_CONNECTING(d) \
(((d)->state & INET_F_CON) == INET_F_CON)
#define IS_BUSY(d) \
(((d)->state & INET_F_BUSY) == INET_F_BUSY)
#define INET_MAX_OPT_BUFFER (64*1024)
#define INET_DEF_BUFFER 1460 /* default buffer size */
#define INET_MIN_BUFFER 1 /* internal min buffer */
#define INET_HIGH_WATERMARK (1024*8) /* 8k pending high => busy */
#define INET_LOW_WATERMARK (1024*4) /* 4k pending => allow more */
#define INET_HIGH_MSGQ_WATERMARK (1024*8) /* 8k pending high => busy */
#define INET_LOW_MSGQ_WATERMARK (1024*4) /* 4k pending => allow more */
#define INET_INFINITY 0xffffffff /* infinity value */
#define INET_MAX_ASYNC 1 /* max number of async queue ops */
/* INET_LOPT_UDP_PACKETS */
#define INET_PACKET_POLL 5 /* maximum number of packets to poll */
/* Max interface name */
#define INET_IFNAMSIZ 16
/* INET Ignore states */
#define INET_IGNORE_NONE 0
#define INET_IGNORE_READ (1 << 0)
#define INET_IGNORE_WRITE (1 << 1)
#define INET_IGNORE_PASSIVE (1 << 2)
/* Max length of Erlang Term Buffer (for outputting structured terms): */
#ifdef HAVE_SCTP
#define PACKET_ERL_DRV_TERM_DATA_LEN 512
#else
#ifndef __WIN32__
/* Assume we have recvmsg() and might need room for ancillary data */
#define PACKET_ERL_DRV_TERM_DATA_LEN 64
#else
#define PACKET_ERL_DRV_TERM_DATA_LEN 32
#endif
#endif
typedef struct _tcp_descriptor tcp_descriptor;
#if defined(TCP_CORK)
#define INET_TCP_NOPUSH TCP_CORK
#elif defined(TCP_NOPUSH) && !defined(__DARWIN__)
#define INET_TCP_NOPUSH TCP_NOPUSH
#endif
#define BIN_REALLOC_MARGIN(x) ((x)/4) /* 25% */
/* The general purpose sockaddr */
typedef union {
struct sockaddr sa;
struct sockaddr_in sai;
#ifdef HAVE_IN6
struct sockaddr_in6 sai6;
#endif
#ifdef HAVE_SYS_UN_H
struct sockaddr_un sal;
#endif
} inet_address;
#define inet_address_port(x) \
((((x)->sai.sin_family == AF_INET) || \
((x)->sai.sin_family == AF_INET6)) ? \
((x)->sai.sin_port) : -1)
#ifdef HAVE_SYS_UN_H
#define localaddrlen(data) \
((((unsigned char*)(data))[0] == INET_AF_LOCAL) ? \
(1 + 1 + ((unsigned char*)(data))[1]) : 1)
#else
#define localaddrlen(data) (1)
#endif
#if defined(HAVE_IN6) && defined(AF_INET6)
#define addrlen(data) \
((((unsigned char*)(data))[0] == INET_AF_INET) ? \
(1 + 2 + 4) : \
((((unsigned char*)(data))[0] == INET_AF_INET6) ? \
(1 + 2 + 16) : localaddrlen(data)))
#else
#define addrlen(data) \
((((unsigned char*)(data))[0] == INET_AF_INET) ? \
(1 + 2 + 4) : localaddrlen(data))
#endif
typedef struct _multi_timer_data {
ErlDrvTime when;
ErlDrvTermData caller;
void (*timeout_function)(ErlDrvData drv_data, ErlDrvTermData caller);
struct _multi_timer_data *next;
struct _multi_timer_data *prev;
} MultiTimerData;
static MultiTimerData *add_multi_timer(tcp_descriptor *desc, ErlDrvPort port,
ErlDrvTermData caller, unsigned timeout,
void (*timeout_fun)(ErlDrvData drv_data,
ErlDrvTermData caller));
static void fire_multi_timers(tcp_descriptor *desc, ErlDrvPort port,
ErlDrvData data);
static void remove_multi_timer(tcp_descriptor *desc, ErlDrvPort port, MultiTimerData *p);
static void cancel_multi_timer(tcp_descriptor *desc, ErlDrvPort port,
void (*timeout_fun)(ErlDrvData drv_data,
ErlDrvTermData caller));
static void tcp_inet_multi_timeout(ErlDrvData e, ErlDrvTermData caller);
static void clean_multi_timers(tcp_descriptor *desc, ErlDrvPort port);
typedef struct {
int id; /* id used to identify reply */
ErlDrvTermData caller; /* recipient of async reply */
int req; /* Request id (CONNECT/ACCEPT/RECV) */
union {
unsigned value; /* Request timeout (since op issued,not started) */
MultiTimerData *mtd;
} tmo;
ErlDrvMonitor monitor;
} inet_async_op;
typedef struct inet_async_multi_op_ {
inet_async_op op;
struct inet_async_multi_op_ *next;
} inet_async_multi_op;
typedef struct subs_list_ {
ErlDrvTermData subscriber;
struct subs_list_ *next;
} subs_list;
#define NO_PROCESS 0
#define NO_SUBSCRIBERS(SLP) ((SLP)->subscriber == NO_PROCESS)
static void send_to_subscribers(ErlDrvTermData, subs_list *, int,
ErlDrvTermData [], int);
static void free_subscribers(subs_list*);
static int save_subscriber(subs_list *, ErlDrvTermData);
typedef struct {
SOCKET s; /* the socket or INVALID_SOCKET if not open */
HANDLE event; /* Event handle (same as s in unix) */
long event_mask; /* current FD events */
#ifdef __WIN32__
long forced_events; /* Mask of events that are forcefully signalled
on windows see winsock_event_select
for details */
int send_would_block; /* Last send attempt failed with "WOULDBLOCK" */
#endif
ErlDrvPort port; /* the port identifier */
ErlDrvTermData dport; /* the port identifier as DriverTermData */
int state; /* status */
int prebound; /* only set when opened with inet_fdopen */
int mode; /* BINARY | LIST
(affect how to interpret hsz) */
int exitf; /* exit port on close or not */
int deliver; /* Delivery mode, TERM or PORT */
ErlDrvTermData caller; /* recipient of sync reply */
ErlDrvTermData busy_caller; /* recipient of sync reply when caller busy.
* Only valid while INET_F_BUSY. */
inet_async_op* oph; /* queue head or NULL */
inet_async_op* opt; /* queue tail or NULL */
inet_async_op op_queue[INET_MAX_ASYNC]; /* call queue */
int op_ref; /* queue reference generator */
int active; /* 0 = passive, 1 = active, 2 = active once */
Sint16 active_count; /* counter for {active,N} */
int stype; /* socket type:
SOCK_STREAM/SOCK_DGRAM/SOCK_SEQPACKET */
int sprotocol; /* socket protocol:
IPPROTO_TCP|IPPROTO_UDP|IPPROTO_SCTP */
int sfamily; /* address family */
enum PacketParseType htype; /* header type (TCP only?) */
unsigned int psize; /* max packet size (TCP only?) */
inet_address remote; /* remote address for connected sockets */
inet_address peer_addr; /* fake peer address */
inet_address name_addr; /* fake local address */
inet_address* peer_ptr; /* fake peername or NULL */
inet_address* name_ptr; /* fake sockname or NULL */
SOCKLEN_T peer_addr_len; /* fake peername size */
SOCKLEN_T name_addr_len; /* fake sockname size */
int bufsz; /* minimum buffer constraint */
unsigned int hsz; /* the list header size, -1 is large !!! */
/* statistics */
#ifdef ARCH_64
Uint64 recv_oct; /* number of received octets, 64 bits */
#else
Uint32 recv_oct[2]; /* number of received octets, 64 bits */
#endif
unsigned long recv_cnt; /* number of packets received */
unsigned long recv_max; /* maximum packet size received */
double recv_avg; /* average packet size received */
double recv_dvi; /* avarage deviation from avg_size */
#ifdef ARCH_64
Uint64 send_oct; /* number of octets sent, 64 bits */
#else
Uint32 send_oct[2]; /* number of octets sent, 64 bits */
#endif
char delimiter; /* Line delimiting character (def: '\n') */
unsigned long send_cnt; /* number of packets sent */
unsigned long send_max; /* maximum packet send */
double send_avg; /* average packet size sent */
subs_list empty_out_q_subs; /* Empty out queue subscribers */
int is_ignored; /* if a fd is ignored by the inet_drv.
This flag should be set to true when
the fd is used outside of inet_drv. */
#ifdef HAVE_SETNS
char *netns; /* Socket network namespace name
as full file path */
#endif
int recv_cmsgflags; /* Which ancillary data to expect */
} inet_descriptor;
#define TCP_MAX_PACKET_SIZE 0x4000000 /* 64 M */
#define MAX_VSIZE 16 /* Max number of entries allowed in an I/O
* vector sock_sendv().
*/
static int tcp_inet_init(void);
static void tcp_inet_stop(ErlDrvData);
static void tcp_inet_command(ErlDrvData, char*, ErlDrvSizeT);
static void tcp_inet_commandv(ErlDrvData, ErlIOVec*);
static void tcp_inet_flush(ErlDrvData drv_data);
static void tcp_inet_drv_input(ErlDrvData, ErlDrvEvent);
static void tcp_inet_drv_output(ErlDrvData data, ErlDrvEvent event);
static ErlDrvData tcp_inet_start(ErlDrvPort, char* command);
static ErlDrvSSizeT tcp_inet_ctl(ErlDrvData, unsigned int,
char*, ErlDrvSizeT, char**, ErlDrvSizeT);
static void tcp_inet_timeout(ErlDrvData);
static void tcp_inet_process_exit(ErlDrvData, ErlDrvMonitor *);
static void inet_stop_select(ErlDrvEvent, void*);
static void inet_emergency_close(ErlDrvData);
#ifdef __WIN32__
static void tcp_inet_event(ErlDrvData, ErlDrvEvent);
static void find_dynamic_functions(void);
#endif
static struct erl_drv_entry tcp_inet_driver_entry =
{
tcp_inet_init, /* inet_init will add this driver !! */
tcp_inet_start,
tcp_inet_stop,
tcp_inet_command,
#ifdef __WIN32__
tcp_inet_event,
NULL,
#else
tcp_inet_drv_input,
tcp_inet_drv_output,
#endif
"tcp_inet",
NULL,
NULL,
tcp_inet_ctl,
tcp_inet_timeout,
tcp_inet_commandv,
NULL,
tcp_inet_flush,
NULL,
NULL,
ERL_DRV_EXTENDED_MARKER,
ERL_DRV_EXTENDED_MAJOR_VERSION,
ERL_DRV_EXTENDED_MINOR_VERSION,
ERL_DRV_FLAG_USE_PORT_LOCKING|ERL_DRV_FLAG_SOFT_BUSY,
NULL,
tcp_inet_process_exit,
inet_stop_select,
inet_emergency_close
};
#ifdef HAVE_UDP
static int packet_inet_init(void);
static void packet_inet_stop(ErlDrvData);
static void packet_inet_command(ErlDrvData, char*, ErlDrvSizeT);
static void packet_inet_drv_input(ErlDrvData data, ErlDrvEvent event);
static ErlDrvData udp_inet_start(ErlDrvPort, char* command);
#ifdef HAVE_SCTP
static ErlDrvData sctp_inet_start(ErlDrvPort, char* command);
#endif
static ErlDrvSSizeT packet_inet_ctl(ErlDrvData, unsigned int, char*,
ErlDrvSizeT, char**, ErlDrvSizeT);
static void packet_inet_timeout(ErlDrvData);
#ifdef __WIN32__
static void packet_inet_event(ErlDrvData, ErlDrvEvent);
static SOCKET make_noninheritable_handle(SOCKET s);
static int winsock_event_select(inet_descriptor *, int, int);
#endif
static struct erl_drv_entry udp_inet_driver_entry =
{
packet_inet_init, /* inet_init will add this driver !! */
udp_inet_start,
packet_inet_stop,
packet_inet_command,
#ifdef __WIN32__
packet_inet_event,
NULL,
#else
packet_inet_drv_input,
NULL,
#endif
"udp_inet",
NULL,
NULL,
packet_inet_ctl,
packet_inet_timeout,
NULL,
NULL,
NULL,
NULL,
NULL,
ERL_DRV_EXTENDED_MARKER,
ERL_DRV_EXTENDED_MAJOR_VERSION,
ERL_DRV_EXTENDED_MINOR_VERSION,
ERL_DRV_FLAG_USE_PORT_LOCKING,
NULL,
NULL,
inet_stop_select,
inet_emergency_close
};
#endif
#ifdef HAVE_SCTP
static struct erl_drv_entry sctp_inet_driver_entry =
{
packet_inet_init, /* inet_init will add this driver !! */
sctp_inet_start,
packet_inet_stop,
packet_inet_command,
#ifdef __WIN32__
packet_inet_event,
NULL,
#else
packet_inet_drv_input,
NULL,
#endif
"sctp_inet",
NULL,
NULL,
packet_inet_ctl,
packet_inet_timeout,
NULL,
NULL,
NULL,
NULL,
NULL,
ERL_DRV_EXTENDED_MARKER,
ERL_DRV_EXTENDED_MAJOR_VERSION,
ERL_DRV_EXTENDED_MINOR_VERSION,
ERL_DRV_FLAG_USE_PORT_LOCKING,
NULL,
NULL, /* process_exit */
inet_stop_select,
inet_emergency_close
};
#endif
struct _tcp_descriptor {
inet_descriptor inet; /* common data structure (DON'T MOVE) */
int high; /* high watermark */
int low; /* low watermark */
int send_timeout; /* timeout to use in send */
int send_timeout_close; /* auto-close socket on send_timeout */
int busy_on_send; /* busy on send with timeout! */
int i_bufsz; /* current input buffer size (<= bufsz) */
ErlDrvBinary* i_buf; /* current binary buffer */
char* i_ptr; /* current pos in buf */
char* i_ptr_start; /* packet start pos in buf */
int i_remain; /* remaining chars to read */
int tcp_add_flags;/* Additional TCP descriptor flags */
int http_state; /* 0 = response|request 1=headers fields */
inet_async_multi_op *multi_first;/* NULL == no multi-accept-queue, op is in ordinary queue */
inet_async_multi_op *multi_last;
MultiTimerData *mtd; /* Timer structures for multiple accept */
MultiTimerData *mtd_cache; /* A cache for timer allocations */
#ifdef HAVE_SENDFILE
struct {
ErlDrvSizeT ioq_skip; /* The number of bytes in the queue at the time
* sendfile was issued, which must be sent
* before issuing the sendfile call itself. */
int dup_file_fd; /* The file handle to send from; this is
* duplicated when sendfile is issued to
* reduce (but not eliminate) the impact of a
* nasty race, so we have to remember to close
* it. */
Uint64 bytes_sent;
Uint64 offset;
Uint64 length;
} sendfile;
#endif
};
/* send function */
static int tcp_send(tcp_descriptor* desc, char* ptr, ErlDrvSizeT len);
static int tcp_sendv(tcp_descriptor* desc, ErlIOVec* ev);
static int tcp_recv(tcp_descriptor* desc, int request_len);
static int tcp_deliver(tcp_descriptor* desc, int len);
static int tcp_shutdown_error(tcp_descriptor* desc, int err);
#ifdef HAVE_SENDFILE
static int tcp_inet_sendfile(tcp_descriptor* desc);
static int tcp_sendfile_aborted(tcp_descriptor* desc, int socket_error);
#endif
static int tcp_inet_output(tcp_descriptor* desc, HANDLE event);
static int tcp_inet_input(tcp_descriptor* desc, HANDLE event);
static void tcp_desc_close(tcp_descriptor*);
#ifdef HAVE_UDP
typedef struct {
inet_descriptor inet; /* common data structure (DON'T MOVE) */
int read_packets; /* Number of packets to read per invocation */
int i_bufsz; /* current input buffer size */
ErlDrvBinary* i_buf; /* current binary buffer */
char* i_ptr; /* current pos in buf */
} udp_descriptor;
static int packet_inet_input(udp_descriptor* udesc, HANDLE event);
#endif
/* convert descriptor pointer to inet_descriptor pointer */
#define INETP(d) (&(d)->inet)
#define NEW_ASYNC_ID(desc) ((desc)->op_ref++ & 0xffff)
/* check for transition from active to passive */
#define INET_CHECK_ACTIVE_TO_PASSIVE(inet) \
do { \
if ((inet)->active == INET_ONCE) \
(inet)->active = INET_PASSIVE; \
else if ((inet)->active == INET_MULTI && --((inet)->active_count) == 0) { \
(inet)->active = INET_PASSIVE; \
packet_passive_message(inet); \
} \
} while (0)
static ErlDrvTermData am_ok;
static ErlDrvTermData am_undefined;
static ErlDrvTermData am_unspec;
static ErlDrvTermData am_tcp;
static ErlDrvTermData am_error;
static ErlDrvTermData am_einval;
static ErlDrvTermData am_inet_async;
static ErlDrvTermData am_inet_reply;
static ErlDrvTermData am_timeout;
static ErlDrvTermData am_closed;
static ErlDrvTermData am_tcp_passive;
static ErlDrvTermData am_tcp_closed;
static ErlDrvTermData am_tcp_error;
static ErlDrvTermData am_empty_out_q;
static ErlDrvTermData am_ssl_tls;
#ifdef HAVE_UDP
static ErlDrvTermData am_udp;
static ErlDrvTermData am_udp_passive;
static ErlDrvTermData am_udp_error;
#endif
#ifdef HAVE_SYS_UN_H
static ErlDrvTermData am_local;
#endif
#ifndef __WIN32__
static ErlDrvTermData am_tos;
static ErlDrvTermData am_tclass;
static ErlDrvTermData am_ttl;
#endif
#ifdef HAVE_SCTP
static ErlDrvTermData am_sctp;
static ErlDrvTermData am_sctp_passive;
static ErlDrvTermData am_sctp_error;
static ErlDrvTermData am_true;
static ErlDrvTermData am_false;
static ErlDrvTermData am_buffer;
static ErlDrvTermData am_mode;
static ErlDrvTermData am_list;
static ErlDrvTermData am_binary;
static ErlDrvTermData am_active;
static ErlDrvTermData am_once;
static ErlDrvTermData am_multi;
static ErlDrvTermData am_buffer;
static ErlDrvTermData am_linger;
static ErlDrvTermData am_recbuf;
static ErlDrvTermData am_sndbuf;
static ErlDrvTermData am_reuseaddr;
static ErlDrvTermData am_dontroute;
static ErlDrvTermData am_priority;
static ErlDrvTermData am_recvtos;
static ErlDrvTermData am_recvtclass;
static ErlDrvTermData am_recvttl;
static ErlDrvTermData am_ipv6_v6only;
static ErlDrvTermData am_netns;
static ErlDrvTermData am_bind_to_device;
#endif
#ifdef HAVE_SENDFILE
static ErlDrvTermData am_sendfile;
#endif
static char str_eafnosupport[] = "eafnosupport";
static char str_einval[] = "einval";
/* special errors for bad ports and sequences */
#define EXBADPORT "exbadport"
#define EXBADSEQ "exbadseq"
static int inet_init(void);
static ErlDrvSSizeT ctl_reply(int, char*, ErlDrvSizeT, char**, ErlDrvSizeT);
struct erl_drv_entry inet_driver_entry =
{
inet_init, /* inet_init will add TCP, UDP and SCTP drivers */
NULL, /* start */
NULL, /* stop */
NULL, /* output */
NULL, /* ready_input */
NULL, /* ready_output */
"inet",
NULL,
NULL, /* handle */
NULL, /* control */
NULL, /* timeout */
NULL, /* outputv */
NULL, /* ready_async */
NULL, /* flush */
NULL, /* call */
NULL, /* event */
ERL_DRV_EXTENDED_MARKER,
ERL_DRV_EXTENDED_MAJOR_VERSION,
ERL_DRV_EXTENDED_MINOR_VERSION,
0,
NULL,
NULL,
NULL,
};
#if HAVE_IN6
# if ! defined(HAVE_IN6ADDR_ANY) || ! HAVE_IN6ADDR_ANY
# if HAVE_DECL_IN6ADDR_ANY_INIT
static const struct in6_addr in6addr_any = { { IN6ADDR_ANY_INIT } };
# else
static const struct in6_addr in6addr_any =
{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
# endif /* HAVE_IN6ADDR_ANY_INIT */
# endif /* ! HAVE_DECL_IN6ADDR_ANY */
# if ! defined(HAVE_IN6ADDR_LOOPBACK) || ! HAVE_IN6ADDR_LOOPBACK
# if HAVE_DECL_IN6ADDR_LOOPBACK_INIT
static const struct in6_addr in6addr_loopback =
{ { IN6ADDR_LOOPBACK_INIT } };
# else
static const struct in6_addr in6addr_loopback =
{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
# endif /* HAVE_IN6ADDR_LOOPBACk_INIT */
# endif /* ! HAVE_DECL_IN6ADDR_LOOPBACK */
#endif /* HAVE_IN6 */
/* XXX: is this a driver interface function ??? */
void erts_exit(int n, char*, ...);
/*
* Malloc wrapper,
* we would like to change the behaviour for different
* systems here.
*/
#ifdef FATAL_MALLOC
static void *alloc_wrapper(ErlDrvSizeT size){
void *ret = driver_alloc(size);
if(ret == NULL)
erts_exit(ERTS_ERROR_EXIT,"Out of virtual memory in malloc (%s)", __FILE__);
return ret;
}
#define ALLOC(X) alloc_wrapper(X)
static void *realloc_wrapper(void *current, ErlDrvSizeT size){
void *ret = driver_realloc(current,size);
if(ret == NULL)
erts_exit(ERTS_ERROR_EXIT,"Out of virtual memory in realloc (%s)", __FILE__);
return ret;
}
#define REALLOC(X,Y) realloc_wrapper(X,Y)
#define FREE(P) driver_free((P))
#else /* FATAL_MALLOC */
#define ALLOC(X) driver_alloc((X))
#define REALLOC(X,Y) driver_realloc((X), (Y))
#define FREE(P) driver_free((P))
#endif /* FATAL_MALLOC */
#define INIT_ATOM(NAME) am_ ## NAME = driver_mk_atom(#NAME)
#define LOAD_ATOM_CNT 2
#define LOAD_ATOM(vec, i, atom) \
(((vec)[(i)] = ERL_DRV_ATOM), \
((vec)[(i)+1] = (atom)), \
((i)+LOAD_ATOM_CNT))
#define LOAD_INT_CNT 2
#define LOAD_INT(vec, i, val) \
(((vec)[(i)] = ERL_DRV_INT), \
((vec)[(i)+1] = (ErlDrvTermData)(val)), \
((i)+LOAD_INT_CNT))
#define LOAD_UINT_CNT 2
#define LOAD_UINT(vec, i, val) \
(((vec)[(i)] = ERL_DRV_UINT), \
((vec)[(i)+1] = (ErlDrvTermData)(val)), \
((i)+LOAD_UINT_CNT))
#define LOAD_PORT_CNT 2
#define LOAD_PORT(vec, i, port) \
(((vec)[(i)] = ERL_DRV_PORT), \
((vec)[(i)+1] = (port)), \
((i)+LOAD_PORT_CNT))
#define LOAD_PID_CNT 2
#define LOAD_PID(vec, i, pid) \
(((vec)[(i)] = ERL_DRV_PID), \
((vec)[(i)+1] = (pid)), \
((i)+LOAD_PID_CNT))
#define LOAD_BINARY_CNT 4
#define LOAD_BINARY(vec, i, bin, offs, len) \
(((vec)[(i)] = ERL_DRV_BINARY), \
((vec)[(i)+1] = (ErlDrvTermData)(bin)), \
((vec)[(i)+2] = (len)), \
((vec)[(i)+3] = (offs)), \
((i)+LOAD_BINARY_CNT))
#define LOAD_BUF2BINARY_CNT 3
#define LOAD_BUF2BINARY(vec, i, buf, len) \
(((vec)[(i)] = ERL_DRV_BUF2BINARY), \
((vec)[(i)+1] = (ErlDrvTermData)(buf)), \
((vec)[(i)+2] = (len)), \
((i)+LOAD_BUF2BINARY_CNT))
#define LOAD_STRING_CNT 3
#define LOAD_STRING(vec, i, str, len) \
(((vec)[(i)] = ERL_DRV_STRING), \
((vec)[(i)+1] = (ErlDrvTermData)(str)), \
((vec)[(i)+2] = (len)), \
((i)+LOAD_STRING_CNT))
#define LOAD_STRING_CONS_CNT 3
#define LOAD_STRING_CONS(vec, i, str, len) \
(((vec)[(i)] = ERL_DRV_STRING_CONS), \
((vec)[(i)+1] = (ErlDrvTermData)(str)), \
((vec)[(i)+2] = (len)), \
((i)+LOAD_STRING_CONS_CNT))
#define LOAD_TUPLE_CNT 2
#define LOAD_TUPLE(vec, i, size) \
(((vec)[(i)] = ERL_DRV_TUPLE), \
((vec)[(i)+1] = (size)), \
((i)+LOAD_TUPLE_CNT))
#define LOAD_NIL_CNT 1
#define LOAD_NIL(vec, i) \
(((vec)[(i)] = ERL_DRV_NIL), \
((i)+LOAD_NIL_CNT))
#define LOAD_LIST_CNT 2
#define LOAD_LIST(vec, i, size) \
(((vec)[(i)] = ERL_DRV_LIST), \
((vec)[(i)+1] = (size)), \
((i)+LOAD_LIST_CNT))
#ifdef HAVE_SCTP
/* "IS_SCTP": tells the difference between a UDP and an SCTP socket: */
# define IS_SCTP(desc)((desc)->sprotocol==IPPROTO_SCTP)
/* For AssocID, 4 bytes should be enough -- checked by "init": */
# define GET_ASSOC_ID get_int32
# define ASSOC_ID_LEN 4
# define LOAD_ASSOC_ID LOAD_UINT
# define LOAD_ASSOC_ID_CNT LOAD_UINT_CNT
#else
# define IS_SCTP(desc) 0
#endif
# define ANC_BUFF_SIZE INET_DEF_BUFFER/2 /* XXX: not very good... */
#ifdef HAVE_UDP
static int load_address(ErlDrvTermData* spec, int i, char* buf)
{
int n;
switch (*buf++) { /* Family */
case INET_AF_INET: {
for (n = 2; n < 2+4; n++) {
spec[i++] = ERL_DRV_INT;
spec[i++] = (ErlDrvTermData) ((unsigned char)buf[n]);
}
spec[i++] = ERL_DRV_TUPLE;
spec[i++] = 4;
spec[i++] = ERL_DRV_INT;
spec[i++] = (ErlDrvTermData) get_int16(buf);
break;
}
#if defined(HAVE_IN6) && defined(AF_INET6)
case INET_AF_INET6: {
for (n = 2; n < 2+16; n += 2) {
spec[i++] = ERL_DRV_INT;
spec[i++] = (ErlDrvTermData) get_int16(buf+n);
}
spec[i++] = ERL_DRV_TUPLE;
spec[i++] = 8;
spec[i++] = ERL_DRV_INT;
spec[i++] = (ErlDrvTermData) get_int16(buf);
break;
}
#endif
#ifdef HAVE_SYS_UN_H
case INET_AF_LOCAL: {
int len = *(unsigned char*)buf++;
i = LOAD_ATOM(spec, i, am_local);
i = LOAD_BUF2BINARY(spec, i, buf, len);
spec[i++] = ERL_DRV_TUPLE;
spec[i++] = 2;
spec[i++] = ERL_DRV_INT;
spec[i++] = 0;
break;
}
#endif
case INET_AF_UNSPEC: {
i = LOAD_ATOM(spec, i, am_unspec);
i = LOAD_BUF2BINARY(spec, i, buf, 0);
spec[i++] = ERL_DRV_TUPLE;
spec[i++] = 2;
spec[i++] = ERL_DRV_INT;
spec[i++] = 0;
break;
}
default: { /* INET_AF_UNDEFINED */
i = LOAD_ATOM(spec, i, am_undefined);
i = LOAD_BUF2BINARY(spec, i, buf, 0);
spec[i++] = ERL_DRV_TUPLE;
spec[i++] = 2;
spec[i++] = ERL_DRV_INT;
spec[i++] = 0;
break;
}
}
return i;
}
#endif
#ifdef HAVE_SCTP
/* For SCTP, we often need to return {IP, Port} tuples: */
static int inet_get_address(char* dst, inet_address* src, unsigned int* len);
/* Max of {{int()*8},int()} | {{int()*4},int()} |
* {{'local',binary()},int()}
*/
#define LOAD_INET_GET_ADDRESS_CNT \
(8*LOAD_INT_CNT + LOAD_TUPLE_CNT + LOAD_INT_CNT + LOAD_TUPLE_CNT)
static int load_inet_get_address
(ErlDrvTermData* spec, int i, inet_descriptor* desc,
struct sockaddr_storage* addr)
{
/* The size of the buffer used to stringify the addr is the same as
that of "sockaddr_storage" itself: only their layout is different:
*/
unsigned int len = sizeof(struct sockaddr_storage);
unsigned int alen = len;
char abuf [len];
int res = inet_get_address(abuf, (inet_address*) addr, &alen);
ASSERT(res==0); (void)res;
/* Now "abuf" contains: Family(1b), Port(2b), IP(4|16b) */
/* NB: the following functions are safe to use, as they create tuples
of copied Ints on the "spec", and do not install any String pts --
a ptr to "abuf" would be dangling upon exiting this function: */
i = load_address(spec, i, abuf); /* IP,Port | Family,Addr */
i = LOAD_TUPLE (spec, i, 2);
return i;
}
/* Loading Boolean flags as Atoms: */
#define LOAD_BOOL_CNT LOAD_ATOM_CNT
#define LOAD_BOOL(spec, i, flag) \
LOAD_ATOM((spec), (i), (flag) ? am_true : am_false);
#endif /* HAVE_SCTP */
/* Assume a cache line size of 64 bytes */
#define INET_DRV_CACHE_LINE_SIZE ((ErlDrvUInt) 64)
#define INET_DRV_CACHE_LINE_MASK (INET_DRV_CACHE_LINE_SIZE - 1)
/*
** Binary Buffer Managment
** We keep a stack of usable buffers
*/
#define BUFFER_STACK_SIZE 14
#define BUFFER_STACK_MAX_MEM_SIZE (1024*1024)
ErlDrvTSDKey buffer_stack_key;
typedef struct {
int mem_size;
int pos;
ErlDrvBinary* stk[BUFFER_STACK_SIZE];
} InetDrvBufStkBase;
typedef struct {
InetDrvBufStkBase buf;
char align[(((sizeof(InetDrvBufStkBase) - 1) / INET_DRV_CACHE_LINE_SIZE) + 1)
* INET_DRV_CACHE_LINE_SIZE];
} InetDrvBufStk;
static InetDrvBufStk *get_bufstk(void)
{
InetDrvBufStk *bs = erl_drv_tsd_get(buffer_stack_key);
if (bs)
return bs;
bs = driver_alloc(sizeof(InetDrvBufStk)
+ INET_DRV_CACHE_LINE_SIZE - 1);
if (!bs)
return NULL;
if ((((ErlDrvUInt) bs) & INET_DRV_CACHE_LINE_MASK) != 0)
bs = ((InetDrvBufStk *)
((((ErlDrvUInt) bs) & ~INET_DRV_CACHE_LINE_MASK)
+ INET_DRV_CACHE_LINE_SIZE));
erl_drv_tsd_set(buffer_stack_key, bs);
bs->buf.pos = 0;
bs->buf.mem_size = 0;
ASSERT(bs == erl_drv_tsd_get(buffer_stack_key));
return bs;
}
static ErlDrvBinary* alloc_buffer(ErlDrvSizeT minsz)
{
InetDrvBufStk *bs = get_bufstk();
DEBUGF(("alloc_buffer: "LLU"\r\n", (llu_t)minsz));
if (bs && bs->buf.pos > 0) {
long size;
ErlDrvBinary* buf = bs->buf.stk[--bs->buf.pos];
size = buf->orig_size;
bs->buf.mem_size -= size;
ASSERT(0 <= bs->buf.mem_size
&& bs->buf.mem_size <= BUFFER_STACK_MAX_MEM_SIZE);
if (size >= minsz)
return buf;
driver_free_binary(buf);
}
ASSERT(!bs || bs->buf.pos != 0 || bs->buf.mem_size == 0);
return driver_alloc_binary(minsz);
}
/*#define CHECK_DOUBLE_RELEASE 1*/
#ifdef CHECK_DOUBLE_RELEASE
static void
check_double_release(InetDrvBufStk *bs, ErlDrvBinary* buf)
{
#ifdef __GNUC__
#warning CHECK_DOUBLE_RELEASE is enabled, this is a custom build emulator
#endif
int i;
for (i = 0; i < bs->buf.pos; ++i) {
if (bs->buf.stk[i] == buf) {
erts_exit(ERTS_ABORT_EXIT,
"Multiple buffer release in inet_drv, this "
"is a bug, save the core and send it to "
"support@erlang.ericsson.se!");
}
}
}
#endif
static void release_buffer(ErlDrvBinary* buf)
{
InetDrvBufStk *bs;
long size;
DEBUGF(("release_buffer: %ld\r\n", (buf==NULL) ? 0 : buf->orig_size));
if (!buf)
return;
size = buf->orig_size;
if (size > BUFFER_STACK_MAX_MEM_SIZE)
goto free_binary;
bs = get_bufstk();
if (!bs
|| (bs->buf.mem_size + size > BUFFER_STACK_MAX_MEM_SIZE)
|| (bs->buf.pos >= BUFFER_STACK_SIZE)) {
free_binary:
driver_free_binary(buf);
}
else {
#ifdef CHECK_DOUBLE_RELEASE
check_double_release(bs, buf);
#endif
ASSERT(bs->buf.pos != 0 || bs->buf.mem_size == 0);
bs->buf.mem_size += size;
bs->buf.stk[bs->buf.pos++] = buf;
ASSERT(0 <= bs->buf.mem_size
&& bs->buf.mem_size <= BUFFER_STACK_MAX_MEM_SIZE);
}
}
#ifdef HAVE_UDP
static ErlDrvBinary* realloc_buffer(ErlDrvBinary* buf, ErlDrvSizeT newsz)
{
DEBUGF(("realloc_buffer: %ld -> %ld\r\n", (buf==NULL) ? 0 : buf->orig_size, newsz));
return driver_realloc_binary(buf, newsz);
}
#endif
/* use a TRICK, access the refc field to see if any one else has
* a ref to this buffer then call driver_free_binary else
* release_buffer instead
*/
static void free_buffer(ErlDrvBinary* buf)
{
DEBUGF(("free_buffer: %ld\r\n", (buf==NULL) ? 0 : buf->orig_size));
if (buf != NULL) {
if (driver_binary_get_refc(buf) == 1)
release_buffer(buf);
else
driver_free_binary(buf);
}
}
#ifdef __WIN32__
static ErlDrvData dummy_start(ErlDrvPort port, char* command)
{
return (ErlDrvData)port;
}
static ErlDrvSSizeT dummy_ctl(ErlDrvData data, unsigned int cmd,
char* buf, ErlDrvSizeT len, char** rbuf,
ErlDrvSizeT rsize)
{
static char error[] = "no_winsock2";
driver_failure_atom((ErlDrvPort)data, error);
return ctl_reply(INET_REP_ERROR, error, sizeof(error), rbuf, rsize);
}
static void dummy_command(ErlDrvData data, char* buf, ErlDrvSizeT len)
{
}
static struct erl_drv_entry dummy_tcp_driver_entry =
{
NULL, /* init */
dummy_start, /* start */
NULL, /* stop */
dummy_command, /* command */
NULL, /* input */
NULL, /* output */
"tcp_inet", /* name */
NULL,
NULL,
dummy_ctl,
NULL,
NULL
};
static struct erl_drv_entry dummy_udp_driver_entry =
{
NULL, /* init */
dummy_start, /* start */
NULL, /* stop */
dummy_command, /* command */
NULL, /* input */
NULL, /* output */
"udp_inet", /* name */
NULL,
NULL,
dummy_ctl,
NULL,
NULL
};
#ifdef HAVE_SCTP
static struct erl_drv_entry dummy_sctp_driver_entry =
{ /* Though there is no SCTP for Win32 yet... */
NULL, /* init */
dummy_start, /* start */
NULL, /* stop */
dummy_command, /* command */
NULL, /* input */
NULL, /* output */
"sctp_inet", /* name */
NULL,
NULL,
dummy_ctl,
NULL,
NULL
};
#endif
#endif
/* return lowercase string form of errno value */
static char *errno_str(int err)
{
switch (err) {
case INET_ERRNO_SYSTEM_LIMIT:
return "system_limit";
default:
return erl_errno_id(err);
}
}
/* general control reply function */
static ErlDrvSSizeT ctl_reply(int rep, char* buf, ErlDrvSizeT len,
char** rbuf, ErlDrvSizeT rsize)
{
char* ptr;
if ((len+1) > rsize) {
ptr = ALLOC(len+1);
*rbuf = ptr;
}
else
ptr = *rbuf;
*ptr++ = rep;
memcpy(ptr, buf, len);
return len+1;
}
/* general control error reply function */
static ErlDrvSSizeT ctl_error(int err, char** rbuf, ErlDrvSizeT rsize)
{
char* s = errno_str(err);
return ctl_reply(INET_REP_ERROR, s, strlen(s), rbuf, rsize);
}
static ErlDrvSSizeT ctl_xerror(char* xerr, char** rbuf, ErlDrvSizeT rsize)
{
int n = strlen(xerr);
return ctl_reply(INET_REP_ERROR, xerr, n, rbuf, rsize);
}
static ErlDrvTermData error_atom(int err)
{
return driver_mk_atom(errno_str(err));
}
static void enq_old_multi_op(tcp_descriptor *desc, int id, int req,
ErlDrvTermData caller, MultiTimerData *timeout,
ErlDrvMonitor *monitorp)
{
inet_async_multi_op *opp;
opp = ALLOC(sizeof(inet_async_multi_op));
opp->op.id = id;
opp->op.caller = caller;
opp->op.req = req;
opp->op.tmo.mtd = timeout;
memcpy(&(opp->op.monitor), monitorp, sizeof(ErlDrvMonitor));
opp->next = NULL;
if (desc->multi_first == NULL) {
desc->multi_first = opp;
} else {
desc->multi_last->next = opp;
}
desc->multi_last = opp;
}
static void enq_multi_op(tcp_descriptor *desc, char *buf, int req,
ErlDrvTermData caller, MultiTimerData *timeout,
ErlDrvMonitor *monitorp)
{
int id = NEW_ASYNC_ID(INETP(desc));
enq_old_multi_op(desc,id,req,caller,timeout,monitorp);
if (buf != NULL)
put_int16(id, buf);
}
static int deq_multi_op(tcp_descriptor *desc, int *id_p, int *req_p,
ErlDrvTermData *caller_p, MultiTimerData **timeout_p,
ErlDrvMonitor *monitorp)
{
inet_async_multi_op *opp;
opp = desc->multi_first;
if (!opp) {
return -1;
}
desc->multi_first = opp->next;
if (desc->multi_first == NULL) {
desc->multi_last = NULL;
}
*id_p = opp->op.id;
*req_p = opp->op.req;
*caller_p = opp->op.caller;
if (timeout_p != NULL) {
*timeout_p = opp->op.tmo.mtd;
}
if (monitorp != NULL) {
memcpy(monitorp,&(opp->op.monitor),sizeof(ErlDrvMonitor));
}
FREE(opp);
return 0;
}
static int remove_multi_op(tcp_descriptor *desc, int *id_p, int *req_p,
ErlDrvTermData caller, MultiTimerData **timeout_p,
ErlDrvMonitor *monitorp)
{
inet_async_multi_op *opp, *slap;
for (opp = desc->multi_first, slap = NULL;
opp != NULL && opp->op.caller != caller;
slap = opp, opp = opp->next)
;
if (!opp) {
return -1;
}
if (slap == NULL) {
desc->multi_first = opp->next;
} else {
slap->next = opp->next;
}
if (desc->multi_last == opp) {
desc->multi_last = slap;
}
*id_p = opp->op.id;
*req_p = opp->op.req;
if (timeout_p != NULL) {
*timeout_p = opp->op.tmo.mtd;
}
if (monitorp != NULL) {
memcpy(monitorp,&(opp->op.monitor),sizeof(ErlDrvMonitor));
}
FREE(opp);
return 0;
}
/* setup a new async id + caller (format async_id into buf) */
static int enq_async_w_tmo(inet_descriptor* desc, char* buf, int req, unsigned timeout,
ErlDrvMonitor *monitorp)
{
int id = NEW_ASYNC_ID(desc);
inet_async_op* opp;
if ((opp = desc->oph) == NULL) /* queue empty */
opp = desc->oph = desc->opt = desc->op_queue;
else if (desc->oph == desc->opt) { /* queue full */
DEBUGF(("enq(%ld): queue full\r\n", (long)desc->port));
return -1;
}
opp->id = id;
opp->caller = driver_caller(desc->port);
opp->req = req;
opp->tmo.value = timeout;
if (monitorp != NULL) {
memcpy(&(opp->monitor),monitorp,sizeof(ErlDrvMonitor));
}
DEBUGF(("enq(%ld): %d %ld %d\r\n",
(long) desc->port, opp->id, opp->caller, opp->req));
opp++;
if (opp >= desc->op_queue + INET_MAX_ASYNC)
desc->oph = desc->op_queue;
else
desc->oph = opp;
if (buf != NULL)
put_int16(id, buf);
return 0;
}
static int enq_async(inet_descriptor* desc, char* buf, int req)
{
return enq_async_w_tmo(desc,buf,req,INET_INFINITY, NULL);
}
static int deq_async_w_tmo(inet_descriptor* desc, int* ap, ErlDrvTermData* cp,
int* rp, unsigned *tp, ErlDrvMonitor *monitorp)
{
inet_async_op* opp;
if ((opp = desc->opt) == NULL) { /* queue empty */
DEBUGF(("deq(%ld): queue empty\r\n", (long)desc->port));
return -1;
}
*ap = opp->id;
*cp = opp->caller;
*rp = opp->req;
if (tp != NULL) {
*tp = opp->tmo.value;
}
if (monitorp != NULL) {
memcpy(monitorp,&(opp->monitor),sizeof(ErlDrvMonitor));
}
DEBUGF(("deq(%ld): %d %ld %d\r\n",
(long)desc->port, opp->id, opp->caller, opp->req));
opp++;
if (opp >= desc->op_queue + INET_MAX_ASYNC)
desc->opt = desc->op_queue;
else
desc->opt = opp;
if (desc->opt == desc->oph)
desc->opt = desc->oph = NULL;
return 0;
}
static int deq_async(inet_descriptor* desc, int* ap, ErlDrvTermData* cp, int* rp)
{
return deq_async_w_tmo(desc,ap,cp,rp,NULL,NULL);
}
/* send message:
** {inet_async, Port, Ref, ok}
*/
static int
send_async_ok(ErlDrvTermData Port, int Ref,ErlDrvTermData recipient)
{
ErlDrvTermData spec[2*LOAD_ATOM_CNT + LOAD_PORT_CNT +
LOAD_INT_CNT + LOAD_TUPLE_CNT];
int i = 0;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, Port);
i = LOAD_INT(spec, i, Ref);
i = LOAD_ATOM(spec, i, am_ok);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i == sizeof(spec)/sizeof(*spec));
return erl_drv_send_term(Port, recipient, spec, i);
}
/* send message:
** {inet_async, Port, Ref, {ok,Port2}}
*/
static int
send_async_ok_port(ErlDrvTermData Port, int Ref,
ErlDrvTermData recipient, ErlDrvTermData Port2)
{
ErlDrvTermData spec[2*LOAD_ATOM_CNT + 2*LOAD_PORT_CNT +
LOAD_INT_CNT + 2*LOAD_TUPLE_CNT];
int i = 0;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, Port);
i = LOAD_INT(spec, i, Ref);
{
i = LOAD_ATOM(spec, i, am_ok);
i = LOAD_PORT(spec, i, Port2);
i = LOAD_TUPLE(spec, i, 2);
}
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i == sizeof(spec)/sizeof(*spec));
return erl_drv_send_term(Port, recipient, spec, i);
}
/* send message:
** {inet_async, Port, Ref, {error,Reason}}
*/
static int
send_async_error(ErlDrvTermData Port, int Ref,
ErlDrvTermData recipient, ErlDrvTermData Reason)
{
ErlDrvTermData spec[3*LOAD_ATOM_CNT + LOAD_PORT_CNT +
LOAD_INT_CNT + 2*LOAD_TUPLE_CNT];
int i = 0;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, Port);
i = LOAD_INT(spec, i, Ref);
{
i = LOAD_ATOM(spec, i, am_error);
i = LOAD_ATOM(spec, i, Reason);
i = LOAD_TUPLE(spec, i, 2);
}
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i == sizeof(spec)/sizeof(*spec));
DEBUGF(("send_async_error %ld %ld\r\n", recipient, Reason));
return erl_drv_send_term(Port, recipient, spec, i);
}
static int async_ok(inet_descriptor* desc)
{
int req;
int aid;
ErlDrvTermData caller;
if (deq_async(desc, &aid, &caller, &req) < 0)
return -1;
return send_async_ok(desc->dport, aid, caller);
}
static int async_ok_port(inet_descriptor* desc, ErlDrvTermData Port2)
{
int req;
int aid;
ErlDrvTermData caller;
if (deq_async(desc, &aid, &caller, &req) < 0)
return -1;
return send_async_ok_port(desc->dport, aid, caller, Port2);
}
static int async_error_am(inet_descriptor* desc, ErlDrvTermData reason)
{
int req;
int aid;
ErlDrvTermData caller;
if (deq_async(desc, &aid, &caller, &req) < 0)
return -1;
return send_async_error(desc->dport, aid, caller, reason);
}
/* dequeue all operations */
static int async_error_am_all(inet_descriptor* desc, ErlDrvTermData reason)
{
int req;
int aid;
ErlDrvTermData caller;
while (deq_async(desc, &aid, &caller, &req) == 0) {
send_async_error(desc->dport, aid, caller, reason);
}
return 0;
}
static int async_error(inet_descriptor* desc, int err)
{
return async_error_am(desc, error_atom(err));
}
/* send:
** {inet_reply, S, ok}
*/
static int inet_reply_ok(inet_descriptor* desc)
{
ErlDrvTermData spec[2*LOAD_ATOM_CNT + LOAD_PORT_CNT + LOAD_TUPLE_CNT];
ErlDrvTermData caller = desc->caller;
int i = 0;
desc->caller = 0;
if (is_not_internal_pid(caller))
return 0;
i = LOAD_ATOM(spec, i, am_inet_reply);
i = LOAD_PORT(spec, i, desc->dport);
i = LOAD_ATOM(spec, i, am_ok);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i == sizeof(spec)/sizeof(*spec));
return erl_drv_send_term(desc->dport, caller, spec, i);
}
#ifdef HAVE_SCTP
static int inet_reply_ok_port(inet_descriptor* desc, ErlDrvTermData dport)
{
ErlDrvTermData spec[2*LOAD_ATOM_CNT + 2*LOAD_PORT_CNT + 2*LOAD_TUPLE_CNT];
ErlDrvTermData caller = desc->caller;
int i = 0;
i = LOAD_ATOM(spec, i, am_inet_reply);
i = LOAD_PORT(spec, i, desc->dport);
i = LOAD_ATOM(spec, i, am_ok);
i = LOAD_PORT(spec, i, dport);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i == sizeof(spec)/sizeof(*spec));
desc->caller = 0;
return erl_drv_send_term(desc->dport, caller, spec, i);
}
#endif
/* send:
** {inet_reply, S, {error, Reason}}
*/
static int inet_reply_error_am(inet_descriptor* desc, ErlDrvTermData reason)
{
ErlDrvTermData spec[3*LOAD_ATOM_CNT + LOAD_PORT_CNT + 2*LOAD_TUPLE_CNT];
ErlDrvTermData caller = desc->caller;
int i = 0;
i = LOAD_ATOM(spec, i, am_inet_reply);
i = LOAD_PORT(spec, i, desc->dport);
i = LOAD_ATOM(spec, i, am_error);
i = LOAD_ATOM(spec, i, reason);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i == sizeof(spec)/sizeof(*spec));
desc->caller = 0;
DEBUGF(("inet_reply_error_am %ld %ld\r\n", caller, reason));
return erl_drv_send_term(desc->dport, caller, spec, i);
}
/* send:
** {inet_reply, S, {error, Reason}}
*/
static int inet_reply_error(inet_descriptor* desc, int err)
{
return inet_reply_error_am(desc, error_atom(err));
}
/*
** Deliver port data from buffer
*/
static int inet_port_data(inet_descriptor* desc, const char* buf, int len)
{
unsigned int hsz = desc->hsz;
DEBUGF(("inet_port_data(%ld): len = %d\r\n", (long)desc->port, len));
if ((desc->mode == INET_MODE_LIST) || (hsz > len))
return driver_output2(desc->port, (char*)buf, len, NULL, 0);
else if (hsz > 0)
return driver_output2(desc->port, (char*)buf, hsz, (char*)buf+hsz, len-hsz);
else
return driver_output(desc->port, (char*)buf, len);
}
/*
** Deliver port data from binary (for an active mode socket)
*/
static int
inet_port_binary_data(inet_descriptor* desc, ErlDrvBinary* bin, int offs, int len)
{
unsigned int hsz = desc->hsz;
DEBUGF(("inet_port_binary_data(%ld): offs=%d, len = %d\r\n",
(long)desc->port, offs, len));
if ((desc->mode == INET_MODE_LIST) || (hsz > len))
return driver_output2(desc->port, bin->orig_bytes+offs, len, NULL, 0);
else
return driver_output_binary(desc->port, bin->orig_bytes+offs, hsz,
bin, offs+hsz, len-hsz);
}
static ErlDrvTermData am_http_eoh;
static ErlDrvTermData am_http_header;
static ErlDrvTermData am_http_request;
static ErlDrvTermData am_http_response;
static ErlDrvTermData am_http_error;
static ErlDrvTermData am_abs_path;
static ErlDrvTermData am_absoluteURI;
static ErlDrvTermData am_star;
static ErlDrvTermData am_http;
static ErlDrvTermData am_https;
static ErlDrvTermData am_scheme;
static int http_load_string(tcp_descriptor* desc, ErlDrvTermData* spec, int i,
const char* str, int len)
{
if (desc->inet.htype >= TCP_PB_HTTP_BIN) {
ASSERT(desc->inet.htype == TCP_PB_HTTP_BIN ||
desc->inet.htype == TCP_PB_HTTPH_BIN);
i = LOAD_BUF2BINARY(spec, i, str, len);
} else {
i = LOAD_STRING(spec, i, str, len);
}
return i;
}
static int http_response_inetdrv(void *arg, int major, int minor,
int status, const char* phrase, int phrase_len)
{
tcp_descriptor* desc = (tcp_descriptor*) arg;
int i = 0;
ErlDrvTermData spec[27];
ErlDrvTermData caller = ERL_DRV_NIL;
if (desc->inet.active == INET_PASSIVE) {
/* {inet_async,S,Ref,{ok,{http_response,Version,Status,Phrase}}} */
int req;
int aid;
if (deq_async(INETP(desc), &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
}
else {
/* {http, S, {http_response,Version,Status,Phrase}} */
i = LOAD_ATOM(spec, i, am_http);
i = LOAD_PORT(spec, i, desc->inet.dport);
}
i = LOAD_ATOM(spec, i, am_http_response);
i = LOAD_INT(spec, i, major);
i = LOAD_INT(spec, i, minor);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_INT(spec, i, status);
i = http_load_string(desc, spec, i, phrase, phrase_len);
i = LOAD_TUPLE(spec, i, 4);
if (desc->inet.active == INET_PASSIVE) {
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i<=27);
return erl_drv_send_term(desc->inet.dport, caller, spec, i);
}
else {
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i<=27);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
}
static int http_load_uri(tcp_descriptor* desc, ErlDrvTermData* spec, int i,
const PacketHttpURI* uri)
{
ErlDrvTermData scheme;
switch (uri->type) {
case URI_STAR:
i = LOAD_ATOM(spec, i, am_star);
break;
case URI_ABS_PATH:
i = LOAD_ATOM(spec, i, am_abs_path);
i = http_load_string(desc, spec, i, uri->s1_ptr, uri->s1_len);
i = LOAD_TUPLE(spec, i, 2);
break;
case URI_HTTP:
scheme = am_http;
goto http_common;
case URI_HTTPS:
scheme = am_https;
http_common:
i = LOAD_ATOM(spec, i, am_absoluteURI);
i = LOAD_ATOM(spec, i, scheme);
i = http_load_string(desc, spec, i, uri->s1_ptr, uri->s1_len);
if (uri->port == 0) {
i = LOAD_ATOM(spec, i, am_undefined);
} else {
i = LOAD_INT(spec, i, uri->port);
}
i = http_load_string(desc, spec, i, uri->s2_ptr, uri->s2_len);
i = LOAD_TUPLE(spec, i, 5);
break;
case URI_STRING:
i = http_load_string(desc, spec, i, uri->s1_ptr, uri->s1_len);
break;
case URI_SCHEME:
i = LOAD_ATOM(spec, i, am_scheme);
i = http_load_string(desc, spec, i, uri->s1_ptr, uri->s1_len);
i = http_load_string(desc, spec, i, uri->s2_ptr, uri->s2_len);
i = LOAD_TUPLE(spec, i, 3);
}
return i;
}
static int
http_request_inetdrv(void* arg, const http_atom_t* meth, const char* meth_ptr,
int meth_len, const PacketHttpURI* uri,
int major, int minor)
{
tcp_descriptor* desc = (tcp_descriptor*) arg;
int i = 0;
ErlDrvTermData spec[43];
ErlDrvTermData caller = ERL_DRV_NIL;
if (desc->inet.active == INET_PASSIVE) {
/* {inet_async, S, Ref, {ok,{http_request,Meth,Uri,Version}}} */
int req;
int aid;
if (deq_async(INETP(desc), &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
}
else {
/* {http, S, {http_request,Meth,Uri,Version}}} */
i = LOAD_ATOM(spec, i, am_http);
i = LOAD_PORT(spec, i, desc->inet.dport);
}
i = LOAD_ATOM(spec, i, am_http_request);
if (meth != NULL)
i = LOAD_ATOM(spec, i, meth->atom);
else
i = http_load_string(desc, spec, i, meth_ptr, meth_len);
i = http_load_uri(desc, spec, i, uri);
i = LOAD_INT(spec, i, major);
i = LOAD_INT(spec, i, minor);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
if (desc->inet.active == INET_PASSIVE) {
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= 43);
return erl_drv_send_term(desc->inet.dport, caller, spec, i);
}
else {
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 43);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
}
static int
http_header_inetdrv(void* arg, const http_atom_t* name, const char* name_ptr,
int name_len, const char* value_ptr, int value_len)
{
tcp_descriptor* desc = (tcp_descriptor*) arg;
int i = 0;
ErlDrvTermData spec[26];
ErlDrvTermData caller = ERL_DRV_NIL;
if (desc->inet.active == INET_PASSIVE) {
/* {inet_async,S,Ref,{ok,{http_header,Bit,Name,IValue,Value}} */
int req;
int aid;
if (deq_async(INETP(desc), &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
}
else {
/* {http, S, {http_header,Bit,Name,IValue,Value}} */
i = LOAD_ATOM(spec, i, am_http);
i = LOAD_PORT(spec, i, desc->inet.dport);
}
i = LOAD_ATOM(spec, i, am_http_header);
if (name != NULL) {
i = LOAD_INT(spec, i, name->index+1);
i = LOAD_ATOM(spec, i, name->atom);
}
else {
i = LOAD_INT(spec, i, 0);
i = http_load_string(desc, spec, i, name_ptr, name_len);
}
i = LOAD_ATOM(spec, i, am_undefined);
i = http_load_string(desc, spec, i, value_ptr, value_len);
i = LOAD_TUPLE(spec, i, 5);
if (desc->inet.active == INET_PASSIVE) {
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= 26);
return erl_drv_send_term(desc->inet.dport, caller, spec, i);
}
else {
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 26);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
}
static int http_eoh_inetdrv(void* arg)
{
tcp_descriptor* desc = (tcp_descriptor*) arg;
int i = 0;
ErlDrvTermData spec[14];
if (desc->inet.active == INET_PASSIVE) {
/* {inet_async,S,Ref,{ok,http_eoh}} */
int req;
int aid;
ErlDrvTermData caller;
if (deq_async(INETP(desc), &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
i = LOAD_ATOM(spec, i, am_http_eoh);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= 14);
return erl_drv_send_term(desc->inet.dport, caller, spec, i);
}
else {
/* {http, S, http_eoh} */
i = LOAD_ATOM(spec, i, am_http);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_ATOM(spec, i, am_http_eoh);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 14);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
}
static int http_error_inetdrv(void* arg, const char* buf, int len)
{
tcp_descriptor* desc = (tcp_descriptor*) arg;
int i = 0;
ErlDrvTermData spec[19];
if (desc->inet.active == INET_PASSIVE) {
/* {inet_async,S,Ref,{ok,{http_error,Line}}} */
int req;
int aid;
ErlDrvTermData caller;
if (deq_async(INETP(desc), &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
i = LOAD_ATOM(spec, i, am_http_error);
i = http_load_string(desc, spec, i, buf, len);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= 19);
return erl_drv_send_term(desc->inet.dport, caller, spec, i);
}
else {
/* {http, S, {http_error,Line} */
i = LOAD_ATOM(spec, i, am_http);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_ATOM(spec, i, am_http_error);
i = http_load_string(desc, spec, i, buf, len);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 19);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
}
static
int ssl_tls_inetdrv(void* arg, unsigned type, unsigned major, unsigned minor,
const char* buf, int len, const char* prefix, int plen)
{
tcp_descriptor* desc = (tcp_descriptor*) arg;
int i = 0;
ErlDrvTermData spec[30];
ErlDrvTermData caller = ERL_DRV_NIL;
ErlDrvBinary* bin;
int ret;
if ((bin = driver_alloc_binary(plen+len)) == NULL)
return async_error(&desc->inet, ENOMEM);
memcpy(bin->orig_bytes+plen, buf, len);
if (plen) {
memcpy(bin->orig_bytes, prefix, plen);
len += plen;
}
if (desc->inet.active == INET_PASSIVE) {
/* {inet_async,S,Ref,{ok,{ssl_tls,...}}} */
int req;
int aid;
if (deq_async(INETP(desc), &aid, &caller, &req) < 0) {
ret = -1;
goto done;
}
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
}
/* {ssl_tls,S,ContentType,{Major,Minor},Bin} */
i = LOAD_ATOM(spec, i, am_ssl_tls);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_INT(spec, i, type);
i = LOAD_INT(spec, i, major);
i = LOAD_INT(spec, i, minor);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_BINARY(spec, i, bin, 0, len);
i = LOAD_TUPLE(spec, i, 5);
if (desc->inet.active == INET_PASSIVE) {
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= sizeof(spec)/sizeof(*spec));
ret = erl_drv_send_term(desc->inet.dport, caller, spec, i);
}
else {
ASSERT(i <= sizeof(spec)/sizeof(*spec));
ret = erl_drv_output_term(desc->inet.dport, spec, i);
}
done:
driver_free_binary(bin);
return ret;
}
static PacketCallbacks packet_callbacks =
{
http_response_inetdrv,
http_request_inetdrv,
http_eoh_inetdrv,
http_header_inetdrv,
http_error_inetdrv,
ssl_tls_inetdrv
};
/*
** passive mode reply:
** {inet_async, S, Ref, {ok,[H1,...Hsz | Data]}}
** NB: this is for TCP only;
** UDP and SCTP use inet_async_binary_data .
*/
static int inet_async_data(inet_descriptor* desc, const char* buf, int len)
{
unsigned int hsz = desc->hsz;
ErlDrvTermData spec[20];
ErlDrvTermData caller;
int req;
int aid;
int i = 0;
DEBUGF(("inet_async_data(%ld): len = %d\r\n", (long)desc->port, len));
if (deq_async(desc, &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async);
i = LOAD_PORT(spec, i, desc->dport);
i = LOAD_INT(spec, i, aid);
i = LOAD_ATOM(spec, i, am_ok);
if ((desc->mode == INET_MODE_LIST) || (hsz > len)) {
i = LOAD_STRING(spec, i, buf, len); /* => [H1,H2,...Hn] */
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i == 15);
desc->caller = 0;
return erl_drv_send_term(desc->dport, caller, spec, i);
}
else {
/* INET_MODE_BINARY => [H1,H2,...HSz | Binary] */
int sz = len - hsz;
int code;
i = LOAD_BUF2BINARY(spec, i, buf+hsz, sz);
if (hsz > 0)
i = LOAD_STRING_CONS(spec, i, buf, hsz);
i = LOAD_TUPLE(spec, i, 2);
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= 20);
desc->caller = 0;
code = erl_drv_send_term(desc->dport, caller, spec, i);
return code;
}
}
#ifndef __WIN32__
static int load_cmsg_int(ErlDrvTermData *spec, int i,
struct cmsghdr *cmsg) {
union u {
byte uint8;
Uint16 uint16;
Uint32 uint32;
Uint64 uint64;
} *p;
p = (union u*) CMSG_DATA(cmsg);
switch (LEN_CMSG_DATA(cmsg) * CHAR_BIT) {
case 8:
return LOAD_INT(spec, i, p->uint8);
case 16:
return LOAD_INT(spec, i, p->uint16);
case 32:
return LOAD_INT(spec, i, p->uint32);
case 64:
return LOAD_INT(spec, i, p->uint64);
}
return LOAD_INT(spec, i, 0);
}
static int parse_ancillary_data_item(ErlDrvTermData *spec, int i,
struct cmsghdr *cmsg, int *n) {
#define LOAD_CMSG_INT(proto, type, am) \
if (cmsg->cmsg_level == (proto) && \
cmsg->cmsg_type == (type)) { \
i = LOAD_ATOM(spec, i, (am)); \
i = load_cmsg_int(spec, i, cmsg); \
i = LOAD_TUPLE(spec, i, 2); \
(*n)++; \
return i; \
}
#if defined(IPPROTO_IP) && defined(IP_TOS)
LOAD_CMSG_INT(IPPROTO_IP, IP_TOS, am_tos);
#endif
#if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
LOAD_CMSG_INT(IPPROTO_IPV6, IPV6_TCLASS, am_tclass);
#endif
#if defined(IPPROTO_IP) && defined(IP_TTL)
LOAD_CMSG_INT(IPPROTO_IP, IP_TTL, am_ttl);
#endif
/* BSD uses the RECV* names in CMSG fields */
#if defined(IPPROTO_IP) && defined(IP_RECVTOS)
LOAD_CMSG_INT(IPPROTO_IP, IP_RECVTOS, am_tos);
#endif
#if defined(IPPROTO_IPV6) && defined(IPV6_RECVTCLASS)
LOAD_CMSG_INT(IPPROTO_IPV6, IPV6_RECVTCLASS, am_tclass);
#endif
#if defined(IPPROTO_IP) && defined(IP_RECVTTL)
LOAD_CMSG_INT(IPPROTO_IP, IP_RECVTTL, am_ttl);
#endif
#undef LOAD_CMSG_INT
return i;
}
#endif /* #ifndef __WIN32__ */
#ifdef HAVE_SCTP
/*
** SCTP-related atoms:
*/
static ErlDrvTermData am_sctp_rtoinfo, /* Option names */
am_sctp_associnfo, am_sctp_initmsg,
am_sctp_autoclose, am_sctp_nodelay,
am_sctp_disable_fragments, am_sctp_i_want_mapped_v4_addr,
am_sctp_maxseg, am_sctp_set_peer_primary_addr,
am_sctp_primary_addr, am_sctp_adaptation_layer,
am_sctp_peer_addr_params, am_sctp_default_send_param,
am_sctp_events, am_sctp_delayed_ack_time,
am_sctp_status, am_sctp_get_peer_addr_info,
/* Record names */
am_sctp_sndrcvinfo, am_sctp_assoc_change,
am_sctp_paddr_change, am_sctp_remote_error,
am_sctp_send_failed, am_sctp_shutdown_event,
am_sctp_adaptation_event, am_sctp_pdapi_event,
am_sctp_assocparams, am_sctp_prim,
am_sctp_setpeerprim, am_sctp_setadaptation,
am_sctp_paddrparams, am_sctp_event_subscribe,
am_sctp_assoc_value, am_sctp_paddrinfo,
/* For #sctp_sndrcvinfo{}: */
am_unordered, am_addr_over,
am_abort, am_eof,
/* For #sctp_assoc_change{}: */
am_comm_up, am_comm_lost,
am_restart, am_shutdown_comp,
am_cant_assoc,
/* For #sctp_paddr_change{}: */
am_addr_available, am_addr_unreachable,
am_addr_removed, am_addr_added,
am_addr_made_prim, am_addr_confirmed,
/* For #sctp_remote_error{}: */
am_short_recv, am_wrong_anc_data,
/* For #sctp_pdap_event{}: */
am_partial_delivery_aborted,
/* For #sctp_paddrparams{}: */
am_hb_enable, am_hb_disable,
am_hb_demand, am_pmtud_enable,
am_pmtud_disable, am_sackdelay_enable,
am_sackdelay_disable,
/* For #sctp_paddrinfo{}: */
am_active, am_inactive,
# if HAVE_DECL_SCTP_UNCONFIRMED
am_unconfirmed,
# endif
/* For #sctp_status{}: */
# if HAVE_DECL_SCTP_EMPTY
am_empty,
# endif
# if HAVE_DECL_SCTP_BOUND
am_bound,
# endif
# if HAVE_DECL_SCTP_LISTEN
am_listen,
# endif
am_cookie_wait, am_cookie_echoed,
am_established, am_shutdown_pending,
am_shutdown_sent, am_shutdown_received,
am_shutdown_ack_sent;
/*
** Parsing of "sctp_sndrcvinfo": ancillary data coming with received msgs.
** This function is mainly used by "sctp_parse_ancillary_data", but also
** by "sctp_parse_async_event" in case of SCTP_SEND_FAILED:
*/
#define SCTP_PARSE_SNDRCVINFO_CNT \
(5*LOAD_ATOM_CNT + 5*LOAD_INT_CNT + 2*LOAD_UINT_CNT + \
LOAD_NIL_CNT + LOAD_LIST_CNT + LOAD_ASSOC_ID_CNT + LOAD_TUPLE_CNT)
static int sctp_parse_sndrcvinfo
(ErlDrvTermData * spec, int i, struct sctp_sndrcvinfo * sri)
{
int n;
i = LOAD_ATOM (spec, i, am_sctp_sndrcvinfo);
i = LOAD_INT (spec, i, sri->sinfo_stream);
i = LOAD_INT (spec, i, sri->sinfo_ssn);
/* Now Flags, as a list: */
n = 0;
if (sri->sinfo_flags & SCTP_UNORDERED)
{ i = LOAD_ATOM (spec, i, am_unordered); n++; }
if (sri->sinfo_flags & SCTP_ADDR_OVER)
{ i = LOAD_ATOM (spec, i, am_addr_over); n++; }
if (sri->sinfo_flags & SCTP_ABORT)
{ i = LOAD_ATOM (spec, i, am_abort); n++; }
if (sri->sinfo_flags & SCTP_EOF)
{ i = LOAD_ATOM (spec, i, am_eof); n++; }
/* SCTP_SENDALL is not yet supported by the Linux kernel */
i = LOAD_NIL (spec, i);
i = LOAD_LIST (spec, i, n+1);
/* Continue with other top-level fields: */
i = LOAD_INT (spec, i, sock_ntohl(sri->sinfo_ppid));
i = LOAD_INT (spec, i, sri->sinfo_context);
i = LOAD_INT (spec, i, sri->sinfo_timetolive);
i = LOAD_UINT (spec, i, sri->sinfo_tsn);
i = LOAD_UINT (spec, i, sri->sinfo_cumtsn);
i = LOAD_ASSOC_ID (spec, i, sri->sinfo_assoc_id);
/* Close up the record: */
i = LOAD_TUPLE (spec, i, 10);
return i;
}
/*
** This function skips non-SCTP ancillary data, returns SCTP-specific anc.data
** (currently "sctp_sndrcvinfo" only) as a list of records:
*/
static int sctp_parse_ancillary_data
(ErlDrvTermData * spec, int i, struct msghdr * mptr)
{
/* First of all, check for ancillary data: */
struct cmsghdr * cmsg, * frst_msg = CMSG_FIRSTHDR(mptr);
int s = 0;
for (cmsg = frst_msg; cmsg != NULL; cmsg = CMSG_NXTHDR(mptr,cmsg))
{
struct sctp_sndrcvinfo * sri;
#ifndef __WIN32
int old_s;
/* Parse ancillary data common to UDP */
old_s = s;
i = parse_ancillary_data_item(spec, i, cmsg, &s);
if (s > old_s) continue;
/* Skip other possible ancillary data, e.g. from IPv6: */
if (cmsg->cmsg_level != IPPROTO_SCTP ||
cmsg->cmsg_type != SCTP_SNDRCV)
continue;
#endif
if (((char*)cmsg + cmsg->cmsg_len) - (char*)frst_msg >
mptr->msg_controllen)
/* MUST check this in Linux -- the returned "cmsg" may actually
go too far! */
break;
/* The ONLY kind of ancillary SCTP data which can occur on receiving
is "sctp_sndrcvinfo" (on sending, "sctp_initmsg" can be specified
by the user). So parse this type:
*/
sri = (struct sctp_sndrcvinfo*) CMSG_DATA(cmsg);
i = sctp_parse_sndrcvinfo (spec, i, sri);
s ++;
}
/* Now make the list of tuples created above. Normally, it will be [] or
a singleton list. The list must first be closed with NIL, otherwise
traversing it in Erlang would be problematic:
*/
i = LOAD_NIL (spec, i);
i = LOAD_LIST(spec, i, s+1);
return i;
}
/*
** Parsing of ERROR and ABORT SCTP chunks. The function returns a list of error
** causes (as atoms). The chunks also contain some extended cause info, but it
** is not very detailed anyway, and of no interest at the user level (it only
** concerns the protocol implementation), so we omit it:
*/
static int sctp_parse_error_chunk
(ErlDrvTermData * spec, int i, char * chunk, int chlen)
{
/* The "chunk" itself contains its length, which must not be greater than
the "chlen" derived from the over-all msg size:
*/
char *causes, *cause;
int coff, /* Cause offset */
ccode, /* Cause code */
clen, /* cause length */
s;
int len = sock_ntohs (*((uint16_t*)(chunk+2)));
ASSERT(len >= 4 && len <= chlen);
causes = chunk + 4;
coff = 0;
len -= 4; /* Total length of the "causes" fields */
cause = causes;
s = 0;
while (coff < len)
{
ccode = sock_ntohs (*((uint16_t*)(cause)));
clen = sock_ntohs (*((uint16_t*)(cause + 2)));
if (clen <= 0)
/* Strange, but must guard against that! */
break;
/* Install the corresp atom for this "ccode": */
i = LOAD_INT (spec, i, ccode);
cause += clen;
coff += clen;
s ++;
}
i = LOAD_NIL (spec, i);
i = LOAD_LIST(spec, i, s+1);
return i;
}
/*
** Parsing of SCTP notification events. NB: they are NOT ancillary data: they
** are sent IN PLACE OF, not in conjunction with, the normal data:
*/
static int sctp_parse_async_event
(ErlDrvTermData * spec, int i, int ok_pos,
ErlDrvTermData error_atom, inet_descriptor* desc,
ErlDrvBinary * bin, int offs, int sz)
{
char* body = bin->orig_bytes + offs;
union sctp_notification * nptr = (union sctp_notification *) body;
switch (nptr->sn_header.sn_type)
{
case SCTP_ASSOC_CHANGE:
{ /* {sctp_assoc_change,
State : Atom(),
Error : Atom(),
OutBoundStreams : Int(),
InBoundStreams : Int(),
AssocID : Int(),
// AbortCauses : [Atom()] // NOT YET IMPLEMENTED
}
*/
struct sctp_assoc_change* sptr = &(nptr->sn_assoc_change);
ASSERT(sptr->sac_length <= sz); /* No buffer overrun */
i = LOAD_ATOM (spec, i, am_sctp_assoc_change);
switch (sptr->sac_state)
{
case SCTP_COMM_UP:
i = LOAD_ATOM (spec, i, am_comm_up);
break;
case SCTP_COMM_LOST:
i = LOAD_ATOM (spec, i, am_comm_lost);
break;
case SCTP_RESTART:
i = LOAD_ATOM (spec, i, am_restart);
break;
case SCTP_SHUTDOWN_COMP:
i = LOAD_ATOM (spec, i, am_shutdown_comp);
break;
case SCTP_CANT_STR_ASSOC:
i = LOAD_ATOM (spec, i, am_cant_assoc);
break;
default:
ASSERT(0);
}
i = LOAD_INT (spec, i, sptr->sac_error);
i = LOAD_INT (spec, i, sptr->sac_outbound_streams);
i = LOAD_INT (spec, i, sptr->sac_inbound_streams);
i = LOAD_INT (spec, i, sptr->sac_assoc_id);
/* The ABORT chunk may or may not be present at the end, depending
on whether there was really an ABORT. In the Linux Kernel SCTP
implementation, this chunk is not delivered anyway, so we leave
it out. Just close up the tuple:
*/
i = LOAD_TUPLE (spec, i, 6);
break;
}
case SCTP_PEER_ADDR_CHANGE:
{ /* {sctp_paddr_change,
AffectedAddr : String(),
State : Atom(),
Error : Atom(),
AssocID : Int()
}
*/
struct sctp_paddr_change* sptr = &(nptr->sn_paddr_change);
ASSERT(sptr->spc_length <= sz); /* No buffer overrun */
i = LOAD_ATOM (spec, i, am_sctp_paddr_change);
i = load_inet_get_address(spec, i, desc, &sptr->spc_aaddr);
switch (sptr->spc_state)
{
case SCTP_ADDR_AVAILABLE:
i = LOAD_ATOM (spec, i, am_addr_available);
break;
case SCTP_ADDR_UNREACHABLE:
i = LOAD_ATOM (spec, i, am_addr_unreachable);
break;
case SCTP_ADDR_REMOVED:
i = LOAD_ATOM (spec, i, am_addr_removed);
break;
case SCTP_ADDR_ADDED:
i = LOAD_ATOM (spec, i, am_addr_added);
break;
case SCTP_ADDR_MADE_PRIM:
i = LOAD_ATOM (spec, i, am_addr_made_prim);
break;
#if HAVE_DECL_SCTP_ADDR_CONFIRMED
case SCTP_ADDR_CONFIRMED:
i = LOAD_ATOM (spec, i, am_addr_confirmed);
break;
#endif
default:
ASSERT(0);
}
i = LOAD_INT (spec, i, sptr->spc_error);
i = LOAD_INT (spec, i, sptr->spc_assoc_id);
i = LOAD_TUPLE (spec, i, 5);
break;
}
case SCTP_REMOTE_ERROR:
{ /* This is an error condition, so we return an error term
{sctp_remote_error,
Error : Int(),
AssocID : Int(),
RemoteCauses : [Atom()] // Remote Error flags
}
*/
char *chunk;
int chlen;
struct sctp_remote_error * sptr = &(nptr->sn_remote_error);
ASSERT(sptr->sre_length <= sz); /* No buffer overrun */
/* Over-write the prev part of the response with an error: */
(void)LOAD_ATOM(spec, ok_pos, error_atom);
/* Continue from the curr pos: */
i = LOAD_ATOM (spec, i, am_sctp_remote_error);
i = LOAD_INT (spec, i, sock_ntohs(sptr->sre_error));
i = LOAD_INT (spec, i, sptr->sre_assoc_id);
# ifdef HAVE_STRUCT_SCTP_REMOTE_ERROR_SRE_DATA
chunk = (char*) (&(sptr->sre_data));
# else
chunk = ((char*) &(sptr->sre_assoc_id))
+ sizeof(sptr->sre_assoc_id);
# endif
chlen = sptr->sre_length - (chunk - (char *)sptr);
i = sctp_parse_error_chunk(spec, i, chunk, chlen);
i = LOAD_TUPLE (spec, i, 4);
/* The {error, {...}} will be closed by the caller */
break;
}
case SCTP_SEND_FAILED:
{ /* {sctp_send_failed,
DataSent : Atom() // true or false
Error : Atom(),
OrigInfo : Tuple(),
AssocID : Int(),
OrigData : Binary()
}
This is also an ERROR condition -- overwrite the 'ok':
*/
char *chunk;
int chlen, choff;
struct sctp_send_failed * sptr = &(nptr->sn_send_failed);
ASSERT(sptr->ssf_length <= sz); /* No buffer overrun */
/* Over-write 'ok' with 'error', continue from curr "i": */
(void)LOAD_ATOM(spec, ok_pos, error_atom);
i = LOAD_ATOM (spec, i, am_sctp_send_failed);
switch (sptr->ssf_flags) {
case SCTP_DATA_SENT:
i = LOAD_ATOM (spec, i, am_true);
break;
case SCTP_DATA_UNSENT:
i = LOAD_ATOM (spec, i, am_false);
break;
default:
ASSERT(0);
}
i = LOAD_INT (spec, i, sptr->ssf_error);
/* Now parse the orig SCTP_SNDRCV info */
i = sctp_parse_sndrcvinfo (spec, i, &sptr->ssf_info);
i = LOAD_ASSOC_ID (spec, i, sptr->ssf_assoc_id);
/* Load the orig data chunk, as an unparsed binary. Note that
in LOAD_BINARY below, we must specify the offset wrt bin->
orig_bytes. In Solaris 10, we don't have ssf_data:
*/
# ifdef HAVE_STRUCT_SCTP_SEND_FAILED_SSF_DATA
chunk = (char*) (&(sptr->ssf_data));
# else
chunk = ((char*) &(sptr->ssf_assoc_id))
+ sizeof(sptr->ssf_assoc_id);
# endif
chlen = sptr->ssf_length - (chunk - (char*) sptr);
choff = chunk - bin->orig_bytes;
i = LOAD_BINARY(spec, i, bin, choff, chlen);
i = LOAD_TUPLE (spec, i, 6);
/* The {error, {...}} tuple is not yet closed */
break;
}
case SCTP_SHUTDOWN_EVENT:
{ /* {sctp_shutdown_event,
AssocID : Int()
}
*/
struct sctp_shutdown_event * sptr = &(nptr->sn_shutdown_event);
ASSERT (sptr->sse_length == sizeof(struct sctp_shutdown_event) &&
sptr->sse_length <= sz); /* No buffer overrun */
i = LOAD_ATOM (spec, i, am_sctp_shutdown_event);
i = LOAD_INT (spec, i, sptr->sse_assoc_id);
i = LOAD_TUPLE (spec, i, 2);
break;
}
case SCTP_ADAPTATION_INDICATION:
{ /* {sctp_adaptation_event,
Indication : Atom(),
AssocID : Int()
}
*/
struct sctp_adaptation_event * sptr =
&(nptr->sn_adaptation_event);
ASSERT (sptr->sai_length == sizeof(struct sctp_adaptation_event)
&& sptr->sai_length <= sz); /* No buffer overrun */
i = LOAD_ATOM (spec, i, am_sctp_adaptation_event);
i = LOAD_INT (spec, i, sock_ntohl(sptr->sai_adaptation_ind));
i = LOAD_INT (spec, i, sptr->sai_assoc_id);
i = LOAD_TUPLE (spec, i, 3);
break;
}
case SCTP_PARTIAL_DELIVERY_EVENT:
{ /* It is not clear whether this event is sent to the sender
(when the receiver gets only a part of a message), or to
the receiver itself. In any case, we do not support partial
delivery of msgs in this implementation, so this is an error
condition:
{sctp_pdapi_event, sctp_partial_delivery_aborted, AssocID}:
*/
struct sctp_pdapi_event * sptr;
(void) LOAD_ATOM (spec, ok_pos, error_atom);
sptr = &(nptr->sn_pdapi_event);
ASSERT (sptr->pdapi_length == sizeof(struct sctp_pdapi_event) &&
sptr->pdapi_length <= sz); /* No buffer overrun */
i = LOAD_ATOM (spec, i, am_sctp_pdapi_event);
/* Currently, there is only one indication possible: */
ASSERT (sptr->pdapi_indication == SCTP_PARTIAL_DELIVERY_ABORTED);
i = LOAD_ATOM (spec, i, am_partial_delivery_aborted);
i = LOAD_INT (spec, i, sptr->pdapi_assoc_id);
i = LOAD_TUPLE (spec, i, 3);
/* The {error, {...}} tuple is not yet closed */
break;
}
/* XXX: No more supported SCTP Event types. The standard also provides
SCTP_AUTHENTICATION_EVENT, but it is not implemented in the Linux
kernel, hence not supported here either. It is not possible to
request delivery of such events in this implementation, so they
cannot occur:
*/
default: ASSERT(0);
}
return i;
}
#endif /* HAVE_SCTP */
#ifndef __WIN32__
static int udp_parse_ancillary_data(ErlDrvTermData *spec, int i,
struct msghdr *mptr) {
struct cmsghdr *cmsg;
int n;
n = 0;
for (cmsg = CMSG_FIRSTHDR(mptr);
cmsg != NULL;
cmsg = CMSG_NXTHDR(mptr, cmsg)) {
i = parse_ancillary_data_item(spec, i, cmsg, &n);
}
i = LOAD_NIL(spec, i);
return LOAD_LIST(spec, i, n+1);
}
#endif /* ifndef __WIN32__ */
/*
** passive mode reply:
** for UDP:
** {inet_async, S, Ref, {ok, Data=[H1,...,Hsz | BinData]}}
** or (in the list mode)
** {inet_async, S, Ref, {ok, Data=[H1,...,Hsz]}}
**
** for SCTP:
** {inet_async, S, Ref, {ok, {[H1,...,HSz], [AncilData], Data_OR_Event}}}
** where each AncilDatum:Tuple();
** Data:List() or Binary(), but if List(), then without the Addr part,
** which is moved in front;
** Event:Tuple();
** or
** {inet_async, S, Ref, {error, {[H1,...,HSz], [AncilData], ErrorTerm}}}
**
** Cf: the output of send_async_error() is
** {inet_async, S, Ref, {error, Cause:Atom()}}
*/
static int
inet_async_binary_data
(inet_descriptor* desc, unsigned int phsz,
ErlDrvBinary * bin, int offs, int len, void *mp)
{
unsigned int hsz = desc->hsz + phsz;
ErlDrvTermData spec [PACKET_ERL_DRV_TERM_DATA_LEN];
ErlDrvTermData caller = desc->caller;
int aid;
int req;
int i = 0;
#ifdef HAVE_SCTP
int ok_pos;
#endif
DEBUGF(("inet_async_binary_data(%ld): offs=%d, len=%d\r\n",
(long)desc->port, offs, len));
if (deq_async(desc, &aid, &caller, &req) < 0)
return -1;
i = LOAD_ATOM(spec, i, am_inet_async); /* 'inet_async' */
i = LOAD_PORT(spec, i, desc->dport); /* S */
i = LOAD_INT (spec, i, aid); /* Ref */
#ifdef HAVE_SCTP
/* Need to memoise the position of the 'ok' atom written, as it may
later be overridden by an 'error': */
ok_pos = i;
#endif
i = LOAD_ATOM(spec, i, am_ok);
#ifdef HAVE_SCTP
if (IS_SCTP(desc))
{ /* For SCTP we always have desc->hsz==0 (i.e., no application-level
headers are used), so hsz==phsz (see above): */
int sz;
struct msghdr *mptr;
mptr = mp;
ASSERT (hsz == phsz && hsz != 0);
sz = len - hsz; /* Size of the msg data proper, w/o the addr */
/* We always put the Addr as a list in front */
i = LOAD_STRING(spec, i, bin->orig_bytes+offs, hsz);
/* Put in the list (possibly empty) of Ancillary Data: */
i = sctp_parse_ancillary_data (spec, i, mptr);
/* Then: Data or Event (Notification)? */
if (mptr->msg_flags & MSG_NOTIFICATION)
/* This is an Event, parse it. It may indicate a normal or an error
condition; in the latter case, the 'ok' above is overridden by
an 'error', and the Event we receive contains the error term: */
i = sctp_parse_async_event
(spec, i, ok_pos, am_error, desc, bin, offs+hsz, sz);
else
/* This is SCTP data, not a notification event. The data can be
returned as a List or as a Binary, similar to the generic case:
*/
if (desc->mode == INET_MODE_LIST)
/* INET_MODE_LIST => [H1,H2,...Hn], addr and data together,
butthe Addr has already been parsed, so start at offs+hsz:
*/
i = LOAD_STRING(spec, i, bin->orig_bytes+offs+hsz, sz);
else
/* INET_MODE_BINARY => Binary */
i = LOAD_BINARY(spec, i, bin, offs+hsz, sz);
/* Close up the {[H1,...,HSz], [AncilData], Event_OR_Data} tuple. This
is valid even in the case when Event is a error notification: */
i = LOAD_TUPLE (spec, i, 3);
}
else
#endif /* HAVE_SCTP */
{
/* Generic case. Both Addr and Data
* (or a single list of them together) are returned: */
if ((desc->mode == INET_MODE_LIST) || (hsz > len)) {
/* INET_MODE_LIST => [H1,H2,...Hn] */
i = LOAD_STRING(spec, i, bin->orig_bytes+offs, len);
}
else {
/* INET_MODE_BINARY => [H1,H2,...HSz | Binary] or [Binary]: */
int sz = len - hsz;
i = LOAD_BINARY(spec, i, bin, offs+hsz, sz);
if (hsz > 0)
i = LOAD_STRING_CONS(spec, i, bin->orig_bytes+offs, hsz);
}
#ifndef __WIN32__
if (mp) {
/* We got ancillary data from an UDP recvmsg.
* Insert an additional tuple level {[F|AddrData],AncData} */
i = udp_parse_ancillary_data(spec, i, (struct msghdr*)mp);
i = LOAD_TUPLE(spec, i, 2);
}
#endif
}
/* Close up the {ok, ...} or {error, ...} tuple: */
i = LOAD_TUPLE(spec, i, 2);
/* Close up the outer {inet_async, S, Ref, {ok|error, ...}} tuple: */
i = LOAD_TUPLE(spec, i, 4);
ASSERT(i <= PACKET_ERL_DRV_TERM_DATA_LEN);
desc->caller = 0;
return erl_drv_send_term(desc->dport, caller, spec, i);
}
/*
** active mode message:
** {tcp, S, [H1,...Hsz | Data]}
*/
static int tcp_message(inet_descriptor* desc, const char* buf, int len)
{
unsigned int hsz = desc->hsz;
ErlDrvTermData spec[20];
int i = 0;
DEBUGF(("tcp_message(%ld): len = %d\r\n", (long)desc->port, len));
/* XXX fprintf(stderr,"tcp_message send.\r\n"); */
i = LOAD_ATOM(spec, i, am_tcp);
i = LOAD_PORT(spec, i, desc->dport);
if ((desc->mode == INET_MODE_LIST) || (hsz > len)) {
i = LOAD_STRING(spec, i, buf, len); /* => [H1,H2,...Hn] */
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 20);
return erl_drv_output_term(desc->dport, spec, i);
}
else {
/* INET_MODE_BINARY => [H1,H2,...HSz | Binary] */
int sz = len - hsz;
int code;
i = LOAD_BUF2BINARY(spec, i, buf+hsz, sz);
if (hsz > 0)
i = LOAD_STRING_CONS(spec, i, buf, hsz);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 20);
code = erl_drv_output_term(desc->dport, spec, i);
return code;
}
}
/*
** active mode message:
** {tcp, S, [H1,...Hsz | Data]}
*/
static int
tcp_binary_message(inet_descriptor* desc, ErlDrvBinary* bin, int offs, int len)
{
unsigned int hsz = desc->hsz;
ErlDrvTermData spec[20];
int i = 0;
DEBUGF(("tcp_binary_message(%ld): len = %d\r\n", (long)desc->port, len));
i = LOAD_ATOM(spec, i, am_tcp);
i = LOAD_PORT(spec, i, desc->dport);
if ((desc->mode == INET_MODE_LIST) || (hsz > len)) {
/* INET_MODE_LIST => [H1,H2,...Hn] */
i = LOAD_STRING(spec, i, bin->orig_bytes+offs, len);
}
else {
/* INET_MODE_BINARY => [H1,H2,...HSz | Binary] */
int sz = len - hsz;
i = LOAD_BINARY(spec, i, bin, offs+hsz, sz);
if (hsz > 0)
i = LOAD_STRING_CONS(spec, i, bin->orig_bytes+offs, hsz);
}
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 20);
return erl_drv_output_term(desc->dport, spec, i);
}
/*
** send: active mode {tcp_closed, S}
*/
static int tcp_closed_message(tcp_descriptor* desc)
{
ErlDrvTermData spec[6];
int i = 0;
DEBUGF(("tcp_closed_message(%ld):\r\n", (long)desc->inet.port));
if (!(desc->tcp_add_flags & TCP_ADDF_CLOSE_SENT)) {
desc->tcp_add_flags |= TCP_ADDF_CLOSE_SENT;
i = LOAD_ATOM(spec, i, am_tcp_closed);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_TUPLE(spec, i, 2);
ASSERT(i <= 6);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
return 0;
}
/*
** send active message {tcp_error, S, Error}
*/
static int tcp_error_message(tcp_descriptor* desc, int err)
{
ErlDrvTermData spec[8];
ErlDrvTermData am_err = error_atom(err);
int i = 0;
DEBUGF(("tcp_error_message(%ld): %d\r\n", (long)desc->inet.port, err));
i = LOAD_ATOM(spec, i, am_tcp_error);
i = LOAD_PORT(spec, i, desc->inet.dport);
i = LOAD_ATOM(spec, i, am_err);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i <= 8);
return erl_drv_output_term(desc->inet.dport, spec, i);
}
#ifdef HAVE_UDP
/*
** active mode message:
** {udp, S, IP, Port, [H1,...Hsz | Data]} or
** {sctp, S, IP, Port, {[AncilData], Event_or_Data}}
** where
** [H1,...,HSz] are msg headers (without IP/Port, UDP only),
** [AddrLen, H2,...,HSz] are msg headers for UDP AF_UNIX only
** Data : List() | Binary()
*/
static int packet_binary_message(inet_descriptor* desc,
ErlDrvBinary* bin, int offs, int len,
void *mp)
{
unsigned int hsz = desc->hsz;
ErlDrvTermData spec [PACKET_ERL_DRV_TERM_DATA_LEN];
int i = 0;
int alen;
char* data = bin->orig_bytes+offs;
DEBUGF(("packet_binary_message(%ld): len = %d\r\n",
(long)desc->port, len));
# ifdef HAVE_SCTP
i = LOAD_ATOM(spec, i, IS_SCTP(desc) ? am_sctp : am_udp); /* UDP|SCTP */
# else
i = LOAD_ATOM(spec, i, am_udp ); /* UDP only */
# endif
i = LOAD_PORT(spec, i, desc->dport); /* S */
alen = addrlen(data);
i = load_address(spec, i, data); /* IP,Port | Family,Addr */
offs += alen;
len -= alen;
# ifdef HAVE_SCTP
if (!IS_SCTP(desc))
# endif
{
#ifndef __WIN32__
if (mp) i = udp_parse_ancillary_data(spec, i, (struct msghdr*)mp);
#endif
/* We got ancillary data from an UDP recvmsg.
* Insert an additional tuple level {AncData,[F|AddrData]}
*/
if ((desc->mode == INET_MODE_LIST) || (hsz > len))
/* INET_MODE_LIST, or only headers => [H1,H2,...Hn] */
i = LOAD_STRING(spec, i, bin->orig_bytes+offs, len);
else {
/* INET_MODE_BINARY => [H1,H2,...HSz | Binary] */
int sz = len - hsz;
i = LOAD_BINARY(spec, i, bin, offs+hsz, sz);
if (hsz > 0)
i = LOAD_STRING_CONS(spec, i, bin->orig_bytes+offs, hsz);
}
/* Close up the outer 5-or-6-tuple */
#ifndef __WIN32__
if (mp) i = LOAD_TUPLE(spec, i, 6);
else
#endif
i = LOAD_TUPLE(spec, i, 5);
}
# ifdef HAVE_SCTP
else
{
struct msghdr *mptr;
mptr = mp;
/* For SCTP we always have desc->hsz==0 (i.e., no application-level
headers are used): */
ASSERT(hsz == 0);
/* Put in the list (possibly empty) of Ancillary Data: */
i = sctp_parse_ancillary_data (spec, i, mptr);
/* Then: Data or Event (Notification)? */
if (mptr->msg_flags & MSG_NOTIFICATION)
/* This is an Event, parse it. It may indicate a normal or an error
condition; in the latter case, the initial 'sctp' atom is over-
ridden by 'sctp_error', and the Event we receive contains the
error term: */
i = sctp_parse_async_event
(spec, i, 0, am_sctp_error, desc, bin, offs, len);
else
/* This is SCTP data, not a notification event. The data can be
returned as a List or as a Binary, similar to the generic case:
*/
if (desc->mode == INET_MODE_LIST)
/* INET_MODE_LIST => [H1,H2,...Hn], addr and data together,
but the Addr has already been parsed, so start at offs:
*/
i = LOAD_STRING(spec, i, bin->orig_bytes+offs, len);
else
/* INET_MODE_BINARY => Binary */
i = LOAD_BINARY(spec, i, bin, offs, len);
/* Close up the {[AncilData], Event_OR_Data} tuple: */
i = LOAD_TUPLE (spec, i, 2);
/* Close up the outer 5-tuple: */
i = LOAD_TUPLE(spec, i, 5);
}
# endif /* HAVE_SCTP */
ASSERT(i <= PACKET_ERL_DRV_TERM_DATA_LEN);
return erl_drv_output_term(desc->dport, spec, i);
}
#endif
/*
** active mode message: send active-to-passive transition message
** {tcp_passive, S} or
** {udp_passive, S} or
** {sctp_passive, S}
*/
static int packet_passive_message(inet_descriptor* desc)
{
ErlDrvTermData spec[6];
int i = 0;
DEBUGF(("packet_passive_message(%ld):\r\n", (long)desc->port));
#if !defined(HAVE_UDP) && !defined(HAVE_SCTP)
i = LOAD_ATOM(spec, i, am_tcp_passive);
#else
if (desc->sprotocol == IPPROTO_TCP)
i = LOAD_ATOM(spec, i, am_tcp_passive);
else {
#ifdef HAVE_SCTP
i = LOAD_ATOM(spec, i, IS_SCTP(desc) ? am_sctp_passive : am_udp_passive);
#else
i = LOAD_ATOM(spec, i, am_udp_passive);
#endif
}
#endif
i = LOAD_PORT(spec, i, desc->dport);
i = LOAD_TUPLE(spec, i, 2);
ASSERT(i <= 6);
return erl_drv_output_term(desc->dport, spec, i);
}
#ifdef HAVE_UDP
/*
** send active message {udp_error|sctp_error, S, Error}
*/
static int packet_error_message(udp_descriptor* udesc, int err)
{
inet_descriptor* desc = INETP(udesc);
ErlDrvTermData spec[2*LOAD_ATOM_CNT + LOAD_PORT_CNT + LOAD_TUPLE_CNT];
ErlDrvTermData am_err = error_atom(err);
int i = 0;
DEBUGF(("packet_error_message(%ld): %d\r\n",
(long)desc->port, err));
# ifdef HAVE_SCTP
if (IS_SCTP(desc) )
i = LOAD_ATOM(spec, i, am_sctp_error);
else
# endif
i = LOAD_ATOM(spec, i, am_udp_error);
i = LOAD_PORT(spec, i, desc->dport);
i = LOAD_ATOM(spec, i, am_err);
i = LOAD_TUPLE(spec, i, 3);
ASSERT(i == sizeof(spec)/sizeof(*spec));
return erl_drv_output_term(desc->dport, spec, i);
}
#endif
/*
** active=TRUE:
** (NOTE! distribution MUST use active=TRUE, deliver=PORT)
** deliver=PORT {S, {data, [H1,..Hsz | Data]}}
** deliver=TERM {tcp, S, [H1..Hsz | Data]}
**
** active=FALSE:
** {async, S, Ref, {ok,[H1,...Hsz | Data]}}
*/
static int tcp_reply_data(tcp_descriptor* desc, char* buf, int len)
{
int code;
const char* body = buf;
int bodylen = len;
packet_get_body(desc->inet.htype, &body, &bodylen);
if (desc->inet.deliver == INET_DELIVER_PORT) {
code = inet_port_data(INETP(desc), body, bodylen);
}
else if ((code=packet_parse(desc->inet.htype, buf, len,
&desc->http_state, &packet_callbacks,
desc)) == 0) {
/* No body parsing, return raw binary */
if (desc->inet.active == INET_PASSIVE)
return inet_async_data(INETP(desc), body, bodylen);
else
code = tcp_message(INETP(desc), body, bodylen);
}
if (code < 0)
return code;
INET_CHECK_ACTIVE_TO_PASSIVE(INETP(desc));
return code;
}
static int
tcp_reply_binary_data(tcp_descriptor* desc, ErlDrvBinary* bin, int offs, int len)
{
int code;
const char* buf = bin->orig_bytes + offs;
const char* body = buf;
int bodylen = len;
packet_get_body(desc->inet.htype, &body, &bodylen);
offs = body - bin->orig_bytes; /* body offset now */
if (desc->inet.deliver == INET_DELIVER_PORT)
code = inet_port_binary_data(INETP(desc), bin, offs, bodylen);
else if ((code=packet_parse(desc->inet.htype, buf, len, &desc->http_state,
&packet_callbacks,desc)) == 0) {
/* No body parsing, return raw data */
if (desc->inet.active == INET_PASSIVE)
return inet_async_binary_data(INETP(desc), 0, bin, offs, bodylen, NULL);
else
code = tcp_binary_message(INETP(desc), bin, offs, bodylen);
}
if (code < 0)
return code;
INET_CHECK_ACTIVE_TO_PASSIVE(INETP(desc));
return code;
}
#ifdef HAVE_UDP
static int
packet_reply_binary_data(inet_descriptor* desc, unsigned int hsz,
ErlDrvBinary * bin, int offs, int len,
void *mp)
{
int code;
if (desc->active == INET_PASSIVE)
/* "inet" is actually for both UDP and SCTP, as well as TCP! */
return inet_async_binary_data(desc, hsz, bin, offs, len, mp);
else
{ /* INET_ACTIVE or INET_ONCE: */
if (desc->deliver == INET_DELIVER_PORT)
code = inet_port_binary_data(desc, bin, offs, len);
else
code = packet_binary_message(desc, bin, offs, len, mp);
if (code < 0)
return code;
INET_CHECK_ACTIVE_TO_PASSIVE(desc);
return code;
}
}
#endif
/* ----------------------------------------------------------------------------
INET
-----------------