#include <errno.h>
#include <deque>
#include <errno.h>
+#include <unistd.h>
+#include <sched.h>
#include "connection.h"
#include "users.h"
#include "servers.h"
int DieDelay = 5;
time_t startup_time = time(NULL);
int NetBufferSize = 10240; // NetBufferSize used as the buffer size for all read() ops
-time_t nb_start = time(NULL);
+time_t nb_start = 0;
extern vector<Module*> modules;
std::vector<std::string> module_names;
typedef std::deque<command_t> command_table;
serverrec* me[32];
-serverrec* servers[255];
FILE *log_file;
bool IsDenied(userrec *user);
void AddWhoWas(userrec* u);
+std::vector<long> auth_cookies;
std::stringstream config_f(stringstream::in | stringstream::out);
void safedelete(userrec *p)
}
+
+std::string GetServerDescription(char* servername)
+{
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),servername))
+ {
+ return me[j]->connectors[k].GetDescription();
+ }
+ }
+ }
+ return "";
+ }
+}
+
+
/* return 0 or 1 depending if users u and u2 share one or more common channels
* (used by QUIT, NICK etc which arent channel specific notices) */
// returns true if user 'u' shares any common channels with any users on server 'servername'
-bool CommonOnThisServer(userrec* u,char* servername)
+bool CommonOnThisServer(userrec* u,const char* servername)
{
log(DEBUG,"ChanAnyOnThisServer");
for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if (!strcasecmp(i->second->server,servername))
{
+ log(DEBUG,"%s is common to %s sharing with %s",i->second->nick,servername,u->nick);
return true;
}
}
}
+void NetSendToCommon(userrec* u, char* s)
+{
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"%s",s);
+
+ log(DEBUG,"NetSendToCommon: '%s' '%s'",u->nick,s);
+
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (CommonOnThisServer(u,me[j]->connectors[k].GetServerName().c_str()))
+ {
+ me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
+ }
+ }
+ }
+ }
+}
+
+
+void NetSendToAll(char* s)
+{
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"%s",s);
+
+ log(DEBUG,"NetSendToAll: '%s'",s);
+
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
+ }
+ }
+ }
+}
+
+void NetSendToAllAlive(char* s)
+{
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"%s",s);
+
+ log(DEBUG,"NetSendToAllAlive: '%s'",s);
+
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (me[j]->connectors[k].GetState() != STATE_DISCONNECTED)
+ {
+ me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
+ }
+ else
+ {
+ log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName().c_str());
+ }
+ }
+ }
+ }
+}
+
+
+void NetSendToOne(char* target,char* s)
+{
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"%s",s);
+
+ log(DEBUG,"NetSendToOne: '%s' '%s'",target,s);
+
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
+ {
+ me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
+ }
+ }
+ }
+ }
+}
+
+void NetSendToAllExcept(const char* target,char* s)
+{
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"%s",s);
+
+ log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
+
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
+ {
+ me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
+ }
+ }
+ }
+ }
+}
+
bool hasumode(userrec* user, char mode)
{
}
}
-void WriteWallOps(userrec *source, char* text, ...)
+void ChangeName(userrec* user, const char* gecos)
+{
+ strncpy(user->fullname,gecos,MAXBUF);
+
+ // TODO: replace these with functions:
+ // NetSendToAll - to all
+ // NetSendToCommon - to all that hold users sharing a common channel with another user
+ // NetSendToOne - to one server
+ // NetSendToAllExcept - send to all but one
+ // all by servername
+
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
+ NetSendToAll(buffer);
+}
+
+void ChangeDisplayedHost(userrec* user, const char* host)
+{
+ strncpy(user->dhost,host,160);
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"b %s :%s",user->nick,host);
+ NetSendToAll(buffer);
+}
+
+void WriteWallOps(userrec *source, bool local_only, char* text, ...)
{
if ((!text) || (!source))
{
if (i->second)
{
if (strchr(i->second->modes,'w'))
- {
+ {
WriteTo(source,i->second,"WALLOPS %s",textbuffer);
}
}
}
+
+ if (!local_only)
+ {
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"@ %s :%s",source->nick,textbuffer);
+ NetSendToAll(buffer);
+ }
}
/* convert a string to lowercase. Note following special circumstances
// use the stamdard J token with no privilages.
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"J %s :%s",user->nick,Ptr->name);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (ChanAnyOnThisServer(Ptr,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent J token");
- }
- }
- }
+ NetSendToAll(buffer);
}
log(DEBUG,"Sent JOIN to client");
{
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"L %s %s :%s",user->nick,Ptr->name,reason);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (ChanAnyOnThisServer(Ptr,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent L token (with reason)");
- }
- }
- }
+ NetSendToAll(buffer);
}
{
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"L %s %s :",user->nick,Ptr->name);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (ChanAnyOnThisServer(Ptr,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent L token (no reason)");
- }
- }
- }
+ NetSendToAll(buffer);
}
WriteChannel(Ptr,user,"PART :%s",Ptr->name);
for (int i =0; i != MAXCHANS; i++)
{
/* zap it from the channel list of the user */
- if (user->chans[i].channel == Ptr)
+ if (user->chans[i].channel)
+ if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
{
WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
user->chans[i].uc_modes = 0;
log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
return 0;
}
- if (status != STATUS_OP)
+ if (status < STATUS_OP)
{
+ log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
return 0;
}
{
if (!isnick(dest))
{
+ log(DEFAULT,"the target nickname given to give_ops was invalid");
WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
return 0;
}
d = Find(dest);
if (!d)
{
+ log(DEFAULT,"the target nickname given to give_ops couldnt be found");
WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
return 0;
}
{
for (int i = 0; i != MAXCHANS; i++)
{
- if ((d->chans[i].channel == chan) && (chan != NULL))
+ if ((d->chans[i].channel != NULL) && (chan != NULL))
+ if (!strcasecmp(d->chans[i].channel->name,chan->name))
{
if (d->chans[i].uc_modes & UCMODE_OP)
{
/* mode already set on user, dont allow multiple */
+ log(DEFAULT,"The target user given to give_ops was already opped on the channel");
return 0;
}
d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
+ return 1;
}
}
+ log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
}
}
return 1;
{
for (int i = 0; i != MAXCHANS; i++)
{
- if ((d->chans[i].channel == chan) && (chan != NULL))
+ if ((d->chans[i].channel != NULL) && (chan != NULL))
+ if (!strcasecmp(d->chans[i].channel->name,chan->name))
{
if (d->chans[i].uc_modes & UCMODE_HOP)
{
}
d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
+ return 1;
}
}
}
{
for (int i = 0; i != MAXCHANS; i++)
{
- if ((d->chans[i].channel == chan) && (chan != NULL))
+ if ((d->chans[i].channel != NULL) && (chan != NULL))
+ if (!strcasecmp(d->chans[i].channel->name,chan->name))
{
if (d->chans[i].uc_modes & UCMODE_VOICE)
{
}
d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
+ return 1;
}
}
}
log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
return 0;
}
- if (status != STATUS_OP)
+ if (status < STATUS_OP)
{
+ log(DEBUG,"%s cant give ops to %s because they have status %d and needs %d",user->nick,dest,status,STATUS_OP);
WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
return 0;
}
d = Find(dest);
if (!isnick(dest))
{
+ log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
return 0;
}
if (!d)
{
+ log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
return 0;
}
{
for (int i = 0; i != MAXCHANS; i++)
{
- if ((d->chans[i].channel == chan) && (chan != NULL))
+ if ((d->chans[i].channel != NULL) && (chan != NULL))
+ if (!strcasecmp(d->chans[i].channel->name,chan->name))
{
if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
{
}
d->chans[i].uc_modes ^= UCMODE_OP;
log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
+ return 1;
}
}
+ log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
}
}
return 1;
{
for (int i = 0; i != MAXCHANS; i++)
{
- if ((d->chans[i].channel == chan) && (chan != NULL))
+ if ((d->chans[i].channel != NULL) && (chan != NULL))
+ if (!strcasecmp(d->chans[i].channel->name,chan->name))
{
if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
{
}
d->chans[i].uc_modes ^= UCMODE_HOP;
log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
+ return 1;
}
}
}
{
for (int i = 0; i != MAXCHANS; i++)
{
- if ((d->chans[i].channel == chan) && (chan != NULL))
+ if ((d->chans[i].channel != NULL) && (chan != NULL))
+ if (!strcasecmp(d->chans[i].channel->name,chan->name))
{
if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
{
}
d->chans[i].uc_modes ^= UCMODE_VOICE;
log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
+ return 1;
}
}
}
return 0;
}
-void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent)
+void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
{
if (!parameters) {
log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
return;
}
- log(DEBUG,"process_modes: start");
+ log(DEBUG,"process_modes: start: parameters=%d",pcnt);
strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
/* parameters[2] onwards are parameters for
break;
case 'o':
+ log(DEBUG,"Ops");
if ((param >= pcnt)) break;
+ log(DEBUG,"Enough parameters left");
if (mdir == 1)
{
+ log(DEBUG,"calling give_ops");
r = give_ops(user,parameters[param++],chan,status);
}
else
{
+ log(DEBUG,"calling take_ops");
r = take_ops(user,parameters[param++],chan,status);
}
if (r)
strcat(outstr," ");
strcat(outstr,outpars[ptr]);
}
- if (servermode)
+ if (local)
+ {
+ log(DEBUG,"Local mode change");
+ WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
+ }
+ else
{
- if (!silent)
+ if (servermode)
{
- WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
- // M token for a usermode must go to all servers
- char buffer[MAXBUF];
- snprintf(buffer,MAXBUF,"M %s %s",ServerName,chan->name, outstr);
- for (int j = 0; j < 255; j++)
+ if (!silent)
{
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- if (ChanAnyOnThisServer(chan,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent M token");
- }
- }
- }
+ WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
+ // M token for a usermode must go to all servers
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
+ NetSendToAll(buffer);
}
+
}
-
- }
- else
- {
- if (!silent)
+ else
{
- WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
- // M token for a usermode must go to all servers
- char buffer[MAXBUF];
- snprintf(buffer,MAXBUF,"m %s %s %s",user,chan->name, outstr);
- for (int j = 0; j < 255; j++)
+ if (!silent)
{
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- if (ChanAnyOnThisServer(chan,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent m token");
- }
- }
- }
+ WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
+ // M token for a usermode must go to all servers
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
+ NetSendToAll(buffer);
}
}
}
}
else
{
- log(DEBUG,"*** BUG *** Non-module umode passed to process_module_umode!");
if (faked)
{
delete s2;
// M token for a usermode must go to all servers
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent m token");
- }
- }
- }
+ NetSendToAll(buffer);
if (strlen(dmodes)>MAXMODES)
{
WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
}
WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
+ return;
}
}
return;
}
- process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false);
+ process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
}
else
{
// M token for a usermode must go to all servers
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent m token");
- }
- }
- }
+ NetSendToAll(buffer);
if (strlen(dmodes)>MAXMODES)
{
Ptr = FindChan(parameters[0]);
if (Ptr)
{
- process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false);
+ process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
}
else
{
strncpy(s2.nick,ServerName,NICKMAX);
strcpy(s2.modes,"o");
s2.fd = -1;
- process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true);
+ process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
}
}
if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
return;
+ WriteTo(user,dest,"MODE :%s",b);
+
if (strlen(dmodes)>MAXMODES)
{
dmodes[MAXMODES-1] = '\0';
Ptr = FindChan(parameters[0]);
if (Ptr)
{
- process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,true);
+ if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
+ {
+ return;
+ }
+
+ process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);
}
}
WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
return;
}
+
+ char reason[MAXBUF];
if (pcnt > 2)
{
- char reason[MAXBUF];
strncpy(reason,parameters[2],MAXBUF);
if (strlen(reason)>MAXKICK)
{
}
else
{
- kick_channel(user,u,Ptr,user->nick);
+ strcpy(reason,user->nick);
+ kick_channel(user,u,Ptr,reason);
}
+
+ // this must be propogated so that channel membership is kept in step network-wide
+
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"k %s %s %s :%s",user->nick,u->nick,Ptr->name,reason);
+ NetSendToAll(buffer);
}
// Q token must go to ALL servers!!!
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"Q %s :%s",user->nick,reason);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent Q token");
- }
- }
- }
+ NetSendToAll(buffer);
}
/* push the socket on a stack of sockets due to be closed at the next opportunity
log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
if (u)
{
- WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
- // :Brain!brain@NetAdmin.chatspike.net KILL [Brain] :homer!NetAdmin.chatspike.net!Brain (test kill)
- WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
- sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
- kill_link(u,killreason);
+ if (strcmp(ServerName,u->server))
+ {
+ // remote kill
+ WriteOpers("*** Remote kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
+ sprintf(killreason,"[%s] Killed (%s (%s))",ServerName,user->nick,parameters[1]);
+ WriteCommonExcept(u,"QUIT :%s",killreason);
+ // K token must go to ALL servers!!!
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"K %s %s :%s",user->nick,u->nick,killreason);
+ NetSendToAll(buffer);
+
+ user_hash::iterator iter = clientlist.find(u->nick);
+ if (iter != clientlist.end())
+ {
+ log(DEBUG,"deleting user hash value %d",iter->second);
+ if ((iter->second) && (user->registered == 7)) {
+ delete iter->second;
+ }
+ clientlist.erase(iter);
+ }
+ purge_empty_chans();
+ }
+ else
+ {
+ // local kill
+ WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
+ WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
+ sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
+ kill_link(u,killreason);
+ }
}
else
{
u->InviteTo(c->name);
WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
+
+ // i token must go to ALL servers!!!
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"i %s %s %s",u->nick,user->nick,c->name);
+ NetSendToAll(buffer);
}
void handle_topic(char **parameters, int pcnt, userrec *user)
strcpy(Ptr->setby,user->nick);
Ptr->topicset = time(NULL);
WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
+
+ // t token must go to ALL servers!!!
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"t %s %s :%s",user->nick,Ptr->name,topic);
+ NetSendToAll(buffer);
}
else
{
}
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
{
Start();
+ srand(time(NULL));
log(DEBUG,"*** InspIRCd starting up!");
if (!FileExists(CONFIG_FILE))
{
// if any users of this channel are on remote servers, broadcast the packet
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"P %s %s :%s",user->nick,chan->name,parameters[1]);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (ChanAnyOnThisServer(chan,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- }
- }
- }
+ NetSendToCommon(user,buffer);
}
else
{
}
else
{
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (!strcasecmp(servers[j]->name,dest->server))
- {
- // direct write, same server
- char buffer[MAXBUF];
- snprintf(buffer,MAXBUF,"P %s %s :%s",user->nick,dest->nick,parameters[1]);
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- }
- }
- }
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"P %s %s :%s",user->nick,dest->nick,parameters[1]);
+ NetSendToOne(dest->server,buffer);
}
}
else
// if any users of this channel are on remote servers, broadcast the packet
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"V %s %s :%s",user->nick,chan->name,parameters[1]);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (ChanAnyOnThisServer(chan,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- }
- }
- }
+ NetSendToCommon(user,buffer);
}
else
{
}
else
{
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (!strcasecmp(servers[j]->name,dest->server))
- {
- // direct write, same server
- char buffer[MAXBUF];
- snprintf(buffer,MAXBUF,"V %s %s :%s",user->nick,dest->nick,parameters[1]);
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- }
- }
- }
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"V %s %s :%s",user->nick,dest->nick,parameters[1]);
+ NetSendToOne(dest->server,buffer);
}
}
else
{
WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
}
- WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
+ WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server));
if (strcmp(dest->awaymsg,""))
{
WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
}
}
+void send_network_quit(const char* nick, const char* reason)
+{
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
+ NetSendToAll(buffer);
+}
+
void handle_quit(char **parameters, int pcnt, userrec *user)
{
user_hash::iterator iter = clientlist.find(user->nick);
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"Q %s :%s%s",user->nick,PrefixQuit,parameters[0]);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (CommonOnThisServer(user,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent Q token");
- }
- }
- }
+ NetSendToAll(buffer);
}
else
{
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"Q %s :Client exited",user->nick);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (CommonOnThisServer(user,servers[j]->name))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent Q token");
- }
- }
- }
+ NetSendToAll(buffer);
}
FOREACH_MOD OnUserQuit(user);
AddWhoWas(user);
void handle_wallops(char **parameters, int pcnt, userrec *user)
{
- WriteWallOps(user,"%s",parameters[0]);
+ WriteWallOps(user,false,"%s",parameters[0]);
}
void handle_list(char **parameters, int pcnt, userrec *user)
return c;
}
-int chancount(void)
+long chancount(void)
{
return chanlist.size();
}
-int servercount(void)
+long count_servs(void)
{
- return 1;
+ int c = 0;
+ //for (int j = 0; j < 255; j++)
+ //{
+ // if (servers[j] != NULL)
+ // c++;
+ //}
+ return c;
+}
+
+long servercount(void)
+{
+ return count_servs()+1;
+}
+
+long local_count()
+{
+ int c = 0;
+ for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
+ {
+ if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,ServerName))) c++;
+ }
+ return c;
}
+
void handle_lusers(char **parameters, int pcnt, userrec *user)
{
WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
- WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
+ WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),count_servs());
}
void handle_admin(char **parameters, int pcnt, userrec *user)
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"N %d %s %s %s %s +%s %s :%s",user->age,user->nick,user->host,user->dhost,user->ident,user->modes,ServerName,user->fullname);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent N token");
- }
- }
- }
-
+ NetSendToAll(buffer);
}
void handle_version(char **parameters, int pcnt, userrec *user)
}
}
+// calls a handler function for a command
+
+void call_handler(const char* commandname,char **parameters, int pcnt, userrec *user)
+{
+ for (int i = 0; i < cmdlist.size(); i++)
+ {
+ if (!strcasecmp(cmdlist[i].command,commandname))
+ {
+ if (cmdlist[i].handler_function)
+ {
+ if (pcnt>=cmdlist[i].min_params)
+ {
+ if (strchr(user->modes,cmdlist[i].flags_needed))
+ {
+ cmdlist[i].handler_function(parameters,pcnt,user);
+ }
+ }
+ }
+ }
+ }
+}
+
void handle_stats(char **parameters, int pcnt, userrec *user)
{
if (pcnt != 1)
for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
{
- ConfValue("link","name",i,Link_ServerName,&config_f);
- ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
- ConfValue("link","port",i,Link_Port,&config_f);
- ConfValue("link","sendpass",i,Link_Pass,&config_f);
- log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
- LinkPort = atoi(Link_Port);
- if (match(Link_ServerName,parameters[0])) {
- found = true;
+ if (!found)
+ {
+ ConfValue("link","name",i,Link_ServerName,&config_f);
+ ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
+ ConfValue("link","port",i,Link_Port,&config_f);
+ ConfValue("link","sendpass",i,Link_Pass,&config_f);
+ log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
+ LinkPort = atoi(Link_Port);
+ if (match(Link_ServerName,parameters[0])) {
+ found = true;
+ break;
+ }
}
}
if (me[defaultRoute])
{
+ me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass,Link_ServerName,me[defaultRoute]->port);
+ return;
+ }
+ else
+ {
+ WriteServ(user->fd,"NOTICE %s :No default route is defined for server connections on this server. You must define a server connection to be default route so that sockets can be bound to it.",user->nick);
+ }
+}
- // at this point parameters[0] is an ip in a string.
- // TODO: Look this up from the <link> blocks instead!
- for (int j = 0; j < 255; j++) {
- if (servers[j] == NULL) {
- servers[j] = new serverrec;
- strcpy(servers[j]->internal_addr,Link_IPAddr);
- servers[j]->internal_port = LinkPort;
- strcpy(servers[j]->name,Link_ServerName);
- log(DEBUG,"Allocated new serverrec");
- if (!me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass))
+void DoSplitEveryone()
+{
+ bool go_again = true;
+ while (go_again)
+ {
+ go_again = false;
+ for (int i = 0; i < 32; i++)
+ {
+ if (me[i] != NULL)
+ {
+ for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
{
- WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,Link_IPAddr);
+ if (strcasecmp(j->GetServerName().c_str(),ServerName))
+ {
+ j->routes.clear();
+ j->CloseConnection();
+ me[i]->connectors.erase(j);
+ go_again = true;
+ break;
+ }
}
- return;
}
}
- WriteServ(user->fd,"NOTICE %s :*** Failed to create server record for address %s!",user->nick,Link_IPAddr);
}
- else
+ log(DEBUG,"Removed server. Will remove clients...");
+ // iterate through the userlist and remove all users on this server.
+ // because we're dealing with a mesh, we dont have to deal with anything
+ // "down-route" from this server (nice huh)
+ go_again = true;
+ char reason[MAXBUF];
+ while (go_again)
{
- WriteServ(user->fd,"NOTICE %s :No default route is defined for server connections on this server. You must define a server connection to be default route so that sockets can be bound to it.",user->nick);
+ go_again = false;
+ for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
+ {
+ if (strcasecmp(u->second->server,ServerName))
+ {
+ snprintf(reason,MAXBUF,"%s %s",ServerName,u->second->server);
+ kill_link(u->second,reason);
+ go_again = true;
+ break;
+ }
+ }
}
}
+
+
void handle_squit(char **parameters, int pcnt, userrec *user)
{
// send out an squit across the mesh and then clear the server list (for local squit)
+ if (!pcnt)
+ {
+ WriteOpers("SQUIT command issued by %s",user->nick);
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"& %s",ServerName);
+ NetSendToAll(buffer);
+ DoSplitEveryone();
+ }
+ else
+ {
+ WriteServ(user->fd,"NOTICE :*** Remote SQUIT not supported yet.");
+ }
}
-char islast(serverrec* s)
+char islast(const char* s)
{
char c = '`';
- for (int j = 0; j < 255; j++)
+ for (int j = 0; j < 32; j++)
{
- if (servers[j] != NULL)
- {
- c = '|';
- }
- if (servers[j] == s)
- {
- c = '`';
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
+ {
+ c = '|';
+ }
+ if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
+ {
+ c = '`';
+ }
+ }
}
}
return c;
}
+long map_count(const char* s)
+{
+ int c = 0;
+ for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
+ {
+ if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
+ }
+ return c;
+}
+
void handle_links(char **parameters, int pcnt, userrec *user)
{
WriteServ(user->fd,"364 %s %s %s :0 %s",user->nick,ServerName,ServerName,ServerDesc);
- for (int j = 0; j < 255; j++)
+ for (int j = 0; j < 32; j++)
{
- if (servers[j] != NULL)
+ if (me[j] != NULL)
{
- WriteServ(user->fd,"364 %s %s %s :1 %s",user->nick,servers[j]->name,ServerName,servers[j]->description);
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ WriteServ(user->fd,"364 %s %s %s :1 %s",user->nick,me[j]->connectors[k].GetServerName().c_str(),ServerName,me[j]->connectors[k].GetDescription().c_str());
+ }
}
}
WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
void handle_map(char **parameters, int pcnt, userrec *user)
{
- WriteServ(user->fd,"006 %s :%s",user->nick,ServerName);
- for (int j = 0; j < 255; j++)
+ char line[MAXBUF];
+ snprintf(line,MAXBUF,"006 %s :%s",user->nick,ServerName);
+ while (strlen(line) < 50)
+ strcat(line," ");
+ WriteServ(user->fd,"%s%d (%.2f%%)",line,local_count(),(float)(((float)local_count()/(float)usercnt())*100));
+ for (int j = 0; j < 32; j++)
{
- if (servers[j] != NULL)
+ if (me[j] != NULL)
{
- WriteServ(user->fd,"006 %s :%c-%s",user->nick,islast(servers[j]),servers[j]->name);
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ snprintf(line,MAXBUF,"006 %s :%c-%s",user->nick,islast(me[j]->connectors[k].GetServerName().c_str()),me[j]->connectors[k].GetServerName().c_str());
+ while (strlen(line) < 50)
+ strcat(line," ");
+ WriteServ(user->fd,"%s%d (%.2f%%)",line,map_count(me[j]->connectors[k].GetServerName().c_str()),(float)(((float)map_count(me[j]->connectors[k].GetServerName().c_str())/(float)usercnt())*100));
+ }
}
}
WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
{
/* found this oper's opertype */
ConfValue("type","host",j,Hostname,&config_f);
- strncpy(user->dhost,Hostname,256);
+ ChangeDisplayedHost(user,Hostname);
}
}
if (!strchr(user->modes,'o'))
// Q token must go to ALL servers!!!
char buffer[MAXBUF];
snprintf(buffer,MAXBUF,"n %s %s",user->nick,parameters[0]);
- for (int j = 0; j < 255; j++)
- {
- if (servers[j] != NULL)
- {
- if (strcmp(servers[j]->name,ServerName))
- {
- me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
- log(DEBUG,"Sent n token");
- }
- }
- }
+ NetSendToAll(buffer);
}
strcpy(temp,cmd);
- string tmp = cmd;
- FOREACH_MOD OnServerRaw(tmp,true);
- const char* cmd2 = tmp.c_str();
- snprintf(cmd,512,"%s",cmd2);
+ std::string tmp = cmd;
+ for (int i = 0; i <= MODCOUNT; i++)
+ {
+ std::string oldtmp = tmp;
+ modules[i]->OnServerRaw(tmp,true);
+ if (oldtmp != tmp)
+ {
+ log(DEBUG,"A Module changed the input string!");
+ log(DEBUG,"New string: %s",tmp.c_str());
+ log(DEBUG,"Old string: %s",oldtmp.c_str());
+ break;
+ }
+ }
+ strncpy(cmd,tmp.c_str(),MAXBUF);
+ strcpy(temp,cmd);
if (!strchr(cmd,' '))
{
{
if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
{
- if (strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
+ if (!strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
{
kill_link(user,"Protocol violation (3)");
return;
createcommand("TRACE",handle_trace,'o',0);
createcommand("WHOWAS",handle_whowas,0,1);
createcommand("CONNECT",handle_connect,'o',1);
- createcommand("SQUIT",handle_squit,'o',1);
+ createcommand("SQUIT",handle_squit,'o',0);
createcommand("MODULES",handle_modules,'o',0);
createcommand("LINKS",handle_links,0,0);
createcommand("MAP",handle_map,0,0);
}
}
-void DoSync(serverrec* serv, char* udp_host,int udp_port, long MyKey)
+void DoSync(serverrec* serv, char* udp_host)
{
char data[MAXBUF];
// send start of sync marker: Y <timestamp>
// at this point the ircd receiving it starts broadcasting this netburst to all ircds
// except the ones its receiving it from.
snprintf(data,MAXBUF,"Y %d",time(NULL));
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
// send users and channels
for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
{
snprintf(data,MAXBUF,"N %d %s %s %s %s +%s %s :%s",u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,u->second->server,u->second->fullname);
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
if (strcmp(chlist(u->second),""))
{
snprintf(data,MAXBUF,"J %s %s",u->second->nick,chlist(u->second));
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
}
}
// send channel modes, topics etc...
for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
{
snprintf(data,MAXBUF,"M %s +%s",c->second->name,chanmodes(c->second));
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
if (strcmp(c->second->topic,""))
{
snprintf(data,MAXBUF,"T %d %s %s :%s",c->second->topicset,c->second->setby,c->second->name,c->second->topic);
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
}
// send current banlist
for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
{
snprintf(data,MAXBUF,"M %s +b %s",b->set_time,c->second->name,b->data);
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
}
}
// send end of sync marker: E <timestamp>
snprintf(data,MAXBUF,"F %d",time(NULL));
- serv->SendPacket(data,udp_host,udp_port,MyKey);
+ serv->SendPacket(data,udp_host);
// ircd sends its serverlist after the end of sync here
}
-void handle_V(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_V(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* src = strtok(params," ");
char* dest = strtok(NULL," :");
}
-void handle_P(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_P(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* src = strtok(params," ");
char* dest = strtok(NULL," :");
}
+void handle_i(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* nick = strtok(params," ");
+ char* from = strtok(NULL," ");
+ char* channel = strtok(NULL," ");
+ userrec* u = Find(nick);
+ userrec* user = Find(from);
+ chanrec* c = FindChan(channel);
+ if ((c) && (u) && (user))
+ {
+ u->InviteTo(c->name);
+ WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
+ }
+}
+
+void handle_t(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* setby = strtok(params," ");
+ char* channel = strtok(NULL," :");
+ char* topic = strtok(NULL,"\r\n");
+ topic++;
+ userrec* u = Find(setby);
+ chanrec* c = FindChan(channel);
+ if ((c) && (u))
+ {
+ WriteChannelLocal(c,u,"TOPIC %s :%s",c->name,topic);
+ strncpy(c->topic,topic,MAXTOPIC);
+ strncpy(c->setby,u->nick,NICKMAX);
+ }
+}
+
-void handle_T(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_T(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* tm = strtok(params," ");
char* setby = strtok(NULL," ");
}
}
-void handle_M(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_M(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* pars[128];
char original[MAXBUF],target[MAXBUF];
pars[index++] = parameter;
parameter = strtok(NULL," ");
}
- merge_mode(pars,--index);
+ merge_mode(pars,index);
if (FindChan(target))
{
WriteChannelLocal(FindChan(target), NULL, "MODE %s",original);
// m is modes set by users only (not servers) valid targets are channels or users.
-void handle_m(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_m(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
+ // m blah #chatspike +b *!test@*4
char* pars[128];
- char original[MAXBUF],target[MAXBUF];
+ char original[MAXBUF];
strncpy(original,params,MAXBUF);
+
+ if (!strchr(params,' '))
+ {
+ WriteOpers("WARNING! 'm' token in data stream without any parameters! Something fishy is going on!");
+ return;
+ }
+
int index = 0;
char* src = strtok(params," ");
if (user)
{
+ log(DEBUG,"Found user: %s",user->nick);
char* parameter = strtok(NULL," ");
- strncpy(target,parameter,MAXBUF);
while (parameter)
{
pars[index++] = parameter;
parameter = strtok(NULL," ");
}
- merge_mode2(pars,--index,user);
- if (FindChan(target))
- {
- WriteChannelLocal(FindChan(target), user, "MODE %s",original);
- }
- if (Find(target))
- {
- WriteTo(user,Find(target),"MODE %s",original);
- }
+
+ log(DEBUG,"Calling merge_mode2");
+ merge_mode2(pars,index,user);
}
}
-void handle_L(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_L(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* nick = strtok(params," ");
char* channel = strtok(NULL," :");
}
}
-void handle_Q(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_K(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* src = strtok(params," ");
+ char* nick = strtok(NULL," :");
+ char* reason = strtok(NULL,"\r\n");
+ char kreason[MAXBUF];
+ reason++;
+
+ userrec* u = Find(nick);
+ userrec* user = Find(src);
+
+ if ((user) && (u))
+ {
+ WriteTo(user, u, "KILL %s :%s!%s!%s!%s (%s)", u->nick, source->name, ServerName, user->dhost,user->nick,reason);
+ WriteOpers("*** Remote kill from %s by %s: %s!%s@%s (%s)",source->name,user->nick,u->nick,u->ident,u->host,reason);
+ snprintf(kreason,MAXBUF,"[%s] Killed (%s (%s))",source->name,user->nick,reason);
+ kill_link(u,kreason);
+ }
+}
+
+void handle_Q(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* nick = strtok(params," :");
char* reason = strtok(NULL,"\r\n");
}
}
-void handle_n(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_n(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* oldnick = strtok(params," ");
char* newnick = strtok(NULL," ");
}
}
-void handle_N(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+// k <SOURCE> <DEST> <CHANNEL> :<REASON>
+void handle_k(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* src = strtok(params," ");
+ char* dest = strtok(NULL," ");
+ char* channel = strtok(NULL," :");
+ char* reason = strtok(NULL,"\r\n");
+ reason++;
+ userrec* s = Find(src);
+ userrec* d = Find(dest);
+ chanrec* c = FindChan(channel);
+ if ((s) && (d) && (c))
+ {
+ kick_channel(s,d,c,reason);
+ }
+}
+
+void handle_AT(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* who = strtok(params," :");
+ char* text = strtok(NULL,"\r\n");
+ text++;
+ userrec* s = Find(who);
+ if (s)
+ {
+ WriteWallOps(s,true,text);
+ }
+}
+
+
+void handle_N(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
char* tm = strtok(params," ");
char* nick = strtok(NULL," ");
{
// nick collision
WriteOpers("Nickname collision: %s@%s != %s@%s",nick,server,iter->second->nick,iter->second->server);
- if (TS >= iter->second->age)
- {
- char str[MAXBUF];
- snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
- WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
- // client on remote server is older than the local user, kill the local user
- kill_link(iter->second,str);
- }
+ char str[MAXBUF];
+ snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
+ WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
+ kill_link(iter->second,str);
}
clientlist[nick] = new userrec();
// remote users have an fd of -1. This is so that our Write abstraction
strncpy(clientlist[nick]->host, host,160);
strncpy(clientlist[nick]->dhost, dhost,160);
strncpy(clientlist[nick]->server, server,256);
- strncpy(clientlist[nick]->ident, ident,10); // +1 char to compensate for '~'
+ strncpy(clientlist[nick]->ident, ident,10); // +1 char to compensate for tilde
strncpy(clientlist[nick]->fullname, gecos,128);
clientlist[nick]->signon = TS;
clientlist[nick]->nping = 0; // this is ignored for a remote user anyway.
}
}
-void handle_F(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_F(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
long tdiff = time(NULL) - atoi(params);
if (tdiff)
WriteOpers("TS split for %s -> %s: %d",source->name,reply->name,tdiff);
}
-void handle_J(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void handle_a(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* nick = strtok(params," :");
+ char* gecos = strtok(NULL,"\r\n");
+
+ userrec* user = Find(nick);
+
+ if (user)
+ strncpy(user->fullname,gecos,MAXBUF);
+}
+
+void handle_b(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* nick = strtok(params," :");
+ char* host = strtok(NULL,"\r\n");
+
+ userrec* user = Find(nick);
+
+ if (user)
+ strncpy(user->dhost,host,160);
+}
+
+void handle_plus(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ // %s %s %d %d
+ // + test3.chatspike.net 7010 -2016508415
+ char* servername = strtok(params," ");
+ char* ipaddr = strtok(NULL," ");
+ char* ipport = strtok(NULL," ");
+ char* cookie = strtok(NULL," ");
+ log(DEBUG,"*** Connecting back to %s:%d",ipaddr,atoi(ipport));
+
+
+ bool conn_already = false;
+ for (int i = 0; i < 32; i++)
+ {
+ if (me[i] != NULL)
+ {
+ for (int j = 0; j < me[i]->connectors.size(); j++)
+ {
+ if (!strcasecmp(me[i]->connectors[j].GetServerName().c_str(),servername))
+ {
+ if (me[i]->connectors[j].GetServerPort() == atoi(ipport))
+ {
+ log(DEBUG,"Already got a connection to %s:%d, ignoring +",ipaddr,atoi(ipport));
+ conn_already = true;
+ }
+ }
+ }
+ }
+ }
+ if (!conn_already)
+ me[defaultRoute]->MeshCookie(ipaddr,atoi(ipport),atoi(cookie),servername);
+}
+
+void handle_R(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ char* server = strtok(params," ");
+ char* data = strtok(NULL,"\r\n");
+ log(DEBUG,"Forwarded packet '%s' to '%s'",data,server);
+ NetSendToOne(server,data);
+}
+
+void handle_J(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
// IMPORTANT NOTE
// The J token currently has no timestamp - this needs looking at
if (privilage == '+')
{
user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_VOICE;
- WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +o %s",channel,user->nick);
+ WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +v %s",channel,user->nick);
}
}
}
}
}
-void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
+void NetSendMyRoutingTable()
+{
+ // send out a line saying what is reachable to us.
+ // E.g. if A is linked to B C and D, send out:
+ // $ A B C D
+ // if its only linked to B and D send out:
+ // $ A B D
+ // if it has no links, dont even send out the line at all.
+ char buffer[MAXBUF];
+ sprintf(buffer,"$ %s",ServerName);
+ bool sendit = false;
+ for (int i = 0; i < 32; i++)
+ {
+ if (me[i] != NULL)
+ {
+ for (int j = 0; j < me[i]->connectors.size(); j++)
+ {
+ if (me[i]->connectors[j].GetState() != STATE_DISCONNECTED)
+ {
+ strncat(buffer," ",MAXBUF);
+ strncat(buffer,me[i]->connectors[j].GetServerName().c_str(),MAXBUF);
+ sendit = true;
+ }
+ }
+ }
+ }
+ if (sendit)
+ NetSendToAll(buffer);
+}
+
+void handle_dollar(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
{
+ log(DEBUG,"Storing routing table...");
+ char* sourceserver = strtok(params," ");
+ char* server = strtok(NULL," ");
+ for (int i = 0; i < 32; i++)
+ {
+ if (me[i] != NULL)
+ {
+ for (int j = 0; j < me[i]->connectors.size(); j++)
+ {
+ if (!strcasecmp(me[i]->connectors[j].GetServerName().c_str(),sourceserver))
+ {
+ me[i]->connectors[j].routes.clear();
+ log(DEBUG,"Found entry for source server.");
+ while (server)
+ {
+ // store each route
+ me[i]->connectors[j].routes.push_back(server);
+ log(DEBUG,"*** Stored route: %s -> %s -> %s",ServerName,sourceserver,server);
+ server = strtok(NULL," ");
+ }
+ return;
+ }
+ }
+ }
+ }
+ log(DEBUG,"Warning! routing table received from nonexistent server!");
+}
+
+
+void DoSplit(const char* params)
+{
+ bool go_again = true;
+ while (go_again)
+ {
+ go_again = false;
+ for (int i = 0; i < 32; i++)
+ {
+ if (me[i] != NULL)
+ {
+ for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
+ {
+ if (!strcasecmp(j->GetServerName().c_str(),params))
+ {
+ j->routes.clear();
+ j->CloseConnection();
+ me[i]->connectors.erase(j);
+ go_again = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+ log(DEBUG,"Removed server. Will remove clients...");
+ // iterate through the userlist and remove all users on this server.
+ // because we're dealing with a mesh, we dont have to deal with anything
+ // "down-route" from this server (nice huh)
+ go_again = true;
+ char reason[MAXBUF];
+ snprintf(reason,MAXBUF,"%s %s",ServerName,params);
+ while (go_again)
+ {
+ go_again = false;
+ for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
+ {
+ if (!strcasecmp(u->second->server,params))
+ {
+ kill_link(u->second,reason);
+ go_again = true;
+ break;
+ }
+ }
+ }
+}
+
+void handle_amp(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+ log(DEBUG,"Netsplit! %s split from mesh, removing!",params);
+ bool go_again = true;
+ while (go_again)
+ {
+ go_again = false;
+ for (int i = 0; i < 32; i++)
+ {
+ if (me[i] != NULL)
+ {
+ for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
+ {
+ if (!strcasecmp(j->GetServerName().c_str(),params))
+ {
+ j->routes.clear();
+ j->CloseConnection();
+ me[i]->connectors.erase(j);
+ go_again = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+ log(DEBUG,"Removed server. Will remove clients...");
+ // iterate through the userlist and remove all users on this server.
+ // because we're dealing with a mesh, we dont have to deal with anything
+ // "down-route" from this server (nice huh)
+ go_again = true;
+ char reason[MAXBUF];
+ snprintf(reason,MAXBUF,"%s %s",ServerName,params);
+ while (go_again)
+ {
+ go_again = false;
+ for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
+ {
+ if (!strcasecmp(u->second->server,params))
+ {
+ kill_link(u->second,reason);
+ go_again = true;
+ break;
+ }
+ }
+ }
+}
+
+long authcookie;
+
+
+void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,char* ipaddr,int port)
+{
+ char buffer[MAXBUF];
+
switch(token)
{
// Y <TS>
// start netburst
case 'Y':
nb_start = time(NULL);
- WriteOpers("Server %s is starting netburst.",source->name);
+ WriteOpers("Server %s is starting netburst.",udp_host);
+ // now broadcast this new servers address out to all servers that are linked to us,
+ // except the newcomer. They'll all attempt to connect back to it.
+ authcookie = rand()*rand();
+ snprintf(buffer,MAXBUF,"~ %d",authcookie);
+ NetSendToAll(buffer);
+ break;
+ // ~
+ // Store authcookie
+ // once stored, this authcookie permits other servers to log in
+ // without user or password, using it.
+ case '~':
+ auth_cookies.push_back(atoi(params));
+ log(DEBUG,"*** Stored auth cookie, will permit servers with auth-cookie %d",atoi(params));
+ break;
+ // connect back to a server using an authcookie
+ case '+':
+ handle_plus(token,params,source,reply,udp_host);
+ break;
+ // routing table
+ case '$':
+ handle_dollar(token,params,source,reply,udp_host);
+ break;
+ // node unreachable - we cant route to a server, sooooo we slit it off.
+ // servers can generate these for themselves for an squit.
+ case '&':
+ handle_amp(token,params,source,reply,udp_host);
+ break;
+ // R <server> <data>
+ // redirect token, send all of <data> along to the given
+ // server as this server has been found to still have a route to it
+ case 'R':
+ handle_R(token,params,source,reply,udp_host);
+ break;
+ // ?
+ // ping
+ case '?':
+ reply->SendPacket("!",udp_host);
+ break;
+ // ?
+ // pong
+ case '!':
+ break;
+ // *
+ // no operation
+ case '*':
break;
// N <TS> <NICK> <HOST> <DHOST> <IDENT> <MODES> <SERVER> :<GECOS>
// introduce remote client
case 'N':
- handle_N(token,params,source,reply,udp_host,udp_port);
+ handle_N(token,params,source,reply,udp_host);
+ break;
+ // a <NICK> :<GECOS>
+ // change GECOS (SETNAME)
+ case 'a':
+ handle_a(token,params,source,reply,udp_host);
+ break;
+ // b <NICK> :<HOST>
+ // change displayed host (SETHOST)
+ case 'b':
+ handle_b(token,params,source,reply,udp_host);
+ break;
+ // t <NICK> <CHANNEL> :<TOPIC>
+ // change a channel topic
+ case 't':
+ handle_t(token,params,source,reply,udp_host);
+ break;
+ // i <NICK> <CHANNEL>
+ // invite a user to a channel
+ case 'i':
+ handle_i(token,params,source,reply,udp_host);
+ break;
+ // k <SOURCE> <DEST> <CHANNEL> :<REASON>
+ // kick a user from a channel
+ case 'k':
+ handle_k(token,params,source,reply,udp_host);
break;
// n <NICK> <NEWNICK>
// change nickname of client -- a server should only be able to
// change the nicknames of clients that reside on it unless
// they are ulined.
case 'n':
- handle_n(token,params,source,reply,udp_host,udp_port);
+ handle_n(token,params,source,reply,udp_host);
break;
// J <NICK> <CHANLIST>
// Join user to channel list, merge channel permissions
case 'J':
- handle_J(token,params,source,reply,udp_host,udp_port);
+ handle_J(token,params,source,reply,udp_host);
break;
// T <TS> <CHANNEL> <TOPICSETTER> :<TOPIC>
// change channel topic (netburst only)
case 'T':
- handle_T(token,params,source,reply,udp_host,udp_port);
+ handle_T(token,params,source,reply,udp_host);
break;
- // M <TS> <TARGET> <MODES> [MODE-PARAMETERS]
- // Set modes on an object
+ // M <TARGET> <MODES> [MODE-PARAMETERS]
+ // Server setting modes on an object
case 'M':
- handle_M(token,params,source,reply,udp_host,udp_port);
+ handle_M(token,params,source,reply,udp_host);
+ break;
+ // m <SOURCE> <TARGET> <MODES> [MODE-PARAMETERS]
+ // User setting modes on an object
+ case 'm':
+ handle_m(token,params,source,reply,udp_host);
break;
// P <SOURCE> <TARGET> :<TEXT>
// Send a private/channel message
case 'P':
- handle_P(token,params,source,reply,udp_host,udp_port);
+ handle_P(token,params,source,reply,udp_host);
break;
// V <SOURCE> <TARGET> :<TEXT>
// Send a private/channel notice
case 'V':
- handle_V(token,params,source,reply,udp_host,udp_port);
+ handle_V(token,params,source,reply,udp_host);
break;
// L <SOURCE> <CHANNEL> :<REASON>
// User parting a channel
case 'L':
- handle_L(token,params,source,reply,udp_host,udp_port);
+ handle_L(token,params,source,reply,udp_host);
break;
// Q <SOURCE> :<REASON>
// user quitting
case 'Q':
- handle_Q(token,params,source,reply,udp_host,udp_port);
+ handle_Q(token,params,source,reply,udp_host);
+ break;
+ // K <SOURCE> <DEST> :<REASON>
+ // remote kill
+ case 'K':
+ handle_K(token,params,source,reply,udp_host);
+ break;
+ // @ <SOURCE> :<TEXT>
+ // wallops
+ case '@':
+ handle_AT(token,params,source,reply,udp_host);
break;
// F <TS>
// end netburst
case 'F':
- WriteOpers("Server %s has completed netburst. (%d secs)",source->name,time(NULL)-nb_start);
- handle_F(token,params,source,reply,udp_host,udp_port);
+ WriteOpers("Server %s has completed netburst. (%d secs)",udp_host,time(NULL)-nb_start);
+ handle_F(token,params,source,reply,udp_host);
+ nb_start = 0;
+ // tell all the other servers to use this authcookie to connect back again
+ // got '+ test3.chatspike.net 7010 -2016508415' from test.chatspike.net
+ snprintf(buffer,MAXBUF,"+ %s %s %d %d",udp_host,ipaddr,port,authcookie);
+ NetSendToAllExcept(udp_host,buffer);
+ break;
+ // X <reserved>
+ // Send netburst now
+ case 'X':
+ WriteOpers("Sending my netburst to %s",udp_host);
+ DoSync(source,udp_host);
+ WriteOpers("Send of netburst to %s completed",udp_host);
+ NetSendMyRoutingTable();
break;
// anything else
default:
}
-void handle_link_packet(long theirkey, char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
+void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
{
char response[10240];
char token = udp_msg[0];
char* params = udp_msg + 2;
char finalparam[1024];
strcpy(finalparam," :xxxx");
- if (strstr(params," :")) {
- strncpy(finalparam,strstr(params," :"),1024);
+ if (strstr(udp_msg," :")) {
+ strncpy(finalparam,strstr(udp_msg," :"),1024);
}
+ if (token == '-') {
+ char* cookie = strtok(params," ");
+ char* servername = strtok(NULL," ");
+ char* serverdesc = finalparam+2;
+
+ WriteOpers("AuthCookie CONNECT from %s (%s)",servername,udp_host);
+
+ for (int u = 0; u < auth_cookies.size(); u++)
+ {
+ if (auth_cookies[u] == atoi(cookie))
+ {
+ WriteOpers("Allowed cookie from %s, is now part of the mesh",servername);
+
+
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),udp_host))
+ {
+ me[j]->connectors[k].SetServerName(servername);
+ me[j]->connectors[k].SetDescription(serverdesc);
+ me[j]->connectors[k].SetState(STATE_CONNECTED);
+ NetSendMyRoutingTable();
+ return;
+ }
+ }
+ }
+ WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right now! Possible intrusion attempt!",udp_host);
+ return;
+ }
+
+
+ return;
+ }
+ }
+ // bad cookie, bad bad! go sit in the corner!
+ WriteOpers("Bad cookie from %s!",servername);
+ return;
+ }
+ else
if (token == 'S') {
- // S test.chatspike.net password :ChatSpike InspIRCd test server
+ // S test.chatspike.net password portn :ChatSpike InspIRCd test server
char* servername = strtok(params," ");
char* password = strtok(NULL," ");
+ char* myport = strtok(NULL," ");
char* serverdesc = finalparam+2;
- WriteOpers("CONNECT from %s (%s)",servername,udp_host);
+ WriteOpers("CONNECT from %s (%s) (their port: %d)",servername,udp_host,atoi(myport));
+
+ for (int j = 0; j < serv->connectors.size(); j++)
+ {
+ if (!strcasecmp(serv->connectors[j].GetServerName().c_str(),udp_host))
+ {
+ serv->connectors[j].SetServerName(servername);
+ serv->connectors[j].SetDescription(serverdesc);
+ serv->connectors[j].SetServerPort(atoi(myport));
+ }
+ }
char Link_ServerName[1024];
ConfValue("link","sendpass",i,Link_SendPass,&config_f);
log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
LinkPort = atoi(Link_Port);
- if (!strcasecmp(Link_ServerName,servername)) {
- if (!strcasecmp(Link_IPAddr,udp_host)) {
- if (LinkPort == udp_port) {
- // we have a matching link line -
- // send a 'diminutive' server message back...
- snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
- serv->SendPacket(response,udp_host,udp_port,0);
- WriteOpers("CONNECT from %s accepted, authenticating",servername);
- for (int j = 0; j < 255; j++) {
- if (servers[j] == NULL) {
- servers[j] = new serverrec;
- strcpy(servers[j]->internal_addr,udp_host);
- strcpy(servers[j]->name,servername);
- strcpy(servers[j]->description,serverdesc);
- servers[j]->internal_port = udp_port;
- // create a server record for this server
- snprintf(response,10240,"O %d",MyKey);
- serv->SendPacket(response,udp_host,udp_port,0);
- return;
- }
- }
- WriteOpers("Internal error connecting to %s, failed to create server record!",servername);
- return;
- }
- else {
- log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
+ if (!strcasecmp(Link_ServerName,servername))
+ {
+ // we have a matching link line -
+ // send a 'diminutive' server message back...
+ snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
+ serv->SendPacket(response,servername);
+
+ for (int t = 0; t < serv->connectors.size(); t++)
+ {
+ if (!strcasecmp(serv->connectors[t].GetServerName().c_str(),servername))
+ {
+ serv->connectors[t].SetState(STATE_CONNECTED);
}
}
- else {
- log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
- }
- }
- else {
- log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
+
+ return;
}
}
- serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
+ char buffer[MAXBUF];
+ sprintf(buffer,"E :Access is denied (no matching link block)");
+ serv->SendPacket(buffer,udp_host);
WriteOpers("CONNECT from %s denied, no matching link block",servername);
return;
}
else
- if (token == 'O') {
- // if this is received, this means the server-ip that sent it said "OK" to credentials.
- // only when a server says this do we exchange keys. The server MUST have an entry in the servers
- // array, which is only added by an 'S' packet or BeginLink().
- for (int i = 0; i < 255; i++) {
- if (servers[i] != NULL) {
- if (!strcasecmp(servers[i]->internal_addr,udp_host)) {
- servers[i]->key = atoi(params);
- log(DEBUG,"Key for this server is now %d",servers[i]->key);
- DoSync(serv,udp_host,udp_port,MyKey);
- return;
- }
- }
- }
- WriteOpers("\2WARNING!\2 Server ip %s attempted a key exchange, but is not in the authentication state! Possible intrusion attempt!",udp_host);
- }
- else
if (token == 's') {
// S test.chatspike.net password :ChatSpike InspIRCd test server
char* servername = strtok(params," ");
// to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
// a connect, so put out an oper alert!
-
-
-
// for now, just accept all, we'll fix that later.
WriteOpers("%s accepted our link credentials ",servername);
ConfValue("link","sendpass",i,Link_SendPass,&config_f);
log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
LinkPort = atoi(Link_Port);
- if (!strcasecmp(Link_ServerName,servername)) {
- if (!strcasecmp(Link_IPAddr,udp_host)) {
- if (LinkPort == udp_port) {
- // matching link at this end too, we're all done!
- // at this point we must begin key exchange and insert this
- // server into our 'active' table.
- for (int j = 0; j < 255; j++) {
- if (servers[j] != NULL) {
- if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
- strcpy(servers[j]->description,serverdesc);
- WriteOpers("Server %s authenticated, exchanging server keys...",servername);
- snprintf(response,10240,"O %d",MyKey);
- serv->SendPacket(response,udp_host,udp_port,0);
- return;
- }
+ if (!strcasecmp(Link_ServerName,servername))
+ {
+ // matching link at this end too, we're all done!
+ // at this point we must begin key exchange and insert this
+ // server into our 'active' table.
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int k = 0; k < me[j]->connectors.size(); k++)
+ {
+ if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),udp_host))
+ {
+ char buffer[MAXBUF];
+ me[j]->connectors[k].SetDescription(serverdesc);
+ me[j]->connectors[k].SetState(STATE_CONNECTED);
+ sprintf(buffer,"X 0");
+ serv->SendPacket(buffer,udp_host);
+ DoSync(me[j],udp_host);
+ NetSendMyRoutingTable();
+ return;
}
}
- WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
- return;
-
- }
- else {
- log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
}
- }
- else {
- log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
+ WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
+ return;
}
}
else {
log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
}
}
- serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
+ char buffer[MAXBUF];
+ sprintf(buffer,"E :Access is denied (no matching link block)");
+ serv->SendPacket(buffer,udp_host);
WriteOpers("CONNECT from %s denied, no matching link block",servername);
return;
}
char* error_message = finalparam+2;
WriteOpers("ERROR from %s: %s",udp_host,error_message);
// remove this server from any lists
- for (int j = 0; j < 255; j++) {
- if (servers[j] != NULL) {
- if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
- delete servers[j];
- return;
- }
- }
- }
return;
}
else {
serverrec* source_server = NULL;
- for (int j = 0; j < 255; j++) {
- if (servers[j] != NULL) {
- if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
- if (servers[j]->key == theirkey) {
- // found a valid key for this server, can process restricted stuff here
- process_restricted_commands(token,params,servers[j],serv,udp_host,udp_port);
- return;
+ for (int j = 0; j < 32; j++)
+ {
+ if (me[j] != NULL)
+ {
+ for (int x = 0; x < me[j]->connectors.size(); x++)
+ {
+ log(DEBUG,"Servers are: '%s' '%s'",udp_host,me[j]->connectors[x].GetServerName().c_str());
+ if (!strcasecmp(me[j]->connectors[x].GetServerName().c_str(),udp_host))
+ {
+ if (me[j]->connectors[x].GetState() == STATE_CONNECTED)
+ {
+ // found a valid ircd_connector.
+ process_restricted_commands(token,params,me[j],serv,udp_host,me[j]->connectors[x].GetServerIP(),me[j]->connectors[x].GetServerPort());
+ return;
+ }
}
}
}
}
- log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s:%d: %c",udp_host,udp_port,token);
+ log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s: %c",udp_host,token);
}
}
return (ERROR);
}
+
length = sizeof (client);
- int flip_flop = 0, udp_port = 0;
char udp_msg[MAXBUF], udp_host[MAXBUF];
/* main loop, this never returns */
for (;;)
{
+#ifdef _POSIX_PRIORITY_SCHEDULING
+ sched_yield();
+#endif
fd_set sfd;
- struct timeval tval;
+ timeval tval;
FD_ZERO(&sfd);
user_hash::iterator count2 = clientlist.begin();
// *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
// them in a list, then reap the list every second or so.
- if (reap_counter>5000)
+ if (reap_counter>300)
{
if (fd_reap.size() > 0)
{
fd_reap.clear();
reap_counter=0;
}
+ reap_counter++;
-
+ fd_set serverfds;
+ FD_ZERO(&serverfds);
+ timeval tvs;
+
for (int x = 0; x != UDPportCount; x++)
{
- long theirkey = 0;
- if (me[x]->RecvPacket(udp_msg, udp_host, udp_port, theirkey))
+ FD_SET(me[x]->fd, &serverfds);
+ }
+
+ tvs.tv_usec = 0;
+ tvs.tv_sec = 0;
+
+ int servresult = select(32767, &serverfds, NULL, NULL, &tvs);
+ if (servresult > 0)
+ {
+ for (int x = 0; x != UDPportCount; x++)
{
- if (strlen(udp_msg)<1) {
- log(DEBUG,"Invalid datagram from %s:%d:%d [route%d]",udp_host,udp_port,me[x]->port,x);
+ if (FD_ISSET (me[x]->fd, &serverfds))
+ {
+ char remotehost[MAXBUF],resolved[MAXBUF];
+ length = sizeof (client);
+ incomingSockfd = accept (me[x]->fd, (sockaddr *) &client, &length);
+ strncpy(remotehost,(char *)inet_ntoa(client.sin_addr),MAXBUF);
+ if(CleanAndResolve(resolved, remotehost) != TRUE)
+ {
+ strncpy(resolved,remotehost,MAXBUF);
+ }
+ // add to this connections ircd_connector vector
+ // *FIX* - we need the LOCAL port not the remote port in &client!
+ me[x]->AddIncoming(incomingSockfd,resolved,me[x]->port);
+ }
}
- else
+ }
+
+ for (int x = 0; x < UDPportCount; x++)
+ {
+ std::deque<std::string> msgs;
+ msgs.clear();
+ if (me[x]->RecvPacket(msgs, udp_host))
{
- FOREACH_MOD OnPacketReceive(udp_msg);
- // Packets must go back via the route they arrived on :)
- handle_link_packet(theirkey, udp_msg, udp_host, udp_port, me[x]);
- // link packets can manipulate the usertable so beware of
- // any loops here watching the user or channels hash
- log(DEBUG,"Sync: exit 3");
+ for (int ctr = 0; ctr < msgs.size(); ctr++)
+ {
+ char udp_msg[MAXBUF];
+ strncpy(udp_msg,msgs[ctr].c_str(),MAXBUF);
+ if (strlen(udp_msg)<1)
+ {
+ log(DEBUG,"Invalid string from %s [route%d]",udp_host,x);
+ break;
+ }
+ // during a netburst, send all data to all other linked servers
+ if ((nb_start>0) && (udp_msg[0] != 'Y') && (udp_msg[0] != 'X') && (udp_msg[0] != 'F'))
+ {
+ NetSendToAllExcept(udp_host,udp_msg);
+ }
+ FOREACH_MOD OnPacketReceive(udp_msg);
+ handle_link_packet(udp_msg, udp_host, me[x]);
+ }
goto label;
}
}
- }
while (count2 != clientlist.end())
//if (selectResult2 > 0)
for (user_hash::iterator count2a = xcount; count2a != endingiter; count2a++)
{
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+ sched_yield();
+#endif
+
result = EAGAIN;
if ((count2a->second->fd != -1) && (FD_ISSET (count2a->second->fd, &sfd)))
{
if (result)
{
- log(DEBUG,"Read %d characters from socket",result);
+ if (result > 0)
+ log(DEBUG,"Read %d characters from socket",result);
userrec* current = count2a->second;
int currfd = current->fd;
char* l = strtok(data,"\n");
FD_SET (openSockfd[count], &selectFds);
}
- /* added timeout! select was waiting forever... wank... :/ */
- tv.tv_usec = 0;
-
- flip_flop++;
- reap_counter++;
- if (flip_flop > 20)
- {
- tv.tv_usec = 1;
- flip_flop = 0;
- }
-
- tv.tv_sec = 0;
+ tv.tv_usec = 1;
selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
/* select is reporting a waiting socket. Poll them all to find out which */
{
if (FD_ISSET (openSockfd[count], &selectFds))
{
+ length = sizeof (client);
incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
address_cache::iterator iter = IP.find(client.sin_addr);