- Timestamp:
- 06/03/10 22:43:33 (21 months ago)
- Location:
- Whitix/branches/netchannel/user/sdk/network
- Files:
-
- 15 added
- 12 modified
-
Makefile (modified) (2 diffs)
-
aio.c (modified) (2 diffs)
-
channels.c (modified) (1 diff)
-
checksum.h (modified) (1 diff)
-
dns.c (modified) (2 diffs)
-
icmp.c (modified) (3 diffs)
-
ipv4.c (modified) (3 diffs)
-
ipv4.h (modified) (1 diff)
-
seq.h (modified) (1 diff)
-
socket.c (modified) (3 diffs)
-
stats.c (modified) (8 diffs)
-
tcp (added)
-
tcp/Makefile (added)
-
tcp/tcp.c (added)
-
tcp/tcp.h (added)
-
tcp/tcp_buffer.h (added)
-
tcp/tcp_congestion.c (added)
-
tcp/tcp_congestion.h (added)
-
tcp/tcp_events.c (added)
-
tcp/tcp_options.c (added)
-
tcp/tcp_options.h (added)
-
tcp/tcp_output.c (added)
-
tcp/tcp_output.h (added)
-
tcp/tcp_states.c (added)
-
tcp/tcp_states.h (added)
-
tcp/tcp_timer.c (added)
-
udp.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
Whitix/branches/netchannel/user/sdk/network/Makefile
r2088 r2099 1 CFLAGS = -Wall -I../include -I../../libc/include -nostdlib -ffreestanding -fno-builtin -fPIC -m32 -O 3-Werror1 CFLAGS = -Wall -I../include -I../../libc/include -nostdlib -ffreestanding -fno-builtin -fPIC -m32 -O2 -Werror 2 2 3 OBJS = aio.o byteorder.o ipv4.o udp.o memory.o dns.o icmp.o tcp.o tcp_options.o channels.o init.o socket.o stats.o3 OBJS = aio.o byteorder.o ipv4.o udp.o memory.o dns.o icmp.o channels.o init.o socket.o stats.o events.o 4 4 5 5 .c.o: … … 7 7 8 8 build: $(OBJS) 9 make -C tcp 9 10 gcc -m32 -nostdlib -ffreestanding -fno-builtin -shared -L../libs/ \ 10 11 -L../../posix/ -lpthread -o ../libs/libnetwork.so -Wl,-init=NetworkPreInit \ 11 $(OBJS) ../../libc/init/crt_begin.o ../../libc/init/crt_end.o12 $(OBJS) tcp/*.o ../../libc/init/crt_begin.o ../../libc/init/crt_end.o 12 13 13 14 clean: -
Whitix/branches/netchannel/user/sdk/network/aio.c
r2090 r2099 5 5 #include <pthread.h> 6 6 7 int globalAioThr; 8 int aioWork; 9 struct AioCtx* workHead; 10 pthread_mutex_t workLock; 11 12 int SocketAsyncThreaded(Socket* socket) 7 int SocketAsyncRecv(Socket* socket, struct AsyncCtx* ctx, char* buffer, int length) 13 8 { 14 printf("Create thread to handle async requests separately\n"); 15 return 0; 16 } 17 18 int SocketAioCreate(struct AioCtx* aio, AioCallback func, int flags) 19 { 20 aio->magic = AIO_MAGIC; 21 aio->func = func; 22 aio->prev = aio->next = NULL; 23 aio->flags = flags; 24 25 return 0; 26 } 27 28 int SocketAsyncRecv(Socket* socket, struct AioCtx* aio) 29 { 30 if (aio->magic != AIO_MAGIC) 31 return AIO_INVALID; 32 33 socket->recvAio = aio; 9 socket->recvAio = ctx; 10 ctx->buffer = buffer; 11 ctx->bufferSize = length; 34 12 35 13 return AIO_SUCCESS; 36 14 } 37 15 38 int SocketAsyncSend(Socket* socket, struct A ioCtx* aio, char* buffer, int len, int flags)16 int SocketAsyncSend(Socket* socket, struct AsyncCtx* aio, char* buffer, int len, int flags) 39 17 { 40 18 /* Add to queue */ … … 42 20 } 43 21 44 void AioWorkerThread(void* data); 45 46 int SocketAioFire(Socket* socket, struct AioCtx* aio, int len) 47 { 48 if (!globalAioThr) 49 { 50 globalAioThr = SysCreateThread((unsigned long)AioWorkerThread, 0, NULL); 51 pthread_mutex_init(&workLock, NULL); 52 } 53 54 aio->length = len; 55 workHead = aio; 56 57 aioWork = 1; 58 SysResumeThread(globalAioThr); 59 SysYield(); 60 61 return 0; 62 } 63 64 int SocketAioCancel(struct AioCtx* aio) 22 int SocketAioCancel(struct AsyncCtx* aio) 65 23 { 66 24 return 0; 67 25 } 68 26 69 int SocketAioError(struct A ioCtx* aio)27 int SocketAioError(struct AsyncCtx* aio) 70 28 { 71 29 return 0; 72 30 } 73 74 int SocketAioReturn(struct AioCtx* aio)75 {76 return 0;77 }78 79 void AioWorkerThread(void* data)80 {81 /* Main thread */82 struct AioCtx* curr;83 84 while (1)85 {86 if (!workHead)87 SysSuspendThread(-1);88 89 curr = workHead;90 workHead = NULL;91 92 if (!curr)93 continue;94 95 if (curr->func)96 curr->func(curr);97 }98 } -
Whitix/branches/netchannel/user/sdk/network/channels.c
r2095 r2099 67 67 int ChannelFree(Channel* channel) 68 68 { 69 SysMemoryUnmap((unsigned long)channel->baseAddress, PAGE_SIZE*(1 + ChanRecvPages(channel) + ChanSendPages(channel)));69 // SysMemoryUnmap((unsigned long)channel->baseAddress, PAGE_SIZE*(1 + ChanRecvPages(channel) + ChanSendPages(channel))); 70 70 SysClose(channel->fd); 71 71 free(channel); -
Whitix/branches/netchannel/user/sdk/network/checksum.h
r2089 r2099 10 10 ushort length; 11 11 }__attribute__((packed)); 12 13 static inline ushort IpPartialSum(void* _data, ulong bytes, ushort _sum)14 {15 uchar* data = (uchar*)_data;16 ulong sum = _sum;17 18 while (bytes > 1)19 {20 sum += (((ushort)*(data+1) << 8) | *data);21 data += 2;22 bytes -= 2;23 }24 25 if (bytes)26 sum += *data;27 28 while (sum >> 16)29 sum = (ushort)sum + (sum >> 16);30 31 return (ushort)sum;32 }33 34 static inline ushort TcpUdpHeaderSum(ulong sourceAddr, ulong destAddr, ushort len, uchar proto)35 {36 struct IpPsuedoHdr hdr;37 38 hdr.sourceIp = sourceAddr;39 hdr.destIp = destAddr;40 hdr.zero = 0;41 hdr.length = HostToNetShort(len);42 hdr.proto = proto;43 44 return IpPartialSum(&hdr, sizeof(struct IpPsuedoHdr), 0);45 }46 47 12 #endif -
Whitix/branches/netchannel/user/sdk/network/dns.c
r2094 r2099 10 10 #include "udp.h" 11 11 12 /* Protocol structures. */13 struct DnsHeader14 {15 ushort id;16 ushort flags;17 ushort questions;18 ushort answers;19 ushort authorities;20 ushort additional;21 };22 23 struct DnsQuestionEnd24 {25 ushort type;26 ushort class;27 };28 29 struct DnsAnswer30 {31 ushort name;32 ushort type;33 ushort class;34 ulong ttl;35 ushort length;36 }__attribute__((packed));37 38 12 void DnsStartHeader(struct DnsState* state, char* buf, int maxLength) 39 13 { … … 231 205 if (class != DNS_CLASS_IN) 232 206 { 233 printf(" TODO\n");207 printf("dns: TODO\n"); 234 208 exit(0); 235 209 } -
Whitix/branches/netchannel/user/sdk/network/icmp.c
r2097 r2099 15 15 16 16 chanSrc.address = 0; 17 18 if (address != NULL) 19 { 20 chanDest.address = 0x7F000001; /* TODO: Why? */ 21 }else{ 22 chanDest.address = 0xFFFFFFFF; 23 } 17 chanDest.address = 0xFFFFFFFF; 24 18 25 19 socket->channel = ChannelCreate(CHAN_MAKE_TYPE(CHAN_FAMILY_IP, … … 89 83 }; 90 84 85 int _IcmpSocketCreate(Socket* socket) 86 { 87 socket->ops = &icmpSocketOps; 88 return 0; 89 } 90 91 91 Socket* IcmpSocketCreate() 92 92 { … … 94 94 95 95 socket = SocketAllocate(); 96 socket->ops = &icmpSocketOps;97 96 _IcmpSocketCreate(socket); 97 98 98 return socket; 99 99 } -
Whitix/branches/netchannel/user/sdk/network/ipv4.c
r2091 r2099 6 6 #include <net/ipv4.h> 7 7 8 ulong IpCheckSum(char* data, ulong bytes, ulong sum) 9 { 10 ulong i; 11 12 for (i=0; i<(bytes & ~0x1); i+=2) 13 { 14 sum+=NetToHostShort(*((ushort*)(data+i))); 15 if (sum > 0xFFFF) /* Wrap to 16 bits. */ 16 sum-=0xFFFF; 17 } 18 19 if (i < bytes) 20 { 21 sum += data[i] << 8; 22 23 if (sum > 0xFFFF) 24 sum-=0xFFFF; 25 } 26 27 return sum; 28 } 8 #include "checksum.h" 29 9 30 10 int Ipv4StringToAddr(char* address, unsigned long* retAddr) … … 74 54 ipHeader->flagsFragOffset = 1 << 6; /* DF */ 75 55 ipHeader->totalLength = NetToHostShort(length + sizeof(struct IpHeader)); 76 56 77 57 /* Checksum is performed when we choose the source address when routing. */ 78 58 … … 80 60 } 81 61 62 int IpRecvNb(Channel* channel, ChanRecvBuffer** buffer, char** buf) 63 { 64 int length; 65 struct IpHeader* ipHeader; 66 ulong recvCsum; 82 67 68 *buffer = NULL; 69 *buf = NULL; 70 71 length = ChanBufferRecvNb(channel, buffer, buf); 72 73 /* 1. Length of packet must be at least size of IP header */ 74 if (length <= sizeof(struct IpHeader)) 75 return -1; 76 77 ipHeader = (struct IpHeader*)(*buf); 78 79 /* 2. IP checksum must be correct */ 80 recvCsum = ipHeader->checkSum; 81 ipHeader->checkSum = 0; 82 if (IpCheckSum(*buf, IpGetHeaderSize(ipHeader)) != recvCsum) 83 return -1; 84 ipHeader->checkSum = recvCsum; 85 86 /* 3. Length fields must match */ 87 if (NetToHostShort(ipHeader->totalLength) != length) 88 return -1; 89 90 return length; 91 } 92 93 ushort IpCheckSum(void* data, ulong bytes) 94 { 95 return ~IpPartialSum(data, bytes, 0); 96 } 97 98 99 ushort IpPartialSum(void* _data, ulong bytes, ushort _sum) 100 { 101 uchar* data = (uchar*)_data; 102 ulong sum = _sum; 103 104 while (bytes > 1) 105 { 106 sum += (((ushort)*(data+1) << 8) | *data); 107 data += 2; 108 bytes -= 2; 109 } 110 111 if (bytes) 112 sum += *data; 113 114 while (sum >> 16) 115 sum = (ushort)sum + (sum >> 16); 116 117 return (ushort)sum; 118 } 119 120 ushort TcpUdpHeaderSum(ulong sourceAddr, ulong destAddr, ushort len, uchar proto) 121 { 122 struct IpPsuedoHdr hdr; 123 124 hdr.sourceIp = sourceAddr; 125 hdr.destIp = destAddr; 126 hdr.zero = 0; 127 hdr.length = HostToNetShort(len); 128 hdr.proto = proto; 129 130 return IpPartialSum(&hdr, sizeof(struct IpPsuedoHdr), 0); 131 } 132 -
Whitix/branches/netchannel/user/sdk/network/ipv4.h
r2084 r2099 6 6 int IpBuildHeader(void* start, struct Ipv4EndPoint* destIp, int length, int 7 7 protocol); 8 int IpRecvNb(Channel* channel, ChanRecvBuffer** buffer, char** buf); 9 10 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 11 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 8 12 9 13 #endif -
Whitix/branches/netchannel/user/sdk/network/seq.h
r2088 r2099 2 2 #define TCP_SEQ_H 3 3 4 #define SeqBefore(a, b) (((long)(a - b)) < 0) 5 #define SeqBeforeEq(a, b) (((long)(a - b)) <= 0) 6 #define SeqAfter(a, b) (((long)(a - b)) > 0) 7 #define SeqAfterEq(a, b) (((long)(a - b)) >= 0) 4 #define SeqBefore(a, b) (((long)((a) - (b))) < 0) 5 #define SeqBeforeEq(a, b) (((long)((a) - (b))) <= 0) 6 #define SeqAfterEq(a, b) (((long)((b) - (a))) <= 0) 8 7 #define SeqBetween(a, b, c) (((c) - (b)) >= ((a) - (b))) 9 8 9 static inline ulong SeqAfter(ulong seq1, ulong seq2) 10 { 11 return (long)(seq2-seq1) < 0; 12 } 13 10 14 #endif -
Whitix/branches/netchannel/user/sdk/network/socket.c
r2096 r2099 1 1 #include <net/socket.h> 2 #include <net/inet_raw.h> 2 3 3 4 #include <stdio.h> 4 5 5 #include "tcp .h"6 #include "tcp/tcp.h" 6 7 #include "udp.h" 7 8 … … 12 13 return -1; 13 14 14 if (type == NET_TYPE_STREAM) 15 memset(socket, 0, sizeof(Socket)); 16 17 switch (type) 15 18 { 16 return _TcpSocketCreate(socket); 17 }else{ 18 return _UdpSocketCreate(socket); 19 case NET_TYPE_STREAM: 20 return _TcpSocketCreate(socket); 21 22 case NET_TYPE_DGRAM: 23 return _UdpSocketCreate(socket); 24 25 case NET_TYPE_DIAG: 26 return _IcmpSocketCreate(socket); 19 27 } 20 28 … … 40 48 int SocketAccept(Socket* socket, Socket* child, struct SockAddr* addr, int* addrLen) 41 49 { 42 printf("SocketAccept(%#X, %#X, %#X, %#X)\n", socket, child, addr, addrLen);43 44 printf("accept = %#X\n", socket->ops->accept);45 46 50 if (socket && socket->ops && socket->ops->accept) 47 51 return socket->ops->accept(socket, child, addr, addrLen); -
Whitix/branches/netchannel/user/sdk/network/stats.c
r2088 r2099 1 1 #include <stdio.h> 2 #include <types.h> 3 #include <syscalls.h> 2 4 3 #include "tcp .h"5 #include "tcp/tcp.h" 4 6 5 7 #define NS_MAGIC 0xDE23FA11 … … 11 13 12 14 const char* output = NULL; 13 extern struct TcpSocket Info* tcpHead;15 extern struct TcpSocket* tcpHead; 14 16 15 17 struct PortEntry … … 17 19 ushort port, flags; 18 20 ulong totalBytesSent, totalPacketsSent; 21 ulong totalBytesRecv, totalPacketsRecv; 19 22 struct PortEntry* next; 23 }__attribute__((packed)); 24 25 struct Host 26 { 27 ulong address, flags; 28 ulong droppedPackets, droppedBytes; 29 ulong retxPacketsSent, retxBytesSent; 30 struct Host* next; 20 31 }__attribute__((packed)); 21 32 … … 25 36 struct PortEntry* PortFindEntry(int port) 26 37 { 27 if (!portHead) 38 struct PortEntry* curr = portHead; 39 40 if (!curr) 28 41 return NULL; 29 30 printf("TODO: Find\n"); 42 43 do 44 { 45 if (curr->port == port) 46 return curr; 47 } while ((curr = curr->next)); 31 48 32 49 return NULL; … … 35 52 void PortAppendEntry(struct PortEntry* entry) 36 53 { 54 struct PortEntry* curr = portHead; 55 37 56 numPorts++; 38 57 … … 43 62 } 44 63 45 printf("AppendEntry: TODO\n"); 64 while (curr->next != NULL) 65 { 66 curr = curr->next; 67 } 68 69 curr->next = entry; 46 70 } 47 71 … … 81 105 void NetStatsWrite() 82 106 { 83 struct TcpSocket Info* sock = tcpHead;107 struct TcpSocket* sock = tcpHead; 84 108 85 109 if (!sock) … … 106 130 entry->totalBytesSent += sock->stats.totalBytesSent; 107 131 entry->totalPacketsSent += sock->stats.totalPacketsSent; 132 133 entry->totalBytesRecv += sock->stats.totalBytesRecv; 134 entry->totalPacketsRecv += sock->stats.totalPacketsRecv; 108 135 109 136 PortAppendEntry(entry); -
Whitix/branches/netchannel/user/sdk/network/udp.c
r2092 r2099 36 36 } 37 37 38 int UdpSocketBind(Socket* socket, struct SockAddr* address) 39 { 40 struct Ipv4EndPoint chanSrc, chanDest, *addr; 41 42 addr = (struct Ipv4EndPoint*)address; 43 44 chanSrc.address = 0; 45 chanSrc.port = addr->port; 46 47 chanDest.address = IPV4_BROADCAST; 48 chanDest.port = 0; 49 50 socket->channel = ChannelCreate(CHAN_MAKE_TYPE(CHAN_FAMILY_IP, 51 CHAN_TYPE_UDP), &chanSrc, &chanDest, NULL); 52 53 if (!socket->channel) 54 return -1; 55 56 return 0; 57 } 58 38 59 #define CHAN_TYPE_UDP 0x01 39 60 … … 74 95 } 75 96 97 #define UDP_IP_HDR_BYTES (sizeof(struct IpHeader) + sizeof(struct UdpHeader)) 98 76 99 int UdpSocketSendTo(Socket* socket, const void* buffer, unsigned long length, int flags, struct SockAddr* dest) 77 100 { 78 ChanSendBuffer* chanBuff; 79 int ret; 101 const char* p = (const char*)buffer; 80 102 struct Ipv4EndPoint* destIp = (struct Ipv4EndPoint*)dest; 81 103 struct Ipv4EndPoint* srcIp = (struct Ipv4EndPoint*)&socket->channel->src; 82 struct IpHeader* ipHeader; 83 struct UdpHeader* udpHeader; 84 char* data; 85 int totalLength; 86 87 totalLength = length + sizeof(struct UdpHeader) + sizeof(struct IpHeader); 88 104 89 105 if (!dest) 90 106 return -1; 91 107 92 chanBuff = ChanSendBufferAlloc(socket->channel, &data); 93 94 if (!chanBuff) 95 return -1; 96 97 ipHeader=(struct IpHeader*)data; 98 IpBuildHeader(ipHeader, destIp, length + sizeof(struct UdpHeader), PROTOCOL_RAW_UDP); 99 100 /* Add UDP header. */ 101 udpHeader = (struct UdpHeader*)(data + sizeof(struct IpHeader)); 102 103 udpHeader->sourcePort = NetToHostShort(srcIp->port); 104 udpHeader->destPort = NetToHostShort(destIp->port); 105 udpHeader->length = NetToHostShort(length + sizeof(struct UdpHeader)); 106 107 memcpy(data + sizeof(struct IpHeader) + sizeof(struct UdpHeader), buffer, length); 108 109 ret = ChanBufferSend(socket->channel, chanBuff, totalLength); 110 111 return ret; 108 while (length > 0) 109 { 110 ChanSendBuffer* chanBuff; 111 int pktLength = MIN(length, ChanMaxPacketSize(socket->channel) - UDP_IP_HDR_BYTES); 112 int ret; 113 char* data; 114 struct IpHeader* ipHeader; 115 struct UdpHeader* udpHeader; 116 117 chanBuff = ChanSendBufferAlloc(socket->channel, &data); 118 119 if (!chanBuff || !data) 120 goto out; 121 122 ipHeader=(struct IpHeader*)data; 123 udpHeader = (struct UdpHeader*)(data + sizeof(struct IpHeader)); 124 125 IpBuildHeader(ipHeader, destIp, pktLength + sizeof(struct UdpHeader), PROTOCOL_RAW_UDP); 126 127 udpHeader->sourcePort = NetToHostShort(srcIp->port); 128 udpHeader->destPort = NetToHostShort(destIp->port); 129 udpHeader->length = NetToHostShort(pktLength + sizeof(struct UdpHeader)); 130 131 memcpy(data + UDP_IP_HDR_BYTES, p, pktLength); 132 133 ret = ChanBufferSend(socket->channel, chanBuff, pktLength + UDP_IP_HDR_BYTES) - UDP_IP_HDR_BYTES; 134 135 if (ret < pktLength) 136 goto out; 137 138 p += ret; 139 length -= ret; 140 } 141 142 out: 143 return p - (const char*)buffer; 112 144 } 113 145 … … 136 168 struct IpHeader* ipHeader; 137 169 struct UdpHeader* udpHeader; 170 struct Ipv4EndPoint* address = (struct Ipv4EndPoint*)dest; 138 171 139 172 again: 140 read = ChanBufferRecv(socket->channel, &chanBuff, &buf, 2000);173 read = ChanBufferRecv(socket->channel, &chanBuff, &buf, -1); 141 174 142 175 if (read <= 0) 143 { 144 printf("read = %d\n", read); 145 return read; 146 } 176 return -1; 147 177 148 178 /* Verify checksum */ … … 152 182 goto again; 153 183 154 /* TODO: Current buffer. Partial recv */155 156 184 dataByteCnt = read - sizeof(struct UdpHeader) - sizeof(struct IpHeader); 157 185 158 /* FIXME: More checking. */159 186 memcpy(buffer, buf + sizeof(struct IpHeader) + sizeof(struct UdpHeader), 160 187 dataByteCnt); 161 188 162 // ChanBufferFree(socket->channel, chanBuff); 163 164 return dataByteCnt; 189 ChanRecvBuffFree(socket->channel, chanBuff); 190 191 /* Fill in the structure, so we know where the packet came from */ 192 address->address = NetToHostLong(ipHeader->sourceAddr); 193 address->port = NetToHostShort(udpHeader->sourcePort); 194 195 return NetToHostShort(udpHeader->length) - sizeof(struct UdpHeader); 165 196 } 166 197 167 198 static struct SocketOps udpSocketOps = 168 199 { 200 .bind = UdpSocketBind, 169 201 .connect = UdpSocketConnect, 170 202 .connectEx = UdpSocketConnectEx,
