fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
socket_bind.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_if.h"
7#include "ip.h"
8
16int socket_bind4(int s,const char ip[4],uint16 port)
17{
18 struct sockaddr_in sa;
19
20 byte_zero(&sa,sizeof(sa));
21 sa.sin_family = AF_INET;
22 uint16_pack_big((char *)&sa.sin_port,port);
23 byte_copy((char *)&sa.sin_addr,4,ip);
24
25 return bind(s,(struct sockaddr *)&sa,sizeof(sa));
26}
27
28int socket_bind4_reuse(int s,const char ip[4],uint16 port)
29{
30 int opt = 1;
31 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
32 return socket_bind4(s,ip,port);
33}
34
35/* seems not to be used here -- djbdns requires it */
36void socket_tryreservein(int s,int size)
37{
38 while (size >= 1024) {
39 if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size)) == 0) return;
40 size -= (size >> 5);
41 }
42}
43
44int socket_bind6(int s,const char ip[16],uint16 port,uint32 scope_id)
45{
46 struct sockaddr_in6 sa;
47
48 byte_zero(&sa,sizeof(sa));
49 sa.sin6_family = AF_INET6;
50 uint16_pack_big((char *)&sa.sin6_port,port);
51/* implicit: sa.sin6_flowinfo = 0; */
52 byte_copy((char *)&sa.sin6_addr,16,ip);
53 sa.sin6_scope_id = scope_id;
54
55 return bind(s,(struct sockaddr *)&sa,sizeof(sa));
56}
57
58int socket_bind6_reuse(int s,const char ip[16],uint16 port,uint32 scope_id)
59{
60 int opt = 1;
61 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
62 return socket_bind6(s,ip,port,scope_id);
63}
64
65int socket_bind(int s,const char ip[16],uint16 port,uint32 scope_id)
66{
67 if (ip6_isv4mapped(ip))
68 return socket_bind4(s,ip + 12,port);
69
70 return socket_bind6(s,ip,port,scope_id);
71}
72
73int socket_bind_reuse(int s,const char ip[16],uint16 port,uint32 scope_id)
74{
75 if (ip6_isv4mapped(ip))
76 return socket_bind4_reuse(s,ip + 12,port);
77
78 return socket_bind6_reuse(s,ip,port,scope_id);
79}
int socket_bind(int s, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_bind.c:65
int socket_bind4(int s, const char ip[4], uint16 port)
Definition: socket_bind.c:16
int socket_bind4_reuse(int s, const char ip[4], uint16 port)
Definition: socket_bind.c:28
int socket_bind6_reuse(int s, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_bind.c:58
void socket_tryreservein(int s, int size)
Definition: socket_bind.c:36
int socket_bind6(int s, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_bind.c:44
int socket_bind_reuse(int s, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_bind.c:73
void uint16_pack_big(char[16], uint16)
uint16_t uint16
Definition: uint_t.h:29
uint32_t uint32
Definition: uint_t.h:40
#define ip6_isv4mapped(ip)
Definition: ip.h:105
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
void byte_zero(void *, unsigned int)
Definition: byte.c:65