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 "inspircd_io.h"
19 #include "inspircd_util.h"
21 void WriteOpers(char* text, ...);
23 void Exit (int status)
25 send_error("Server shutdown.");
29 void Killed(int status)
31 send_error("Server terminated.");
35 void Rehash(int status)
38 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
45 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
46 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
47 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug, Raider, RD\n");
48 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug\n");
49 printf("\033[1;37mTesters:\033[0;37m MrBOFH, piggles, Lord_Zathras, typobox43, CC\n");
50 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
54 void DeadPipe(int status)
56 signal (SIGPIPE, DeadPipe);
62 signal (SIGALRM, SIG_IGN);
63 signal (SIGHUP, Rehash);
64 signal (SIGPIPE, DeadPipe);
65 signal (SIGTERM, Exit);
66 signal (SIGABRT, Exit);
67 signal (SIGSEGV, Error);
68 signal (SIGURG, Exit);
69 signal (SIGKILL, Exit);
70 if ((childpid = fork ()) < 0)
72 else if (childpid > 0)
76 /* close stdout, stdin, stderr */
86 /* Make sure the config file is available */
87 int CheckConfig (void)
91 if ((input = fopen (CONFIG_FILE, "r")) == NULL)
93 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
102 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
104 int EnumConf(const char* filename, const char* tag)
108 char buffer[MAXBUF], c_tag[MAXBUF], c;
109 int in_token, in_quotes, tptr, j, idx = 0;
112 if ((config = fopen (filename, "r")) == NULL)
121 while (!feof(config))
124 if ((c == '<') && (!in_quotes))
139 in_quotes = (!in_quotes);
141 if ((c == '>') && (!in_quotes))
144 if (!strcmp(c_tag,tag))
146 /* correct tag, but wrong index */
156 if ((in_token) && (c != '\n') && (c != '\r'))
170 int ConfValueEnum(char* tag)
172 EnumConf(CONFIG_FILE,tag);
177 /* Retrieves a value from the config file. If there is more than one value of the specified
178 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
180 * ConfValue("oper","name",2,result);
183 int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result)
187 char buffer[MAXBUF], c_tag[MAXBUF], c;
188 int in_token, in_quotes, tptr, j, idx = 0;
191 if ((config = fopen (filename, "r")) == NULL)
200 while (!feof(config))
203 if ((c == '<') && (!in_quotes))
218 in_quotes = (!in_quotes);
220 if ((c == '>') && (!in_quotes))
225 if (!strcmp(c_tag,tag))
227 if ((buffer) && (c_tag) && (var))
229 key = strstr(buffer,var);
232 /* value not found in tag */
249 for (j = 0; j < strlen(key); j++)
263 if (!strcmp(c_tag,tag))
265 /* correct tag, but wrong index */
275 if ((in_token) && (c != '\n') && (c != '\r'))
289 int ConfValue(char* tag, char* var, int index, char *result)
291 ReadConf(CONFIG_FILE, tag, var, index, result);
296 /* This will bind a socket to a port. It works for UDP/TCP */
297 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
299 bzero((char *)&server,sizeof(server));
301 inet_aton(addr,&addy);
303 server.sin_family = AF_INET;
304 if (!strcmp(addr,""))
306 server.sin_addr.s_addr = htonl(INADDR_ANY);
310 server.sin_addr = addy;
313 server.sin_port = htons(port);
315 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
327 /* Open a TCP Socket */
328 int OpenTCPSocket (void)
332 struct linger linger = { 0 };
334 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
338 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
339 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
342 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));