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