Skip to content

Commit f4e08b0

Browse files
Gil PitneyAnas Nashif
authored andcommitted
net: sockets: Define POSIX function symbols as inline functions
Previously, POSIX function names were aliased to zsock_ function names using, for example: #define connect zsock_connect This caused the C preprocessor to replace any symbol named 'connect', whether a function or not, in all source code which included socket.h, with 'zsock_connect'. This generated unintended code where the symbol 'connect' was used as the name of a structure field (as in mqtt.h). This new inline definition is applied to all the POSIX function symbols, with the exception of fcntl, a redefinition of which would conflict with the definiton in the toolchain's fcntl.h. Fixes #5817 Signed-off-by: Gil Pitney <[email protected]>
1 parent d1675bf commit f4e08b0

File tree

1 file changed

+78
-15
lines changed

1 file changed

+78
-15
lines changed

include/net/socket.h

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,97 @@ int zsock_getaddrinfo(const char *host, const char *service,
7474
struct zsock_addrinfo **res);
7575

7676
#if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
77-
#define socket zsock_socket
78-
#define close zsock_close
79-
#define bind zsock_bind
80-
#define connect zsock_connect
81-
#define listen zsock_listen
82-
#define accept zsock_accept
83-
#define send zsock_send
84-
#define recv zsock_recv
77+
static inline int socket(int family, int type, int proto)
78+
{
79+
return zsock_socket(family, type, proto);
80+
}
81+
82+
static inline int close(int sock)
83+
{
84+
return zsock_close(sock);
85+
}
86+
87+
static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
88+
{
89+
return zsock_bind(sock, addr, addrlen);
90+
}
91+
92+
static inline int connect(int sock, const struct sockaddr *addr,
93+
socklen_t addrlen)
94+
{
95+
return zsock_connect(sock, addr, addrlen);
96+
}
97+
98+
static inline int listen(int sock, int backlog)
99+
{
100+
return zsock_listen(sock, backlog);
101+
}
102+
103+
static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
104+
{
105+
return zsock_accept(sock, addr, addrlen);
106+
}
107+
108+
static inline ssize_t send(int sock, const void *buf, size_t len, int flags)
109+
{
110+
return zsock_send(sock, buf, len, flags);
111+
}
112+
113+
static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
114+
{
115+
return zsock_recv(sock, buf, max_len, flags);
116+
}
117+
118+
/* This conflicts with fcntl.h, so code must include fcntl.h before socket.h: */
85119
#define fcntl zsock_fcntl
86-
#define sendto zsock_sendto
87-
#define recvfrom zsock_recvfrom
88120

89-
#define poll zsock_poll
121+
static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
122+
const struct sockaddr *dest_addr,
123+
socklen_t addrlen)
124+
{
125+
return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
126+
}
127+
128+
static inline ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags,
129+
struct sockaddr *src_addr, socklen_t *addrlen)
130+
{
131+
return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
132+
}
133+
134+
static inline int poll(struct zsock_pollfd *fds, int nfds, int timeout)
135+
{
136+
return zsock_poll(fds, nfds, timeout);
137+
}
138+
90139
#define pollfd zsock_pollfd
91140
#define POLLIN ZSOCK_POLLIN
92141
#define POLLOUT ZSOCK_POLLOUT
93142

94-
#define inet_ntop net_addr_ntop
95-
#define inet_pton zsock_inet_pton
143+
static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
144+
size_t size)
145+
{
146+
return net_addr_ntop(family, src, dst, size);
147+
}
148+
149+
static inline int inet_pton(sa_family_t family, const char *src, void *dst)
150+
{
151+
return zsock_inet_pton(family, src, dst);
152+
}
153+
154+
static inline int getaddrinfo(const char *host, const char *service,
155+
const struct zsock_addrinfo *hints,
156+
struct zsock_addrinfo **res)
157+
{
158+
return zsock_getaddrinfo(host, service, hints, res);
159+
}
96160

97-
#define getaddrinfo zsock_getaddrinfo
98161
#define addrinfo zsock_addrinfo
99162
#define EAI_BADFLAGS DNS_EAI_BADFLAGS
100163
#define EAI_NONAME DNS_EAI_NONAME
101164
#define EAI_AGAIN DNS_EAI_AGAIN
102165
#define EAI_FAIL DNS_EAI_FAIL
103166
#define EAI_NODATA DNS_EAI_NODATA
104-
#endif
167+
#endif /* defined(CONFIG_NET_SOCKETS_POSIX_NAMES) */
105168

106169
#ifdef __cplusplus
107170
}

0 commit comments

Comments
 (0)