AROS: Add an example for using the SYNC API to AROS
[deb_libnfs.git] / aros / aros_compat.c
CommitLineData
03aea5e9
RS
1/*
2 Copyright (C) 2013 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
d7c6e9aa
RS
17
18#ifdef AROS
19
2cdf4fcb
RS
20#include <stdio.h>
21#include <stdlib.h>
f69bd554 22#include <string.h>
d7c6e9aa
RS
23#include <sys/types.h>
24#include <sys/time.h>
f69bd554
RS
25#include <sys/socket.h>
26#include <netdb.h>
d7c6e9aa 27#include "aros_compat.h"
f95e1d11
RS
28#include <errno.h>
29#include <bsdsocket/socketbasetags.h>
d7c6e9aa
RS
30
31#undef poll
32
f69bd554
RS
33int aros_getnameinfo(const struct sockaddr *sa, socklen_t salen,
34char *host, size_t hostlen,
35char *serv, size_t servlen, int flags)
36{
37 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
38
39 if (host) {
40 snprintf(host, hostlen, Inet_NtoA(sin->sin_addr.s_addr));
41 }
42
43 return 0;
44}
45
46int aros_getaddrinfo(const char *node, const char*service,
47const struct addrinfo *hints,
48struct addrinfo **res)
49{
50 struct sockaddr_in *sin;
51
52 sin = malloc(sizeof(struct sockaddr_in));
53 sin->sin_len = sizeof(struct sockaddr_in);
54 sin->sin_family=AF_INET;
55
56 /* Some error checking would be nice */
57 sin->sin_addr.s_addr = inet_addr(node);
58
59 sin->sin_port=0;
60 if (service) {
5c9c5f15 61 sin->sin_port=htons(atoi(service));
f69bd554
RS
62 }
63
64 *res = malloc(sizeof(struct addrinfo));
65
66 (*res)->ai_family = AF_INET;
67 (*res)->ai_addrlen = sizeof(struct sockaddr_in);
68 (*res)->ai_addr = (struct sockaddr *)sin;
69
70 return 0;
71}
72
73void aros_freeaddrinfo(struct addrinfo *res)
74{
75 free(res->ai_addr);
76 free(res);
77}
78
79int aros_inet_pton(int af, char *src, void *dst)
80{
9a9126c3
RS
81 struct sockaddr_in sin;
82
83 sin.sin_addr.s_addr = inet_addr(src);
a41dbfe2 84 memcpy(dst, &sin.sin_addr.s_addr, sizeof(sin.sin_addr.s_addr));
9a9126c3 85 return 1;
f69bd554
RS
86}
87
88
89/* unix device numbers dont really make much sense on aros ... */
e77d093c
RS
90int major(int i)
91{
92 return 1;
93}
94int minor(int i)
95{
96 return 2;
97}
d7c6e9aa 98
2cdf4fcb
RS
99struct Library * SocketBase = NULL;
100
f95e1d11
RS
101extern int errno;
102int h_errno = 0;
103
104
2cdf4fcb
RS
105void aros_init_socket(void)
106{
107 if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) {
f69bd554 108 printf("NoTCP/IP Stack available");
2cdf4fcb
RS
109 exit(10);
110 }
f95e1d11
RS
111 if (SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))),
112 (IPTR)&errno,
113 SBTM_SETVAL(SBTC_HERRNOLONGPTR),
114 (IPTR)&h_errno, TAG_DONE)) {
115 printf("Failed to set ERRNO");
116 exit(10);
117 }
2cdf4fcb
RS
118}
119
d7c6e9aa
RS
120int aros_poll(struct pollfd *fds, unsigned int nfds, int timo)
121{
122 struct timeval timeout, *toptr;
123 fd_set ifds, ofds, efds, *ip, *op;
a7954132 124 unsigned int i, maxfd = 0;
d7c6e9aa
RS
125 int rc;
126
127 // Set up the file-descriptor sets in ifds, ofds and efds.
128 FD_ZERO(&ifds);
129 FD_ZERO(&ofds);
130 FD_ZERO(&efds);
131 for (i = 0, op = ip = 0; i < nfds; ++i)
132 {
133 fds[i].revents = 0;
134 if(fds[i].events & (POLLIN|POLLPRI))
135 {
136 ip = &ifds;
137 FD_SET(fds[i].fd, ip);
138 }
139 if(fds[i].events & POLLOUT)
140 {
141 op = &ofds;
142 FD_SET(fds[i].fd, op);
143 }
144 FD_SET(fds[i].fd, &efds);
a7954132
RS
145 if (fds[i].fd > maxfd) {
146 maxfd = fds[i].fd;
147 }
d7c6e9aa
RS
148 }
149
150 // Set up the timeval structure for the timeout parameter
151 if(timo < 0)
152 {
153 toptr = 0;
154 }
155 else
156 {
157 toptr = &timeout;
158 timeout.tv_sec = timo / 1000;
159 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
160 }
161
a7954132 162 rc = WaitSelect(maxfd + 1, ip, op, &efds, toptr, NULL);
d7c6e9aa
RS
163
164 if(rc <= 0)
165 return rc;
166
167 if(rc > 0)
168 {
169 for (i = 0; i < nfds; ++i)
170 {
171 int fd = fds[i].fd;
172 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
173 fds[i].revents |= POLLIN;
174 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
175 fds[i].revents |= POLLOUT;
176 if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know.
177 fds[i].revents |= POLLHUP;
178 }
179 }
180 return rc;
181}
182
183#endif
f69bd554 184