1 #include <connection.h>
6 #include <sys/utsname.h>
10 extern std::vector<Module*> modules;
11 extern std::vector<ircd_module*> factory;
16 #define STATE_WAIT_FOR_ACK 2
28 connection::connection()
36 bool connection::CreateListener(char* host, int p)
38 sockaddr_in host_address;
42 struct linger linger = { 0 };
44 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
50 memset((void*)&host_address, 0, sizeof(host_address));
52 host_address.sin_family = AF_INET;
56 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
60 inet_aton(host,&addy);
61 host_address.sin_addr = addy;
64 host_address.sin_port = htons(p);
66 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
71 // make the socket non-blocking
72 flags = fcntl(fd, F_GETFL, 0);
73 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
77 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
80 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
85 bool connection::BeginLink(char* targethost, int port, char* password)
91 sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
92 this->haspassed = false;
93 return this->SendPacket(connect, targethost, port, 0);
98 // targethost: in dot notation a.b.c.d
99 void connection::TerminateLink(char* targethost)
103 // host: in dot notation a.b.c.d
104 // port: host byte order
105 bool connection::SendPacket(char *message, char* host, int port, long ourkey)
107 sockaddr_in host_address;
111 memset((void*)&host_address, 0, sizeof(host_address));
113 host_address.sin_family = AF_INET;
114 inet_aton(host,&addy);
115 host_address.sin_addr = addy;
117 host_address.sin_port = htons(port);
119 strcpy(p.data,message);
120 p.type = PT_SYN_WITH_DATA;
124 FOREACH_MOD OnPacketTransmit(p.data);
126 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);
128 // returns false if the packet could not be sent (e.g. target host down)
129 if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
131 log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d",sizeof(p));
134 this->state = STATE_WAIT_FOR_ACK;
137 // host_address remains unchanged. we only want to receive from where we just sent the packet to.
139 // retry the packet up to 5 times
140 for (int retries = 0; retries < 5; retries++)
142 socklen_t host_address_size;
143 host_address.sin_family=AF_INET;
144 host_address_size=sizeof(host_address);
146 // wait for ack, or timeout.
147 // if reached a timeout, send again.
148 // the packet id in the ack must match that in the original packet
149 // this MUST operate in lock/step fashion!!!
160 int res = select(65535, &sfd, NULL, NULL, &tval);
163 while ((recvfrom(fd,&p2,sizeof(p2),MSG_PEEK,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
167 log(DEFAULT,"ERROR! connection::SendPacket() waited >10000 nanosecs for an ACK. Will resend up to 5 times");
171 if (p2.type != PT_ACK_ONLY)
173 log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting a syn!!!");
174 this->state = STATE_CLEAR;
180 recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
181 log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
182 this->state = STATE_CLEAR;
187 recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
188 log(DEFAULT,"Successfully received ACK");
189 this->state = STATE_CLEAR;
195 log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
196 this->state = STATE_CLEAR;
201 bool connection::SendSYN(char* host, int port)
203 sockaddr_in host_address;
207 memset((void*)&host_address, 0, sizeof(host_address));
209 host_address.sin_family = AF_INET;
210 inet_aton(host,&addy);
211 host_address.sin_addr = addy;
213 host_address.sin_port = htons(port);
215 p.type = PT_SYN_ONLY;
219 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
227 bool connection::SendACK(char* host, int port, int reply_id)
229 sockaddr_in host_address;
233 memset((void*)&host_address, 0, sizeof(host_address));
235 host_address.sin_family = AF_INET;
236 inet_aton(host,&addy);
237 host_address.sin_addr = addy;
239 host_address.sin_port = htons(port);
241 p.type = PT_ACK_ONLY;
246 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
254 // Generates a server key. This is pseudo-random.
255 // the server always uses the same server-key in all communications
256 // across the network. All other servers must remember the server key
257 // of servers in the network, e.g.:
259 // ServerA: key=5555555555
260 // ServerB: key=6666666666
261 // I am ServerC: key=77777777777
263 // If ServerC sees a packet from ServerA, and the key stored for ServerA
264 // is 0, then cache the key as the servers key.
265 // after this point, any packet from ServerA which does not contain its key,
266 // 555555555, will be silently dropped.
267 // This should prevent blind spoofing, as to fake a server you must know its
268 // assigned key, and to do that you must receive messages that are origintated
269 // from it or hack the running executable.
271 // During the AUTH phase (when server passwords are checked, the key in any
272 // packet MUST be 0). Only the initial SERVER/PASS packets may have a key
273 // of 0 (and any ACK responses to them).
276 long connection::GenKey()
279 return (random()*time(NULL));
282 // host: in dot notation a.b.c.d
283 // port: host byte order
284 bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
286 // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
287 sockaddr_in host_address;
288 socklen_t host_address_size;
291 memset((void*)&host_address, 0, sizeof(host_address));
293 host_address.sin_family=AF_INET;
294 host_address_size=sizeof(host_address);
296 //int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
297 if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
302 log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));
304 if (p.type == PT_SYN_ONLY)
306 strcpy(message,p.data);
307 strcpy(host,inet_ntoa(host_address.sin_addr));
308 prt = ntohs(host_address.sin_port);
309 SendACK(host,this->port,p.id);
313 if (p.type == PT_ACK_ONLY)
315 strcpy(message,p.data);
316 strcpy(host,inet_ntoa(host_address.sin_addr));
317 prt = ntohs(host_address.sin_port);
318 this->state = STATE_CLEAR;
322 if (p.type == PT_SYN_WITH_DATA)
324 strcpy(message,p.data);
325 strcpy(host,inet_ntoa(host_address.sin_addr));
327 prt = ntohs(host_address.sin_port); // the port we received it on
328 SendACK(host,prt,p.id);
332 log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);