Project Alice
Loading...
Searching...
No Matches
pcp_socket.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2014 by Cisco Systems, Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PCP_SOCKET_H
27#define PCP_SOCKET_H
28
29#include "pcp.h"
30
31#ifdef PCP_SOCKET_IS_VOIDPTR
32#define PD_SOCKET_STARTUP()
33#define PD_SOCKET_CLEANUP()
34#define PCP_INVALID_SOCKET NULL
35#define PCP_SOCKET_ERROR (-1)
36
37#ifdef WIN32
38#define CLOSE(sockfd) closesocket(sockfd)
39#else
40#define CLOSE(sockfd) close(sockfd)
41#endif
42
43#else
44
45#ifdef WIN32
46
47int pcp_win_sock_startup();
48int pcp_win_sock_cleanup();
49
50#define PD_SOCKET_STARTUP pcp_win_sock_startup
51#define PD_SOCKET_CLEANUP pcp_win_sock_cleanup
52#define PCP_SOCKET_ERROR SOCKET_ERROR
53#define PCP_INVALID_SOCKET INVALID_SOCKET
54#define CLOSE(sockfd) closesocket(sockfd)
55
56#else
57
58#define PD_SOCKET_STARTUP()
59#define PD_SOCKET_CLEANUP()
60#define PCP_SOCKET_ERROR (-1)
61#define PCP_INVALID_SOCKET (-1)
62#define CLOSE(sockfd) close(sockfd)
63
64#endif
65#endif
66
67struct pcp_ctx_s;
68
70
71void pcp_fill_in6_addr(struct in6_addr *dst_ip6, uint16_t *dst_port,
72 struct sockaddr *src);
73
74void pcp_fill_sockaddr(struct sockaddr *dst, struct in6_addr *sip,
75 uint16_t sport, int ret_ipv6_mapped_ipv4, uint32_t scope_id);
76
77PCP_SOCKET pcp_socket_create(struct pcp_ctx_s *ctx, int domain, int type,
78 int protocol);
79
80ssize_t pcp_socket_recvfrom(struct pcp_ctx_s *ctx, void *buf, size_t len,
81 int flags, struct sockaddr *src_addr, socklen_t *addrlen);
82
83ssize_t pcp_socket_sendto(struct pcp_ctx_s *ctx, const void *buf, size_t len,
84 int flags, struct sockaddr *dest_addr, socklen_t addrlen);
85
86int pcp_socket_close(struct pcp_ctx_s *ctx);
87
88/*In Visual Studio inline keyword only available in C++ */
89#if (defined(_MSC_VER) && !defined(inline))
90#define inline __inline
91#endif
92
93#ifndef SA_LEN
94#ifdef HAVE_SOCKADDR_SA_LEN
95#define SA_LEN(addr) ((addr)->sa_len)
96#else /* HAVE_SOCKADDR_SA_LEN */
97
98static inline size_t get_sa_len(struct sockaddr *addr)
99{
100 switch (addr->sa_family) {
101
102 case AF_INET:
103 return (sizeof(struct sockaddr_in));
104
105 case AF_INET6:
106 return (sizeof(struct sockaddr_in6));
107
108 default:
109 return (sizeof(struct sockaddr));
110 }
111}
112#define SA_LEN(addr) (get_sa_len(addr))
113#endif /* HAVE_SOCKADDR_SA_LEN */
114#endif /* SA_LEN */
115
116#ifdef HAVE_SOCKADDR_SA_LEN
117#define SET_SA_LEN(s, l) ((struct sockaddr*)s)->sa_len=l
118#else
119#define SET_SA_LEN(s, l)
120#endif
121
122#endif /* PCP_SOCKET_H*/
uint uint32_t
#define PCP_SOCKET
Definition: pcp.h:55
void pcp_fill_in6_addr(struct in6_addr *dst_ip6, uint16_t *dst_port, struct sockaddr *src)
Definition: pcp_socket.c:99
int pcp_socket_close(struct pcp_ctx_s *ctx)
Definition: pcp_socket.c:207
pcp_socket_vt_t default_socket_vt
Definition: pcp_socket.c:58
ssize_t pcp_socket_recvfrom(struct pcp_ctx_s *ctx, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
Definition: pcp_socket.c:189
PCP_SOCKET pcp_socket_create(struct pcp_ctx_s *ctx, int domain, int type, int protocol)
Definition: pcp_socket.c:181
void pcp_fill_sockaddr(struct sockaddr *dst, struct in6_addr *sip, uint16_t sport, int ret_ipv6_mapped_ipv4, uint32_t scope_id)
Definition: pcp_socket.c:132
ssize_t pcp_socket_sendto(struct pcp_ctx_s *ctx, const void *buf, size_t len, int flags, struct sockaddr *dest_addr, socklen_t addrlen)
Definition: pcp_socket.c:198
#define ssize_t