00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __PANALIX_NET_HPP
00012 #define __PANALIX_NET_HPP
00013
00014
00015 #include "src/collection/list.hpp"
00016
00017
00018
00019
00020
00028 namespace Net {
00029
00034 #define DUMP_OUTBOUND_ETHERNET_FRAMES 0
00035 #define DUMP_INBOUND_ETHERNET_FRAMES 0
00036 #define DEBUG_ETH_TIMES 0
00037
00038 #define DUMP_OUTBOUND_ETHERNET_FRAMES_BYTES 64
00039 #define DUMP_INBOUND_ETHERNET_FRAMES_BYTES 64
00040
00042 uint16 htons(uint16 hostshort);
00043 uint16 ntohs(uint16 netshort);
00044 uint32 htonl(uint32 hostlong);
00045 uint32 ntohl(uint32 netlong);
00046
00047 void init();
00048 int init_3c556();
00049
00050
00051 #if 0
00052 struct size_and_frame {
00053 uint32 len;
00054 char frame[1518];
00055 };
00056 #endif
00057
00058 struct netbuf {
00059 uint32 len;
00060 uint8 *data;
00061 struct netbuf *next;
00062 struct netif *nif;
00063 void *freeptr;
00064 netbuf(uint32 len, uint8 *data, struct netbuf *next, struct netif *nif) : len(len),data(data), next(next), nif(nif), freeptr(null) { }
00065
00066 };
00067
00068 struct netbuf *netbuf_alloc(uint32 datasize);
00069 void netbuf_free(struct netbuf *nb);
00070
00071
00072
00073
00074 struct packet_t {
00075 uint8 *buf;
00076 uint32 capacity;
00077 uint32 off;
00078 uint32 end;
00079
00080 };
00081
00084 struct packet_t *pbuf_alloc(uint32 capacity);
00085 void pbuf_free(struct packet_t *&p);
00086
00087 struct ethernet_address {
00088 uint8 d[6];
00089 ethernet_address() { }
00090 ethernet_address(uint8 d[6]) { for (int i=0;i<6;i++) this->d[i]=d[i]; }
00091 ethernet_address(uint8 d0, uint8 d1,uint8 d2,uint8 d3,uint8 d4, uint8 d5) { d[0]=d0; d[1]=d1; d[2]=d2; d[3]=d3; d[4]=d4; d[5]=d5;}
00092 } __attribute__ ((packed));
00093
00094 struct ip_address {
00095 uint32 ip;
00096 ip_address(uint32 v) {
00097 ip=htonl(v);
00098 }
00099
00100 ip_address() { }
00101 } __attribute__ ((packed));
00102
00103 struct netmask_t {
00104 uint32 v;
00105 netmask_t() : v(0) { }
00106 netmask_t(uint32 v) : v(v) { }
00107 } ;
00108
00109 struct outbound_packet {
00110 struct ethernet_address eth_addr;
00111 uint32 t;
00112 struct netbuf *nb;
00113
00114 outbound_packet () { }
00115 outbound_packet(struct ethernet_address eth_addr, uint32 t, struct netbuf *nb) : eth_addr(eth_addr), t(t), nb(nb) { }
00116 };
00117
00118 struct netif {
00119 char name[8];
00120 struct ethernet_address eth_addr;
00121 struct ip_address ip_addr;
00122 struct netmask_t netmask;
00123 void (*transmit)(uint8 *, uint32, uint32, uint8 *);
00124 int (*poll)(uint8 *, uint32 *);
00125 struct netbuf *netbuf_curr;
00126 struct Collection::List<struct outbound_packet> outbound;
00127 class IPC::Lock::lock_t outbound_lock;
00128
00129 netif() : transmit(null), poll(null), outbound_lock(const_cast<char*>("outbound")) { netbuf_curr = netbuf_alloc(1518); netbuf_curr->nif=this; }
00130
00131 netif(struct netif &ni) : outbound_lock(const_cast<char*>("outbound")) {
00132 memmove(this, &ni, sizeof(struct netif));
00133 netbuf_curr->nif=this;
00134 }
00135
00136 netif(char name[8],
00137 struct ethernet_address eth_addr,
00138 struct ip_address ip_addr,
00139 struct netmask_t netmask,
00140 void (*transmit)(uint8*, uint32, uint32, uint8*),
00141 int (*poll) (uint8 *, uint32 *))
00142 : eth_addr(eth_addr), ip_addr(ip_addr), netmask(netmask), transmit(transmit), poll(poll), outbound_lock(const_cast<char*>("outbound")) { for (int i=0; i<8; i++) this->name[i]=name[i]; netbuf_curr = netbuf_alloc(1518); netbuf_curr->nif=this; }
00143
00144 };
00145
00150
00151 struct netbuf * onFramesReceived (struct netif *nif );
00152 void onFramesTransmitted(struct netif *nif);
00153
00154 #if 0
00155
00158 int eth_poll(char **packet, uint32 *packetlen);
00159 void eth_transmit(const char *d, unsigned int t, unsigned int s, const char *p);
00160 #endif
00161 int eth_poll(struct netbuf **nb);
00162 void eth_transmit(struct netif *nif, struct ethernet_address eth_addr, unsigned int t, struct netbuf *nb);
00163
00164
00165
00166
00168 uint16 count_checksum16(uint16 *data, uint32 len);
00169
00170
00171
00172 }
00173
00178 void print(struct Net::ip_address ip);
00179 void print(struct Net::ethernet_address eth);
00180 void print(struct Net::netbuf nb);
00181 inline void print(struct Net::netmask_t nm) {
00182 print(static_cast<struct Net::ip_address>(nm.v));
00183 }
00184
00185
00186
00187
00188 #endif
00189