1 #include <sys/types.h>
2 #include <sys/param.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include "byte.h"
6 #include "socket.h"
7 #include "ip4.h"
8 #include "ip6.h"
9 #include "haveip6.h"
10 #include "error.h"
11
socket_send6(int s,const char * buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id)12 int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id)
13 {
14 #ifdef LIBC_HAS_IP6
15 struct sockaddr_in6 sa;
16 #else
17 struct sockaddr_in sa;
18 #endif
19
20 byte_zero(&sa,sizeof sa);
21 #ifdef LIBC_HAS_IP6
22 if (ipv4) {
23 #endif
24 if (ip6_isv4mapped(ip))
25 return socket_send4(s,buf,len,ip+12,port);
26 if (byte_equal(ip,16,V6loopback))
27 return socket_send4(s,buf,len,ip4loopback,port);
28 #ifdef LIBC_HAS_IP6
29 errno=error_proto;
30 return -1;
31 }
32 sa.sin6_family = AF_INET6;
33 uint16_pack_big((char *) &sa.sin6_port,port);
34 byte_copy((char *) &sa.sin6_addr,16,ip);
35 return sendto(s,buf,len,0,(struct sockaddr *) &sa,sizeof sa);
36 #else
37 errno=error_proto;
38 return -1;
39 #endif
40 }
41