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 void WriteOpers(char* text, ...);
29 void Exit (int status)
31 send_error("Server shutdown.");
35 void Killed(int status)
37 send_error("Server terminated.");
41 void Rehash(int status)
44 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
51 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
52 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
53 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug, Raider, RD\n");
54 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug\n");
55 printf("\033[1;37mTesters:\033[0;37m MrBOFH, piggles, Lord_Zathras, typobox43, CC\n");
56 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
60 void DeadPipe(int status)
62 signal (SIGPIPE, DeadPipe);
68 signal (SIGALRM, SIG_IGN);
69 signal (SIGHUP, Rehash);
70 signal (SIGPIPE, DeadPipe);
71 signal (SIGTERM, Exit);
72 signal (SIGABRT, Exit);
73 signal (SIGSEGV, Error);
74 signal (SIGURG, Exit);
75 signal (SIGKILL, Exit);
76 if ((childpid = fork ()) < 0)
78 else if (childpid > 0)
82 /* close stdout, stdin, stderr */
88 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
95 /* Make Sure Modules Are Avaliable!
96 * (BugFix By Craig.. See? I do work! :p) */
97 int FileExists (char* file)
101 if ((input = fopen (file, "r")) == NULL) { return(false); }
102 else { fclose (input); return(true); }
106 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
108 int EnumConf(const char* filename, const char* tag)
112 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
113 int in_token, in_quotes, tptr, j, idx = 0;
116 if ((config = fopen (filename, "r")) == NULL)
126 while (!feof(config))
130 if ((c == '#') && (lastc == '\n'))
132 while ((c != '\n') && (!feof(config)))
138 if ((c == '<') && (!in_quotes))
153 in_quotes = (!in_quotes);
155 if ((c == '>') && (!in_quotes))
158 if (!strcmp(c_tag,tag))
160 /* correct tag, but wrong index */
170 if ((in_token) && (c != '\n') && (c != '\r'))
184 int ConfValueEnum(char* tag)
186 EnumConf(CONFIG_FILE,tag);
191 /* Retrieves a value from the config file. If there is more than one value of the specified
192 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
194 * ConfValue("oper","name",2,result);
197 int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result)
201 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
202 int in_token, in_quotes, tptr, j, idx = 0;
205 if ((config = fopen (filename, "r")) == NULL)
215 while (!feof(config))
219 if ((c == '#') && (lastc == '\n'))
221 while ((c != '\n') && (!feof(config)))
227 if ((c == '<') && (!in_quotes))
242 in_quotes = (!in_quotes);
244 if ((c == '>') && (!in_quotes))
249 if (!strcmp(c_tag,tag))
251 if ((buffer) && (c_tag) && (var))
253 key = strstr(buffer,var);
256 /* value not found in tag */
273 for (j = 0; j < strlen(key); j++)
287 if (!strcmp(c_tag,tag))
289 /* correct tag, but wrong index */
299 if ((in_token) && (c != '\n') && (c != '\r'))
313 int ConfValue(char* tag, char* var, int index, char *result)
315 ReadConf(CONFIG_FILE, tag, var, index, result);
320 /* This will bind a socket to a port. It works for UDP/TCP */
321 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
323 bzero((char *)&server,sizeof(server));
325 inet_aton(addr,&addy);
327 server.sin_family = AF_INET;
328 if (!strcmp(addr,""))
330 server.sin_addr.s_addr = htonl(INADDR_ANY);
334 server.sin_addr = addy;
337 server.sin_port = htons(port);
339 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
351 /* Open a TCP Socket */
352 int OpenTCPSocket (void)
356 struct linger linger = { 0 };
358 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
362 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
363 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
366 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));