2 ** igmpproxy - IGMP proxy based multicast router
3 ** Copyright (C) 2005 Johnny Egeland <johnny@rlo.org>
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 **----------------------------------------------------------------------------
21 ** This software is derived work from the following software. The original
22 ** source code has been modified from it's original state by the author
25 ** smcroute 0.92 - Copyright (C) 2001 Carsten Schill <carsten@cschill.de>
26 ** - Licensed under the GNU General Public License, version 2
28 ** mrouted 3.9-beta3 - COPYRIGHT 1989 by The Board of Trustees of
29 ** Leland Stanford Junior University.
30 ** - Original license can be found in the Stanford.txt file.
36 * Generic config file reader. Used to open a config file,
37 * and read the tokens from it. The parser is really simple,
38 * and does no backlogging. This means that no form of
39 * text escaping and qouting is currently supported.
40 * '#' chars are read as comments, and the comment lasts until
45 #include "igmpproxy.h"
47 #define READ_BUFFER_SIZE 512 // Inputbuffer size...
49 #ifndef MAX_TOKEN_LENGTH
50 #define MAX_TOKEN_LENGTH 30 // Default max token length
53 FILE *confFilePtr
; // File handle pointer
54 char *iBuffer
; // Inputbuffer for reading...
55 unsigned int bufPtr
; // Buffer position pointer.
56 unsigned int readSize
; // Number of bytes in buffer after last read...
57 char cToken
[MAX_TOKEN_LENGTH
]; // Token buffer...
61 * Opens config file specified by filename.
63 int openConfigFile(char *filename
) {
65 // Set the buffer to null initially...
68 // Open the file for reading...
69 confFilePtr
= fopen(filename
, "r");
71 // On error, return false
72 if(confFilePtr
== NULL
) {
76 // Allocate memory for inputbuffer...
77 iBuffer
= (char*) malloc( sizeof(char) * READ_BUFFER_SIZE
);
84 // Reset bufferpointer and readsize
92 * Closes the currently open config file.
94 void closeConfigFile() {
96 if(confFilePtr
!=NULL
) {
99 // Free input buffer memory...
100 if(iBuffer
!= NULL
) {
106 * Returns the next token from the configfile. The function
107 * return NULL if there are no more tokens in the file.
109 char *nextConfigToken() {
113 // If no file or buffer, return NULL
114 if(confFilePtr
== NULL
|| iBuffer
== NULL
) {
119 unsigned int tokenPtr
= 0;
120 unsigned short finished
= 0;
121 unsigned short commentFound
= 0;
123 // Outer buffer fill loop...
124 while ( !finished
) {
125 // If readpointer is at the end of the buffer, we should read next chunk...
126 if(bufPtr
== readSize
) {
127 // Fill up the buffer...
128 readSize
= fread (iBuffer
, sizeof(char), READ_BUFFER_SIZE
, confFilePtr
);
131 // If the readsize is 0, we should just return...
137 // Inner char loop...
138 while ( bufPtr
< readSize
&& !finished
) {
140 //printf("Char %s", iBuffer[bufPtr]);
143 if(iBuffer
[bufPtr
] == '\0') {
148 if( iBuffer
[bufPtr
] == '\n' ) {
153 // Check current char...
154 switch(iBuffer
[bufPtr
]) {
156 // Found a comment start...
164 // Newline, CR, Tab and space are end of token, or ignored.
166 cToken
[tokenPtr
] = '\0'; // EOL
172 // Append char to token...
173 cToken
[tokenPtr
++] = iBuffer
[bufPtr
];
179 // Check end of token buffer !!!
180 if(tokenPtr
== MAX_TOKEN_LENGTH
- 1) {
181 // Prevent buffer overrun...
182 cToken
[tokenPtr
] = '\0';
189 // If the readsize is less than buffersize, we assume EOF.
190 if(readSize
< READ_BUFFER_SIZE
&& bufPtr
== readSize
) {
207 * Returns the currently active token, or null
208 * if no tokens are availible.
210 char *getCurrentConfigToken() {
211 return validToken
? cToken
: NULL
;