]> git.sesse.net Git - vlc/blob - modules/stream_out/announce.c
* Fix
[vlc] / modules / stream_out / announce.c
1 /*****************************************************************************
2  * announce.c : Session announcement
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  *
6  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
7  *          Damien Lucas <nitrox@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdio.h>                                              /* sprintf() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/sout.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #ifdef WIN32
40 #   include <winsock2.h>
41 #   include <ws2tcpip.h>
42 #   ifndef IN_MULTICAST
43 #       define IN_MULTICAST(a) IN_CLASSD(a)
44 #   endif
45 #else
46 #   include <sys/socket.h>
47 #endif
48
49 #ifdef HAVE_SLP_H
50 # include <slp.h>
51 #endif
52
53 #include "announce.h"
54 #include "network.h"
55
56 #define DEFAULT_PORT 1234
57
58 #ifdef HAVE_SLP_H
59 /*****************************************************************************
60  * sout_SLPBuildName: Builds a service name according to SLP standard
61  *****************************************************************************/
62 static char * sout_SLPBuildName(char *psz_url,char *psz_name)
63 {
64     char *psz_service;
65     unsigned int i_size;
66
67     /* name to build is: service:vlc.services.videolan://$(url) */
68
69     i_size =  8 + 12 + 12 + 5 + strlen(psz_url) + 1;
70
71     psz_service=(char *)malloc(i_size * sizeof(char));
72
73     snprintf( psz_service , i_size,
74               "service:vlc.services.videolan://udp:@%s",
75               psz_url);
76         /* How piggy  ! */
77
78     psz_service[i_size]='\0'; /* Just to make sure */
79
80     return psz_service;
81
82 }
83
84 /*****************************************************************************
85  * sout_SLPReport: Reporting function. Unused at the moment but needed
86  *****************************************************************************/
87 static void sout_SLPReport(SLPHandle slp_handle,SLPError slp_error,void* cookie)
88 {
89 }
90 #endif
91
92 /*****************************************************************************
93  * sout_SLPReg: Registers the program with SLP
94  *****************************************************************************/
95 int sout_SLPReg( sout_instance_t *p_sout, char * psz_url,
96                                char * psz_name)
97 {
98 #ifdef HAVE_SLP_H
99     SLPHandle   slp_handle;
100     SLPError    slp_res;
101     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
102
103     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
104     {
105         msg_Warn(p_sout,"Unable to initialize SLP");
106         return -1;
107     }
108
109     msg_Info(p_sout , "Registering %s (name: %s) in SLP",
110                       psz_service , psz_name);
111
112     slp_res = SLPReg ( slp_handle,
113             psz_service,
114             SLP_LIFETIME_MAXIMUM,
115             NULL,
116             psz_name,
117             SLP_TRUE,
118             sout_SLPReport,
119             NULL );
120
121     if( slp_res != SLP_OK )
122     {
123         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
124         return -1;
125     }
126
127     return 0;
128
129 #else /* This function should never be called if this is false */
130     return -1;
131 #endif
132 }
133
134
135 /*****************************************************************************
136  * sout_SLDePReg: Unregisters the program from SLP
137  *****************************************************************************/
138 int sout_SLPDereg( sout_instance_t *p_sout, char * psz_url,
139                                char * psz_name)
140 {
141 #ifdef HAVE_SLP_H
142
143     SLPHandle   slp_handle;
144     SLPError    slp_res;
145     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
146
147     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
148     {
149         msg_Warn(p_sout,"Unable to initialize SLP");
150         return -1;
151     }
152
153     msg_Info(p_sout , "Unregistering %s from SLP",
154                       psz_service);
155
156     slp_res = SLPDereg ( slp_handle,
157             psz_service,
158             sout_SLPReport,
159             NULL );
160
161     if( slp_res != SLP_OK )
162     {
163         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
164         return -1;
165     }
166
167     return 0;
168
169 #else /* This function should never be called if this is false */
170     return -1;
171 #endif
172 }