📄 tcp.lst
字号:
210 7 // but do not increase my sequence number
211 7 http_server(conxn[nr].query, 0, nr, 1);
212 7 conxn[nr].inactivity = INACTIVITY_TIME;
213 7 }
214 6 else
215 6 {
216 7 // Send reset and close connection
217 7 tcp_send(FLG_RST, 20, nr);
218 7 conxn[nr].ipaddr = 0;
219 7 }
220 6 }
221 5 }
222 4 }
223 3 }
224 2 }
225 1 }
226
227
228
229
230 //------------------------------------------------------------------------
231 // This runs every 0.5 seconds. If the connection has had no activity
232 // it initiates closing the connection.
233 //
234 //------------------------------------------------------------------------
235 void tcp_inactivity(void)
236 {
237 1 UCHAR idata nr;
238 1
239 1 // Look for active connections in the established state
240 1 for (nr = 0; nr < 5; nr++)
241 1 {
C51 COMPILER V7.50 TCP 12/14/2006 13:34:45 PAGE 5
242 2 if ((conxn[nr].ipaddr != 0) &&
243 2 (conxn[nr].state == STATE_ESTABLISHED) &&
244 2 (conxn[nr].inactivity))
245 2 {
246 3 // Decrement the timer and see if it hit 0
247 3 conxn[nr].inactivity--;
248 3 if (conxn[nr].inactivity == 0)
249 3 {
250 4 // Inactivity timer has just timed out.
251 4 // Initiate close of connection
252 4 tcp_send((FLG_ACK | FLG_FIN), 20, nr);
253 4 conxn[nr].my_sequence++; // For my FIN
254 4 conxn[nr].state = STATE_FIN_WAIT_1;
255 4 }
256 3 }
257 2 }
258 1 }
259
260
261
262 //------------------------------------------------------------------------
263 // This handles incoming TCP messages and manages the TCP state machine
264 // Note - both the SYN and FIN flags consume a sequence number.
265 // See "TCP/IP Illustrated, Volume 1" Sect 18.6 for info on TCP states
266 // See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
267 //------------------------------------------------------------------------
268 void tcp_rcve(UCHAR xdata * inbuf, UINT len)
269 {
270 1 UCHAR idata i, j, nr;
271 1 UINT idata result, header_len, data_len;
272 1 TCP_HEADER xdata * tcp;
273 1 IP_HEADER xdata * ip;
274 1 ULONG idata sum;
275 1
276 1 // IP header is always 20 bytes so message starts at index 34
277 1 tcp = (TCP_HEADER xdata *)(inbuf + 34);
278 1 ip = (IP_HEADER xdata *)(inbuf + 14);
279 1
280 1 // Compute TCP checksum including 12 byte pseudoheader
281 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
282 1 sum = (ULONG)cksum(inbuf + 26, 8 + len);
283 1
284 1 // Add in the rest of pseudoheader which is
285 1 // protocol id and TCP segment length
286 1 sum += (ULONG)0x0006;
287 1 sum += (ULONG)len;
288 1
289 1 // In case there was a carry, add it back around
290 1 result = (UINT)(sum + (sum >> 16));
291 1
292 1 if (result != 0xFFFF)
293 1 {
294 2 return;
295 2 }
296 1
297 1
298 1 // See if message is for http server
299 1 if (tcp->dest_port != HTTP_PORT)
300 1 {
301 2
302 2 tcp_send(FLG_RST, 20, NO_CONNECTION);
303 2 return;
C51 COMPILER V7.50 TCP 12/14/2006 13:34:45 PAGE 6
304 2 }
305 1
306 1 // Capture sender's IP address and port number
307 1 sender_ipaddr = ip->source_ipaddr;
308 1 sender_tcpport = tcp->source_port;
309 1
310 1 // See if the TCP segment is from someone we are already
311 1 // connected to.
312 1 for (i=0; i < 5; i++)
313 1 {
314 2 if ((ip->source_ipaddr == conxn[i].ipaddr) &&
315 2 (tcp->source_port == conxn[i].port))
316 2 {
317 3 nr = i;
318 3 break;
319 3 }
320 2 }
321 1
322 1 // If i = 5, we are not connected. If it is a SYN then assign
323 1 // a temporary conection to it for processing
324 1 if (i == 5)
325 1 {
326 2 if (tcp->flags & FLG_SYN)
327 2 {
328 3 // Find first unused connection (one with IP = 0)
329 3 for (j=0; j < 5; j++)
330 3 {
331 4 if (conxn[j].ipaddr == 0)
332 4 {
333 5 nr = j;
334 5 // Initialize new connection
335 5 conxn[nr].state = STATE_LISTEN;
336 5 break;
337 5 }
338 4 }
339 3
340 3 // If all connections are used then drop msg
341 3 if (j == 5) return;
342 3
343 3
344 3 }
345 2 }
346 1
347 1
348 1 // By now we should have a connection number in range of 0-4
349 1 // Do a check to avoid any chance of exceeding size of struct
350 1 if (nr > 4)
351 1 {
352 2 return;
353 2 }
354 1
355 1 // Eventually put in protection against wrapping sequence
356 1 // numbers, for now make the client start over if his
357 1 // sequence number is close to wrapping
358 1 if (tcp->sequence > 0xFFFFFF00L)
359 1 {
360 2 conxn[nr].ipaddr = 0;
361 2 tcp_send(FLG_RST, 20, NO_CONNECTION);
362 2 return;
363 2 }
364 1
365 1 // Handle messages whose action is mostly independent of state
C51 COMPILER V7.50 TCP 12/14/2006 13:34:45 PAGE 7
366 1 // such as RST, SYN, and segment with no ACK. That way the
367 1 // state machine below does not need to worry about it.
368 1 if (tcp->flags & FLG_RST)
369 1 {
370 2 // An RST does not depend on state at all. And it does
371 2 // not count as data so do not send an ACK here. Close
372 2 // connection
373 2 conxn[nr].ipaddr = 0;
374 2 return;
375 2 }
376 1
377 1 else if (tcp->flags & FLG_SYN)
378 1 {
379 2 // A SYN segment only makes sense if connection is in LISTEN
380 2 if ((conxn[nr].state != STATE_LISTEN) &&
381 2 (conxn[nr].state != STATE_CLOSED))
382 2 {
383 3 conxn[nr].ipaddr = 0;
384 3 tcp_send(FLG_RST, 20, NO_CONNECTION);
385 3 return;
386 3 }
387 2 }
388 1
389 1 else if ((tcp->flags & FLG_ACK) == 0)
390 1 {
391 2 // Incoming segments except SYN or RST must have ACK bit set
392 2 // See TCP/IP Illustrated, Vol 2, Page 965
393 2 // Drop segment but do not send a reset
394 2 return;
395 2 }
396 1
397 1 // Compute length of header including options, and from that
398 1 // compute length of actual data
399 1 header_len = (tcp->flags & 0xF000) >> 10;
400 1 data_len = len - header_len;
401 1
402 1
403 1
404 1 // Handle TCP state machine for this connection
405 1 switch (conxn[nr].state)
406 1 {
407 2 case STATE_CLOSED:
408 2 case STATE_LISTEN:
409 2
410 2 // If incoming segment contains SYN and no ACK, then handle
411 2 if ((tcp->flags & FLG_SYN) && ((tcp->flags & FLG_ACK) == 0))
412 2 {
413 3 // Capture his starting sequence number and generate
414 3 // my starting sequence number
415 3 // Fill in connection information
416 3 conxn[nr].ipaddr = ip->source_ipaddr;
417 3 conxn[nr].port = tcp->source_port;
418 3 conxn[nr].state = STATE_LISTEN;
419 3 conxn[nr].his_sequence = 1 + tcp->sequence;
420 3 conxn[nr].his_ack = tcp->ack_number;
421 3
422 3 // Use system clock for initial sequence number
423 3 conxn[nr].my_sequence = initial_sequence_nr;
424 3 initial_sequence_nr += 64000L;
425 3 EA = 1;
426 3
427 3 // Send header options with the next message
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -