Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcurl/lib/amigaos.c
5042 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9
*
10
* This software is licensed as described in the file COPYING, which
11
* you should have received as part of this distribution. The terms
12
* are also available at https://curl.se/docs/copyright.html.
13
*
14
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
* copies of the Software, and permit persons to whom the Software is
16
* furnished to do so, under the terms of the COPYING file.
17
*
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
* KIND, either express or implied.
20
*
21
* SPDX-License-Identifier: curl
22
*
23
***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef __AMIGA__
27
28
#include "hostip.h"
29
#include "amigaos.h"
30
31
#ifdef HAVE_PROTO_BSDSOCKET_H
32
# ifdef __amigaos4__
33
# include <bsdsocket/socketbasetags.h>
34
# elif !defined(USE_AMISSL)
35
# include <amitcp/socketbasetags.h>
36
# endif
37
# ifdef __libnix__
38
# include <stabs.h>
39
# endif
40
#endif
41
42
#ifdef HAVE_PROTO_BSDSOCKET_H
43
44
#ifdef __amigaos4__
45
/*
46
* AmigaOS 4.x specific code
47
*/
48
49
/*
50
* hostip4.c - Curl_ipv4_resolve_r() replacement code
51
*
52
* Logic that needs to be considered are the following build cases:
53
* - newlib networking
54
* - clib2 networking
55
* - direct bsdsocket.library networking (usually AmiSSL builds)
56
* Each with the threaded resolver enabled or not.
57
*
58
* With the threaded resolver enabled, try to use gethostbyname_r() where
59
* available, otherwise (re)open bsdsocket.library and fallback to
60
* gethostbyname().
61
*/
62
63
#include <proto/bsdsocket.h>
64
65
static struct SocketIFace *__CurlISocket = NULL;
66
static uint32 SocketFeatures = 0;
67
68
#define HAVE_BSDSOCKET_GETHOSTBYNAME_R 0x01
69
#define HAVE_BSDSOCKET_GETADDRINFO 0x02
70
71
CURLcode Curl_amiga_init(void)
72
{
73
struct SocketIFace *ISocket;
74
struct Library *base = OpenLibrary("bsdsocket.library", 4);
75
76
if(base) {
77
ISocket = (struct SocketIFace *)GetInterface(base, "main", 1, NULL);
78
if(ISocket) {
79
ULONG enabled = 0;
80
81
SocketBaseTags(SBTM_SETVAL(SBTC_CAN_SHARE_LIBRARY_BASES), TRUE,
82
SBTM_GETREF(SBTC_HAVE_GETHOSTADDR_R_API), (ULONG)&enabled,
83
TAG_DONE);
84
85
if(enabled) {
86
SocketFeatures |= HAVE_BSDSOCKET_GETHOSTBYNAME_R;
87
}
88
89
__CurlISocket = ISocket;
90
91
atexit(Curl_amiga_cleanup);
92
93
return CURLE_OK;
94
}
95
CloseLibrary(base);
96
}
97
98
return CURLE_FAILED_INIT;
99
}
100
101
void Curl_amiga_cleanup(void)
102
{
103
if(__CurlISocket) {
104
struct Library *base = __CurlISocket->Data.LibBase;
105
DropInterface((struct Interface *)__CurlISocket);
106
CloseLibrary(base);
107
__CurlISocket = NULL;
108
}
109
}
110
111
#ifdef CURLRES_AMIGA
112
/*
113
* Because we need to handle the different cases in hostip4.c at runtime,
114
* not at compile-time, based on what was detected in Curl_amiga_init(),
115
* we replace it completely with our own as to not complicate the baseline
116
* code. Assumes malloc/calloc/free are thread-safe because Curl_he2ai()
117
* allocates memory also.
118
*/
119
120
struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port)
121
{
122
struct Curl_addrinfo *ai = NULL;
123
struct hostent *h;
124
struct SocketIFace *ISocket = __CurlISocket;
125
126
if(SocketFeatures & HAVE_BSDSOCKET_GETHOSTBYNAME_R) {
127
LONG h_errnop = 0;
128
struct hostent *buf;
129
130
buf = curlx_calloc(1, CURL_HOSTENT_SIZE);
131
if(buf) {
132
h = gethostbyname_r((STRPTR)hostname, buf,
133
(char *)buf + sizeof(struct hostent),
134
CURL_HOSTENT_SIZE - sizeof(struct hostent),
135
&h_errnop);
136
if(h) {
137
ai = Curl_he2ai(h, port);
138
}
139
curlx_free(buf);
140
}
141
}
142
else {
143
#ifdef CURLRES_THREADED
144
/* gethostbyname() is not thread-safe, so we need to reopen bsdsocket
145
* on the thread's context
146
*/
147
struct Library *base = OpenLibrary("bsdsocket.library", 4);
148
if(base) {
149
ISocket = (struct SocketIFace *)GetInterface(base, "main", 1, NULL);
150
if(ISocket) {
151
h = gethostbyname((STRPTR)hostname);
152
if(h) {
153
ai = Curl_he2ai(h, port);
154
}
155
DropInterface((struct Interface *)ISocket);
156
}
157
CloseLibrary(base);
158
}
159
#else
160
/* not using threaded resolver - safe to use this as-is */
161
h = gethostbyname(hostname);
162
if(h) {
163
ai = Curl_he2ai(h, port);
164
}
165
#endif
166
}
167
168
return ai;
169
}
170
#endif /* CURLRES_AMIGA */
171
172
#ifdef USE_AMISSL
173
#include <signal.h>
174
int Curl_amiga_select(int nfds, fd_set *readfds, fd_set *writefds,
175
fd_set *errorfds, struct timeval *timeout)
176
{
177
int r = WaitSelect(nfds, readfds, writefds, errorfds, timeout, 0);
178
/* Ensure Ctrl-C signal is actioned */
179
if((r == -1) && (SOCKERRNO == SOCKEINTR))
180
raise(SIGINT);
181
return r;
182
}
183
#endif /* USE_AMISSL */
184
185
#elif !defined(USE_AMISSL) /* __amigaos4__ */
186
/*
187
* Amiga OS3 specific code
188
*/
189
190
struct Library *SocketBase = NULL;
191
192
#ifdef __libnix__
193
void __request(const char *msg);
194
#define CURL_AMIGA_REQUEST(msg) __request(msg)
195
#else
196
#define CURL_AMIGA_REQUEST(msg) Printf((const unsigned char *)(msg "\n\a"), 0)
197
#endif
198
199
void Curl_amiga_cleanup(void)
200
{
201
if(SocketBase) {
202
CloseLibrary(SocketBase);
203
SocketBase = NULL;
204
}
205
}
206
207
CURLcode Curl_amiga_init(void)
208
{
209
if(!SocketBase)
210
SocketBase = OpenLibrary((const unsigned char *)"bsdsocket.library", 4);
211
212
if(!SocketBase) {
213
CURL_AMIGA_REQUEST("No TCP/IP Stack running!");
214
return CURLE_FAILED_INIT;
215
}
216
217
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (ULONG)&errno,
218
SBTM_SETVAL(SBTC_LOGTAGPTR), (ULONG)"curl", TAG_DONE)) {
219
CURL_AMIGA_REQUEST("SocketBaseTags ERROR");
220
return CURLE_FAILED_INIT;
221
}
222
223
#ifndef __libnix__
224
atexit(Curl_amiga_cleanup);
225
#endif
226
227
return CURLE_OK;
228
}
229
230
#ifdef __libnix__
231
ADD2EXIT(Curl_amiga_cleanup, -50);
232
#endif
233
234
#endif /* !USE_AMISSL */
235
236
#endif /* HAVE_PROTO_BSDSOCKET_H */
237
238
#endif /* __AMIGA__ */
239
240