11#define LIBCGLUE_SYS_SOCKET_NO_ALIASES
12#define LIBCGLUE_ARPA_INET_NO_ALIASES
13#include <sys/socket.h>
18#include <netinet/in.h>
24extern int __transform_errno(
int res);
27int socket(
int domain,
int type,
int protocol)
32 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->socket == NULL)
38 fd = __fdman_get_new_descriptor();
45 info = &(__descriptormap[fd]->info);
46 iop_fd = _libcglue_fdman_socket_ops->socket(
info, domain, type, protocol);
49 __fdman_release_descriptor(fd);
50 return __transform_errno(iop_fd);
52 __descriptormap[fd]->flags = 0;
59int accept(
int fd,
struct sockaddr *addr, socklen_t *addrlen)
61 int new_iop_fd, new_fd;
64 if (!__IS_FD_VALID(fd)) {
71 fdinfo = &(__descriptormap[fd]->info);
72 if (fdinfo->ops == NULL || fdinfo->ops->accept == NULL)
78 new_fd = __fdman_get_new_descriptor();
85 info = &(__descriptormap[new_fd]->info);
86 new_iop_fd = fdinfo->ops->accept(fdinfo->userdata,
info, addr, addrlen);
89 __fdman_release_descriptor(new_fd);
90 return __transform_errno(new_iop_fd);
92 __descriptormap[new_fd]->flags = 0;
98int bind(
int fd,
const struct sockaddr *my_addr, socklen_t addrlen)
100 if (!__IS_FD_VALID(fd)) {
107 fdinfo = &(__descriptormap[fd]->info);
108 if (fdinfo->ops == NULL || fdinfo->ops->bind == NULL)
113 return __transform_errno(fdinfo->ops->bind(fdinfo->userdata, my_addr, addrlen));
118int connect(
int fd,
const struct sockaddr *serv_addr, socklen_t addrlen)
120 if (!__IS_FD_VALID(fd)) {
127 fdinfo = &(__descriptormap[fd]->info);
128 if (fdinfo->ops == NULL || fdinfo->ops->connect == NULL)
133 return __transform_errno(fdinfo->ops->connect(fdinfo->userdata, serv_addr, addrlen));
138int listen(
int fd,
int backlog)
140 if (!__IS_FD_VALID(fd)) {
147 fdinfo = &(__descriptormap[fd]->info);
148 if (fdinfo->ops == NULL || fdinfo->ops->listen == NULL)
153 return __transform_errno(fdinfo->ops->listen(fdinfo->userdata, backlog));
158ssize_t recv(
int fd,
void *buf,
size_t len,
int flags)
160 if (!__IS_FD_VALID(fd)) {
167 fdinfo = &(__descriptormap[fd]->info);
168 if (fdinfo->ops == NULL || fdinfo->ops->recv == NULL)
173 return __transform_errno(fdinfo->ops->recv(fdinfo->userdata, buf, len, flags));
178ssize_t recvfrom(
int fd,
void *buf,
size_t len,
int flags,
struct sockaddr *from, socklen_t *fromlen)
180 if (!__IS_FD_VALID(fd)) {
187 fdinfo = &(__descriptormap[fd]->info);
188 if (fdinfo->ops == NULL || fdinfo->ops->recvfrom == NULL)
193 return __transform_errno(fdinfo->ops->recvfrom(fdinfo->userdata, buf, len, flags, from, fromlen));
198ssize_t recvmsg(
int fd,
struct msghdr *msg,
int flags)
200 if (!__IS_FD_VALID(fd)) {
207 fdinfo = &(__descriptormap[fd]->info);
208 if (fdinfo->ops == NULL || fdinfo->ops->recvmsg == NULL)
213 return __transform_errno(fdinfo->ops->recvmsg(fdinfo->userdata, msg, flags));
218ssize_t send(
int fd,
const void *buf,
size_t len,
int flags)
220 if (!__IS_FD_VALID(fd)) {
227 fdinfo = &(__descriptormap[fd]->info);
228 if (fdinfo->ops == NULL || fdinfo->ops->send == NULL)
233 return __transform_errno(fdinfo->ops->send(fdinfo->userdata, buf, len, flags));
238ssize_t sendto(
int fd,
const void *buf,
size_t len,
int flags,
const struct sockaddr *to, socklen_t tolen)
240 if (!__IS_FD_VALID(fd)) {
247 fdinfo = &(__descriptormap[fd]->info);
248 if (fdinfo->ops == NULL || fdinfo->ops->sendto == NULL)
253 return __transform_errno(fdinfo->ops->sendto(fdinfo->userdata, buf, len, flags, to, tolen));
258ssize_t sendmsg(
int fd,
const struct msghdr *msg,
int flags)
260 if (!__IS_FD_VALID(fd)) {
267 fdinfo = &(__descriptormap[fd]->info);
268 if (fdinfo->ops == NULL || fdinfo->ops->sendmsg == NULL)
273 return __transform_errno(fdinfo->ops->sendmsg(fdinfo->userdata, msg, flags));
278int getsockopt(
int fd,
int level,
int optname,
void *optval, socklen_t *optlen)
280 if (!__IS_FD_VALID(fd)) {
287 fdinfo = &(__descriptormap[fd]->info);
288 if (fdinfo->ops == NULL || fdinfo->ops->getsockopt == NULL)
293 return __transform_errno(fdinfo->ops->getsockopt(fdinfo->userdata, level, optname, optval, optlen));
298int setsockopt(
int fd,
int level,
int optname,
const void *optval, socklen_t optlen)
300 if (!__IS_FD_VALID(fd)) {
307 fdinfo = &(__descriptormap[fd]->info);
308 if (fdinfo->ops == NULL || fdinfo->ops->setsockopt == NULL)
313 return __transform_errno(fdinfo->ops->setsockopt(fdinfo->userdata, level, optname, optval, optlen));
318int shutdown(
int fd,
int how)
320 if (!__IS_FD_VALID(fd)) {
327 fdinfo = &(__descriptormap[fd]->info);
328 if (fdinfo->ops == NULL || fdinfo->ops->shutdown == NULL)
333 return __transform_errno(fdinfo->ops->shutdown(fdinfo->userdata, how));
338int getpeername(
int fd,
struct sockaddr *name, socklen_t *namelen)
340 if (!__IS_FD_VALID(fd)) {
347 fdinfo = &(__descriptormap[fd]->info);
348 if (fdinfo->ops == NULL || fdinfo->ops->getpeername == NULL)
353 return __transform_errno(fdinfo->ops->getpeername(fdinfo->userdata, name, namelen));
358int getsockname(
int fd,
struct sockaddr *name, socklen_t *namelen)
360 if (!__IS_FD_VALID(fd)) {
367 fdinfo = &(__descriptormap[fd]->info);
368 if (fdinfo->ops == NULL || fdinfo->ops->getsockname == NULL)
373 return __transform_errno(fdinfo->ops->getsockname(fdinfo->userdata, name, namelen));
377#ifdef F_libcglue_inet_addr
378u32 libcglue_inet_addr(
const char *cp)
380 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_addr == NULL)
385 return _libcglue_fdman_inet_ops->inet_addr(cp);
389#ifdef F_libcglue_inet_ntoa
390char *libcglue_inet_ntoa(
const ip4_addr_t *addr)
392 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_ntoa == NULL)
397 return _libcglue_fdman_inet_ops->inet_ntoa(addr);
401#ifdef F_libcglue_inet_ntoa_r
402char *libcglue_inet_ntoa_r(
const ip4_addr_t *addr,
char *buf,
int buflen)
404 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_ntoa_r == NULL)
409 return _libcglue_fdman_inet_ops->inet_ntoa_r(addr, buf, buflen);
413#ifdef F_libcglue_inet_aton
414int libcglue_inet_aton(
const char *cp,
ip4_addr_t *addr)
416 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_aton == NULL)
421 return _libcglue_fdman_inet_ops->inet_aton(cp, addr);
425#ifdef F_libcglue_ps2ip_setconfig
426int libcglue_ps2ip_setconfig(
t_ip_info *ip_info)
428 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->setconfig == NULL)
433 return _libcglue_fdman_socket_ops->setconfig(ip_info);
437#ifdef F_libcglue_ps2ip_getconfig
438int libcglue_ps2ip_getconfig(
char *netif_name,
t_ip_info *ip_info)
440 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->getconfig == NULL)
445 return _libcglue_fdman_socket_ops->getconfig(netif_name, ip_info);
449#ifdef F_libcglue_dns_setserver
450void libcglue_dns_setserver(u8 numdns,
ip_addr_t *dnsserver)
452 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->dns_setserver == NULL)
457 _libcglue_fdman_socket_ops->dns_setserver(numdns, dnsserver);
461#ifdef F_libcglue_dns_getserver
462const ip_addr_t *libcglue_dns_getserver(u8 numdns)
464 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->dns_getserver == NULL)
469 return _libcglue_fdman_socket_ops->dns_getserver(numdns);