2 * $Source: /cvs/cvsroot/d2x/main/ip_base.h,v $
5 * $Date: 2002-02-14 09:24:19 $
7 * ip_base.h - base for NAT-compatible udp/ip code.
8 * added 2000/02/07 Matt Mueller
10 * $Log: not supported by cvs2svn $
11 * Revision 1.2 2002/02/13 10:45:49 bradleyb
12 * Lotsa networking stuff from d1x
14 * Revision 1.1 2002/02/06 09:22:41 bradleyb
15 * Adding d1x network code
35 class ip_addr;//prototype for arch_ip_queryhost
41 //inline u_int32_t d_rand32(void) { return d_rand() + (d_rand()<<16);}
42 inline u_int32_t d_rand32(void) { return d_rand() + (d_rand()<<15) + (d_rand()<<30);}//d_rand() only returns 15 bits
45 void ip_receive_cfg(ubyte *buf,int buflen,ip_addr fromaddr);
47 extern unsigned char ipx_MyAddress[10];
49 unsigned short ip_portshift(unsigned short baseport, const char *cs);
51 #define UDP_BASEPORT 31017
52 #define UDP_SERV_BASEPORT 30817
53 #define PORTSHIFT_TOLERANCE 0x100
54 #define MAX_PACKETSIZE 8192
56 // Length HAS TO BE 2!
58 // Length HAS TO BE 4!
59 #define D2Xcfgid "\xcfg\xd2x"
64 #define IP_CFG_HANDSHAKE 1
65 #define IP_CFG_RELAY 2
66 //someone giving us an error (ie, outdated version, etc.) ... not handled yet.
67 #define IP_CFG_SORRY 3
70 #define STATE_INEEDINFO 1<<0
71 #define STATE_SENDINGINFO 1<<1
72 //#define STATE_NEED_RESEND (STATE_INEEDINFO | STATE_SENDINGINFO)
73 #define STATE_NEED_RESEND (STATE_INEEDINFO)
74 #define STATE_VALID_STATES (STATE_INEEDINFO | STATE_SENDINGINFO)
75 #define STATE_RELAYREPLY 1<<6
76 #define STATE_ERR 1<<7
77 /* Dump raw form of IP address/port by fancy output to user
82 inline char *ip_hs_statetoa(int state){
87 if ((state&(STATE_INEEDINFO|STATE_SENDINGINFO))==(STATE_INEEDINFO|STATE_SENDINGINFO))
89 if (state&STATE_INEEDINFO)
91 if (state&STATE_SENDINGINFO)
95 inline void dumprid(const unsigned char *a)
97 printf("<%u.%u.%u.%u.%u.%u>",a[0],a[1],a[2],a[3],a[4],a[5]);
99 inline void dumpraddr(const unsigned char *addr)
102 printf("[%u.%u.%u.%u]",addr[0],addr[1],addr[2],addr[3]);
103 port=(unsigned short)ntohs(*(unsigned short *)(addr+4));
113 int fillbuf(ubyte *buf)const {memcpy(buf,id,6);return 6;};
114 int readbuf(const ubyte *buf){memcpy(id,buf,6);return 6;};
115 bool operator < (const ip_id &a_id)const {return memcmp(id,a_id.id,6)<0;}
116 bool operator == (const ip_id &a_id)const {return memcmp(id,a_id.id,6)==0;}
117 bool operator > (const ip_id &a_id)const {return memcmp(id,a_id.id,6)>0;}
118 ip_id& operator = (const ip_id &a_id){readbuf(a_id.id);return *this;}
120 void dump(void){dumprid(id);}
122 ip_id(ubyte*nid){readbuf(nid);}
128 ubyte alen;//alen stuff is to maybe make ipv6 support easier (if the transition ever actually happens)
132 int fillbuf(ubyte *buf)const {buf[0]=alen;memcpy(buf+1,addr,alen);return alen+1;};
133 int readbuf(const ubyte *buf){
136 memcpy(addr,buf+1,6);alen=l;
138 mprintf((0,"ip_addr readbuf bad len %i\n",l));
139 memset(addr,0,6);alen=0;
143 bool operator < (const ip_addr &a_addr)const {
144 if (alen!=a_addr.alen) return alen<a_addr.alen;
145 return memcmp(addr,a_addr.addr,alen)<0;
147 bool operator == (const ip_addr &a_addr)const {return ((alen==a_addr.alen) && (memcmp(addr,a_addr.addr,alen)==0));}
148 bool operator > (const ip_addr &a_addr)const {
149 if (alen!=a_addr.alen) return alen>a_addr.alen;
150 return memcmp(addr,a_addr.addr,alen)>0;
152 ip_addr& operator = (const ip_addr &a_addr){alen=a_addr.alen;memcpy(addr,a_addr.addr,6);return *this;}
154 void dump(void)const{dumpraddr(addr);}
156 void set(int addrsize,ubyte *naddr,ushort port){
157 if (addrsize!=4)return;//not handled yet.
159 memcpy(addr,naddr,addrsize);
161 memcpy(addr+addrsize,&port,2);
163 int dns(char *address,ushort baseport){
164 return arch_ip_queryhost(this,address,baseport);
166 bool ok(void)const{return alen>0;}
167 int addrlen(void)const{return alen-2;}
168 // ip_addr(ubyte*naddr){readbuf(naddr);}
169 ip_addr(void){alen=0;};
176 typedef list<ip_addr>::iterator iterator;
177 typedef list<ip_addr>::const_iterator const_iterator;
181 iterator begin(void){return addrs.begin();}
182 iterator end(void){return addrs.end();}
183 const_iterator begin(void)const{return addrs.begin();}
184 const_iterator end(void)const{return addrs.end();}
185 bool hasaddr(ip_addr addr){
186 iterator i=find(begin(),end(),addr);
187 return (i!=addrs.end());
189 int add(ip_addr addr){
192 addrs.push_back(addr);
196 int add(const ip_addr_list &na){
198 const_iterator i=na.begin();
199 for (;i!=na.end();++i)
203 void setgoodaddr(ip_addr addr){
204 iterator i=find(begin(),end(),addr);
209 addrs.push_back(addr);
210 goodaddr=&(*addrs.end());
212 int fillbuf(ubyte *buf)const {
216 for (i=begin();i!=end();++i)
217 s+=(*i).fillbuf(buf+s);
220 int readbuf(const ubyte *buf){
223 for (n=buf[0];n;--n){
225 // addrs.push_back(a);
234 addrs.erase(begin(),end());
237 ip_addr_list(const ip_addr_list &nl):addrs(nl.addrs),naddr(nl.naddr){setgoodaddr(*nl.goodaddr);}
238 ip_addr_list(void):naddr(0),goodaddr(NULL){}
239 // ip_addr_list(const ubyte * buf):naddr(0),goodaddr(NULL){readbuf(buf);}
242 extern ip_addr_list ip_my_addrs;
244 extern int ip_sendtoca(ip_addr addr,const void *buf,int len);
246 class ip_handshake_base {
258 virtual int fillbuf(ubyte *buf);
259 virtual int readbuf(ubyte *buf);
260 void setrandid(void){tryid=d_rand32() ^ timer_get_approx_seconds();}
261 void setstate(int newstate);
262 int addstate(int newstate);
264 // ip_handshake_base(void):type(IP_CFG_BASE){setstate(IPNOSTATE);}
265 ip_handshake_base(bool initme=0):type(IP_CFG_BASE){
276 virtual ~ip_handshake_base(){setstate(0);}//decrement pendinghandshakes if needed.
278 class ip_handshake_info : public ip_handshake_base {
282 virtual int fillbuf(ubyte *buf);
283 virtual int readbuf(ubyte *buf);
285 ip_handshake_info(ubyte *buf){
288 ip_handshake_info(void):ip_handshake_base(1){
289 type=IP_CFG_HANDSHAKE;
291 // setstate(STATE_INEEDINFO);
295 class ip_handshake_relay : public ip_handshake_base {
301 virtual int fillbuf(ubyte *buf);
302 virtual int readbuf(ubyte *buf);
304 ip_handshake_relay(ubyte *buf){
307 ip_handshake_relay(ip_peer *torelay);
310 //max number of times to try to handshake a host
311 #define IP_MAX_HS_ATTEMPTS 10
312 //how often to resend a hand-shake (3 seconds)
313 #define IP_HS_RETRYTIME (F1_0*3)
314 //how often (max) to search through the resend loop (1 second)
315 #define IP_HS_FRAME_RETRYTIME (F1_0)
323 // ip_hs_state query;
324 // ip_hs_state reply;
325 list<ip_handshake_base*> handshakes;
327 // void set_good_addr(ubyte *fraddr);
328 void add_hs(ip_handshake_base* hs){
329 // mprintf((0,"########## add_hs %p: adding a hs %p %i %i\n",this,hs,hs->type,hs->state));
330 handshakes.push_back(hs);
332 ip_handshake_relay* find_relay(ip_id r_id){
333 list<ip_handshake_base*>::iterator i;
334 ip_handshake_base* hsb;
335 ip_handshake_relay* hsr;
336 for (i=handshakes.begin();i!=handshakes.end();++i){
338 if (hsb->type==IP_CFG_RELAY){
339 hsr=(ip_handshake_relay*)hsb;
346 ip_handshake_info* find_handshake(void){
347 list<ip_handshake_base*>::iterator i;
348 ip_handshake_base* hsb;
349 for (i=handshakes.begin();i!=handshakes.end();++i){
351 if (hsb->type==IP_CFG_HANDSHAKE)
352 return (ip_handshake_info*) hsb;
354 ip_handshake_info *ni=new ip_handshake_info();
355 // handshakes.push_back(ni);
359 /* ip_handshake_base* find_hs(u_int32_t tryid, ubyte type){
360 list<ip_handshake_base*>::iterator i;
361 ip_handshake_base* hsb;
362 for (i=handshakes.begin();i!=handshakes.end();++i){
364 if (hsb->tryid==tryid && hsb->type==type)
369 void send_handshake(ip_handshake_base*hsb);
370 bool verify_addr(ip_addr_list &fraddrs);
371 ip_peer(void){iver=0;}
373 list<ip_handshake_base*>::iterator i;
374 for (i=handshakes.begin();i!=handshakes.end();++i){
375 /* mprintf((0,"###### ~ip_peer %p: deleting a hs %p %i %i\n",this,(*i),(*i)->type,(*i)->state));
377 mprintf((0,"~ip_peer null hs?\n"));
387 typedef map<ip_id,ip_peer *,less<ip_id> > peer_map;
389 list<ip_peer*> unknown_peers;
390 int pendinghandshakes;
391 fix pendinghandshake_lasttime;
393 ip_peer * add_id(ip_id id){
394 ip_peer*n=new ip_peer();
396 peers.insert(peer_map::value_type(n->id,n));
399 ip_peer * add_unk(void){
400 ip_peer*n=new ip_peer();
401 unknown_peers.push_back(n);
404 //ip_peer * add_1(ubyte *addr,ip_id id);
405 ip_peer * add_1(ip_addr addr);
406 ip_peer * add_full(ip_id id, u_int16_t iver, ip_addr_list &addrs);
407 void make_full(ip_peer*p,ip_id id, u_int16_t iver, ip_addr_list &addrs);
408 ip_peer * add_hsi(ip_handshake_info *hsi){return add_full(hsi->id,hsi->iver,hsi->addr);}
409 ip_peer * find_byid(ip_id id);
410 ip_peer * find_by_addr(ip_addr addr);
411 ip_peer * find_unk_by_addr(ip_addr addr);
412 void handshake_frame(void);
416 pendinghandshake_lasttime=0;
420 extern ip_peer_list peer_list;