libowfat/socket/socket_local6.c

54 lines
1.2 KiB
C
Raw Normal View History

2001-02-05 19:06:12 +00:00
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
2001-02-05 19:06:12 +00:00
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
2001-02-05 19:06:12 +00:00
#include "byte.h"
#include "socket.h"
#include "ip6.h"
#include "haveip6.h"
#include "uint32.h"
2001-11-24 20:18:37 +00:00
#include "havesl.h"
#include "havescope.h"
2001-02-05 19:06:12 +00:00
int socket_local6(int s,char ip[16],uint16 *port,uint32 *scope_id)
{
#ifdef LIBC_HAS_IP6
struct sockaddr_in6 si;
#else
struct sockaddr_in si;
#endif
2001-11-24 20:18:37 +00:00
socklen_t len = sizeof si;
2001-02-05 19:06:12 +00:00
if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return winsock2errno(-1);
2001-02-05 19:06:12 +00:00
#ifdef LIBC_HAS_IP6
if (si.sin6_family==AF_INET) {
struct sockaddr_in *si4=(struct sockaddr_in*)&si;
if (ip) {
byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si4->sin_addr);
}
if (port) uint16_unpack_big((char *) &si4->sin_port,port);
2001-02-05 19:06:12 +00:00
return 0;
}
if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
if (port) uint16_unpack_big((char *) &si.sin6_port,port);
#ifdef HAVE_SCOPE_ID
2001-02-05 19:06:12 +00:00
if (scope_id) *scope_id=si.sin6_scope_id;
#else
if (scope_id) *scope_id=0;
#endif
2001-02-05 19:06:12 +00:00
#else
if (ip) {
byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si.sin_addr);
}
if (port) uint16_unpack_big((char *) &si.sin_port,port);
2001-02-05 19:06:12 +00:00
if (scope_id) *scope_id=0;
#endif
return 0;
}