AROS: we need htons to convert socket port to network order
[deb_libnfs.git] / aros / aros_compat.c
1
2 #ifdef AROS
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/time.h>
9 #include <sys/socket.h>
10 #include <netdb.h>
11 #include "aros_compat.h"
12
13 #undef poll
14
15 int aros_getnameinfo(const struct sockaddr *sa, socklen_t salen,
16 char *host, size_t hostlen,
17 char *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
28 int aros_getaddrinfo(const char *node, const char*service,
29 const struct addrinfo *hints,
30 struct 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) {
43 sin->sin_port=htons(atoi(service));
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
55 void aros_freeaddrinfo(struct addrinfo *res)
56 {
57 free(res->ai_addr);
58 free(res);
59 }
60
61 int 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 ... */
69 int major(int i)
70 {
71 return 1;
72 }
73 int minor(int i)
74 {
75 return 2;
76 }
77
78 struct Library * SocketBase = NULL;
79
80 void aros_init_socket(void)
81 {
82 if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) {
83 printf("NoTCP/IP Stack available");
84 exit(10);
85 }
86 }
87
88 int aros_poll(struct pollfd *fds, unsigned int nfds, int timo)
89 {
90 struct timeval timeout, *toptr;
91 fd_set ifds, ofds, efds, *ip, *op;
92 unsigned int i;
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);
113 }
114
115 // Set up the timeval structure for the timeout parameter
116 if(timo < 0)
117 {
118 toptr = 0;
119 }
120 else
121 {
122 toptr = &timeout;
123 timeout.tv_sec = timo / 1000;
124 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
125 }
126
127 rc = WaitSelect(0, ip, op, &efds, toptr, NULL);
128
129 if(rc <= 0)
130 return rc;
131
132 if(rc > 0)
133 {
134 for (i = 0; i < nfds; ++i)
135 {
136 int fd = fds[i].fd;
137 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
138 fds[i].revents |= POLLIN;
139 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
140 fds[i].revents |= POLLOUT;
141 if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know.
142 fds[i].revents |= POLLHUP;
143 }
144 }
145 return rc;
146 }
147
148 #endif
149