Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: net/web2socket_proxy/web2socket_conn.h

Issue 5484001: Web2socket proxy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix for signed/unsigned confusion + cosmetic Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/web2socket_proxy/web2socket.cc ('k') | net/web2socket_proxy/web2socket_conn.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_WEB2SOCKET_PROXY_WEB2SOCKET_CONN_H_
6 #define NET_WEB2SOCKET_PROXY_WEB2SOCKET_CONN_H_
7
8 #include <sys/socket.h>
9
10 #include "base/md5.h"
11 #include "net/web2socket_proxy/web2socket_serv.h"
12 #include "third_party/libevent/event.h"
13
14 class Conn {
15 public:
16 enum Phase {
17 // Initial stage of connection.
18 PHASE_WAIT_HANDSHAKE,
19 PHASE_WAIT_DESTFRAME,
20 PHASE_WAIT_DESTCONNECT,
21
22 // Operational stage of connection.
23 PHASE_OUTSIDE_FRAME,
24 PHASE_INSIDE_FRAME_BASE64,
25 PHASE_INSIDE_FRAME_SKIP,
26
27 // Terminal stage of connection.
28 PHASE_SHUT, // Closing handshake was emitted, buffers may pending.
29 PHASE_DEFUNCT // Connection was nuked.
30 };
31
32 // Channel structure.
33 struct Chan {
34 int sock;
35 struct bufferevent* bev;
36
37 Chan() : sock(-1), bev(NULL) {}
38
39 ~Chan() {
40 if (bev) {
41 bufferevent_disable(bev, EV_READ | EV_WRITE);
42 bufferevent_free(bev);
43 bev = NULL;
44 }
45 if (sock >= 0) {
46 shutdown(sock, SHUT_RDWR);
47 close(sock);
48 sock = -1;
49 }
50 }
51 };
52
53 // Status of processing incoming data.
54 enum Status {
55 STATUS_OK,
56 STATUS_INCOMPLETE, // Not all required data is present in buffer yet.
57 STATUS_SKIP,
58 STATUS_ABORT // Data is invalid. We must shut connection.
59 };
60
61 // Unfortunately evdns callbacks are uncancellable,
62 // so potentially we can receive callback for a deleted Conn.
63 // Even worse, storage of deleted Conn may be reused
64 // for a new connection and new connection can receive callback
65 // destined for deleted Conn.
66 // Conn::Token is introduced in order to prevent that.
67 typedef void* Token;
68 typedef std::map<Token, Conn*> TokenMap;
69
70 explicit Conn(Web2SocketServ* master);
71 ~Conn();
72
73 static Conn* Get(Token token);
74
75 void Shut();
76
77 Status ConsumeHeader(struct evbuffer*);
78 Status ConsumeDestframe(struct evbuffer*);
79 Status ConsumeFrameHeader(struct evbuffer*);
80 Status ProcessFrameData(struct evbuffer*);
81
82 // Returns true on success.
83 bool EmitHandshake(struct bufferevent*);
84
85 // Attempts to establish second connection (to remote TCP service).
86 // Returns true on success.
87 bool TryConnectDest(const struct sockaddr*, socklen_t);
88
89 // Used as libevent callbacks.
90 static void OnDestConnectTimeout(int, short, Token);
91 static void OnPrimchanRead(struct bufferevent*, Token);
92 static void OnPrimchanWrite(struct bufferevent*, Token);
93 static void OnPrimchanError(struct bufferevent*, short what, Token);
94 static void OnDestResolutionIPv4(int result, char type, int count,
95 int ttl, void* addr_list, Token);
96 static void OnDestResolutionIPv6(int result, char type, int count,
97 int ttl, void* addr_list, Token);
98 static void OnDestchanRead(struct bufferevent*, Token);
99 static void OnDestchanWrite(struct bufferevent*, Token);
100 static void OnDestchanError(struct bufferevent*, short what, Token);
101
102 Chan& primchan() { return primchan_; }
103 Token token() const { return token_; }
104
105 private:
106 Web2SocketServ* master_;
107 Phase phase_;
108
109 // We maintain two channels per Conn:
110 // primary channel is websocket connection.
111 Chan primchan_;
112 // Destination channel is a proxied connection.
113 Chan destchan_;
114
115 Token token_;
116
117 // Header fields supplied by client at initial websocket handshake.
118 std::map<std::string, std::string> header_fields_;
119
120 // Cryptohashed answer for websocket handshake.
121 MD5Digest handshake_response_;
122
123 // Hostname and port of destination socket.
124 // Websocket client supplies them in first data frame (destframe).
125 std::string destname_;
126 uint32_t destport_;
127
128 // We try to DNS resolve hostname in both IPv4 and IPv6 domains.
129 // Track resolution failures here.
130 bool destresolution_ipv4_failed_;
131 bool destresolution_ipv6_failed_;
132
133 // Used to schedule a timeout for an initial phase of connection.
134 struct event destconnect_timeout_event_;
135
136 static TokenMap token_map_;
137 static Token last_token_;
138
139 DISALLOW_COPY_AND_ASSIGN(Conn);
140 };
141
142 #endif // NET_WEB2SOCKET_PROXY_WEB2SOCKET_CONN_H_
OLDNEW
« no previous file with comments | « net/web2socket_proxy/web2socket.cc ('k') | net/web2socket_proxy/web2socket_conn.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698