/* [<][>][^][v][top][bottom][index][help] */
1 /****************************************************************************
2 * Copyright (C) 2006-2008 by Jason Ansel, Kapil Arya, Gene Cooperman, *
3 * and Rohan Garg *
4 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu, and *
5 * rohgarg@ccs.neu.edu *
6 * *
7 * This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src). *
8 * *
9 * DMTCP:dmtcp/src is free software: you can redistribute it and/or *
10 * modify it under the terms of the GNU Lesser General Public License as *
11 * published by the Free Software Foundation, either version 3 of the *
12 * License, or (at your option) any later version. *
13 * *
14 * DMTCP:dmtcp/src is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Lesser General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with DMTCP:dmtcp/src. If not, see *
21 * <http://www.gnu.org/licenses/>. *
22 ****************************************************************************/
23
24 #pragma once
25 #ifndef SOCKETCONNECTION_H
26 #define SOCKETCONNECTION_H
27
28 // THESE INCLUDES ARE IN RANDOM ORDER. LET'S CLEAN IT UP AFTER RELEASE. - Gene
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <signal.h>
36 #include "jbuffer.h"
37 #include "connection.h"
38
39 namespace dmtcp
40 {
41 class SocketConnection
42 {
43 public:
44 enum PeerType
45 {
46 PEER_UNKNOWN,
47 PEER_INTERNAL,
48 PEER_EXTERNAL
49 };
50
51 uint32_t peerType() const { return _peerType; }
52
53 SocketConnection() {}
54 SocketConnection(int domain, int type, int protocol);
55 void addSetsockopt(int level, int option, const char* value, int len);
56 void restoreSocketOptions(vector<int32_t>& fds);
57 void serialize(jalib::JBinarySerializer& o);
58 int sockDomain() const { return _sockDomain; }
59
60 protected:
61 int64_t _sockDomain;
62 int64_t _sockType;
63 int64_t _sockProtocol;
64 uint32_t _peerType;
65 map< int64_t, map<int64_t, jalib::JBuffer> > _sockOptions;
66 };
67
68 class TcpConnection : public Connection, public SocketConnection
69 {
70 public:
71 enum TcpType
72 {
73 TCP_INVALID = TCP,
74 TCP_ERROR,
75 TCP_CREATED,
76 TCP_BIND,
77 TCP_LISTEN,
78 TCP_ACCEPT,
79 TCP_CONNECT,
80 TCP_PREEXISTING,
81 TCP_EXTERNAL_CONNECT
82 };
83
84 TcpConnection() {}
85
86 // This accessor is needed because _type is protected.
87 void markExternalConnect() { _type = TCP_EXTERNAL_CONNECT; }
88 bool isBlacklistedTcp(const sockaddr* saddr, socklen_t len);
89
90 //basic commands for updating state from wrappers
91 /*onSocket*/
92 TcpConnection(int domain, int type, int protocol);
93 void onBind(const struct sockaddr* addr, socklen_t len);
94 void onListen(int backlog);
95 void onConnect(const struct sockaddr *serv_addr = NULL,
96 socklen_t addrlen = 0);
97 /*onAccept*/
98 TcpConnection(const TcpConnection& parent,
99 const ConnectionIdentifier& remote);
100 void onError();
101 void onDisconnect();
102
103 void markPreExisting() { _type = TCP_PREEXISTING; }
104
105 //basic checkpointing commands
106 virtual void drain();
107 virtual void refill(bool isRestart);
108 virtual void postRestart();
109
110 void doSendHandshakes(const ConnectionIdentifier& coordId);
111 void doRecvHandshakes(const ConnectionIdentifier& coordId);
112
113 void sendHandshake(int remotefd, const ConnectionIdentifier& coordId);
114 void recvHandshake(int remotefd, const ConnectionIdentifier& coordId);
115
116 virtual string str() { return "<TCP Socket>"; }
117 virtual void serializeSubClass(jalib::JBinarySerializer& o);
118 private:
119 TcpConnection& asTcp();
120 private:
121 int32_t _listenBacklog;
122 union {
123 socklen_t _bindAddrlen;
124 socklen_t _connectAddrlen;
125 };
126 union {
127 /* See 'man socket.h' or POSIX for 'struct sockaddr_storage' */
128 struct sockaddr_storage _bindAddr;
129 struct sockaddr_storage _connectAddr;
130 };
131 ConnectionIdentifier _remotePeerId;
132 };
133
134 class RawSocketConnection : public Connection, public SocketConnection
135 {
136 public:
137 RawSocketConnection() {};
138 //basic commands for updating state from wrappers
139 RawSocketConnection(int domain, int type, int protocol);
140
141 //basic checkpointing commands
142 virtual void drain();
143 virtual void refill(bool isRestart);
144 virtual void postRestart();
145
146 virtual void serializeSubClass(jalib::JBinarySerializer& o);
147 virtual string str() { return "<TCP Socket>"; }
148 private:
149 map< int64_t, map< int64_t, jalib::JBuffer > > _sockOptions;
150 };
151 }
152
153 #endif