1 #include <connection.h>
6 #include <sys/utsname.h>
11 extern std::vector<Module*> modules;
12 extern std::vector<ircd_module*> factory;
17 #define STATE_WAIT_FOR_ACK 2
29 connection::connection()
37 bool connection::CreateListener(char* host, int p)
39 sockaddr_in host_address;
43 struct linger linger = { 0 };
45 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
51 memset((void*)&host_address, 0, sizeof(host_address));
53 host_address.sin_family = AF_INET;
57 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
61 inet_aton(host,&addy);
62 host_address.sin_addr = addy;
65 host_address.sin_port = htons(p);
67 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
72 // make the socket non-blocking
73 flags = fcntl(fd, F_GETFL, 0);
74 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
78 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
81 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
86 bool connection::BeginLink(char* targethost, int port, char* password)
92 sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
93 this->haspassed = false;
94 return this->SendPacket(connect, targethost, port, 0);
99 // targethost: in dot notation a.b.c.d
100 void connection::TerminateLink(char* targethost)
104 // host: in dot notation a.b.c.d
105 // port: host byte order
106 bool connection::SendPacket(char *message, char* host, int port, long ourkey)
108 sockaddr_in host_address;
112 memset((void*)&host_address, 0, sizeof(host_address));
114 host_address.sin_family = AF_INET;
115 inet_aton(host,&addy);
116 host_address.sin_addr = addy;
118 host_address.sin_port = htons(port);
120 strcpy(p.data,message);
121 p.type = PT_SYN_WITH_DATA;
125 FOREACH_MOD OnPacketTransmit(p.data);
127 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);
129 // returns false if the packet could not be sent (e.g. target host down)
130 if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
132 log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d: %s",sizeof(p),strerror(errno));
135 this->state = STATE_WAIT_FOR_ACK;
138 // host_address remains unchanged. we only want to receive from where we just sent the packet to.
140 // retry the packet up to 5 times
141 for (int retries = 0; retries < 5; retries++)
143 socklen_t host_address_size;
144 host_address.sin_family=AF_INET;
145 host_address_size=sizeof(host_address);
147 // wait for ack, or timeout.
148 // if reached a timeout, send again.
149 // the packet id in the ack must match that in the original packet
150 // this MUST operate in lock/step fashion!!!
161 int res = select(65535, &sfd, NULL, NULL, &tval);
164 while ((recvfrom(fd,&p2,sizeof(p2),MSG_PEEK,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
168 log(DEFAULT,"ERROR! connection::SendPacket() waited >10000 nanosecs for an ACK. Will resend up to 5 times");
172 if (p2.type != PT_ACK_ONLY)
174 log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting a syn!!!");
175 this->state = STATE_CLEAR;
181 recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
182 log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
183 this->state = STATE_CLEAR;
188 recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
189 log(DEFAULT,"Successfully received ACK");
190 this->state = STATE_CLEAR;
196 log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
197 this->state = STATE_CLEAR;
202 bool connection::SendSYN(char* host, int port)
204 sockaddr_in host_address;
208 memset((void*)&host_address, 0, sizeof(host_address));
210 host_address.sin_family = AF_INET;
211 inet_aton(host,&addy);
212 host_address.sin_addr = addy;
214 host_address.sin_port = htons(port);
216 p.type = PT_SYN_ONLY;
220 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
228 bool connection::SendACK(char* host, int port, int reply_id)
230 sockaddr_in host_address;
234 memset((void*)&host_address, 0, sizeof(host_address));
236 host_address.sin_family = AF_INET;
237 inet_aton(host,&addy);
238 host_address.sin_addr = addy;
240 host_address.sin_port = htons(port);
242 p.type = PT_ACK_ONLY;
247 if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
255 // Generates a server key. This is pseudo-random.
256 // the server always uses the same server-key in all communications
257 // across the network. All other servers must remember the server key
258 // of servers in the network, e.g.:
260 // ServerA: key=5555555555
261 // ServerB: key=6666666666
262 // I am ServerC: key=77777777777
264 // If ServerC sees a packet from ServerA, and the key stored for ServerA
265 // is 0, then cache the key as the servers key.
266 // after this point, any packet from ServerA which does not contain its key,
267 // 555555555, will be silently dropped.
268 // This should prevent blind spoofing, as to fake a server you must know its
269 // assigned key, and to do that you must receive messages that are origintated
270 // from it or hack the running executable.
272 // During the AUTH phase (when server passwords are checked, the key in any
273 // packet MUST be 0). Only the initial SERVER/PASS packets may have a key
274 // of 0 (and any ACK responses to them).
277 long connection::GenKey()
280 return (random()*time(NULL));
283 // host: in dot notation a.b.c.d
284 // port: host byte order
285 bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
287 // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
288 sockaddr_in host_address;
289 socklen_t host_address_size;
292 memset((void*)&host_address, 0, sizeof(host_address));
294 host_address.sin_family=AF_INET;
295 host_address_size=sizeof(host_address);
297 //int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
298 if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
303 log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));
305 if (p.type == PT_SYN_ONLY)
307 strcpy(message,p.data);
308 strcpy(host,inet_ntoa(host_address.sin_addr));
309 prt = ntohs(host_address.sin_port);
310 SendACK(host,this->port,p.id);
314 if (p.type == PT_ACK_ONLY)
316 strcpy(message,p.data);
317 strcpy(host,inet_ntoa(host_address.sin_addr));
318 prt = ntohs(host_address.sin_port);
319 this->state = STATE_CLEAR;
323 if (p.type == PT_SYN_WITH_DATA)
325 strcpy(message,p.data);
326 strcpy(host,inet_ntoa(host_address.sin_addr));
328 prt = ntohs(host_address.sin_port); // the port we received it on
329 SendACK(host,prt,p.id);
333 log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);