AROS: We have to provide the correct nfds to WaitSelect
[deb_libnfs.git] / aros / aros_compat.c
CommitLineData
d7c6e9aa
RS
1
2#ifdef AROS
3
2cdf4fcb
RS
4#include <stdio.h>
5#include <stdlib.h>
f69bd554 6#include <string.h>
d7c6e9aa
RS
7#include <sys/types.h>
8#include <sys/time.h>
f69bd554
RS
9#include <sys/socket.h>
10#include <netdb.h>
d7c6e9aa
RS
11#include "aros_compat.h"
12
13#undef poll
14
f69bd554
RS
15int aros_getnameinfo(const struct sockaddr *sa, socklen_t salen,
16char *host, size_t hostlen,
17char *serv, size_t servlen, int flags)
18{
19 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
20
21 if (host) {
22 snprintf(host, hostlen, Inet_NtoA(sin->sin_addr.s_addr));
23 }
24
25 return 0;
26}
27
28int aros_getaddrinfo(const char *node, const char*service,
29const struct addrinfo *hints,
30struct addrinfo **res)
31{
32 struct sockaddr_in *sin;
33
34 sin = malloc(sizeof(struct sockaddr_in));
35 sin->sin_len = sizeof(struct sockaddr_in);
36 sin->sin_family=AF_INET;
37
38 /* Some error checking would be nice */
39 sin->sin_addr.s_addr = inet_addr(node);
40
41 sin->sin_port=0;
42 if (service) {
5c9c5f15 43 sin->sin_port=htons(atoi(service));
f69bd554
RS
44 }
45
46 *res = malloc(sizeof(struct addrinfo));
47
48 (*res)->ai_family = AF_INET;
49 (*res)->ai_addrlen = sizeof(struct sockaddr_in);
50 (*res)->ai_addr = (struct sockaddr *)sin;
51
52 return 0;
53}
54
55void aros_freeaddrinfo(struct addrinfo *res)
56{
57 free(res->ai_addr);
58 free(res);
59}
60
61int aros_inet_pton(int af, char *src, void *dst)
62{
63 printf("No inet_pton yet");
64 exit(10);
65}
66
67
68/* unix device numbers dont really make much sense on aros ... */
e77d093c
RS
69int major(int i)
70{
71 return 1;
72}
73int minor(int i)
74{
75 return 2;
76}
d7c6e9aa 77
2cdf4fcb
RS
78struct Library * SocketBase = NULL;
79
80void aros_init_socket(void)
81{
82 if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) {
f69bd554 83 printf("NoTCP/IP Stack available");
2cdf4fcb
RS
84 exit(10);
85 }
86}
87
d7c6e9aa
RS
88int aros_poll(struct pollfd *fds, unsigned int nfds, int timo)
89{
90 struct timeval timeout, *toptr;
91 fd_set ifds, ofds, efds, *ip, *op;
a7954132 92 unsigned int i, maxfd = 0;
d7c6e9aa
RS
93 int rc;
94
95 // Set up the file-descriptor sets in ifds, ofds and efds.
96 FD_ZERO(&ifds);
97 FD_ZERO(&ofds);
98 FD_ZERO(&efds);
99 for (i = 0, op = ip = 0; i < nfds; ++i)
100 {
101 fds[i].revents = 0;
102 if(fds[i].events & (POLLIN|POLLPRI))
103 {
104 ip = &ifds;
105 FD_SET(fds[i].fd, ip);
106 }
107 if(fds[i].events & POLLOUT)
108 {
109 op = &ofds;
110 FD_SET(fds[i].fd, op);
111 }
112 FD_SET(fds[i].fd, &efds);
a7954132
RS
113 if (fds[i].fd > maxfd) {
114 maxfd = fds[i].fd;
115 }
d7c6e9aa
RS
116 }
117
118 // Set up the timeval structure for the timeout parameter
119 if(timo < 0)
120 {
121 toptr = 0;
122 }
123 else
124 {
125 toptr = &timeout;
126 timeout.tv_sec = timo / 1000;
127 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
128 }
129
a7954132 130 rc = WaitSelect(maxfd + 1, ip, op, &efds, toptr, NULL);
d7c6e9aa
RS
131
132 if(rc <= 0)
133 return rc;
134
135 if(rc > 0)
136 {
137 for (i = 0; i < nfds; ++i)
138 {
139 int fd = fds[i].fd;
140 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
141 fds[i].revents |= POLLIN;
142 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
143 fds[i].revents |= POLLOUT;
144 if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know.
145 fds[i].revents |= POLLHUP;
146 }
147 }
148 return rc;
149}
150
151#endif
f69bd554 152