1 #include <connection.h>
6 #include <sys/utsname.h>
12 extern std::vector<Module*> modules;
13 extern std::vector<ircd_module*> factory;
18 #define STATE_WAIT_FOR_ACK 2
30 connection::connection()
38 bool connection::CreateListener(char* host, int p)
40 sockaddr_in host_address;
44 struct linger linger = { 0 };
46 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
52 memset((void*)&host_address, 0, sizeof(host_address));
54 host_address.sin_family = AF_INET;
58 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
62 inet_aton(host,&addy);
63 host_address.sin_addr = addy;
66 host_address.sin_port = htons(p);
68 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
73 // make the socket non-blocking
74 flags = fcntl(fd, F_GETFL, 0);
75 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
79 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
82 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
87 bool connection::BeginLink(char* targethost, int port, char* password)
93 sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
94 this->haspassed = false;
95 return this->SendPacket(connect, targethost, port, 0);
100 // targethost: in dot notation a.b.c.d
101 void connection::TerminateLink(char* targethost)
105 // host: in dot notation a.b.c.d
106 // port: host byte order
107 bool connection::SendPacket(char *message, char* host, int port, long ourkey)
109 sockaddr_in host_address;
113 memset((void*)&host_address, 0, sizeof(host_address));
115 host_address.sin_family = AF_INET;
116 inet_aton(host,&addy);
117 host_address.sin_addr = addy;
119 host_address.sin_port = htons(port);
121 strcpy(p.data,message);
122 p.type = PT_SYN_WITH_DATA;
126 FOREACH_MOD OnPacketTransmit(p.data);
128 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);
130 // returns false if the packet could not be sent (e.g. target host down)
131 if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
133 log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d: %s",sizeof(p),strerror(errno));
136 this->state = STATE_WAIT_FOR_ACK;
139 // host_address remains unchanged. we only want to receive from where we just sent the packet to.
141 // retry the packet up to 5 times
142 for (int retries = 0; retries < 5; retries++)
144 socklen_t host_address_size;
145 host_address.sin_family=AF_INET;
146 host_address_size=sizeof(host_address);
148 // wait for ack, or timeout.
149 // if reached a timeout, send again.
150 // the packet id in the ack must match that in the original packet
151 // this MUST operate in lock/step fashion!!!
162 int res = select(65535, &sfd, NULL, NULL, &tval);
165 while ((recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
169 log(DEFAULT,"ERROR! connection::SendPacket() waited >10000 nanosecs for an ACK. Will resend up to 5 times");
173 if (p2.type != PT_ACK_ONLY)
179 strcpy(pb.host,inet_ntoa(host_address.sin_addr));
180 pb.port = ntohs(host_address.sin_port);
181 this->buffer.push_back(pb);
183 log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting an ACK!!!");
184 this->state = STATE_CLEAR;
190 log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
191 this->state = STATE_CLEAR;
196 log(DEFAULT,"Successfully received ACK");
197 this->state = STATE_CLEAR;
203 log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
204 this->state = STATE_CLEAR;
209 bool connection::SendSYN(char* host, int port)
211 sockaddr_in host_address;
215 memset((void*)&host_address, 0, sizeof(host_address));
217 host_address.sin_family = AF_INET;
218 inet_aton(host,&addy);
219 host_address.sin_addr = addy;
221 host_address.sin_port = htons(port);
223 p.type = PT_SYN_ONLY;
227 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
235 bool connection::SendACK(char* host, int port, int reply_id)
237 sockaddr_in host_address;
241 memset((void*)&host_address, 0, sizeof(host_address));
243 host_address.sin_family = AF_INET;
244 inet_aton(host,&addy);
245 host_address.sin_addr = addy;
247 host_address.sin_port = htons(port);
249 p.type = PT_ACK_ONLY;
254 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
262 // Generates a server key. This is pseudo-random.
263 // the server always uses the same server-key in all communications
264 // across the network. All other servers must remember the server key
265 // of servers in the network, e.g.:
267 // ServerA: key=5555555555
268 // ServerB: key=6666666666
269 // I am ServerC: key=77777777777
271 // If ServerC sees a packet from ServerA, and the key stored for ServerA
272 // is 0, then cache the key as the servers key.
273 // after this point, any packet from ServerA which does not contain its key,
274 // 555555555, will be silently dropped.
275 // This should prevent blind spoofing, as to fake a server you must know its
276 // assigned key, and to do that you must receive messages that are origintated
277 // from it or hack the running executable.
279 // During the AUTH phase (when server passwords are checked, the key in any
280 // packet MUST be 0). Only the initial SERVER/PASS packets may have a key
281 // of 0 (and any ACK responses to them).
284 long connection::GenKey()
287 return (random()*time(NULL));
290 // host: in dot notation a.b.c.d
291 // port: host byte order
292 bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
294 // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
295 sockaddr_in host_address;
296 socklen_t host_address_size;
299 memset((void*)&host_address, 0, sizeof(host_address));
301 host_address.sin_family=AF_INET;
302 host_address_size=sizeof(host_address);
304 //int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
305 if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
307 if (this->buffer.size())
309 log(DEBUG,"Fetching a buffered packet");
311 strcpy(message,buffer[0].p.data);
312 theirkey = buffer[0].p.key;
313 strcpy(host,buffer[0].host);
314 prt = buffer[0].port;
323 log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));
325 if (p.type == PT_SYN_ONLY)
327 strcpy(message,p.data);
328 strcpy(host,inet_ntoa(host_address.sin_addr));
329 prt = ntohs(host_address.sin_port);
330 SendACK(host,this->port,p.id);
334 if (p.type == PT_ACK_ONLY)
336 strcpy(message,p.data);
337 strcpy(host,inet_ntoa(host_address.sin_addr));
338 prt = ntohs(host_address.sin_port);
339 this->state = STATE_CLEAR;
343 if (p.type == PT_SYN_WITH_DATA)
345 strcpy(message,p.data);
346 strcpy(host,inet_ntoa(host_address.sin_addr));
348 prt = ntohs(host_address.sin_port); // the port we received it on
349 SendACK(host,prt,p.id);
351 if (this->buffer.size())
353 log(DEBUG,"Fetching a buffered packet");
358 strcpy(pb.host,inet_ntoa(host_address.sin_addr));
359 pb.port = ntohs(host_address.sin_port);
360 this->buffer.push_back(pb);
362 strcpy(message,buffer[0].p.data);
363 theirkey = buffer[0].p.key;
364 strcpy(host,buffer[0].host);
365 prt = buffer[0].port;
373 log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);