ucspi-ssl  0.99e
TLS encryption for IPv6 communication
socket_bind6.c
Go to the documentation of this file.
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 "ip6.h"
8 #include "haveip6.h"
9 #include "error.h"
10 
11 int socket_bind6(int s,const char ip[16],uint16 port,uint32 scope_id)
12 {
13 #ifdef LIBC_HAS_IP6
14  struct sockaddr_in6 sa;
15 
16  if (ipv4) {
17 #endif
18  int i;
19  for (i=0; i<16; i++)
20  if (ip[i]!=0) break;
21  if (i==16 || ip6_isv4mapped(ip))
22  return socket_bind4(s,ip+12,port);
23 #ifdef LIBC_HAS_IP6
24  }
25  byte_zero(&sa,sizeof sa);
26  sa.sin6_family = AF_INET6;
27  uint16_pack_big((char *) &sa.sin6_port,port);
28 /* implicit: sa.sin6_flowinfo = 0; */
29  byte_copy((char *) &sa.sin6_addr,16,ip);
30  sa.sin6_scope_id=scope_id;
31 
32  return bind(s,(struct sockaddr *) &sa,sizeof sa);
33 #else
34  errno=error_proto;
35  return -1;
36 #endif
37 }
38 
39 int socket_bind6_reuse(int s,const char ip[16],uint16 port,uint32 scope_id)
40 {
41  int opt = 1;
42  setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof opt);
43  return socket_bind6(s,ip,port,scope_id);
44 }
45 
void byte_copy(void *, unsigned int, const void *)
int error_proto
Definition: error.c:106
void uint16_pack_big(char *, uint16)
int socket_bind6(int s, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_bind6.c:11
#define ip6_isv4mapped(ip)
Definition: ip6.h:28
int ipv4
Definition: socket_tcp6.c:15
int socket_bind4(int, const char *, uint16)
void byte_zero(void *, unsigned int)
int socket_bind6_reuse(int s, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_bind6.c:39
unsigned short uint16
Definition: uint16.h:4