1 /*
2 *			GPAC - Multimedia Framework C SDK
3 *
4 *			Authors: Jean Le Feuvre
5 *			Copyright (c) Telecom ParisTech 2000-2012
6 *					All rights reserved
7 *
8 *  This file is part of GPAC / common tools sub-project
9 *
10 *  GPAC is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License as published by
12 *  the Free Software Foundation; either version 2, or (at your option)
13 *  any later version.
14 *
15 *  GPAC is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU Lesser General Public License for more details.
19 *
20 *  You should have received a copy of the GNU Lesser General Public
21 *  License along with this library; see the file COPYING.  If not, write to
22 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 
26 #ifndef _GF_NET_H_
27 #define _GF_NET_H_
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 	/*!
34 	*	\file <gpac/network.h>
35 	*	\brief Networking.
36 	*/
37 
38 	/*!
39 	*	\addtogroup net_grp Network
40 	*	\ingroup utils_grp
41 	*	\brief Networking tools (URL resolution, TCP/UDP sockets)
42 	*
43 	*This section documents the IP network functions of the GPAC framework.
44 	*	@{
45 	*/
46 
47 #include <gpac/tools.h>
48 
49 
50 	/*!
51 	*\brief URL local test
52 	*
53 	*Tests whether a URL describes a local file or not
54 	*\param url the url to analyze
55 	*\return 1 if the URL describes a local file, 0 otherwise
56 	*/
57 	Bool gf_url_is_local(const char *url);
58 
59 	/*!
60 	*\brief gets absolute file path
61 	*
62 	*Gets the absolute file path from a relative path and its parent absolute one. This can only be used with file paths.
63 	*\param pathName the relative path name of a file
64 	*\param parentPath the absolute parent path name
65 	*\return absolute path name of the file, or NULL if bad paths are provided.
66 	\note the returned string must be freed by user
67 	*/
68 	char *gf_url_get_absolute_path(const char *pathName, const char *parentPath);
69 
70 	/*concatenates URL and gets back full URL - returned string must be freed by user*/
71 	/*!
72 	*\brief URL concatenation
73 	*
74 	*Concatenates a relative URL with its parent URL
75 	*\param parentName URL of the parent service
76 	*\param pathName URL of the service
77 	*\return absolute path name of the service, or NULL if bad paths are provided or if the service path is already an absolute one.
78 	\note the returned string must be freed by user
79 	*/
80 	char *gf_url_concatenate(const char *parentName, const char *pathName);
81 
82 	/*!
83 	*\brief URL encodin
84 	*
85 	*Encodes URL by replacing special characters with their % encodings.
86 	*\param path URL of the service
87 	*\return encoded path name , or NULL if bad paths are provided.
88 	\note the returned string must be freed by user
89 	*/
90 	char *gf_url_percent_encode(const char *path);
91 
92 	/*!
93 	*\brief URL to file system
94 	*
95 	*Converts a local URL to a file system value. Removes all white spaces and similar
96 	*\param url url to convert
97 	*/
98 	void gf_url_to_fs_path(char *url);
99 
100 
101 	/*!
102 	*\brief Extract resource name from URL
103 	*
104 	* Extracts the resource name from the URL
105 	*\param url input url
106 	*\return resource name.
107 	*/
108 	const char *gf_url_get_resource_name(const char *url);
109 
110 	/*!
111 	*\brief Extract resource path from URL
112 	*
113 	*Extracts the reource path from the URL
114 	*\param url input url
115 	*\param res_path buffer for resulting path storage
116 	*\return 1 if path was extracted, 0 if url is a single file name.
117 	*/
118 	Bool gf_url_get_resource_path(const char *url, char *res_path);
119 
120 	/*!
121 	*\brief Remove last delimenter from URL
122 	*
123 	* Remove last delimenter from URL
124 	*\param sURL input url
125 	*\param res_path buffer for resulting path storage
126 	*\return GF_TRUE if delimiter was extracted, otherwise GF_FALSE.
127 	*/
128 	Bool gf_url_remove_last_delimiter(const char *sURL, char *res_path);
129 
130 	/*!
131 	*\brief Extract extension from a resource path in URL
132 	*
133 	* Extract a resource path of URL and analyze its extension
134 	*\param sURL input url
135 	*\return The corresponding extension if exists, otherwise NULL.
136 	*/
137 	const char* gf_url_get_ressource_extension(const char *sURL);
138 
139 	/*!
140 	*\brief Portable version of UNIX gettimeofday()
141 	*/
142 	struct timeval;
143 	s32 gf_gettimeofday(struct timeval *tp, void *tz);
144 
145 	/*!
146 	*\brief gets UTC time
147 	*
148 	*Gets UTC time since midnight Jan 1970
149 	*\param sec number of seconds
150 	*\param msec number of milliseconds
151 	*/
152 	void gf_utc_time_since_1970(u32 *sec, u32 *msec);
153 
154 
155 	/*!
156 	*	\brief NTP seconds from 1900 to 1970
157 	*	\hideinitializer
158 	*
159 	*	Macro giving the number of seconds from from 1900 to 1970
160 	*/
161 #define GF_NTP_SEC_1900_TO_1970 2208988800ul
162 
163 	/*!
164 	*\brief gets NTP time
165 	*
166 	*Gets NTP (Network Time Protocol) in seconds and fractional side
167 	\param sec NTP time in seconds
168 	\param frac fractional NTP time expressed in 1 / (1<<32 - 1) seconds units
169 	*/
170 	void gf_net_get_ntp(u32 *sec, u32 *frac);
171 
172 	/*!
173 	*\brief offsets NTP time by a given amount of seconds
174 	*
175 	*Offsets NTP time of the system by a given amount of seconds in the future or the past (default value is 0).
176 	\param sec seconds to add or remove to the system NTP
177 	*/
178 	void gf_net_set_ntp_shift(s32 sec);
179 
180 	/*!
181 	*\brief gets NTP time
182 	*
183 	*Gets NTP (Network Time Protocol) timestamp (high 32 bit is seconds, low 32 bit is fraction)
184 	\return NTP timestamp
185 	*/
186 	u64 gf_net_get_ntp_ts();
187 
188 	/*!
189 	*
190 	*Gets diff in milliseconds between NTP time and current time
191 	\param ntp NTP timestamp
192 	\return diff in milliseconds with the current time
193 	*/
194 	s32 gf_net_get_ntp_diff_ms(u64 ntp);
195 
196 	/*!
197 	* Socket options
198 	*	\hideinitializer
199 	*/
200 	enum
201 	{
202 		/*!Reuses port.*/
203 		GF_SOCK_REUSE_PORT = 1,
204 		/*!Forces IPV6 if available.*/
205 		GF_SOCK_FORCE_IPV6 = 1 << 1
206 	};
207 
208 	/*!
209 	*\brief abstracted socket object
210 	*
211 	*The abstracted socket object allows you to build client and server applications very simply
212 	*with support for unicast and multicast (no IPv6 yet)
213 	*/
214 	typedef struct __tag_socket GF_Socket;
215 
216 	/*!Buffer size to pass for IP address retrieval*/
217 #define GF_MAX_IP_NAME_LEN	516
218 
219 	/*!socket is a TCP socket*/
220 #define GF_SOCK_TYPE_TCP		0x01
221 	/*!socket is a UDP socket*/
222 #define GF_SOCK_TYPE_UDP		0x02
223 
224 	/*!
225 	*\brief socket constructor
226 	*
227 	*Constructs a socket object
228 	*\param SocketType the socket type to create, either UDP or TCP
229 	*\return the socket object or NULL if network initialization failure
230 	*/
231 	GF_Socket *gf_sk_new(u32 SocketType);
232 	/*!
233 	*\brief socket destructor
234 	*
235 	*Deletes a socket object
236 	*\param sock the socket object
237 	*/
238 	void gf_sk_del(GF_Socket *sock);
239 
240 	/*!
241 	*\brief reset internal buffer
242 	*
243 	*Forces the internal socket buffer to be reseted (discarded)
244 	*\param sock the socket object
245 	*/
246 	void gf_sk_reset(GF_Socket *sock);
247 	/*!
248 	*\brief socket buffer size control
249 	*
250 	*Sets the size of the internal buffer of the socket. The socket MUST be bound or connected before.
251 	*\param sock the socket object
252 	*\param send_buffer if 0, sets the size of the reception buffer, otherwise sets the size of the emission buffer
253 	*\param new_size new size of the buffer in bytes.
254 	*\warning This operation may fail depending on the provider, hardware...
255 	*/
256 	GF_Err gf_sk_set_buffer_size(GF_Socket *sock, Bool send_buffer, u32 new_size);
257 
258 	/*!
259 	*\brief blocking mode control
260 	*
261 	*Sets the blocking mode of a socket on or off. A blocking socket will wait for the net operation to be possible
262 	*while a non-blocking one would return an error. By default, sockets are created in blocking mode
263 	*\param sock the socket object
264 	*\param NonBlockingOn set to 1 to use on-blocking sockets, 0 otherwise
265 	*/
266 	GF_Err gf_sk_set_block_mode(GF_Socket *sock, Bool NonBlockingOn);
267 	/*!
268 	*\brief socket binding
269 	*
270 	*Binds the given socket to the specified port.
271 	*\param local_ip the local interface IP address if desired. If NULL, the default interface will be used.
272 	*\param sock the socket object
273 	*\param port port number to bind this socket to
274 	*\param peer_name the remote server address, if NULL, will use localhost
275 	*\param peer_port remote port number to connect the socket to
276 	*\param options list of option for the bind operation.
277 	*/
278 	GF_Err gf_sk_bind(GF_Socket *sock, const char *local_ip, u16 port, const char *peer_name, u16 peer_port, u32 options);
279 	/*!
280 	*\brief connects a socket
281 	*
282 	*Connects a socket to a remote peer on a given port
283 	*\param sock the socket object
284 	*\param peer_name the remote server address (IP or DNS)
285 	*\param port remote port number to connect the socket to
286 	*\param local_ip the local (client) address (IP or DNS) if any, NULL otherwise.
287 	*/
288 	GF_Err gf_sk_connect(GF_Socket *sock, const char *peer_name, u16 port, const char *local_ip);
289 	/*!
290 	*\brief data emission
291 	*
292 	*Sends a buffer on the socket. The socket must be in a bound or connected mode
293 	*\param sock the socket object
294 	*\param buffer the data buffer to send
295 	*\param length the data length to send
296 	*/
297 	GF_Err gf_sk_send(GF_Socket *sock, const char *buffer, u32 length);
298 	/*!
299 	*\brief data reception
300 	*
301 	*Fetches data on a socket. The socket must be in a bound or connected state
302 	*\param sock the socket object
303 	*\param buffer the reception buffer where data is written
304 	*\param length the allocated size of the reception buffer
305 	*\param start_from the offset in the reception buffer where to start writing
306 	*\param read the actual number of bytes received
307 	*/
308 	GF_Err gf_sk_receive(GF_Socket *sock, char *buffer, u32 length, u32 start_from, u32 *read);
309 	/*!
310 	*\brief socket listening
311 	*
312 	*Sets the socket in a listening state. This socket must have been bound to a port before
313 	*\param sock the socket object
314 	*\param max_conn the maximum number of simultaneous connection this socket will accept
315 	*/
316 	GF_Err gf_sk_listen(GF_Socket *sock, u32 max_conn);
317 	/*!
318 	*\brief socket accept
319 	*
320 	*Accepts an incoming connection on a listening socket
321 	*\param sock the socket object
322 	*\param new_conn the resulting connection socket object
323 	*/
324 	GF_Err gf_sk_accept(GF_Socket *sock, GF_Socket **new_conn);
325 
326 	/*!
327 	*\brief server socket mode
328 	*
329 	*Disable the Nable algo (e.g. set TCP_NODELAY) and set the KEEPALIVE on
330 	*\param sock the socket object
331 	*\param server_on sets server mode on or off
332 	*/
333 	GF_Err gf_sk_server_mode(GF_Socket *sock, Bool server_on);
334 
335 	/*!
336 	*\brief get local host name
337 	*
338 	*Retrieves local host name.
339 	*\param buffer destination buffer for name. Buffer must be GF_MAX_IP_NAME_LEN long
340 	*/
341 	GF_Err gf_sk_get_host_name(char *buffer);
342 
343 	/*!
344 	*\brief get local IP
345 	*
346 	*Gets local IP address of a connected socket, typically used for server after an ACCEPT
347 	*\param sock the socket object
348 	*\param buffer destination buffer for IP address. Buffer must be GF_MAX_IP_NAME_LEN long
349 	*/
350 	GF_Err gf_sk_get_local_ip(GF_Socket *sock, char *buffer);
351 	/*!
352 	*\brief get local info
353 	*
354 	*Gets local socket info of a socket
355 	*\param sock the socket object
356 	*\param port local port number of the socket
357 	*\param sock_type socket type (UDP or TCP)
358 	*/
359 	GF_Err gf_sk_get_local_info(GF_Socket *sock, u16 *port, u32 *sock_type);
360 
361 	/*!
362 	*\brief get remote address
363 	*
364 	*Gets the remote address of a peer. The socket MUST be connected.
365 	*\param sock the socket object
366 	*\param buffer destination buffer for IP address. Buffer must be GF_MAX_IP_NAME_LEN long
367 	*/
368 	GF_Err gf_sk_get_remote_address(GF_Socket *sock, char *buffer);
369 
370 	/*!
371 	*\brief set remote address
372 	*
373 	*Sets the remote address of a socket. This is used by connectionless sockets using SendTo and ReceiveFrom
374 	*\param sock the socket object
375 	*\param address the remote peer address
376 	*\param port the remote peer port
377 	*/
378 	GF_Err gf_sk_set_remote(GF_Socket *sock, char *address, u16 port);
379 
380 
381 	/*!
382 	*\brief multicast setup
383 	*
384 	*Performs multicast setup (BIND and JOIN) for the socket object
385 	*\param sock the socket object
386 	*\param multi_ip_add the multicast IP address
387 	*\param multi_port the multicast port number
388 	*\param TTL the multicast TTL (Time-To-Live)
389 	*\param no_bind if sets, only join the multicast
390 	*\param local_interface_ip the local interface IP address if desired. If NULL, the default interface will be used.
391 	*/
392 	GF_Err gf_sk_setup_multicast(GF_Socket *sock, const char *multi_ip_add, u16 multi_port, u32 TTL, Bool no_bind, char *local_interface_ip);
393 	/*!
394 	*brief multicast address test
395 	*
396 	*tests whether an IP address is a multicast one or not
397 	*\param multi_ip_add the multicast IP address to test
398 	*\return 1 if the address is a multicast one, 0 otherwise
399 	*/
400 	u32 gf_sk_is_multicast_address(const char *multi_ip_add);
401 
402 	/*!
403 	*\brief send data with wait delay
404 	*
405 	*Sends data with a max wait delay. This is used for http / ftp sockets mainly. The socket must be connected.
406 	*\param sock the socket object
407 	*\param buffer the data buffer to send
408 	*\param length the data length to send
409 	*\param delay_sec the maximum delay in second to wait before aborting
410 	*\return If the operation timed out, the function will return a GF_IP_SOCK_WOULD_BLOCK error.
411 	*/
412 	GF_Err gf_sk_send_wait(GF_Socket *sock, const char *buffer, u32 length, u32 delay_sec);
413 	/* receive data with a max wait delay of Second - used for http / ftp sockets mainly*/
414 	/*!
415 	*\brief receive data with wait delay
416 	*
417 	*Fetches data with a max wait delay. This is used for http / ftp sockets mainly. The socket must be connected.
418 	*\param sock the socket object
419 	*\param buffer the reception buffer where data is written
420 	*\param length the allocated size of the reception buffer
421 	*\param start_from the offset in the reception buffer where to start writing
422 	*\param read the actual number of bytes received
423 	*\param delay_sec the maximum delay in second to wait before aborting
424 	*\return If the operation timed out, the function will return a GF_IP_SOCK_WOULD_BLOCK error.
425 	*/
426 	GF_Err gf_sk_receive_wait(GF_Socket *sock, char *buffer, u32 length, u32 start_from, u32 *read, u32 delay_sec);
427 
428 	/*!
429 	*\brief gets socket handle
430 	*
431 	*Gets the socket low-level handle as used by OpenSSL.
432 	*\param sock the socket object
433 	*\return the socket handle
434 	*/
435 	s32 gf_sk_get_handle(GF_Socket *sock);
436 
437 
438 	/*!
439 	*\brief gets ipv6 support
440 	*
441 	*Returns IPV6 support information.
442 	*\return 2 if the machine has IPV6 support, 1 if the library was compiled with IPV6 support, 0 otherwise
443 	*/
444 	u32 gf_net_has_ipv6();
445 
446 
447 	/*!
448 	*\brief checks address type
449 	*
450 	*Checks if an address is an IPV6 or IPV4 one.
451 	*\param address Adress to check
452 	*\return true 1 if address is IPV6 one, 0 otherwise
453 	*/
454 	Bool gf_net_is_ipv6(const char *address);
455 
456 
457 	/*!
458 	*host to network conversion of integer
459 	*
460 	*\param val integrer to convert
461 	*\return converted integer
462 	*/
463 	u32 gf_htonl(u32 val);
464 	/*!
465 	*network to host conversion of integer
466 	*
467 	*\param val integrer to convert
468 	*\return converted integer
469 	*/
470 	u32 gf_ntohl(u32 val);
471 	/*!
472 	*host to network conversion of short integer
473 	*
474 	*\param val short integrer to convert
475 	*\return converted integer
476 	*/
477 	u16 gf_htons(u16 val);
478 	/*!
479 	*network to host conversion of short integer
480 	*
481 	*\param val short integrer to convert
482 	*\return converted integer
483 	*/
484 	u16 gf_tohs(u16 val);
485 
486 	/*!
487 	*	\brief MobileIP Callback
488 	*
489 	* The gf_net_mobileip_ctrl_cbk type is the type for the callback of the \ref gf_net_mobileip_set_callback function. By default no mobileip is used
490 	*	\param cbck Opaque user data.
491 	*	\param start boolean indicating wether the MobileIP subsystem should be started or stopped.
492 	*	\return Error code if needed.
493 	*
494 	*/
495 	typedef GF_Err(*gf_net_mobileip_ctrl_cbk)(Bool start);
496 
497 	/*!
498 	*\brief Assigns MobileIP callback
499 	*
500 	*Assigns the MobileIP control callback.
501 	*\param _mobip_cbk MobileIP control callback
502 	*\param MobileIP MobileIP address
503 	*/
504 	void gf_net_mobileip_set_callback(gf_net_mobileip_ctrl_cbk _mobip_cbk, const char *MobileIP);
505 
506 	/*! @} */
507 
508 #ifdef __cplusplus
509 }
510 #endif
511 
512 
513 #endif		/*_GF_NET_H_*/
514 
515