Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

sock.h

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: sock.h,v 1.17 2005/12/29 04:36:49 klmitch Exp $
00019 */
00020 #ifndef __include_event_sock_h__
00021 #define __include_event_sock_h__
00022 
00039 #ifndef __EVENT_LIBRARY__
00040 # include <event/event.h>
00041 #endif /* __EVENT_LIBRARY__ */
00042 
00043 #include <sys/socket.h>
00044 #include <sys/un.h>
00045 
00046 EV_BEGIN_C_DECLS
00047 
00054 #ifndef SHUT_RD
00055 #define SHUT_RD                 0
00056 #endif
00057 
00064 #ifndef SHUT_WR
00065 #define SHUT_WR                 1
00066 #endif
00067 
00076 #ifndef SHUT_RDWR
00077 #define SHUT_RDWR               2
00078 #endif
00079 
00085 #define SOCK_ADDRBUFV4_LEN      4
00086 
00092 #define SOCK_ADDRSTRV4_LEN      16
00093 
00099 #define SOCK_ADDRBUFV6_LEN      16
00100 
00106 #define SOCK_ADDRSTRV6_LEN      46
00107 
00113 #define SOCK_LOCALADDR_LEN      (sizeof(((struct sockaddr_un *)0)->sun_path))
00114 
00124 typedef enum {
00125   ST_NONE,              
00126   ST_STREAM,            
00127   ST_DGRAM,             
00128   ST_SEQPACKET,         
00129   ST_PIPE               
00130 } ev_socktype_t;
00131 
00137 typedef enum {
00138   SS_NONE,              
00139   SS_CONNECTING,        
00140   SS_LISTENING,         
00141   SS_CONNECTED,         
00142   SS_UNCONNECTED,       
00144   SS_CLOSED             
00145 } ev_sockstate_t;
00146 
00152 typedef enum {
00153   AT_NONE,              
00154   AT_IPv6,              
00155   AT_IPv4,              
00156   AT_LOCAL              
00157 } ev_satype_t;
00158 
00164 typedef struct _ev_sockaddr_s ev_sockaddr_t;
00165 
00170 typedef struct _ev_sock_s ev_sock_t;
00171 
00177 typedef struct _ev_sockattr_t *ev_sockattr_t;
00178 
00184 struct _ev_sockaddr_s {
00185   ev_satype_t           sa_type;        
00186   union {
00187     struct {
00188       unsigned char     saai_addrbuf[SOCK_ADDRBUFV6_LEN];
00190       unsigned short    saai_port;      
00191     }                   saa_ipaddr;     
00192     char                saa_localaddr[SOCK_LOCALADDR_LEN];
00194   }                     sa_addr;        
00195 };
00196 
00205 #define sa_type(sa)             ((sa)->sa_type)
00206 
00217 #define sa_ipaddrbuf(sa)        ((sa)->sa_addr.saa_ipaddr.saai_addrbuf)
00218 
00229 #define sa_ipaddrport(sa)       ((sa)->sa_addr.saa_ipaddr.saai_port)
00230 
00241 #define sa_localaddr(sa)        ((sa)->sa_addr.saa_localaddr)
00242 
00254 #define _sa_ipbuf8(sa, idx)     ((sa)->sa_addr.saa_ipaddr.saai_addrbuf[(idx)])
00255 
00272 #define _sa_ipbuf16(sa, idx)    ((_sa_ipbuf8((sa), (idx)    ) <<  8) |        \
00273                                  (_sa_ipbuf8((sa), (idx) + 1)      ))
00274 
00291 #define _sa_ipbuf32(sa, idx)    ((_sa_ipbuf8((sa), (idx)    ) << 24) |        \
00292                                  (_sa_ipbuf8((sa), (idx) + 1) << 16) |        \
00293                                  (_sa_ipbuf8((sa), (idx) + 2) <<  8) |        \
00294                                  (_sa_ipbuf8((sa), (idx) + 3)      ))
00295 
00317 #define _sa_ipmask(bits, sa, idx, mask)                                       \
00318                                 (_sa_ipbuf##bits((sa), (idx)) & (mask))
00319 
00330 #define sa_is_ipv4(sa)          ((sa)->sa_type == AT_IPv4)
00331 
00342 #define sa_is_ipv6(sa)          ((sa)->sa_type == AT_IPv6)
00343 
00354 #define sa_is_local(sa)         ((sa)->sa_type == AT_LOCAL)
00355 
00369 #define sa_is_unspec(sa)        ((sa_is_ipv4((sa)) &&                         \
00370                                   _sa_ipbuf32((sa), 0) == 0) ||               \
00371                                  (sa_is_ipv6((sa)) &&                         \
00372                                   _sa_ipbuf32((sa), 0) == 0 &&                \
00373                                   _sa_ipbuf32((sa), 4) == 0 &&                \
00374                                   _sa_ipbuf32((sa), 8) == 0 &&                \
00375                                   _sa_ipbuf32((sa), 12) == 0) ||              \
00376                                  (sa_is_local((sa)) &&                        \
00377                                   !((sa)->sa_addr.saa_localaddr[0])))
00378 
00391 #define sa_is_loopback(sa)      ((sa_is_ipv4((sa)) &&                         \
00392                                   _sa_ipbuf32((sa), 0) == 0x7f000001) ||      \
00393                                  (sa_is_ipv6((sa)) &&                         \
00394                                   _sa_ipbuf32((sa), 0) == 0 &&                \
00395                                   _sa_ipbuf32((sa), 4) == 0 &&                \
00396                                   _sa_ipbuf32((sa), 8) == 0 &&                \
00397                                   _sa_ipbuf32((sa), 12) == 0x00000001))
00398 
00413 #define sa_is_v4mapped(sa)      (sa_is_ipv6((sa)) &&                          \
00414                                  _sa_ipbuf32((sa), 0) == 0 &&                 \
00415                                  _sa_ipbuf32((sa), 4) == 0 &&                 \
00416                                  _sa_ipbuf32((sa), 8) == 0x0000ffff)
00417 
00432 #define sa_is_v4compat(sa)      (sa_is_ipv6((sa)) &&                          \
00433                                  _sa_ipbuf32((sa), 0) == 0 &&                 \
00434                                  _sa_ipbuf32((sa), 4) == 0 &&                 \
00435                                  _sa_ipbuf32((sa), 8) == 0 &&                 \
00436                                  _sa_ipbuf32((sa), 12) > 1)
00437 
00452 #define sa_is_v4encap(sa)       (sa_is_ipv6((sa)) &&                          \
00453                                  _sa_ipbuf32((sa), 0) == 0 &&                 \
00454                                  _sa_ipbuf32((sa), 4) == 0 &&                 \
00455                                  (_sa_ipbuf32((sa), 8) == 0 ||                \
00456                                   (_sa_ipbuf32((sa), 8) == 0x0000ffff &&      \
00457                                    _sa_ipbuf32((sa), 12) > 1)))
00458 
00475 #define _sa_ip4mask(sa, mask)   _sa_ipmask(32, (sa),                          \
00476                                            sa_is_ipv4((sa)) ? 0 : 12, (mask))
00477 
00491 #define sa_is_classa(sa)        ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00492                                  _sa_ip4mask((sa), 0x80000000) == 0)
00493 
00507 #define sa_is_classb(sa)        ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00508                                  _sa_ip4mask((sa), 0xc0000000) == 0x80000000)
00509 
00523 #define sa_is_classc(sa)        ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00524                                  _sa_ip4mask((sa), 0xe0000000) == 0xc0000000)
00525 
00539 #define sa_is_classd(sa)        ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00540                                  _sa_ip4mask((sa), 0xf0000000) == 0xe0000000)
00541 
00555 #define sa_is_experimental(sa)  ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00556                                  _sa_ip4mask((sa), 0xe0000000) == 0xe0000000)
00557 
00571 #define sa_is_broadcast(sa)     ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00572                                  _sa_ipbuf32((sa), sa_is_ipv4((sa)) ?         \
00573                                              0 : 12) == 0xffffffff)
00574 
00587 #define sa_is_multicast(sa)     (sa_is_classd((sa)) ||                        \
00588                                  (sa_is_ipv6((sa)) &&                         \
00589                                   _sa_ipbuf8((sa), 0) == 0xff))
00590 
00604 #define sa_is_linklocal(sa)     (sa_is_ipv6((sa)) &&                          \
00605                                  _sa_ipmask(32, (sa), 0, 0xffc00000) ==       \
00606                                  0xfe800000)
00607 
00621 #define sa_is_sitelocal(sa)     (sa_is_ipv6((sa)) &&                          \
00622                                  _sa_ipmask(32, (sa), 0, 0xffc00000) ==       \
00623                                  0xfec00000)
00624 
00638 #define _sa_ip4mc_local(sa)     ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) && \
00639                                  _sa_ip4mask((sa), 0xffffff00) == 0xe0000000)
00640 
00654 #define sa_mc_nodelocal(sa)     (sa_is_multicast((sa)) &&                     \
00655                                  (_sa_ip4mc_local((sa)) ||                    \
00656                                   (sa_is_ipv6((sa)) &&                        \
00657                                    _sa_ipmask(8, sa, 1, 0x0f) == 0x01)))
00658 
00672 #define sa_mc_linklocal(sa)     (sa_is_multicast((sa)) &&                     \
00673                                  (_sa_ip4mc_local((sa)) ||                    \
00674                                   (sa_is_ipv6((sa)) &&                        \
00675                                    _sa_ipmask(8, sa, 1, 0x0f) == 0x02)))
00676 
00690 #define sa_mc_sitelocal(sa)     (sa_is_multicast((sa)) &&                     \
00691                                  sa_is_ipv6((sa)) &&                          \
00692                                  _sa_ipmask(8, sa, 1, 0x0f) == 0x05)
00693 
00707 #define sa_mc_orglocal(sa)      (sa_is_multicast((sa)) &&                     \
00708                                  sa_is_ipv6((sa)) &&                          \
00709                                  _sa_ipmask(8, sa, 1, 0x0f) == 0x08)
00710 
00724 #define sa_mc_global(sa)        (sa_is_multicast((sa)) &&                     \
00725                                  ((sa_is_ipv4((sa)) || sa_is_v4encap((sa))) ? \
00726                                   (_sa_ip4mask((sa), 0xffffff00) !=           \
00727                                    0xe0000000) :                              \
00728                                   (sa_is_ipv6((sa)) &&                        \
00729                                    _sa_ipmask(8, (sa), 1, 0x0f) == 0x0e)))
00730 
00736 struct _ev_sock_s {
00737   ev_genhdr_t           so_hdr;         
00738   ev_socktype_t         so_type;        
00739   ev_sockstate_t        so_state;       
00740   ev_sockaddr_t         so_sockname;    
00741   ev_sockaddr_t         so_peername;    
00742   ev_flags_t            so_flags;       
00743   int                   so_fd;          
00744 };
00745 
00750 #define EV_SOCK_MAGIC 0x60be0671
00751 
00757 #define EV_SOCK_READ            0x00000001
00758 
00764 #define EV_SOCK_WRITE           0x00000002
00765 
00770 #define EV_SOCK_EVMASK          (EV_SOCK_READ | EV_SOCK_WRITE)
00771 
00787 #define _so_decev(flags, shift) (((flags) >> (shift)) & EV_SOCK_EVMASK)
00788 
00805 #define _so_encev(flags, shift) (((flags) & EV_SOCK_EVMASK) << (shift))
00806 
00815 #define _so_set(sock, flags)    ((sock)->so_flags |= (flags))
00816 
00825 #define _so_clr(sock, flags)    ((sock)->so_flags &= ~(flags))
00826 
00833 #define _EV_SOCK_UEVENTS        0
00834 
00841 #define _EV_SOCK_EEVENTS        2
00842 
00849 #define _EV_SOCK_BLOCKED        4
00850 
00857 #define _EV_SOCK_CLOSED         6
00858 
00869 #define so_uevents(sock)        _so_decev((sock)->so_flags, _EV_SOCK_UEVENTS)
00870 
00881 #define so_eevents(sock)        _so_decev((sock)->so_flags, _EV_SOCK_EEVENTS)
00882 
00893 #define so_blocked(sock)        _so_decev((sock)->so_flags, _EV_SOCK_BLOCKED)
00894 
00905 #define so_closed(sock)         _so_decev((sock)->so_flags, _EV_SOCK_CLOSED)
00906 
00914 #define EV_SOCK_TYPE            0x80000000
00915 
00923 #define EV_SOCK_STATE           0x40000000
00924 
00933 #define EV_SOCK_LOCAL           0x20000000
00934 
00943 #define EV_SOCK_REMOTE          0x10000000
00944 
00957 #define so_verify(sock)         eg_verify((sock), EV_SOCK_MAGIC)
00958 
00969 #define so_type(sock)           ((sock)->so_type)
00970 
00981 #define so_state(sock)          ((sock)->so_state)
00982 
00993 #define so_sockname(sock)       (&((sock)->so_sockname))
00994 
01006 #define so_peername(sock)       (&((sock)->so_peername))
01007 
01016 #define so_flags(sock)          ((sock)->so_flags)
01017 
01028 #define so_fd(sock)             ((sock)->so_fd)
01029 
01044 extern ev_err_t sa_type_set(ev_sockaddr_t *sa, ev_satype_t type)
01045      _gca_nonnull((1));
01046 
01063 extern ev_err_t sa_ipaddrbuf_set(ev_sockaddr_t *sa,
01064                                  const unsigned char *address)
01065      _gca_nonnull((1, 2));
01066 
01082 extern ev_err_t sa_ipaddrport_set(ev_sockaddr_t *sa, unsigned short port)
01083      _gca_nonnull((1));
01084 
01100 extern ev_err_t sa_localaddr_set(ev_sockaddr_t *sa, const char *address)
01101      _gca_nonnull((1, 2));
01102 
01126 extern ev_err_t sockaddr_ptoa(ev_sockaddr_t *sa, const char *address, int alen)
01127      _gca_nonnull((1, 2));
01128 
01152 extern ev_err_t sockaddr_atop(const ev_sockaddr_t *sa, char *buf, int size,
01153                               ev_flags_t flags)
01154      _gca_nonnull((1, 2));
01155 
01161 #define SOCKADDR_ATOP_NOPORT    0x0002
01162 
01179 extern ev_err_t sockaddr_import(ev_sockaddr_t *sa, const struct sockaddr *addr)
01180      _gca_nonnull((1, 2));
01181 
01206 extern ev_err_t sockaddr_export(const ev_sockaddr_t *sa, struct sockaddr *addr,
01207                                 int *len)
01208      _gca_nonnull((1, 2, 3));
01209 
01259 extern ev_err_t socket_open(ev_ctx_t *ctx, ev_socktype_t type,
01260                             const ev_sockaddr_t *local,
01261                             const ev_sockaddr_t *remote,
01262                             ev_flags_t events, const ev_sockattr_t *attr,
01263                             ev_call_t call, void *data,
01264                             ev_sock_t **sock_p)
01265      _gca_nonnull((1));
01266 
01329 extern ev_err_t socket_fdopen(ev_ctx_t *ctx, int fd, ev_socktype_t type,
01330                               ev_sockstate_t state, const ev_sockaddr_t *local,
01331                               const ev_sockaddr_t *remote, ev_flags_t events,
01332                               const ev_sockattr_t *attr, ev_call_t call,
01333                               void *data, ev_sock_t **sock_p)
01334      _gca_nonnull((1));
01335 
01394 extern ev_err_t socket_pair(ev_ctx_t *ctx, ev_socktype_t type,
01395                             ev_flags_t events1, const ev_sockattr_t *attr1,
01396                             ev_call_t call1, void *data1,
01397                             ev_flags_t events2, const ev_sockattr_t *attr2,
01398                             ev_call_t call2, void *data2,
01399                             ev_sock_t **sock1_p, ev_sock_t **sock2_p)
01400      _gca_nonnull((1));
01401 
01420 extern ev_err_t socket_connect(ev_ctx_t *ctx, ev_sock_t *sock,
01421                                const ev_sockaddr_t *addr)
01422      _gca_nonnull((1, 2));
01423 
01439 extern ev_err_t socket_close(ev_ctx_t *ctx, ev_sock_t *sock)
01440      _gca_nonnull((1, 2));
01441 
01465 extern ev_err_t socket_shutdown(ev_ctx_t *ctx, ev_sock_t *sock, int how)
01466      _gca_nonnull((1, 2));
01467 
01489 extern ev_err_t socket_events(ev_ctx_t *ctx, ev_sock_t *sock,
01490                               ev_flags_t events)
01491      _gca_nonnull((1, 2));
01492 
01508 extern ev_err_t socket_destroy(ev_ctx_t *ctx, ev_sock_t *sock)
01509      _gca_nonnull((1, 2));
01510 
01511 EV_END_C_DECLS
01512 
01515 #endif /* __include_event_sock_h */

Generated on Wed Dec 28 23:36:56 2005 for event by  doxygen 1.4.4