1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include <sys/resource.h>
18 #include <sys/types.h>
21 #include "inspircd_io.h"
22 #include "inspircd_util.h"
24 void WriteOpers(char* text, ...);
26 void Exit (int status)
28 send_error("Server shutdown.");
32 void Killed(int status)
34 send_error("Server terminated.");
38 void Rehash(int status)
41 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
48 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
49 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
50 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug, Raider, RD\n");
51 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug\n");
52 printf("\033[1;37mTesters:\033[0;37m MrBOFH, piggles, Lord_Zathras, typobox43, CC\n");
53 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
57 void DeadPipe(int status)
59 signal (SIGPIPE, DeadPipe);
65 signal (SIGALRM, SIG_IGN);
66 signal (SIGHUP, Rehash);
67 signal (SIGPIPE, DeadPipe);
68 signal (SIGTERM, Exit);
69 signal (SIGABRT, Exit);
70 signal (SIGSEGV, Error);
71 signal (SIGURG, Exit);
72 signal (SIGKILL, Exit);
73 if ((childpid = fork ()) < 0)
75 else if (childpid > 0)
79 /* close stdout, stdin, stderr */
83 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
88 /* Make Sure Modules Are Avaliable!
89 * (BugFix By Craig.. See? I do work! :p) */
90 int CheckModule (char* module)
94 if ((input = fopen (module, "r")) == NULL) { return(FALSE); }
95 else { fclose (input); return(TRUE); }
98 /* Make sure the config file is available */
99 int CheckConfig (void)
103 if ((input = fopen (CONFIG_FILE, "r")) == NULL)
105 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
114 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
116 int EnumConf(const char* filename, const char* tag)
120 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
121 int in_token, in_quotes, tptr, j, idx = 0;
124 if ((config = fopen (filename, "r")) == NULL)
134 while (!feof(config))
138 if ((c == '#') && (lastc == '\n'))
140 while ((c != '\n') && (!feof(config)))
146 if ((c == '<') && (!in_quotes))
161 in_quotes = (!in_quotes);
163 if ((c == '>') && (!in_quotes))
166 if (!strcmp(c_tag,tag))
168 /* correct tag, but wrong index */
178 if ((in_token) && (c != '\n') && (c != '\r'))
192 int ConfValueEnum(char* tag)
194 EnumConf(CONFIG_FILE,tag);
199 /* Retrieves a value from the config file. If there is more than one value of the specified
200 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
202 * ConfValue("oper","name",2,result);
205 int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result)
209 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
210 int in_token, in_quotes, tptr, j, idx = 0;
213 if ((config = fopen (filename, "r")) == NULL)
223 while (!feof(config))
227 if ((c == '#') && (lastc == '\n'))
229 while ((c != '\n') && (!feof(config)))
235 if ((c == '<') && (!in_quotes))
250 in_quotes = (!in_quotes);
252 if ((c == '>') && (!in_quotes))
257 if (!strcmp(c_tag,tag))
259 if ((buffer) && (c_tag) && (var))
261 key = strstr(buffer,var);
264 /* value not found in tag */
281 for (j = 0; j < strlen(key); j++)
295 if (!strcmp(c_tag,tag))
297 /* correct tag, but wrong index */
307 if ((in_token) && (c != '\n') && (c != '\r'))
321 int ConfValue(char* tag, char* var, int index, char *result)
323 ReadConf(CONFIG_FILE, tag, var, index, result);
328 /* This will bind a socket to a port. It works for UDP/TCP */
329 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
331 bzero((char *)&server,sizeof(server));
333 inet_aton(addr,&addy);
335 server.sin_family = AF_INET;
336 if (!strcmp(addr,""))
338 server.sin_addr.s_addr = htonl(INADDR_ANY);
342 server.sin_addr = addy;
345 server.sin_port = htons(port);
347 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
359 /* Open a TCP Socket */
360 int OpenTCPSocket (void)
364 struct linger linger = { 0 };
366 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
370 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
371 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
374 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));