fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
socket_setup.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_accept(int s,char ip[16],uint16 *port,uint32 *scope_id)
17{
18 struct sockaddr_in6 sa;
19 unsigned int dummy = sizeof(sa);
20 int fd;
21
22 fd = accept(s,(struct sockaddr *)&sa,&dummy);
23 if (fd == -1) return -1;
24
25 if (sa.sin6_family == AF_INET) {
26 struct sockaddr_in *sa4 = (struct sockaddr_in*)&sa;
28 byte_copy(ip + 12,4,(char *)&sa4->sin_addr);
29 uint16_unpack_big((char *)&sa4->sin_port,port);
30 if (scope_id) *scope_id = 0;
31 } else {
32 byte_copy(ip,16,(char *)&sa.sin6_addr);
33 uint16_unpack_big((char *)&sa.sin6_port,port);
34 if (scope_id) *scope_id = sa.sin6_scope_id;
35 }
36
37 return fd;
38}
39
40int socket_accept4(int s,char ip[4],uint16 *port)
41{
42 struct sockaddr_in sa;
43 unsigned int dummy = sizeof(sa);
44 int fd;
45
46 fd = accept(s,(struct sockaddr *) &sa,&dummy);
47 if (fd == -1) return -1;
48
49 byte_copy(ip,4,(char *) &sa.sin_addr);
50 uint16_unpack_big((char *) &sa.sin_port,port);
51
52 return fd;
53}
54
55int socket_listen(int s,int backlog)
56{
57 return listen(s,backlog);
58}
59
61{
62 int r;
63
64 r = setsockopt(s,IPPROTO_IP,1,(char *) 0,0); /* 1 == IP_OPTIONS */
65 r = setsockopt(s,IPPROTO_IPV6,1,(char *) 0,0);
66
67 return r;
68}
69
71{
72 int opt = 1;
73 int r;
74
75#ifdef GEN_IP_PKTINFO /* Linux */
76 r = setsockopt(s,IPPROTO_IP,GEN_IP_PKTINFO,&opt,sizeof(opt));
77#elif IP_PKTINFO /* Solaris */
78 r = setsockopt(s,IPPROTO_IP,IP_PKTINFO,&opt,sizeof(opt));
79#elif IP_RECVDSTADDR /* BSD */
80 r = setsockopt(s,IPPROTO_IP,IP_RECVDSTADDR,&opt,sizeof(opt));
81#elif IPV6_RECVDSTADDR
82 r = setsockopt(s,IPPROTO_IPV6,IP_RECVDSTADDR,&opt,sizeof(opt));
83#endif
84 return r;
85}
86
88{
89 int opt = 0;
90
91 return setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&opt,sizeof(opt));
92}
93
95{
96 int opt = 1;
97
98 return setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&opt,sizeof(opt));
99}
int socket_listen(int s, int backlog)
Definition: socket_setup.c:55
int socket_accept4(int s, char ip[4], uint16 *port)
Definition: socket_setup.c:40
int socket_ipoptionskill(int s)
Definition: socket_setup.c:60
int socket_ip6anycast(int s)
Definition: socket_setup.c:70
int socket_dualstack(int s)
Definition: socket_setup.c:87
int socket_accept(int s, char ip[16], uint16 *port, uint32 *scope_id)
Definition: socket_setup.c:16
int socket_nodualstack(int s)
Definition: socket_setup.c:94
const unsigned char V4mappedprefix[12]
Definition: socket_if.c:18
uint16_t uint16
Definition: uint_t.h:29
uint32_t uint32
Definition: uint_t.h:40
void uint16_unpack_big(char[16], uint16 *)
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20