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 * ---------------------------------------------------
18 #include <sys/resource.h>
21 #include <sys/types.h>
24 #include "inspircd_io.h"
25 #include "inspircd_util.h"
27 extern FILE *log_file;
29 void WriteOpers(char* text, ...);
31 void Exit (int status)
35 send_error("Server shutdown.");
39 void Killed(int status)
43 send_error("Server terminated.");
47 void Rehash(int status)
50 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
57 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
58 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
59 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug, RD\n");
60 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug, w00t\n");
61 printf("\033[1;37mTesters:\033[0;37m typobox43, piggles, Lord_Zathras, CC\n");
62 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
66 void DeadPipe(int status)
68 signal (SIGPIPE, DeadPipe);
74 signal (SIGALRM, SIG_IGN);
75 signal (SIGHUP, Rehash);
76 signal (SIGPIPE, DeadPipe);
77 signal (SIGTERM, Exit);
78 signal (SIGABRT, Exit);
79 signal (SIGSEGV, Error);
80 signal (SIGURG, Exit);
81 signal (SIGKILL, Exit);
82 if ((childpid = fork ()) < 0)
84 else if (childpid > 0)
88 /* close stdout, stdin, stderr */
94 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
101 /* Make Sure Modules Are Avaliable!
102 * (BugFix By Craig.. See? I do work! :p) */
103 int FileExists (char* file)
107 if ((input = fopen (file, "r")) == NULL) { return(false); }
108 else { fclose (input); return(true); }
112 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
114 int EnumConf(const char* filename, const char* tag)
118 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
119 int in_token, in_quotes, tptr, j, idx = 0;
122 if ((config = fopen (filename, "r")) == NULL)
132 while (!feof(config))
136 if ((c == '#') && (lastc == '\n'))
138 while ((c != '\n') && (!feof(config)))
144 if ((c == '<') && (!in_quotes))
159 in_quotes = (!in_quotes);
161 if ((c == '>') && (!in_quotes))
164 if (!strcmp(c_tag,tag))
166 /* correct tag, but wrong index */
176 if ((in_token) && (c != '\n') && (c != '\r'))
190 int ConfValueEnum(char* tag)
192 EnumConf(CONFIG_FILE,tag);
197 /* Retrieves a value from the config file. If there is more than one value of the specified
198 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
200 * ConfValue("oper","name",2,result);
203 int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result)
207 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
208 int in_token, in_quotes, tptr, j, idx = 0;
211 if ((config = fopen (filename, "r")) == NULL)
221 while (!feof(config))
225 if ((c == '#') && (lastc == '\n'))
227 while ((c != '\n') && (!feof(config)))
233 if ((c == '<') && (!in_quotes))
248 in_quotes = (!in_quotes);
250 if ((c == '>') && (!in_quotes))
255 if (!strcmp(c_tag,tag))
257 if ((buffer) && (c_tag) && (var))
259 key = strstr(buffer,var);
262 /* value not found in tag */
279 for (j = 0; j < strlen(key); j++)
293 if (!strcmp(c_tag,tag))
295 /* correct tag, but wrong index */
305 if ((in_token) && (c != '\n') && (c != '\r'))
319 int ConfValue(char* tag, char* var, int index, char *result)
321 ReadConf(CONFIG_FILE, tag, var, index, result);
326 /* This will bind a socket to a port. It works for UDP/TCP */
327 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
329 bzero((char *)&server,sizeof(server));
331 inet_aton(addr,&addy);
333 server.sin_family = AF_INET;
334 if (!strcmp(addr,""))
336 server.sin_addr.s_addr = htonl(INADDR_ANY);
340 server.sin_addr = addy;
343 server.sin_port = htons(port);
345 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
357 /* Open a TCP Socket */
358 int OpenTCPSocket (void)
362 struct linger linger = { 0 };
364 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
368 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
369 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
372 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));