mirror of
https://code.videolan.org/videolan/vlc
synced 2024-09-16 16:02:54 +02:00
inet_pton() replacement
This commit is contained in:
parent
5cd565b2af
commit
2c91228b55
@ -360,6 +360,12 @@ VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, c
|
||||
#define net_GetPeerAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_TRUE,b,c,d)
|
||||
VLC_EXPORT( int, __net_GetAddress, ( vlc_object_t *p_this, vlc_bool_t peer, int fd, char *address, int *port ) );
|
||||
|
||||
#if !HAVE_INET_PTON
|
||||
/* only in core, so no need for C++ extern "C" */
|
||||
int inet_pton(int af, const char *src, void *dst);
|
||||
#endif
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* net_StopRecv/Send
|
||||
*****************************************************************************
|
||||
|
@ -1185,3 +1185,62 @@ int __net_GetAddress( vlc_object_t *p_this, vlc_bool_t peer, int fd,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* inet_pton replacement for obsolete and/or crap operating systems
|
||||
*****************************************************************************/
|
||||
#ifndef HAVE_INET_PTON
|
||||
int inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
# ifdef WIN32
|
||||
/* As we already know, Microsoft always go its own way, so even if they do
|
||||
* provide IPv6, they don't provide the API. */
|
||||
struct sockaddr_storage addr;
|
||||
int len = sizeof( addr );
|
||||
|
||||
/* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
|
||||
char *workaround_for_ill_designed_api = strdup( src );
|
||||
|
||||
if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
|
||||
(LPSOCKADDR)&addr, &len ) )
|
||||
{
|
||||
free( workaround_for_ill_designed_api );
|
||||
return -1;
|
||||
}
|
||||
free( workaround_for_ill_designed_api );
|
||||
|
||||
switch( af )
|
||||
{
|
||||
case AF_INET6:
|
||||
memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
|
||||
break;
|
||||
|
||||
case AF_INET:
|
||||
memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
|
||||
break;
|
||||
|
||||
default:
|
||||
WSASetLastError( WSAEAFNOSUPPORT );
|
||||
return -1;
|
||||
}
|
||||
# else
|
||||
/* Assume IPv6 is not supported. */
|
||||
/* Would be safer and more simpler to use inet_aton() but it is most
|
||||
* likely not provided either. */
|
||||
uint32_t ipv4;
|
||||
|
||||
if( af != AF_INET )
|
||||
{
|
||||
errno = EAFNOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ipv4 = inet_addr( src );
|
||||
if( ipv4 == INADDR_NONE )
|
||||
return -1;
|
||||
|
||||
memcpy( dst; &ipv4, 4 );
|
||||
# endif /* WIN32 */
|
||||
return 0;
|
||||
}
|
||||
#endif /* HAVE_INET_PTON */
|
||||
|
@ -300,7 +300,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
|
||||
|
||||
switch( addr.ss_family )
|
||||
{
|
||||
#if defined (HAVE_GETADDRINFO) || defined (WIN32)
|
||||
#if defined (INET_PTON) || defined (WIN32)
|
||||
case AF_INET6:
|
||||
{
|
||||
/* See RFC3513 for list of valid IPv6 scopes */
|
||||
@ -457,29 +457,8 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
|
||||
psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
|
||||
psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */
|
||||
|
||||
#if defined(WIN32)
|
||||
if( b_ipv6 )
|
||||
{
|
||||
struct sockaddr_in6 saddr;
|
||||
int len = sizeof(saddr);
|
||||
if( ! WSAStringToAddress(p_sap_session->p_address->psz_machine,
|
||||
AF_INET6, NULL, (LPSOCKADDR)&saddr, &len) )
|
||||
return VLC_ENOMEM;
|
||||
memcpy(psz_head+4, &saddr.sin6_addr, sizeof(struct in6_addr));
|
||||
}
|
||||
else
|
||||
{
|
||||
struct sockaddr_in saddr;
|
||||
int len = sizeof(saddr);
|
||||
if( ! WSAStringToAddress(p_sap_session->p_address->psz_machine,
|
||||
AF_INET, NULL, (LPSOCKADDR)&saddr, &len) )
|
||||
return VLC_ENOMEM;
|
||||
memcpy(psz_head+4, &saddr.sin_addr, sizeof(struct in_addr));
|
||||
}
|
||||
#else
|
||||
inet_pton( b_ipv6 ? AF_INET6 : AF_INET, /* can't fail */
|
||||
p_sap_session->p_address->psz_machine, psz_head + 4 );
|
||||
#endif
|
||||
|
||||
memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user