slp.cpp 3.07 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * slp.cpp : SLP registration class
 ****************************************************************************
 * Copyright (C) 1998-2003 VideoLAN
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Authors: Clment Stenac <zorglub@via.ecp.fr>
 *
 * 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 <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>

33 34
using namespace std;

35
#include "sapserver.h"
36 37
#include "program.h"

38
#ifdef CONFIG_SLP
39 40 41
  #include <slp.h>
#endif

42
#include "lslp.h"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

using namespace std;

SLP::SLP()
{
    if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
    {
	fprintf(stderr,"- Unable to initialize SLP\n");
	abort();
    } 
}


SLP::~SLP(){return;}


void RegReport(SLPHandle slp_handle,SLPError slp_error,void* cookie)
{
}

char * BuildService(Program *p)
{
    char *psz_msg;
    string msg="";
    string url="";
    unsigned int i=0;

70
    string head = "service:vlc.services.videolan.org://";
71 72 73 74 75 76 77 78 79

    if(p->IsHTTP() )
    {
	url += "http://";
    }
    else
    {
	url+="udp:@";
    }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    if(p->GetIPVersion() == 6)
    {
        url += "[";
    }

    url += p->GetAddress();
    
    if(p->GetIPVersion() == 6)
    {
        url += "]";
    }
    
    url+=":";
    url += p->GetPort();

96 97 98 99 100 101 102
#if 0
    /* This code is for the new naming scheme after vlc 0.7.0 */
    /* service:vlc.services.videolan.org://MRL//NAME */
    url += "//";
    url += p->GetName();
#endif

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    msg += head + url;

    psz_msg = (char *)malloc( msg.size() * sizeof(char) );

    for(i=0 ; i< msg.size() ; i++)
    {
       psz_msg[i] = msg[i] ;
    }

    psz_msg[i] = '\0';

    return psz_msg;
}

int SLP::Register(Program *p)
{
    char *psz_msg = BuildService(p);
120
 
121 122 123 124 125
    string name;
    name += p->GetName();

    fprintf(stdout,"+ Registering %s (name: %s)\n",psz_msg,name.data());

126 127 128 129 130 131 132 133
    SLPReg (    slp_handle, 
                psz_msg,
                SLP_LIFETIME_MAXIMUM,
                NULL,
                name.data(),
                SLP_TRUE,
                RegReport,
                NULL );
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

    if(psz_msg) free(psz_msg);

    return 0;
}

int SLP::UnRegister(Program *p)
{
    char *psz_msg = BuildService(p);
   
    fprintf(stdout,"+ Unregistering %s\n",psz_msg);

    SLPDereg (	slp_handle,
		psz_msg,
		RegReport,
		NULL );

    return 0;
}