]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
d8e0954416a19e1ee4660717b0de8b70d1f5bce1
[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 #include "inspircd.h"
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20
21 void WriteOpers(char* text, ...);
22
23 void Exit (int status)
24 {
25   send_error("Server shutdown.");
26   exit (status);
27 }
28
29 void Killed(int status)
30 {
31   send_error("Server terminated.");
32   exit(status);
33 }
34
35 void Rehash(int status)
36 {
37   ReadConfig();
38   WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
39 }
40
41
42
43 void Start (void)
44 {
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");
51 }
52
53
54 void DeadPipe(int status)
55 {
56   signal (SIGPIPE, DeadPipe);
57 }
58
59 int DaemonSeed (void)
60 {
61   int childpid;
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)
71     return (ERROR);
72   else if (childpid > 0)
73     exit (0);
74   setsid ();
75   umask (077);
76   /* close stdout, stdin, stderr */
77   close(0);
78   close(1);
79   close(2);
80   return (TRUE);
81 }
82
83
84
85
86 /* Make sure the config file is available */
87 int CheckConfig (void)
88 {
89   FILE *input;
90
91   if ((input = fopen (CONFIG_FILE, "r")) == NULL)
92     {
93       printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
94       return(FALSE);
95     }
96   else
97     fclose (input);
98
99 return(TRUE);
100 }
101
102 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
103
104 int EnumConf(const char* filename, const char* tag)
105 {
106         FILE *config;
107         int ptr = 0;
108         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
109         int in_token, in_quotes, tptr, j, idx = 0;
110         char* key;
111
112         if ((config = fopen (filename, "r")) == NULL)
113         {
114                 return 0;
115         }
116         else
117         {
118                 ptr = 0;
119                 in_token = 0;
120                 in_quotes = 0;
121                 lastc = '\0';
122                 while (!feof(config))
123                 {
124                         lastc = c;
125                         c = fgetc(config);
126                         if ((c == '#') && (lastc == '\n'))
127                         {
128                                 while ((c != '\n') && (!feof(config)))
129                                 {
130                                         lastc = c;
131                                         c = fgetc(config);
132                                 }
133                         }
134                         if ((c == '<') && (!in_quotes))
135                         {
136                                 tptr = 0;
137                                 in_token = 1;
138                                 do {
139                                         c = fgetc(config);
140                                         if (c != ' ')
141                                         {
142                                                 c_tag[tptr++] = c;
143                                                 c_tag[tptr] = '\0';
144                                         }
145                                 } while (c != ' ');
146                         }
147                         if (c == '"')
148                         {
149                                 in_quotes = (!in_quotes);
150                         }
151                         if ((c == '>') && (!in_quotes))
152                         {
153                                 in_token = 0;
154                                 if (!strcmp(c_tag,tag))
155                                 {
156                                         /* correct tag, but wrong index */
157                                         idx++;
158                                 }
159                                 c_tag[0] = '\0';
160                                 buffer[0] = '\0';
161                                 ptr = 0;
162                                 tptr = 0;
163                         }
164                         if (c != '>')
165                         {
166                                 if ((in_token) && (c != '\n') && (c != '\r'))
167                                 {
168                                         buffer[ptr++] = c;
169                                         buffer[ptr] = '\0';
170                                 }
171                         }
172                 }
173         }
174         fclose(config);
175         return idx;
176 }
177
178
179
180 int ConfValueEnum(char* tag)
181 {
182         EnumConf(CONFIG_FILE,tag);
183 }
184
185
186
187 /* Retrieves a value from the config file. If there is more than one value of the specified
188  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
189  *
190  * ConfValue("oper","name",2,result);
191  */
192
193 int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result)
194 {
195         FILE *config;
196         int ptr = 0;
197         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
198         int in_token, in_quotes, tptr, j, idx = 0;
199         char* key;
200
201         if ((config = fopen (filename, "r")) == NULL)
202         {
203                 return 0;
204         }
205         else
206         {
207                 ptr = 0;
208                 in_token = 0;
209                 in_quotes = 0;
210                 lastc = '\0';
211                 while (!feof(config))
212                 {
213                         lastc = c;
214                         c = fgetc(config);
215                         if ((c == '#') && (lastc == '\n'))
216                         {
217                                 while ((c != '\n') && (!feof(config)))
218                                 {
219                                         lastc = c;
220                                         c = fgetc(config);
221                                 }
222                         }
223                         if ((c == '<') && (!in_quotes))
224                         {
225                                 tptr = 0;
226                                 in_token = 1;
227                                 do {
228                                         c = fgetc(config);
229                                         if (c != ' ')
230                                         {
231                                                 c_tag[tptr++] = c;
232                                                 c_tag[tptr] = '\0';
233                                         }
234                                 } while (c != ' ');
235                         }
236                         if (c == '"')
237                         {
238                                 in_quotes = (!in_quotes);
239                         }
240                         if ((c == '>') && (!in_quotes))
241                         {
242                                 in_token = 0;
243                                 if (idx == index)
244                                 {
245                                         if (!strcmp(c_tag,tag))
246                                         {
247                                                 if ((buffer) && (c_tag) && (var))
248                                                 {
249                                                         key = strstr(buffer,var);
250                                                         if (!key)
251                                                         {
252                                                                 /* value not found in tag */
253                                                                 fclose(config);
254                                                                 return 0;
255                                                         }
256                                                         else
257                                                         {
258                                                                 key+=strlen(var);
259                                                                 while (key[0] !='"')
260                                                                 {
261                                                                         if (!strlen(key))
262                                                                         {
263                                                                                 /* missing quote */
264                                                                                 return 0;
265                                                                         }
266                                                                         key++;
267                                                                 }
268                                                                 key++;
269                                                                 for (j = 0; j < strlen(key); j++)
270                                                                 {
271                                                                         if (key[j] == '"')
272                                                                         {
273                                                                                 key[j] = '\0';
274                                                                         }
275                                                                 }
276                                                                 fclose(config);
277                                                                 strcpy(result,key);
278                                                                 return 1;
279                                                         }
280                                                 }
281                                         }
282                                 }
283                                 if (!strcmp(c_tag,tag))
284                                 {
285                                         /* correct tag, but wrong index */
286                                         idx++;
287                                 }
288                                 c_tag[0] = '\0';
289                                 buffer[0] = '\0';
290                                 ptr = 0;
291                                 tptr = 0;
292                         }
293                         if (c != '>')
294                         {
295                                 if ((in_token) && (c != '\n') && (c != '\r'))
296                                 {
297                                         buffer[ptr++] = c;
298                                         buffer[ptr] = '\0';
299                                 }
300                         }
301                 }
302         }
303         fclose(config);
304         return 0;
305 }
306
307
308
309 int ConfValue(char* tag, char* var, int index, char *result)
310 {
311         ReadConf(CONFIG_FILE, tag, var, index, result);
312 }
313
314
315
316 /* This will bind a socket to a port. It works for UDP/TCP */
317 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
318 {
319   bzero((char *)&server,sizeof(server));
320   struct in_addr addy;
321   inet_aton(addr,&addy);
322
323   server.sin_family = AF_INET;
324   if (!strcmp(addr,""))
325   {
326           server.sin_addr.s_addr = htonl(INADDR_ANY);
327   }
328   else
329   {
330           server.sin_addr = addy;
331   }
332
333   server.sin_port = htons(port);
334
335   if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
336   {
337     return(ERROR);
338   }
339   else
340   {
341     listen(sockfd,5);
342     return(TRUE);
343   }
344 }
345
346
347 /* Open a TCP Socket */
348 int OpenTCPSocket (void)
349 {
350   int sockfd;
351   int on = 0;
352   struct linger linger = { 0 };
353   
354   if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
355     return (ERROR);
356   else
357   {
358     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
359     /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
360     linger.l_onoff = 1;
361     linger.l_linger = 0;
362     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
363     return (sockfd);
364   }
365 }
366