2 Copyright (C) 2013 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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.
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/>.
23 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include "aros_compat.h"
29 #include <bsdsocket/socketbasetags.h>
33 int aros_getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
34 char *host
, size_t hostlen
,
35 char *serv
, size_t servlen
, int flags
)
37 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
40 snprintf(host
, hostlen
, Inet_NtoA(sin
->sin_addr
.s_addr
));
46 int aros_getaddrinfo(const char *node
, const char*service
,
47 const struct addrinfo
*hints
,
48 struct addrinfo
**res
)
50 struct sockaddr_in
*sin
;
52 sin
= malloc(sizeof(struct sockaddr_in
));
53 sin
->sin_len
= sizeof(struct sockaddr_in
);
54 sin
->sin_family
=AF_INET
;
56 /* Some error checking would be nice */
57 sin
->sin_addr
.s_addr
= inet_addr(node
);
61 sin
->sin_port
=htons(atoi(service
));
64 *res
= malloc(sizeof(struct addrinfo
));
66 (*res
)->ai_family
= AF_INET
;
67 (*res
)->ai_addrlen
= sizeof(struct sockaddr_in
);
68 (*res
)->ai_addr
= (struct sockaddr
*)sin
;
73 void aros_freeaddrinfo(struct addrinfo
*res
)
79 int aros_inet_pton(int af
, char *src
, void *dst
)
81 printf("No inet_pton yet");
86 /* unix device numbers dont really make much sense on aros ... */
96 struct Library
* SocketBase
= NULL
;
102 void aros_init_socket(void)
104 if (!(SocketBase
= OpenLibrary("bsdsocket.library", 4))) {
105 printf("NoTCP/IP Stack available");
108 if (SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno
))),
110 SBTM_SETVAL(SBTC_HERRNOLONGPTR
),
111 (IPTR
)&h_errno
, TAG_DONE
)) {
112 printf("Failed to set ERRNO");
117 int aros_poll(struct pollfd
*fds
, unsigned int nfds
, int timo
)
119 struct timeval timeout
, *toptr
;
120 fd_set ifds
, ofds
, efds
, *ip
, *op
;
121 unsigned int i
, maxfd
= 0;
124 // Set up the file-descriptor sets in ifds, ofds and efds.
128 for (i
= 0, op
= ip
= 0; i
< nfds
; ++i
)
131 if(fds
[i
].events
& (POLLIN
|POLLPRI
))
134 FD_SET(fds
[i
].fd
, ip
);
136 if(fds
[i
].events
& POLLOUT
)
139 FD_SET(fds
[i
].fd
, op
);
141 FD_SET(fds
[i
].fd
, &efds
);
142 if (fds
[i
].fd
> maxfd
) {
147 // Set up the timeval structure for the timeout parameter
155 timeout
.tv_sec
= timo
/ 1000;
156 timeout
.tv_usec
= (timo
- timeout
.tv_sec
* 1000) * 1000;
159 rc
= WaitSelect(maxfd
+ 1, ip
, op
, &efds
, toptr
, NULL
);
166 for (i
= 0; i
< nfds
; ++i
)
169 if(fds
[i
].events
& (POLLIN
|POLLPRI
) && FD_ISSET(fd
, &ifds
))
170 fds
[i
].revents
|= POLLIN
;
171 if(fds
[i
].events
& POLLOUT
&& FD_ISSET(fd
, &ofds
))
172 fds
[i
].revents
|= POLLOUT
;
173 if(FD_ISSET(fd
, &efds
)) // Some error was detected ... should be some way to know.
174 fds
[i
].revents
|= POLLHUP
;