parser.cpp 6.96 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * parser.cpp : SAP configuration file parser
 ****************************************************************************
 * Copyright (C) 1998-2002 VideoLAN
5
 * $Id: parser.cpp,v 1.8 2003/09/11 15:33:53 zorglub Exp $
6
 *
7
 * Authors: Arnaud Schauly <gitan@via.ecp.fr>
8
 *          Clment Stenac <zorglub@via.ecp.fr>
9 10
 *          Damien Lucas <nitrox@videolan.org>
 *          Philippe Van Hecke <philippe.vanhecke@belnet.be>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <vector>

using namespace std;

#include "sapserver.h"
#include "program.h"
#include "message.h"
#include "parser.h"

Config::Config(string f)
{
48 49 50
    file	=	f;
    ttl		=	DEFAULT_TTL;
    scope	=	DEFAULT_SCOPE;
51 52
    type	=	TYPE_SAP;
    reverse	=	false;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

Config::~Config()
{
    //TODO free the Programs table
}

unsigned int Config::GetTTL()
{
    return ttl;
}

void Config::SetTTL(unsigned int t)
{
    ttl=t;
}

bool Config::GetDaemonMode(void)
{
    return daemon;
}

75 76 77 78 79
bool Config::GetReverse(void)
{
    return reverse;
}

80 81 82 83 84
void Config::SetDaemonMode(bool d)
{
    daemon=true;
}

85 86 87 88 89 90 91 92 93 94 95
bool Config::GetDotMode(void)
{
    return dot;
}

void Config::SetDotMode(bool d)
{
    dot=true;
}


96 97 98 99
void Config::SetFile(char* s)
{
    file=s;
}
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
void Config::SetReverse(bool d)
{
    reverse= true;
}

void Config::SetType(char *s)
{
    if(!strcasecmp(s,"slp"))
    {
	type=TYPE_SLP;  
    }
    else
    {
        type=TYPE_SAP;
    }
}

118 119 120 121 122
int Config::GetIPVersion()
{
    return ipversion;
}

123 124 125 126 127
int Config::GetType()
{
    return type;
}

128 129 130 131 132
char Config::GetScope()
{
    return scope[0];
}

133 134 135 136 137 138
/**********************************************************
 * Gets what is before delim in source. Puts it into dest *
 **********************************************************/
void Config::strgetb(char *source,char *dest,char delim)
{
        unsigned long i = 0;
139 140 141 142
        unsigned long j;

        // Cut the line just before the delim char
        for(i=0; source[i] != delim && i < strlen(source); i++)
143
                dest[i]=source[i];
144 145 146 147 148
        dest[i]='\0';

        // Remove the trailing blanks
        for(j=i-1; dest[j] == ' '|| dest[j] == '\t' || dest[j] == '\n'; j--)
            dest[j] = '\0';
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}

/********************************************************** 
 * Gets what is before delim in source. Puts it into dest * 
 **********************************************************/ 
void Config::strgeta(char *source,char *dest,char delim)
{
    unsigned long i=0,j=0;
    dest[0]=0;
    while(source[i] != delim && i < strlen(source))
    {
        i++;
    }
    i++;
    if(i>=strlen(source))
    {
        return;
    }
    while(i < strlen(source))
    {
        if(source[i]!='\n') dest[j]=source[i];
        else dest[j]=0;
        j++;i++;
    }
    dest[j]=0;
}


/********************************************************************
  Reads the configuration file and fills the program list
*********************************************************************/
int Config::Parse()
{
    FILE *fd;
    char line[1024];   //TODO line length should not be limited
    char tline[1024];
    unsigned int something=0;
    Program *pp= new Program();

    fd=fopen(file.c_str(),"r");
    if(!fd)
    {
191
        fprintf(stderr, "- Unable to open %s\n",file.c_str());
192 193 194
        return(-1);
    }

195
    while(fgets(line,1024,fd))
196
    {
197
        if(!strlen(line))
198 199 200 201 202 203
        {
            break;
        }

        strgetb(line,line,'#'); /* Handle the comments */

204
        /* Set SAP TTL  (ttl is a member of config) */
205
        if(strstr(line,"sap_ttl="))
206 207 208 209
        {
            strgeta(line,tline,'=');
            ttl=atoi(tline);
        }
210 211

        /* Set SAP IP Version  (ipversion is a member of config) */
212
        if(strstr(line,"sap_ipversion="))
213 214 215 216
        {
            strgeta(line,tline,'=');
            ipversion=atoi(tline);
        }
217

218
        /* Set SAP IPv6 Scope  (scope is a member of config) */
219 220 221 222 223
        if(strstr(line,"ipv6_scope="))
        {
            strgeta(line,tline,'=');
            scope=strdup(tline);
        }
224

225
        /*  Beginning of programs parsing */
226 227 228 229 230
        if(strstr(line,"[program]"))
        {
            if(something)
            {
                /* We were in a program with at least one field filled */
231 232 233 234
                if (!pp->GetPort().length())
                {
                    pp->SetPort(DEFAULT_PORT);
                }
235 236 237 238 239 240 241 242 243 244 245 246
                Programs.push_back(pp);
                pp=new Program();
                something=0;
            }
        }

        if(strstr(line,"name="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetName(tline);
        }
247

248 249 250 251 252 253
        if(strstr(line,"program_ttl="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetTTL(tline);
        }
254

255 256 257 258 259 260 261
        if(strstr(line,"program_ipversion="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetIPVersion(tline);
        }

262 263 264 265 266 267 268 269 270 271 272 273
	if(strstr(line,"type=") && strstr(line,"http") )
	{
	    pp->SetHTTP(true);
	}

        if(strstr(line,"path="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetHTTPPath(tline);
        }

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        if(strstr(line,"user="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetUser(tline);
        }

        if(strstr(line,"machine="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetMachine(tline);
        }

        if(strstr(line,"site="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetSite(tline);
        }

        if(strstr(line,"address="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetAddress(tline);
        }
301 302 303 304 305 306
        if(strstr(line,"port="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetPort(tline); 
        }
307 308
    }

309
    if(something)
310 311 312
    {
     if (!pp->GetPort().length())
     {
313
          pp->SetPort(DEFAULT_PORT);
314 315 316
     }
     Programs.push_back(pp);
    }
317 318
    return(0);
}