Changeset 2099 for Whitix

Show
Ignore:
Timestamp:
06/03/10 22:43:33 (21 months ago)
Author:
mwhitworth
Message:

Add to network stack.

Location:
Whitix/branches/netchannel/user/sdk/network
Files:
15 added
12 modified

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 -O3 -Werror 
     1CFLAGS = -Wall -I../include -I../../libc/include -nostdlib -ffreestanding -fno-builtin -fPIC -m32 -O2 -Werror 
    22 
    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.o 
     3OBJS = aio.o byteorder.o ipv4.o udp.o memory.o dns.o icmp.o channels.o init.o socket.o stats.o events.o 
    44 
    55.c.o: 
     
    77 
    88build: $(OBJS) 
     9        make -C tcp 
    910        gcc -m32 -nostdlib -ffreestanding -fno-builtin -shared -L../libs/ \ 
    1011        -L../../posix/ -lpthread -o ../libs/libnetwork.so -Wl,-init=NetworkPreInit \ 
    11                 $(OBJS) ../../libc/init/crt_begin.o ../../libc/init/crt_end.o 
     12                $(OBJS) tcp/*.o ../../libc/init/crt_begin.o ../../libc/init/crt_end.o 
    1213 
    1314clean: 
  • Whitix/branches/netchannel/user/sdk/network/aio.c

    r2090 r2099  
    55#include <pthread.h> 
    66 
    7 int globalAioThr; 
    8 int aioWork; 
    9 struct AioCtx* workHead; 
    10 pthread_mutex_t workLock; 
    11  
    12 int SocketAsyncThreaded(Socket* socket) 
     7int SocketAsyncRecv(Socket* socket, struct AsyncCtx* ctx, char* buffer, int length) 
    138{ 
    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; 
    3412 
    3513        return AIO_SUCCESS; 
    3614} 
    3715 
    38 int SocketAsyncSend(Socket* socket, struct AioCtx* aio, char* buffer, int len, int flags) 
     16int SocketAsyncSend(Socket* socket, struct AsyncCtx* aio, char* buffer, int len, int flags) 
    3917{ 
    4018        /* Add to queue */ 
     
    4220} 
    4321 
    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) 
     22int SocketAioCancel(struct AsyncCtx* aio) 
    6523{ 
    6624        return 0; 
    6725} 
    6826 
    69 int SocketAioError(struct AioCtx* aio) 
     27int SocketAioError(struct AsyncCtx* aio) 
    7028{ 
    7129        return 0; 
    7230} 
    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  
    6767int ChannelFree(Channel* channel) 
    6868{ 
    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))); 
    7070        SysClose(channel->fd); 
    7171        free(channel); 
  • Whitix/branches/netchannel/user/sdk/network/checksum.h

    r2089 r2099  
    1010        ushort length; 
    1111}__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  
    4712#endif 
  • Whitix/branches/netchannel/user/sdk/network/dns.c

    r2094 r2099  
    1010#include "udp.h" 
    1111 
    12 /* Protocol structures. */ 
    13 struct DnsHeader 
    14 { 
    15         ushort id; 
    16         ushort flags; 
    17         ushort questions; 
    18         ushort answers; 
    19         ushort authorities; 
    20         ushort additional; 
    21 }; 
    22  
    23 struct DnsQuestionEnd 
    24 { 
    25         ushort type; 
    26         ushort class; 
    27 }; 
    28  
    29 struct DnsAnswer 
    30 { 
    31         ushort name; 
    32         ushort type; 
    33         ushort class; 
    34         ulong ttl; 
    35         ushort length; 
    36 }__attribute__((packed)); 
    37  
    3812void DnsStartHeader(struct DnsState* state, char* buf, int maxLength) 
    3913{ 
     
    231205                if (class != DNS_CLASS_IN) 
    232206                { 
    233                         printf("TODO\n"); 
     207                        printf("dns: TODO\n"); 
    234208                        exit(0); 
    235209                } 
  • Whitix/branches/netchannel/user/sdk/network/icmp.c

    r2097 r2099  
    1515         
    1616        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; 
    2418                 
    2519        socket->channel = ChannelCreate(CHAN_MAKE_TYPE(CHAN_FAMILY_IP, 
     
    8983}; 
    9084 
     85int _IcmpSocketCreate(Socket* socket) 
     86{ 
     87        socket->ops = &icmpSocketOps; 
     88        return 0; 
     89} 
     90 
    9191Socket* IcmpSocketCreate() 
    9292{ 
     
    9494         
    9595        socket = SocketAllocate(); 
    96         socket->ops = &icmpSocketOps; 
    97          
     96        _IcmpSocketCreate(socket); 
     97 
    9898        return socket; 
    9999} 
  • Whitix/branches/netchannel/user/sdk/network/ipv4.c

    r2091 r2099  
    66#include <net/ipv4.h> 
    77 
    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" 
    299 
    3010int Ipv4StringToAddr(char* address, unsigned long* retAddr) 
     
    7454        ipHeader->flagsFragOffset = 1 << 6; /* DF */ 
    7555        ipHeader->totalLength = NetToHostShort(length + sizeof(struct IpHeader)); 
    76          
     56 
    7757        /* Checksum is performed when we choose the source address when routing. */ 
    7858 
     
    8060} 
    8161 
     62int IpRecvNb(Channel* channel, ChanRecvBuffer** buffer, char** buf) 
     63{ 
     64        int length; 
     65        struct IpHeader* ipHeader; 
     66        ulong recvCsum; 
    8267 
     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 
     93ushort IpCheckSum(void* data, ulong bytes) 
     94{ 
     95        return ~IpPartialSum(data, bytes, 0); 
     96} 
     97 
     98 
     99ushort 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 
     120ushort 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  
    66int IpBuildHeader(void* start, struct Ipv4EndPoint* destIp, int length, int 
    77                                protocol); 
     8int 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)) 
    812 
    913#endif 
  • Whitix/branches/netchannel/user/sdk/network/seq.h

    r2088 r2099  
    22#define TCP_SEQ_H 
    33 
    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) 
    87#define SeqBetween(a, b, c) (((c) - (b)) >= ((a) - (b))) 
    98 
     9static inline ulong SeqAfter(ulong seq1, ulong seq2) 
     10{ 
     11        return (long)(seq2-seq1) < 0; 
     12} 
     13 
    1014#endif 
  • Whitix/branches/netchannel/user/sdk/network/socket.c

    r2096 r2099  
    11#include <net/socket.h> 
     2#include <net/inet_raw.h> 
    23 
    34#include <stdio.h> 
    45 
    5 #include "tcp.h" 
     6#include "tcp/tcp.h" 
    67#include "udp.h" 
    78 
     
    1213                return -1; 
    1314 
    14         if (type == NET_TYPE_STREAM) 
     15        memset(socket, 0, sizeof(Socket)); 
     16 
     17        switch (type) 
    1518        { 
    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); 
    1927        } 
    2028 
     
    4048int SocketAccept(Socket* socket, Socket* child, struct SockAddr* addr, int* addrLen) 
    4149{ 
    42         printf("SocketAccept(%#X, %#X, %#X, %#X)\n", socket, child, addr, addrLen); 
    43  
    44         printf("accept = %#X\n", socket->ops->accept); 
    45  
    4650        if (socket && socket->ops && socket->ops->accept) 
    4751                return socket->ops->accept(socket, child, addr, addrLen); 
  • Whitix/branches/netchannel/user/sdk/network/stats.c

    r2088 r2099  
    11#include <stdio.h> 
     2#include <types.h> 
     3#include <syscalls.h> 
    24 
    3 #include "tcp.h" 
     5#include "tcp/tcp.h" 
    46 
    57#define NS_MAGIC        0xDE23FA11 
     
    1113 
    1214const char* output = NULL; 
    13 extern struct TcpSocketInfo* tcpHead; 
     15extern struct TcpSocket* tcpHead; 
    1416 
    1517struct PortEntry 
     
    1719        ushort port, flags; 
    1820        ulong totalBytesSent, totalPacketsSent; 
     21        ulong totalBytesRecv, totalPacketsRecv; 
    1922        struct PortEntry* next; 
     23}__attribute__((packed)); 
     24 
     25struct Host 
     26{ 
     27        ulong address, flags; 
     28        ulong droppedPackets, droppedBytes; 
     29        ulong retxPacketsSent, retxBytesSent; 
     30        struct Host* next; 
    2031}__attribute__((packed)); 
    2132 
     
    2536struct PortEntry* PortFindEntry(int port) 
    2637{ 
    27         if (!portHead) 
     38        struct PortEntry* curr = portHead; 
     39 
     40        if (!curr) 
    2841                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)); 
    3148 
    3249        return NULL; 
     
    3552void PortAppendEntry(struct PortEntry* entry) 
    3653{ 
     54        struct PortEntry* curr = portHead; 
     55 
    3756        numPorts++; 
    3857 
     
    4362        } 
    4463 
    45         printf("AppendEntry: TODO\n"); 
     64        while (curr->next != NULL) 
     65        { 
     66                curr = curr->next; 
     67        } 
     68 
     69        curr->next = entry; 
    4670} 
    4771 
     
    81105void NetStatsWrite() 
    82106{ 
    83         struct TcpSocketInfo* sock = tcpHead; 
     107        struct TcpSocket* sock = tcpHead; 
    84108 
    85109        if (!sock) 
     
    106130                entry->totalBytesSent += sock->stats.totalBytesSent; 
    107131                entry->totalPacketsSent += sock->stats.totalPacketsSent; 
     132 
     133                entry->totalBytesRecv += sock->stats.totalBytesRecv; 
     134                entry->totalPacketsRecv += sock->stats.totalPacketsRecv; 
    108135 
    109136                PortAppendEntry(entry); 
  • Whitix/branches/netchannel/user/sdk/network/udp.c

    r2092 r2099  
    3636} 
    3737 
     38int 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 
    3859#define CHAN_TYPE_UDP   0x01 
    3960 
     
    7495} 
    7596 
     97#define UDP_IP_HDR_BYTES        (sizeof(struct IpHeader) + sizeof(struct UdpHeader)) 
     98 
    7699int UdpSocketSendTo(Socket* socket, const void* buffer, unsigned long length, int flags, struct SockAddr* dest) 
    77100{ 
    78         ChanSendBuffer* chanBuff; 
    79         int ret; 
     101        const char* p = (const char*)buffer; 
    80102        struct Ipv4EndPoint* destIp = (struct Ipv4EndPoint*)dest; 
    81103        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         
    89105        if (!dest) 
    90106                return -1; 
    91107 
    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 
     142out: 
     143        return p - (const char*)buffer; 
    112144} 
    113145 
     
    136168        struct IpHeader* ipHeader; 
    137169        struct UdpHeader* udpHeader; 
     170        struct Ipv4EndPoint* address = (struct Ipv4EndPoint*)dest; 
    138171 
    139172again: 
    140         read = ChanBufferRecv(socket->channel, &chanBuff, &buf, 2000); 
     173        read = ChanBufferRecv(socket->channel, &chanBuff, &buf, -1); 
    141174 
    142175        if (read <= 0) 
    143         { 
    144                 printf("read = %d\n", read); 
    145                 return read; 
    146         } 
     176                return -1; 
    147177 
    148178        /* Verify checksum */ 
     
    152182                goto again; 
    153183 
    154         /* TODO: Current buffer. Partial recv */ 
    155  
    156184        dataByteCnt = read - sizeof(struct UdpHeader) - sizeof(struct IpHeader); 
    157185 
    158         /* FIXME: More checking. */ 
    159186        memcpy(buffer, buf + sizeof(struct IpHeader) + sizeof(struct UdpHeader), 
    160187                        dataByteCnt); 
    161188 
    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);  
    165196} 
    166197 
    167198static struct SocketOps udpSocketOps =  
    168199{ 
     200        .bind = UdpSocketBind, 
    169201        .connect = UdpSocketConnect, 
    170202        .connectEx = UdpSocketConnectEx,