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()
39 bool connection::CreateListener(char* host, int p)
41 sockaddr_in host_address;
45 struct linger linger = { 0 };
47 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
53 memset((void*)&host_address, 0, sizeof(host_address));
55 host_address.sin_family = AF_INET;
59 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
63 inet_aton(host,&addy);
64 host_address.sin_addr = addy;
67 host_address.sin_port = htons(p);
69 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
74 // make the socket non-blocking
75 flags = fcntl(fd, F_GETFL, 0);
76 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
80 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
83 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
90 bool connection::BeginLink(char* targethost, int port, char* password)
96 sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
97 this->haspassed = false;
98 return this->SendPacket(connect, targethost, port, 0);
103 // targethost: in dot notation a.b.c.d
104 void connection::TerminateLink(char* targethost)
108 // host: in dot notation a.b.c.d
109 // port: host byte order
110 bool connection::SendPacket(char *message, char* host, int port, long ourkey)
112 sockaddr_in host_address;
116 memset((void*)&host_address, 0, sizeof(host_address));
118 host_address.sin_family = AF_INET;
119 inet_aton(host,&addy);
120 host_address.sin_addr = addy;
122 host_address.sin_port = htons(port);
124 strcpy(p.data,message);
125 p.type = PT_SYN_WITH_DATA;
129 FOREACH_MOD OnPacketTransmit(p.data);
131 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);
133 // returns false if the packet could not be sent (e.g. target host down)
134 if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
136 log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d: %s",sizeof(p),strerror(errno));
139 this->state = STATE_WAIT_FOR_ACK;
142 // host_address remains unchanged. we only want to receive from where we just sent the packet to.
144 // retry the packet up to 5 times
145 for (int retries = 0; retries < 5; retries++)
147 socklen_t host_address_size;
148 host_address.sin_family=AF_INET;
149 host_address_size=sizeof(host_address);
151 // wait for ack, or timeout.
152 // if reached a timeout, send again.
153 // the packet id in the ack must match that in the original packet
154 // this MUST operate in lock/step fashion!!!
165 int res = select(65535, &sfd, NULL, NULL, &tval);
168 while ((recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
172 log(DEFAULT,"ERROR! connection::SendPacket() waited >10000 nanosecs for an ACK. Will resend up to 5 times");
176 if (p2.type != PT_ACK_ONLY)
182 strcpy(pb.p.data,p.data);
183 strcpy(pb.host,inet_ntoa(host_address.sin_addr));
184 pb.port = ntohs(host_address.sin_port);
185 this->buffer.push_back(pb);
187 log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting an ACK!!!");
188 this->state = STATE_CLEAR;
194 log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
195 this->state = STATE_CLEAR;
200 log(DEFAULT,"Successfully received ACK");
201 this->state = STATE_CLEAR;
207 log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
208 this->state = STATE_CLEAR;
213 bool connection::SendSYN(char* host, int port)
215 sockaddr_in host_address;
219 memset((void*)&host_address, 0, sizeof(host_address));
221 host_address.sin_family = AF_INET;
222 inet_aton(host,&addy);
223 host_address.sin_addr = addy;
225 host_address.sin_port = htons(port);
227 p.type = PT_SYN_ONLY;
231 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
239 bool connection::SendACK(char* host, int port, int reply_id)
241 sockaddr_in host_address;
245 memset((void*)&host_address, 0, sizeof(host_address));
247 host_address.sin_family = AF_INET;
248 inet_aton(host,&addy);
249 host_address.sin_addr = addy;
251 host_address.sin_port = htons(port);
253 p.type = PT_ACK_ONLY;
258 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
266 // Generates a server key. This is pseudo-random.
267 // the server always uses the same server-key in all communications
268 // across the network. All other servers must remember the server key
269 // of servers in the network, e.g.:
271 // ServerA: key=5555555555
272 // ServerB: key=6666666666
273 // I am ServerC: key=77777777777
275 // If ServerC sees a packet from ServerA, and the key stored for ServerA
276 // is 0, then cache the key as the servers key.
277 // after this point, any packet from ServerA which does not contain its key,
278 // 555555555, will be silently dropped.
279 // This should prevent blind spoofing, as to fake a server you must know its
280 // assigned key, and to do that you must receive messages that are origintated
281 // from it or hack the running executable.
283 // During the AUTH phase (when server passwords are checked, the key in any
284 // packet MUST be 0). Only the initial SERVER/PASS packets may have a key
285 // of 0 (and any ACK responses to them).
288 long connection::GenKey()
291 return (random()*time(NULL));
294 // host: in dot notation a.b.c.d
295 // port: host byte order
296 bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
298 // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
299 sockaddr_in host_address;
300 socklen_t host_address_size;
303 memset((void*)&host_address, 0, sizeof(host_address));
305 host_address.sin_family=AF_INET;
306 host_address_size=sizeof(host_address);
308 //int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
309 if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
313 log(DEBUG,"Fetching a buffered packet size %d",buffer.size());
314 strcpy(message,buffer[0].p.data);
315 theirkey = buffer[0].p.key;
316 strcpy(host,buffer[0].host);
317 prt = buffer[0].port;
319 buffer.erase(buffer.begin());
326 log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));
328 if (p.type == PT_SYN_ONLY)
330 strcpy(message,p.data);
331 strcpy(host,inet_ntoa(host_address.sin_addr));
332 prt = ntohs(host_address.sin_port);
333 SendACK(host,this->port,p.id);
337 if (p.type == PT_ACK_ONLY)
339 strcpy(message,p.data);
340 strcpy(host,inet_ntoa(host_address.sin_addr));
341 prt = ntohs(host_address.sin_port);
342 this->state = STATE_CLEAR;
346 if (p.type == PT_SYN_WITH_DATA)
348 strcpy(message,p.data);
349 strcpy(host,inet_ntoa(host_address.sin_addr));
351 prt = ntohs(host_address.sin_port); // the port we received it on
352 SendACK(host,prt,p.id);
356 log(DEBUG,"Fetching a buffered packet size %d",buffer.size());
361 strcpy(pb.p.data,p.data);
362 strcpy(pb.host,inet_ntoa(host_address.sin_addr));
363 pb.port = ntohs(host_address.sin_port);
364 this->buffer.push_back(pb);
366 strcpy(message,buffer[0].p.data);
367 theirkey = buffer[0].p.key;
368 strcpy(host,buffer[0].host);
369 prt = buffer[0].port;
371 buffer.erase(buffer.begin());
377 log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);