]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
b267e98ca8f12b20cffdfaed13d6183a798174a7
[user/henk/code/inspircd.git] / src / inspircd_io.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifdef __linux__ 
18 #include <sys/resource.h>
19 #endif
20
21 #include <sys/types.h>
22 #include <string>
23 #include <unistd.h>
24 #include <sstream>
25 #include <iostream>
26 #include <fstream>
27 #include "inspircd.h"
28 #include "inspircd_io.h"
29 #include "inspircd_util.h"
30
31 using namespace std;
32
33 extern FILE *log_file;
34
35 void WriteOpers(char* text, ...);
36
37 void Exit (int status)
38 {
39   if (log_file)
40         fclose(log_file);
41   send_error("Server shutdown.");
42   exit (status);
43 }
44
45 void Killed(int status)
46 {
47   if (log_file)
48         fclose(log_file);
49   send_error("Server terminated.");
50   exit(status);
51 }
52
53 void Rehash(int status)
54 {
55   ReadConfig();
56   WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
57 }
58
59
60
61 void Start (void)
62 {
63   printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
64   printf("(C) ChatSpike Development team.\033[0;37m\n\n");
65   printf("\033[1;37mDevelopers:\033[0;37m     Brain, FrostyCoolSlug, RD\n");
66   printf("\033[1;37mDocumentation:\033[0;37m  FrostyCoolSlug, w00t\n");
67   printf("\033[1;37mTesters:\033[0;37m        typobox43, piggles, Lord_Zathras, CC\n");
68   printf("\033[1;37mName concept:\033[0;37m   Lord_Zathras\n\n");
69 }
70
71
72 void DeadPipe(int status)
73 {
74   signal (SIGPIPE, DeadPipe);
75 }
76
77 int DaemonSeed (void)
78 {
79   int childpid;
80   signal (SIGALRM, SIG_IGN);
81   signal (SIGHUP, Rehash);
82   signal (SIGPIPE, DeadPipe);
83   signal (SIGTERM, Exit);
84   signal (SIGABRT, Exit);
85   signal (SIGSEGV, Error);
86   signal (SIGURG, Exit);
87   signal (SIGKILL, Exit);
88   if ((childpid = fork ()) < 0)
89     return (ERROR);
90   else if (childpid > 0)
91     exit (0);
92   setsid ();
93   umask (077);
94   /* close stdout, stdin, stderr */
95   close(0);
96   close(1);
97   close(2);
98
99   #ifdef __linux__ 
100   setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
101   #endif
102   
103   return (TRUE);
104 }
105
106
107 /* Make Sure Modules Are Avaliable!
108  * (BugFix By Craig.. See? I do work! :p)
109  * Modified by brain, requires const char*
110  * to work with other API functions
111  */
112
113 bool FileExists (const char* file)
114 {
115   FILE *input;
116   
117   if ((input = fopen (file, "r")) == NULL) { return(false); }
118   else { fclose (input); return(true); }
119 }
120
121
122 bool LoadConf(const char* filename, std::stringstream *target)
123 {
124         FILE* conf = fopen(filename,"r");
125         if (!FileExists(filename))
126         {
127                 return false;
128         }
129         char buffer[MAXBUF];
130         if (conf)
131         {
132                 target->clear();
133                 while (!feof(conf))
134                 {
135                         if (fgets(buffer, MAXBUF, conf))
136                         {
137                                 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
138                                 {
139                                         if (buffer[0] != '#')
140                                         {
141                                                 *target << std::string(buffer);
142                                         }
143                                 }
144                         }
145                 }
146                 fclose(conf);
147         }
148         target->seekg(0);
149         return true;
150 }
151
152 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
153
154 int EnumConf(std::stringstream *config, const char* tag)
155 {
156         int ptr = 0;
157         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
158         int in_token, in_quotes, tptr, j, idx = 0;
159         char* key;
160
161         const char* buf = config->str().c_str();
162         long bptr = 0;
163         long len = strlen(buf);
164         
165         ptr = 0;
166         in_token = 0;
167         in_quotes = 0;
168         lastc = '\0';
169         while (bptr<len)
170         {
171                 lastc = c;
172                 c = buf[bptr++];
173                 if ((c == '#') && (lastc == '\n'))
174                 {
175                         while ((c != '\n') && (bptr<len))
176                         {
177                                 lastc = c;
178                                 c = buf[bptr++];
179                         }
180                 }
181                 if ((c == '<') && (!in_quotes))
182                 {
183                         tptr = 0;
184                         in_token = 1;
185                         do {
186                                 c = buf[bptr++];
187                                 if (c != ' ')
188                                 {
189                                         c_tag[tptr++] = c;
190                                         c_tag[tptr] = '\0';
191                                 }
192                         } while (c != ' ');
193                 }
194                 if (c == '"')
195                 {
196                         in_quotes = (!in_quotes);
197                 }
198                 if ((c == '>') && (!in_quotes))
199                 {
200                         in_token = 0;
201                         if (!strcmp(c_tag,tag))
202                         {
203                                 /* correct tag, but wrong index */
204                                 idx++;
205                         }
206                         c_tag[0] = '\0';
207                         buffer[0] = '\0';
208                         ptr = 0;
209                         tptr = 0;
210                 }
211                 if (c != '>')
212                 {
213                         if ((in_token) && (c != '\n') && (c != '\r'))
214                         {
215                                 buffer[ptr++] = c;
216                                 buffer[ptr] = '\0';
217                         }
218                 }
219         }
220         return idx;
221 }
222
223 /* Counts the number of values within a certain tag */
224
225 int EnumValues(std::stringstream *config, const char* tag, int index)
226 {
227         int ptr = 0;
228         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
229         int in_token, in_quotes, tptr, j, idx = 0;
230         char* key;
231         
232         bool correct_tag = false;
233         int num_items = 0;
234
235         const char* buf = config->str().c_str();
236         long bptr = 0;
237         long len = strlen(buf);
238         
239         ptr = 0;
240         in_token = 0;
241         in_quotes = 0;
242         lastc = '\0';
243         while (bptr<len)
244         {
245                 lastc = c;
246                 c = buf[bptr++];
247                 if ((c == '#') && (lastc == '\n'))
248                 {
249                         while ((c != '\n') && (bptr<len))
250                         {
251                                 lastc = c;
252                                 c = buf[bptr++];
253                         }
254                 }
255                 if ((c == '<') && (!in_quotes))
256                 {
257                         tptr = 0;
258                         in_token = 1;
259                         do {
260                                 c = buf[bptr++];
261                                 if (c != ' ')
262                                 {
263                                         c_tag[tptr++] = c;
264                                         c_tag[tptr] = '\0';
265                                         
266                                         if ((!strcmp(c_tag,tag)) && (idx == index))
267                                         {
268                                                 correct_tag = true;
269                                         }
270                                 }
271                         } while (c != ' ');
272                 }
273                 if (c == '"')
274                 {
275                         in_quotes = (!in_quotes);
276                 }
277                 
278                 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
279                 {
280                         num_items++;
281                 }
282                 if ((c == '>') && (!in_quotes))
283                 {
284                         in_token = 0;
285                         if (correct_tag)
286                                 correct_tag = false;
287                         if (!strcmp(c_tag,tag))
288                         {
289                                 /* correct tag, but wrong index */
290                                 idx++;
291                         }
292                         c_tag[0] = '\0';
293                         buffer[0] = '\0';
294                         ptr = 0;
295                         tptr = 0;
296                 }
297                 if (c != '>')
298                 {
299                         if ((in_token) && (c != '\n') && (c != '\r'))
300                         {
301                                 buffer[ptr++] = c;
302                                 buffer[ptr] = '\0';
303                         }
304                 }
305         }
306         return num_items+1;
307 }
308
309
310
311 int ConfValueEnum(char* tag, std::stringstream* config)
312 {
313         return EnumConf(config,tag);
314 }
315
316
317
318 /* Retrieves a value from the config file. If there is more than one value of the specified
319  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
320  *
321  * ConfValue("oper","name",2,result);
322  */
323
324 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
325 {
326         int ptr = 0;
327         char buffer[65535], c_tag[MAXBUF], c, lastc;
328         int in_token, in_quotes, tptr, j, idx = 0;
329         char* key;
330
331         const char* buf = config->str().c_str();
332         long bptr = 0;
333         long len = strlen(buf);
334         
335         ptr = 0;
336         in_token = 0;
337         in_quotes = 0;
338         lastc = '\0';
339         c_tag[0] = '\0';
340         buffer[0] = '\0';
341         while (bptr<len)
342         {
343                 lastc = c;
344                 c = buf[bptr++];
345                 // FIX: Treat tabs as spaces
346                 if (c == 9)
347                         c = 32;
348                 if ((c == '<') && (!in_quotes))
349                 {
350                         tptr = 0;
351                         in_token = 1;
352                         do {
353                                 c = buf[bptr++];
354                                 if (c != ' ')
355                                 {
356                                         c_tag[tptr++] = c;
357                                         c_tag[tptr] = '\0';
358                                 }
359                         // FIX: Tab can follow a tagname as well as space.
360                         } while ((c != ' ') && (c != 9));
361                 }
362                 if (c == '"')
363                 {
364                         in_quotes = (!in_quotes);
365                 }
366                 if ((c == '>') && (!in_quotes))
367                 {
368                         in_token = 0;
369                         if (idx == index)
370                         {
371                                 if (!strcmp(c_tag,tag))
372                                 {
373                                         if ((buffer) && (c_tag) && (var))
374                                         {
375                                                 key = strstr(buffer,var);
376                                                 if (!key)
377                                                 {
378                                                         /* value not found in tag */
379                                                         strcpy(result,"");
380                                                         return 0;
381                                                 }
382                                                 else
383                                                 {
384                                                         key+=strlen(var);
385                                                         while (key[0] !='"')
386                                                         {
387                                                                 if (!strlen(key))
388                                                                 {
389                                                                         /* missing quote */
390                                                                         strcpy(result,"");
391                                                                         return 0;
392                                                                 }
393                                                                 key++;
394                                                         }
395                                                         key++;
396                                                         for (j = 0; j < strlen(key); j++)
397                                                         {
398                                                                 if (key[j] == '"')
399                                                                 {
400                                                                         key[j] = '\0';
401                                                                 }
402                                                         }
403                                                         strcpy(result,key);
404                                                         return 1;
405                                                 }
406                                         }
407                                 }
408                         }
409                         if (!strcmp(c_tag,tag))
410                         {
411                                 /* correct tag, but wrong index */
412                                 idx++;
413                         }
414                         c_tag[0] = '\0';
415                         buffer[0] = '\0';
416                         ptr = 0;
417                         tptr = 0;
418                 }
419                 if (c != '>')
420                 {
421                         if ((in_token) && (c != '\n') && (c != '\r'))
422                         {
423                                 buffer[ptr++] = c;
424                                 buffer[ptr] = '\0';
425                         }
426                 }
427         }
428         strcpy(result,""); // value or its tag not found at all
429         return 0;
430 }
431
432
433
434 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
435 {
436         ReadConf(config, tag, var, index, result);
437         return 0;
438 }
439
440
441
442 /* This will bind a socket to a port. It works for UDP/TCP */
443 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
444 {
445   bzero((char *)&server,sizeof(server));
446   struct in_addr addy;
447   inet_aton(addr,&addy);
448
449   server.sin_family = AF_INET;
450   if (!strcmp(addr,""))
451   {
452           server.sin_addr.s_addr = htonl(INADDR_ANY);
453   }
454   else
455   {
456           server.sin_addr = addy;
457   }
458
459   server.sin_port = htons(port);
460
461   if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
462   {
463     return(ERROR);
464   }
465   else
466   {
467     listen(sockfd,5);
468     return(TRUE);
469   }
470 }
471
472
473 /* Open a TCP Socket */
474 int OpenTCPSocket (void)
475 {
476   int sockfd;
477   int on = 0;
478   struct linger linger = { 0 };
479   
480   if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
481     return (ERROR);
482   else
483   {
484     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
485     /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
486     linger.l_onoff = 1;
487     linger.l_linger = 0;
488     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
489     return (sockfd);
490   }
491 }
492