📄 tcpserver.c
字号:
#include <stdlib.h>
#include <ctype.h> // toupper
#include "net.h"
#include "cksum.h"
#include "ip.h"
#include "tcp.h"
#include "udp.h"
#include "tcpserver.h"
#include "utils.h"
// These structures keep track of connection information
extern CONNECTION conxn[];
extern ulong my_ipaddr;
extern char text[];
extern UCHAR rcve_buf_allocated;
extern UCHAR debug;
extern UCHAR TFTP_BUF[];
char cnt = 0;
//------------------------------------------------------------------------
// This sends an TCP segment to the ip layer. The segment is
// is normally either a web page or a graphic.
// See "TCP/IP Illustrated, Volume 1" Sect 17.3
//------------------------------------------------------------------------
void tcp_server_send(UCHAR * outbuf, uint len, UCHAR nr)
{
TCP_HEADER * tcp;
IP_HEADER * ip;
ulong sum;
uint result;
// Fill in TCP segment header
tcp = (TCP_HEADER *)(outbuf + 34);
ip = (IP_HEADER *)(outbuf + 14);
tcp->source_port = HTTP_PORT;
tcp->dest_port = conxn[nr].port;
tcp->sequence = conxn[nr].my_sequence;
tcp->ack_number = conxn[nr].his_sequence;
// Header is always 20 bytes long
tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
tcp->window = 1024;
tcp->checksum = 0;
tcp->urgent_ptr = 0;
// Compute checksum including 12 bytes of pseudoheader
// Must pre-fill 2 items in ip header to do this
ip->dest_ipaddr = conxn[nr].ipaddr;
ip->source_ipaddr = my_ipaddr;
#ifdef __LITTLEENDIAN__
tcp->flags = ntohs(tcp->flags);
tcp->window = ntohs(tcp->window);
tcp->source_port = ntohs(tcp->source_port);
tcp->sequence = ntohl(tcp->sequence);
tcp->dest_port = ntohs(tcp->dest_port);
tcp->ack_number = ntohl(tcp->ack_number);
ip->source_ipaddr = ntohl(my_ipaddr);
#endif
// Sum source_ipaddr, dest_ipaddr, and entire TCP message
sum = (ulong)cksum(outbuf + 26, 8 + len);
// Add in the rest of pseudoheader which is
// protocol id and TCP segment length
sum += (ulong)0x0006;
sum += (ulong)len;
// In case there was a carry, add it back around
result = (uint)(sum + (sum >> 16));
tcp->checksum = ~result;
#ifdef __LITTLEENDIAN__
tcp->checksum = ntohs(tcp->checksum);
// ip_send(outbuf, ntohl(conxn[nr].ipaddr), TCP_TYPE, len);
conxn[nr].ipaddr = ntohl(conxn[nr].ipaddr);
#endif
// if (debug) serial_send("TCP: Sending msg to IP layer\r");
ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
#ifdef __LITTLEENDIAN__
free(outbuf);
#endif
// (Re)start TCP retransmit timer
conxn[nr].timer = TCP_TIMEOUT;
}
//------------------------------------------------------------------------
// This searches a web page looking for a specified tag. If found,
// it replaces the tag with the text in * sub. Tags are fixed length -
// The first 4 chars of the tag is always "TAG:" and the rest of it
// is always 4 chars for a total of 8 chars.
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// This serves up either a HTML page, a JPEG image, or controls an
// LED, depending what it gets from the browser. The received header
// must contain the word "GET" or "POST" to be considered a valid request.
// With HTTP 1.1 where the connection is left open, the header I send
// should include content length. With HTTP 1.0 you can just close the
// connection after sending the page and the browser knows its done.
//
// The HTTP protocol specification is at http://www.w3.org/Protocols/
//------------------------------------------------------------------------
uint tcp_server_recv(UCHAR * inbuf, uint header_len, UCHAR nr, UCHAR resend, int data_len)
{
UCHAR * outbuf;
UCHAR * tcp_data;
// Make sure this is a valid connection
if (nr == NO_CONNECTION)
return 0;
// Compute start of TCP data
// Save first 20 chars and seq number just in case
// we need to re-generate page
// TODO: if post, then save switch state infomation
if (!resend)
{
tcp_data = inbuf + 34 + header_len;
memcpy((char*)conxn[nr].query, tcp_data, 20);
conxn[nr].old_sequence = conxn[nr].my_sequence;
}
// If this is a resend, set sequence number to what it was
// the last time we sent this
else
{
tcp_data = inbuf;
conxn[nr].my_sequence = conxn[nr].old_sequence;
}
if(data_len>0)
{
// Free memory holding received message. The message from the
// browser can be 500+ bytes long so this is a significant
// chunk out of the available malloc space of 1500 bytes
if (!resend) {rcve_buf_allocated = FALSE;}
outbuf = TX_BUFF;
if (outbuf == NULL)
{
return 0;
}
memcpy(outbuf + 54, tcp_data, data_len);
tcp_server_send(outbuf, 20 + data_len, nr);
conxn[nr].my_sequence += data_len;
// udp_broadcast(TFTP_BUF, 7001, data_len);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -