RTP Audio System  2.0.0
tdsocket.h
Go to the documentation of this file.
00001 /*
00002  *  $Id: tdsocket.h 1285 2011-12-18 13:25:14Z dreibh $
00003  *
00004  * SocketAPI implementation for the sctplib.
00005  * Copyright (C) 2005-2012 by Thomas Dreibholz
00006  *
00007  * Realized in co-operation between
00008  * - Siemens AG
00009  * - University of Essen, Institute of Computer Networking Technology
00010  * - University of Applied Sciences, Muenster
00011  *
00012  * Acknowledgement
00013  * This work was partially funded by the Bundesministerium fuer Bildung und
00014  * Forschung (BMBF) of the Federal Republic of Germany (Foerderkennzeichen 01AK045).
00015  * The authors alone are responsible for the contents.
00016  *
00017  * This program is free software: you can redistribute it and/or modify
00018  * it under the terms of the GNU General Public License as published by
00019  * the Free Software Foundation, either version 3 of the License, or
00020  * (at your option) any later version.
00021 
00022  * This program is distributed in the hope that it will be useful,
00023  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  * GNU General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00029  *
00030  * Contact: discussion@sctp.de
00031  *          dreibh@iem.uni-due.de
00032  *          tuexen@fh-muenster.de
00033  *
00034  * Purpose: Socket Implementation
00035  *
00036  */
00037 
00038 
00039 #ifndef TDSOCKET_H
00040 #define TDSOCKET_H
00041 
00042 
00043 #include "tdsystem.h"
00044 #include "internetaddress.h"
00045 #include "internetflow.h"
00046 #include "ext_socket.h"
00047 
00048 
00049 #include <fcntl.h>
00050 
00051 
00052 
00056 const cardinal UDPHeaderSize = 8;
00057 
00061 const cardinal IPv4HeaderSize = 20;
00062 
00066 const cardinal IPv6HeaderSize = 40;
00067 
00068 
00077 class Socket
00078 {
00079    // ====== Definitions ====================================================
00080    public:
00081    enum SocketFamily {
00082       UndefinedSocketFamily = -1,
00083       IP                    = 255,
00084       IPv4                  = AF_INET,  // Do not use IPv4/IPv6,
00085       IPv6                  = AF_INET6, // use IP instead!
00086       Unix                  = AF_UNIX
00087    };
00088    enum SocketType {
00089       UndefinedSocketType = -1,
00090       UDP                 = SOCK_DGRAM,
00091       Datagram            = SOCK_DGRAM,
00092       TCP                 = SOCK_STREAM,
00093       Stream              = SOCK_STREAM,
00094       Raw                 = SOCK_RAW,
00095       RDM                 = SOCK_RDM,
00096       SeqPacket           = SOCK_SEQPACKET
00097    };
00098    enum SocketProtocol {
00099       UndefinedSocketProtocol = -1,
00100       Default                 = 0,
00101       ICMPv4                  = IPPROTO_ICMP,
00102       ICMPv6                  = IPPROTO_ICMPV6,
00103       SCTP                    = IPPROTO_SCTP
00104    };
00105    enum GetSocketAddressFlags {
00106       GLAF_HideLoopback       = (1 << 0),
00107       GLAF_HideLinkLocal      = (1 << 1),
00108       GLAF_HideSiteLocal      = (1 << 2),
00109       GLAF_HideLocal          = GLAF_HideLoopback|GLAF_HideLinkLocal|GLAF_HideSiteLocal,
00110       GLAF_HideAnycast        = (1 << 3),
00111       GLAF_HideMulticast      = (1 << 4),
00112       GLAF_HideBroadcast      = (1 << 5),
00113       GLAF_HideReserved       = (1 << 6),
00114       GLAF_Default            = GLAF_HideLoopback|GLAF_HideLinkLocal|Socket::GLAF_HideSiteLocal|GLAF_HideBroadcast|GLAF_HideMulticast|GLAF_HideAnycast
00115    };
00116 
00117 
00118    // ====== Constructor/Destructor =========================================
00122    Socket();
00123 
00136    Socket(const integer family,
00137           const integer socketType,
00138           const integer socketProtocol = Default);
00139 
00143    ~Socket();
00144 
00145 
00146    // ====== Create/close socket ============================================
00157    bool create(const integer socketFamily   = IP,
00158                const integer socketType     = TCP,
00159                const integer socketProtocol = Default);
00160 
00164    void close();
00165 
00174    void shutdown(const cardinal shutdownLevel);
00175 
00176 
00177    // ====== Socket properties ==============================================
00183    inline integer getFamily() const;
00184 
00190    inline integer getType() const;
00191 
00197    inline integer getProtocol() const;
00198 
00199 
00200    // ====== Socket control functions =======================================
00206    inline bool ready() const;
00207 
00215    bool bind(const SocketAddress& address = InternetAddress());
00216 
00226    bool bindx(const SocketAddress** addressArray = NULL,
00227               const cardinal        addresses    = 0,
00228               const integer         flags        = 0);
00229 
00237    bool listen(const cardinal backlog = 5);
00238 
00245    Socket* accept(SocketAddress** address = NULL);
00246 
00255    bool connect(const SocketAddress& address, const card8 trafficClass = 0);
00256 
00257 
00267    bool connectx(const SocketAddress** addressArray,
00268                  const size_t          addresses);
00269 
00270    // ====== Error code =====================================================
00276    inline integer getLastError();
00277 
00278 
00279    // ====== Socket options =================================================
00289    inline integer getSocketOption(const cardinal level,
00290                                   const cardinal optionNumber,
00291                                   void*          optionValue,
00292                                   socklen_t*     optionLength);
00293 
00299    cardinal getSoLinger();
00300 
00306    bool getSoReuseAddress();
00307 
00313    bool getSoBroadcast();
00314 
00320    bool getTCPNoDelay();
00321 
00327    bool getBlockingMode();
00328 
00329 
00339    inline integer setSocketOption(const cardinal  level,
00340                                   const cardinal  optionNumber,
00341                                   const void*     optionValue,
00342                                   const socklen_t optionLength);
00343 
00351    bool setSoLinger(const bool on, const cardinal linger);
00352 
00359    bool setSoReuseAddress(const bool on);
00360 
00367    bool setSoBroadcast(const bool on);
00368 
00375    bool setTCPNoDelay(const bool on);
00376 
00384    bool setBlockingMode(const bool on);
00385 
00386 
00387    // ====== Get flow label/traffic class ===================================
00395    inline card32 getSendFlowLabel() const;
00396 
00404    inline card8 getSendTrafficClass() const;
00405 
00411    inline card32 getReceivedFlowLabel() const;
00412 
00418    inline card8 getReceivedTrafficClass() const;
00419 
00420 
00421    // ====== I/O functions ==================================================
00434    ssize_t send(const void*   buffer,
00435                 const size_t  length,
00436                 const integer flags        = 0,
00437                 const card8   trafficClass = 0x00);
00438 
00450    ssize_t sendTo(const void*          buffer,
00451                   const size_t         length,
00452                   const integer        flags,
00453                   const SocketAddress& receiver,
00454                   const card8          trafficClass = 0x00);
00455 
00464    ssize_t sendMsg(const struct msghdr* msg,
00465                    const integer        flags,
00466                    const card8          trafficClass = 0x00);
00467 
00475    inline ssize_t write(const void*  buffer,
00476                         const size_t length);
00477 
00486    inline ssize_t receive(void*        buffer,
00487                           const size_t length,
00488                           integer&     flags);
00489 
00499    ssize_t receiveFrom(void*          buffer,
00500                        const size_t   length,
00501                        SocketAddress& sender,
00502                        integer&       flags);
00503 
00512    ssize_t receiveMsg(struct msghdr* msg,
00513                       const integer  flags,
00514                       const bool     internalCall = false);
00515 
00523    inline ssize_t read(void*        buffer,
00524                        const size_t length);
00525 
00533    inline integer fcntl(const integer cmd, long arg);
00534 
00542    inline integer fcntl(const integer cmd, struct flock* lock);
00543 
00551    inline integer ioctl(const integer request, const void* argp);
00552 
00553 
00554    // ====== Get address ====================================================
00566    bool getSocketAddress(SocketAddress& address) const;
00567 
00579    bool getPeerAddress(SocketAddress& address) const;
00580 
00581 
00582    // ====== Multicast functions ============================================
00590    inline bool addMulticastMembership(const SocketAddress& address,
00591                                       const char* interface = NULL);
00592 
00600    inline bool dropMulticastMembership(const SocketAddress& address,
00601                                        const char* interface = NULL);
00602 
00608    bool getMulticastLoop();
00609 
00616    bool setMulticastLoop(const bool on);
00617 
00623    card8 getMulticastTTL();
00624 
00631    bool setMulticastTTL(const card8 ttl);
00632 
00633 
00634    // ====== IPv6 flow functions ============================================
00645    InternetFlow allocFlow(const InternetAddress& address,
00646                           const card32           flowLabel  = 0,
00647                           const card8            shareLevel = 2);
00648 
00654    void freeFlow(InternetFlow& flow);
00655 
00667    bool renewFlow(InternetFlow&  flow,
00668                   const cardinal expires,
00669                   const cardinal linger = 6);
00670 
00679    bool renewFlow(const cardinal expires,
00680                   const cardinal linger = 6);
00681 
00682 
00683    // ====== Bind pair of sockets ===========================================
00694    static bool bindSocketPair(Socket&              socket1,
00695                               Socket&              socket2,
00696                               const SocketAddress& address = InternetAddress());
00697 
00712    static bool bindxSocketPair(Socket&               socket1,
00713                                Socket&               socket2,
00714                                const SocketAddress** addressArray = NULL,
00715                                const cardinal        addresses    = 0,
00716                                const integer         flags        = 0);
00717 
00718 
00719    // ====== Get system's socket descriptor =================================
00727    inline int getSystemSocketDescriptor() const;
00728 
00729 
00730    // ====== Obtaining Local addresses ======================================
00742    static bool getLocalAddressList(SocketAddress**& addressList,
00743                                    cardinal&        numberOfNets,
00744                                    const cardinal   flags = GLAF_Default);
00745 
00746 
00747    // ====== Constants ======================================================
00753    static const cardinal MinAutoSelectPort = 16384;
00754 
00760    static const cardinal MaxAutoSelectPort = 61000;
00761 
00762 
00763    // ====== Private data ===================================================
00764    private:
00765    friend class TrafficShaper;
00766    void init();
00767    bool setTypeOfService(const card8 trafficClass);
00768    ssize_t recvFrom(int              fd,
00769                     void*            buf,
00770                     const size_t     len,
00771                     integer&         flags,
00772                     struct sockaddr* addr,
00773                     socklen_t*       addrlen);
00774    bool multicastMembership(const SocketAddress& address,
00775                             const char*          interface,
00776                             const bool           add);
00777    void packSocketAddressArray(const sockaddr_storage* addrArray,
00778                                const size_t            addrs,
00779                                sockaddr*               packedArray);
00780 
00781 
00782    int       SocketDescriptor;
00783    integer   Family;
00784    integer   Type;
00785    integer   Protocol;
00786    card32    SendFlow;
00787    card32    ReceivedFlow;
00788    integer   LastError;
00789    cardinal  Backlog;
00790    sockaddr* Destination;
00791 };
00792 
00793 
00794 #include "tdsocket.icc"
00795 
00796 
00797 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines