Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Preparation for checking session transmitted bytes

Modify session record and state to maintain transmitted bytes in both
directions. If the number of bytes is not kept, lost packets will disrupt
the session.

When the client is sending data, the byte count should be equal to our
count plus the size of the new data. Otherwise, the packet is assumed
to be a duplicate and will be dropped.

When the client is requesting pending data, the byte count should be
equal to ours. If it is smaller, we should re-send our last packet.
  • Loading branch information...
commit 596a84d56c5d5a8c1dcaecaf091921c13827f1ce 1 parent 91f6159
Michael Santos authored November 04, 2010

Showing 1 changed file with 35 additions and 18 deletions. Show diff stats Hide diff stats

  1. 53  seds/src/seds_proxy.erl
53  seds/src/seds_proxy.erl
@@ -40,7 +40,8 @@
40 40
         dnsfd,          % dns server socket
41 41
         s,              % proxied socket
42 42
 
43  
-        sum = 0,        % number of bytes sent
  43
+        sum_up = 0,     % number of bytes sent to server
  44
+        sum_down = 0,   % number of bytes received from server
44 45
         data = [<<>>]   % list of binaries: data returned by proxied server
45 46
     }).
46 47
 
@@ -62,9 +63,9 @@
62 63
 %%% Interface
63 64
 %%--------------------------------------------------------------------
64 65
 send(Pid, IP, Port, #dns_rec{} = Query, {up, Data}) when is_pid(Pid) ->
65  
-    gen_fsm:send_event(Pid, {dns_query, IP, Port, Query, Data});
  66
+    gen_fsm:send_event(Pid, {up, IP, Port, Query, Data});
66 67
 send(Pid, IP, Port, #dns_rec{} = Query, {down, _}) when is_pid(Pid) ->
67  
-    gen_fsm:send_event(Pid, {dns_query, IP, Port, Query}).
  68
+    gen_fsm:send_event(Pid, {down, IP, Port, Query}).
68 69
 
69 70
 %%--------------------------------------------------------------------
70 71
 %%% Behaviours
@@ -110,11 +111,13 @@ handle_info({tcp_closed, Socket}, proxy, #state{s = Socket} = State) ->
110 111
 terminate(Reason, StateName, #state{
111 112
         ip = IP,
112 113
         port = Port,
113  
-        sum = Sum
  114
+        sum_up = Up,
  115
+        sum_down = Down
114 116
     }) ->
115 117
     error_logger:info_report([
116 118
             {session_end, {IP, Port}},
117  
-            {bytes_sent, Sum},
  119
+            {bytes_sent, Up},
  120
+            {bytes_rcvd, Down},
118 121
             {state, StateName},
119 122
             {reason, Reason}
120 123
         ]),
@@ -127,6 +130,10 @@ code_change(_OldVsn, StateName, State, _Extra) ->
127 130
 %%--------------------------------------------------------------------
128 131
 %%% States
129 132
 %%--------------------------------------------------------------------
  133
+
  134
+%%
  135
+%% connect
  136
+%%
130 137
 connect(timeout, #state{ip = IP, port = Port} = State) ->
131 138
     {ok, Socket} = gen_tcp:connect(IP, Port, [
132 139
             binary,
@@ -135,7 +142,13 @@ connect(timeout, #state{ip = IP, port = Port} = State) ->
135 142
         ], 5000),
136 143
     {next_state, proxy, State#state{s = Socket}}.
137 144
 
138  
-proxy({dns_query, IP, Port,
  145
+
  146
+%%
  147
+%% proxy
  148
+%%
  149
+
  150
+% client sent data to be forwarded to server
  151
+proxy({up, IP, Port,
139 152
         #dns_rec{
140 153
             header = Header,
141 154
             qdlist = [#dns_query{
@@ -144,17 +157,16 @@ proxy({dns_query, IP, Port,
144 157
                 }|_]
145 158
         } = Rec, Data},
146 159
     #state{
147  
-        sum = Sum,
  160
+        sum_up = Sum,
148 161
         dnsfd = DNSSocket,
149 162
         s = Socket
150 163
     } = State) ->
151 164
 
152 165
     Payload = base32:decode(string:to_upper(Data)),
  166
+    Sum1 = Sum + length(Payload),
153 167
 
154 168
     ok = gen_tcp:send(Socket, Payload),
155 169
 
156  
-    Sum1 = Sum + length(Payload),
157  
-
158 170
     Packet = inet_dns:encode(
159 171
         Rec#dns_rec{
160 172
             header = Header#dns_header{
@@ -174,25 +186,29 @@ proxy({dns_query, IP, Port,
174 186
         ]),
175 187
 
176 188
     ok = gen_udp:send(DNSSocket, IP, Port, Packet),
177  
-    {next_state, proxy, State, ?PROXY_TIMEOUT};
178  
-proxy({dns_query, IP, Port,
  189
+    {next_state, proxy, State#state{sum_up = Sum1},
  190
+        ?PROXY_TIMEOUT};
  191
+
  192
+% client requested any pending data from server
  193
+proxy({down, IP, Port,
179 194
         #dns_rec{
180 195
             header = Header,
181 196
             qdlist = [#dns_query{
182 197
                     domain = Domain,
183 198
                     type = Type
184  
-                }|_]
185  
-        } = Rec},
186  
-    #state{
187  
-        dnsfd = DNSSocket,
188  
-        s = Socket,
189  
-        data = Data
190  
-    } = State) ->
  199
+                }|_]} = Rec},
  200
+        #state{
  201
+            sum_down = Sum,
  202
+            dnsfd = DNSSocket,
  203
+            s = Socket,
  204
+            data = Data
  205
+        } = State) ->
191 206
 
192 207
     % Client polled, allow more data from server
193 208
     ok = inet:setopts(Socket, [{active, once}]),
194 209
 
195 210
     {Payload, Rest} = data(Type, Data),
  211
+    Sum1 = Sum + length(Payload),
196 212
 
197 213
     Response = Rec#dns_rec{
198 214
         header = Header#dns_header{
@@ -215,6 +231,7 @@ proxy({dns_query, IP, Port,
215 231
     ok = gen_udp:send(DNSSocket, IP, Port, Packet),
216 232
 
217 233
     {next_state, proxy, State#state{
  234
+            sum_down = Sum1,
218 235
             data = [Rest]
219 236
         }, ?PROXY_TIMEOUT};
220 237
 proxy(timeout, State) ->

0 notes on commit 596a84d

Please sign in to comment.
Something went wrong with that request. Please try again.