2 * $Source: /cvs/cvsroot/d2x/main/ip_base.h,v $
5 * $Date: 2002-02-06 09:22:41 $
7 * ip_base.h - base for NAT-compatible udp/ip code.
8 * added 2000/02/07 Matt Mueller
10 * $Log: not supported by cvs2svn $
29 class ip_addr;//prototype for arch_ip_queryhost
35 //inline u_int32_t d_rand32(void) { return d_rand() + (d_rand()<<16);}
36 inline u_int32_t d_rand32(void) { return d_rand() + (d_rand()<<15) + (d_rand()<<30);}//d_rand() only returns 15 bits
39 void ip_receive_cfg(ubyte *buf,int buflen,ip_addr fromaddr);
41 extern unsigned char ipx_MyAddress[10];
43 unsigned short ip_portshift(unsigned short baseport, const char *cs);
45 #define UDP_BASEPORT 31017
46 #define UDP_SERV_BASEPORT 30817
47 #define PORTSHIFT_TOLERANCE 0x100
48 #define MAX_PACKETSIZE 8192
50 // Length HAS TO BE 2!
52 // Length HAS TO BE 4!
53 #define D1Xcfgid "\xcfg\xd1x"
58 #define IP_CFG_HANDSHAKE 1
59 #define IP_CFG_RELAY 2
60 //someone giving us an error (ie, outdated version, etc.) ... not handled yet.
61 #define IP_CFG_SORRY 3
64 #define STATE_INEEDINFO 1<<0
65 #define STATE_SENDINGINFO 1<<1
66 //#define STATE_NEED_RESEND (STATE_INEEDINFO | STATE_SENDINGINFO)
67 #define STATE_NEED_RESEND (STATE_INEEDINFO)
68 #define STATE_VALID_STATES (STATE_INEEDINFO | STATE_SENDINGINFO)
69 #define STATE_RELAYREPLY 1<<6
70 #define STATE_ERR 1<<7
71 /* Dump raw form of IP address/port by fancy output to user
76 inline char *ip_hs_statetoa(int state){
81 if ((state&(STATE_INEEDINFO|STATE_SENDINGINFO))==(STATE_INEEDINFO|STATE_SENDINGINFO))
83 if (state&STATE_INEEDINFO)
85 if (state&STATE_SENDINGINFO)
89 inline void dumprid(const unsigned char *a)
91 printf("<%u.%u.%u.%u.%u.%u>",a[0],a[1],a[2],a[3],a[4],a[5]);
93 inline void dumpraddr(const unsigned char *addr)
96 printf("[%u.%u.%u.%u]",addr[0],addr[1],addr[2],addr[3]);
97 port=(unsigned short)ntohs(*(unsigned short *)(addr+4));
107 int fillbuf(ubyte *buf)const {memcpy(buf,id,6);return 6;};
108 int readbuf(const ubyte *buf){memcpy(id,buf,6);return 6;};
109 bool operator < (const ip_id &a_id)const {return memcmp(id,a_id.id,6)<0;}
110 bool operator == (const ip_id &a_id)const {return memcmp(id,a_id.id,6)==0;}
111 bool operator > (const ip_id &a_id)const {return memcmp(id,a_id.id,6)>0;}
112 ip_id& operator = (const ip_id &a_id){readbuf(a_id.id);return *this;}
114 void dump(void){dumprid(id);}
116 ip_id(ubyte*nid){readbuf(nid);}
122 ubyte alen;//alen stuff is to maybe make ipv6 support easier (if the transition ever actually happens)
126 int fillbuf(ubyte *buf)const {buf[0]=alen;memcpy(buf+1,addr,alen);return alen+1;};
127 int readbuf(const ubyte *buf){
130 memcpy(addr,buf+1,6);alen=l;
132 mprintf((0,"ip_addr readbuf bad len %i\n",l));
133 memset(addr,0,6);alen=0;
137 bool operator < (const ip_addr &a_addr)const {
138 if (alen!=a_addr.alen) return alen<a_addr.alen;
139 return memcmp(addr,a_addr.addr,alen)<0;
141 bool operator == (const ip_addr &a_addr)const {return ((alen==a_addr.alen) && (memcmp(addr,a_addr.addr,alen)==0));}
142 bool operator > (const ip_addr &a_addr)const {
143 if (alen!=a_addr.alen) return alen>a_addr.alen;
144 return memcmp(addr,a_addr.addr,alen)>0;
146 ip_addr& operator = (const ip_addr &a_addr){alen=a_addr.alen;memcpy(addr,a_addr.addr,6);return *this;}
148 void dump(void)const{dumpraddr(addr);}
150 void set(int addrsize,ubyte *naddr,ushort port){
151 if (addrsize!=4)return;//not handled yet.
153 memcpy(addr,naddr,addrsize);
155 memcpy(addr+addrsize,&port,2);
157 int dns(char *address,ushort baseport){
158 return arch_ip_queryhost(this,address,baseport);
160 bool ok(void)const{return alen>0;}
161 int addrlen(void)const{return alen-2;}
162 // ip_addr(ubyte*naddr){readbuf(naddr);}
163 ip_addr(void){alen=0;};
170 typedef list<ip_addr>::iterator iterator;
171 typedef list<ip_addr>::const_iterator const_iterator;
175 iterator begin(void){return addrs.begin();}
176 iterator end(void){return addrs.end();}
177 const_iterator begin(void)const{return addrs.begin();}
178 const_iterator end(void)const{return addrs.end();}
179 bool hasaddr(ip_addr addr){
180 iterator i=find(begin(),end(),addr);
181 return (i!=addrs.end());
183 int add(ip_addr addr){
186 addrs.push_back(addr);
190 int add(const ip_addr_list &na){
192 const_iterator i=na.begin();
193 for (;i!=na.end();++i)
197 void setgoodaddr(ip_addr addr){
198 iterator i=find(begin(),end(),addr);
203 addrs.push_back(addr);
204 goodaddr=&(*addrs.end());
206 int fillbuf(ubyte *buf)const {
210 for (i=begin();i!=end();++i)
211 s+=(*i).fillbuf(buf+s);
214 int readbuf(const ubyte *buf){
217 for (n=buf[0];n;--n){
219 // addrs.push_back(a);
228 addrs.erase(begin(),end());
231 ip_addr_list(const ip_addr_list &nl):addrs(nl.addrs),naddr(nl.naddr){setgoodaddr(*nl.goodaddr);}
232 ip_addr_list(void):naddr(0),goodaddr(NULL){}
233 // ip_addr_list(const ubyte * buf):naddr(0),goodaddr(NULL){readbuf(buf);}
236 extern ip_addr_list ip_my_addrs;
238 extern int ip_sendtoca(ip_addr addr,const void *buf,int len);
240 class ip_handshake_base {
252 virtual int fillbuf(ubyte *buf);
253 virtual int readbuf(ubyte *buf);
254 void setrandid(void){tryid=d_rand32() ^ timer_get_approx_seconds();}
255 void setstate(int newstate);
256 int addstate(int newstate);
258 // ip_handshake_base(void):type(IP_CFG_BASE){setstate(IPNOSTATE);}
259 ip_handshake_base(bool initme=0):type(IP_CFG_BASE){
270 virtual ~ip_handshake_base(){setstate(0);}//decrement pendinghandshakes if needed.
272 class ip_handshake_info : public ip_handshake_base {
276 virtual int fillbuf(ubyte *buf);
277 virtual int readbuf(ubyte *buf);
279 ip_handshake_info(ubyte *buf){
282 ip_handshake_info(void):ip_handshake_base(1){
283 type=IP_CFG_HANDSHAKE;
285 // setstate(STATE_INEEDINFO);
289 class ip_handshake_relay : public ip_handshake_base {
295 virtual int fillbuf(ubyte *buf);
296 virtual int readbuf(ubyte *buf);
298 ip_handshake_relay(ubyte *buf){
301 ip_handshake_relay(ip_peer *torelay);
304 //max number of times to try to handshake a host
305 #define IP_MAX_HS_ATTEMPTS 10
306 //how often to resend a hand-shake (3 seconds)
307 #define IP_HS_RETRYTIME (F1_0*3)
308 //how often (max) to search through the resend loop (1 second)
309 #define IP_HS_FRAME_RETRYTIME (F1_0)
317 // ip_hs_state query;
318 // ip_hs_state reply;
319 list<ip_handshake_base*> handshakes;
321 // void set_good_addr(ubyte *fraddr);
322 void add_hs(ip_handshake_base* hs){
323 // mprintf((0,"########## add_hs %p: adding a hs %p %i %i\n",this,hs,hs->type,hs->state));
324 handshakes.push_back(hs);
326 ip_handshake_relay* find_relay(ip_id r_id){
327 list<ip_handshake_base*>::iterator i;
328 ip_handshake_base* hsb;
329 ip_handshake_relay* hsr;
330 for (i=handshakes.begin();i!=handshakes.end();++i){
332 if (hsb->type==IP_CFG_RELAY){
333 hsr=(ip_handshake_relay*)hsb;
340 ip_handshake_info* find_handshake(void){
341 list<ip_handshake_base*>::iterator i;
342 ip_handshake_base* hsb;
343 for (i=handshakes.begin();i!=handshakes.end();++i){
345 if (hsb->type==IP_CFG_HANDSHAKE)
346 return (ip_handshake_info*) hsb;
348 ip_handshake_info *ni=new ip_handshake_info();
349 // handshakes.push_back(ni);
353 /* ip_handshake_base* find_hs(u_int32_t tryid, ubyte type){
354 list<ip_handshake_base*>::iterator i;
355 ip_handshake_base* hsb;
356 for (i=handshakes.begin();i!=handshakes.end();++i){
358 if (hsb->tryid==tryid && hsb->type==type)
363 void send_handshake(ip_handshake_base*hsb);
364 bool verify_addr(ip_addr_list &fraddrs);
365 ip_peer(void){iver=0;}
367 list<ip_handshake_base*>::iterator i;
368 for (i=handshakes.begin();i!=handshakes.end();++i){
369 /* mprintf((0,"###### ~ip_peer %p: deleting a hs %p %i %i\n",this,(*i),(*i)->type,(*i)->state));
371 mprintf((0,"~ip_peer null hs?\n"));
381 typedef map<ip_id,ip_peer *,less<ip_id> > peer_map;
383 list<ip_peer*> unknown_peers;
384 int pendinghandshakes;
385 fix pendinghandshake_lasttime;
387 ip_peer * add_id(ip_id id){
388 ip_peer*n=new ip_peer();
390 peers.insert(peer_map::value_type(n->id,n));
393 ip_peer * add_unk(void){
394 ip_peer*n=new ip_peer();
395 unknown_peers.push_back(n);
398 //ip_peer * add_1(ubyte *addr,ip_id id);
399 ip_peer * add_1(ip_addr addr);
400 ip_peer * add_full(ip_id id, u_int16_t iver, ip_addr_list &addrs);
401 void make_full(ip_peer*p,ip_id id, u_int16_t iver, ip_addr_list &addrs);
402 ip_peer * add_hsi(ip_handshake_info *hsi){return add_full(hsi->id,hsi->iver,hsi->addr);}
403 ip_peer * find_byid(ip_id id);
404 ip_peer * find_by_addr(ip_addr addr);
405 ip_peer * find_unk_by_addr(ip_addr addr);
406 void handshake_frame(void);
410 pendinghandshake_lasttime=0;
414 extern ip_peer_list peer_list;