summaryrefslogtreecommitdiffstats
path: root/src/Net/Connection.h
blob: 1c15a95be352fc49f0c9ca1696ea697b090d95f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Connection.h
 *
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MAD_NET_CONNECTION_H_
#define MAD_NET_CONNECTION_H_

#include "Packet.h"

#include <queue>
#include <string>
#include <gnutls/gnutls.h>
#include <sigc++/signal.h>
#include <poll.h>

namespace Mad {
namespace Net {

class IPAddress;
class Packet;

class Connection {
  private:
    struct Transmission {
      unsigned long length;
      unsigned long transmitted;

      uint8_t *data;

      sigc::slot<void,const void*,unsigned long> notify;
    };

    enum State {
      DISCONNECTED, HANDSHAKE, CONNECTION_HEADER, PACKET_HEADER, PACKET_DATA, DISCONNECT, BYE
    } state;

    Transmission transR;
    std::queue<Transmission> transS;

    Packet::Data header;

    sigc::signal<void,const Packet&> signal;

    void doHandshake();

    void packetHeaderReceiveHandler(const void *data, unsigned long length);
    void packetDataReceiveHandler(const void *data, unsigned long length);

    void doReceive();
    void doSend();

    void doBye();

    void doDisconnect();

    bool receiveComplete() const {
      return (transR.length == transR.transmitted);
    }

    void bye();

    void updateEvents() const;

    // Prevent shallow copy
    Connection(const Connection &o);
    Connection& operator=(const Connection &o);

  protected:
    struct ConnectionHeader {
      uint8_t m;
      uint8_t a;
      uint8_t d;
      uint8_t type;

      uint8_t versionMajor;
      uint8_t versionMinor;
      uint8_t protVerMin;
      uint8_t protVerMax;
    };

    int sock;
    gnutls_session_t session;
    gnutls_certificate_credentials_t x509_cred;

    IPAddress *peer;

    void handshake();

    virtual void connectionHeader() = 0;

    bool rawReceive(unsigned long length, const sigc::slot<void,const void*,unsigned long> &notify);
    bool rawSend(const uint8_t *data, unsigned long length);

    bool enterReceiveLoop();

  public:
    Connection() : state(DISCONNECTED), peer(0) {
      transR.length = transR.transmitted = 0;
      transR.data = 0;

      gnutls_certificate_allocate_credentials(&x509_cred);
    }

    virtual ~Connection();

    bool isConnected() const {return (state != DISCONNECTED);}
    bool isConnecting() const {
      return (state == HANDSHAKE || state == CONNECTION_HEADER);
    }

    bool isDisconnecting() const {
      return (state == DISCONNECT || state == BYE);
    }

    const gnutls_datum_t* getCertificate() const {
      return gnutls_certificate_get_ours(session);
    }

    const gnutls_datum_t* getPeerCertificate() const {
      unsigned int n;
      return gnutls_certificate_get_peers(session, &n);
    }

    const IPAddress* getPeer() const {return peer;}

    void disconnect();

    bool send(const Packet &packet);

    void sendReceive(short events);

    bool sendQueueEmpty() const {return transS.empty();}

    sigc::signal<void,const Packet&> signalReceive() const {return signal;}

    static void init() {
      gnutls_global_init();
    }

    static void deinit() {
      gnutls_global_deinit();
    }
};

}
}

#endif /*MAD_NET_CONNECTION_H_*/