1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mobile Application Link.
*
* The Initial Developer of the Original Code is AvantGo, Inc.
* Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
* AvantGo, Inc. All Rights Reserved.
*
* Contributor(s):
*/
#if (defined(macintosh) && !defined(__palmos__))
#include <AGNet.h>
#include <AGUtil.h>
#include <AGProxy.h>
#include <stdio.h>
#define AGCalloc calloc
#define AGFree free
#define AGMalloc malloc
static int AGNetGetError(void);
static sword AGNetSocketClose(AGNetCtx *ctx, AGSocket *soc);
static sword AGNetConnect(AGNetCtx *ctx, AGSocket *soc, uint32 laddr,
int16 port, AGBool _block);
static int32 AGNetSend(AGNetCtx *ctx, AGSocket *soc, const uint8 *data,
int32 bytes, AGBool block);
static int32 AGNetRead(AGNetCtx *ctx, AGSocket *soc, uint8 *buffer,
int32 bytes, AGBool block);
static void AGNetSocketFree(AGNetCtx *ctx, AGSocket *soc);
static AGSocket *AGNetSocketNew(AGNetCtx *ctx);
/*---------------------------------------------------------------------------*/
ExportFunc uint32 AGNetGetHostAddr(AGNetCtx *ctx, char *name)
{
OSStatus err;
InetHostInfo response;
if (NULL == ctx || NULL == name)
return 0;
bzero(&response, sizeof(InetHostInfo));
err = OTInetStringToAddress(ctx->inet_services, name, &response);
if (err == noErr)
return response.addrs[0];
return 0;
}
/*---------------------------------------------------------------------------*/
static void AGNetSocketFree(AGNetCtx *ctx, AGSocket *soc)
{
if (NULL == ctx || NULL == soc)
return;
(*ctx->close)(ctx, soc);
AGFree(soc);
}
/*---------------------------------------------------------------------------*/
static AGSocket *AGNetSocketNew(AGNetCtx *ctx)
{
AGSocket *soc;
OSStatus err = noErr;
/* Note that in this implementation we don't actually need the
AGNetCtx pointer. Left in for source-code compatibility. */
soc = (AGSocket *)malloc(sizeof(AGSocket));
if (NULL == soc)
return NULL;
bzero(soc, sizeof(AGSocket));
soc->ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, NULL, &err);
if (noErr == err) {
err = OTBind(soc->ep, NULL, NULL);
if (noErr == err) {
/* Success. Return newly created socket. */
soc->bound = TRUE;
return soc;
}
OTCloseProvider(soc->ep);
}
/* Didn't successfully bind, so return error. */
free(soc);
return NULL;
}
/*---------------------------------------------------------------------------*/
static int32 AGNetMacMapError(int32 err)
{
if (err >= 0)
return err;
/* Map Macintosh Open Transport error codes to platform-independent
codes. */
switch (err) {
case kOTNoDataErr:
case kOTFlowErr:
case kOTStateChangeErr:
case kOTLookErr:
return AG_NET_WOULDBLOCK;
break;
case kEISCONNErr:
return AG_NET_EISCONN;
default:
return AG_NET_ERROR;
}
}
/*---------------------------------------------------------------------------*/
static sword AGNetConnect(AGNetCtx *ctx, AGSocket *soc, uint32 laddr,
int16 port, AGBool _block)
{
InetAddress hostInetAddress;
TCall sndCall;
if (NULL == ctx || NULL == soc)
return AG_NET_ERROR;
sndCall.addr.buf = (uint8*)&hostInetAddress;
sndCall.addr.len = sizeof(InetAddress);
OTInitInetAddress(&hostInetAddress, port, (InetHost)laddr);
sndCall.opt.buf = NULL;
sndCall.opt.len = 0;
sndCall.udata.buf = NULL;
sndCall.udata.len = 0;
sndCall.sequence = 0;
return AGNetMacMapError(OTConnect(soc->ep, &sndCall, NULL));
}
/*---------------------------------------------------------------------------*/
static int32 AGNetSend(AGNetCtx *ctx, AGSocket *soc, const uint8 *data,
int32 bytes, AGBool block)
{
if (NULL == ctx || NULL == soc || NULL == data)
return AG_NET_ERROR;
return AGNetMacMapError(OTSnd(soc->ep, (void *)data, bytes, 0));
}
/*---------------------------------------------------------------------------*/
static int32 AGNetRead(AGNetCtx *ctx, AGSocket *soc, uint8 *buffer,
int32 bytes, AGBool block)
{
OTFlags junkFlags = 0;
if (NULL == ctx || NULL == soc || NULL == buffer)
return AG_NET_ERROR;
return AGNetMacMapError(OTRcv(soc->ep,
(void *) buffer,
bytes,
&junkFlags));
}
/*---------------------------------------------------------------------------*/
ExportFunc int32 AGNetGets(AGNetCtx *ctx, AGSocket *soc, uint8 *buf, int32 offset,
int32 n, int32 *bytesread, AGBool block)
{
uint8 b;
int c = 0;
int d;
*bytesread = 0;
buf += offset;
if (NULL == ctx || NULL == soc || NULL == buf)
return AG_NET_ERROR;
if (n > 1)
n -= 1;
if (n == 0)
return 0;
do {
d = AGNETRECV(ctx, soc, &b, 1, block);
if (d == AG_NET_WOULDBLOCK) {
*bytesread = c;
return AG_NET_WOULDBLOCK;
} else if (d < 0) {
soc->state = AG_SOCKET_ERROR;
return d;
}
buf[c++] = b;
} while(c < n && b != '\n');
if (n > 1)
buf[c] = 0;
return c;
}
static int AGNetIsInited = 0;
/*---------------------------------------------------------------------------*/
ExportFunc
sword AGNetInit(AGNetCtx *ctx)
{
OSStatus rc = 0;
if (NULL == ctx)
return AG_NET_ERROR;
rc = InitOpenTransport();
if (noErr == rc) {
AGNetIsInited = 1;
bzero(ctx, sizeof(AGNetCtx));
ctx->inet_services =
OTOpenInternetServices(kDefaultInternetServicesPath,
0,
&rc);
AGNetSetIOFuncs(ctx, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
return rc;
}
/*---------------------------------------------------------------------------*/
ExportFunc
void AGNetSetIOFuncs(AGNetCtx *ctx, AGNetSendFunc send,
AGNetConnectFunc connect,
AGNetReadFunc recv,
AGNetCloseFunc close,
AGNetSocketNewFunc socnew,
AGNetSocketFreeFunc socfree,
AGNetReadProtectedFunc recvdm)
{
if (send)
ctx->send = send;
else
ctx->send = AGNetSend;
if (connect)
ctx->connect = connect;
else
ctx->connect = AGNetConnect;
if (recv)
ctx->recv = recv;
else
ctx->recv = AGNetRead;
if (close)
ctx->close = close;
else
ctx->close = AGNetSocketClose;
if (socnew)
ctx->socnew = socnew;
else
ctx->socnew = AGNetSocketNew;
if (close)
ctx->socfree = socfree;
else
ctx->socfree = AGNetSocketFree;
if (recvdm)
ctx->recvdm = recvdm;
else
ctx->recvdm = NULL; /* AGNetPilot.c will fill this out on __palmos__ */
}
/*---------------------------------------------------------------------------*/
ExportFunc sword AGNetClose(AGNetCtx *ctx)
{
OSStatus rc = 0;
if (NULL == ctx)
return AG_NET_ERROR;
if (AGNetIsInited) {
OTCloseProvider(ctx->inet_services);
CloseOpenTransport();
}
return rc;
}
/*---------------------------------------------------------------------------*/
static sword AGNetSocketClose(AGNetCtx *ctx, AGSocket *soc)
{
/* Note that in this implementation we don't actually need the
AGNetCtx pointer. Left in for source-code compatibility. */
if (NULL != soc) {
if (soc->bound)
OTUnbind(soc->ep);
if (kOTInvalidEndpointRef != soc->ep)
OTCloseProvider(soc->ep);
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Right now just looks for WOULDBLOCK error. You could see it doing
more in the future */
static int AGNetGetError()
{
return AG_NET_ERROR;
}
typedef struct _socksStruct {
int32 bytesread;
int32 bytessent;
int32 bytestosend;
uint8 *data;
} socksStruct;
/*---------------------------------------------------------------------------*/
ExportFunc
sword AGSocksConnect(AGNetCtx *ctx, AGSocket *soc, uint32 socksLaddr,
int16 socksServerPort, char *destAddr, int16 destHostPort,
AGBool block)
{
sword rc = 0;
uint32 laddr;
uint8 *socksBuffer = NULL;
int32 len;
uint8 *buf;
socksStruct *s;
if (NULL == ctx || NULL == soc || NULL == destAddr)
return AG_NET_ERROR;
if (soc->state != AG_SOCKET_CONNECTED) {
rc = (*ctx->connect) (ctx, soc, socksLaddr, socksServerPort, block);
if (rc == AG_NET_WOULDBLOCK)
return rc;
if (rc < 0) {
return AG_NET_SOCKS_ERROR_CONNECTTO;
}
if (rc == 0) {
laddr = AGNetGetHostAddr(ctx, destAddr);
if (!laddr)
return AG_NET_ERROR_BAD_HOSTNAME;
socksBuffer = (uint8 *)AGSocksBufCreate(laddr, destHostPort, &len);
if (!socksBuffer)
return AG_NET_SOCKS_ERROR;
s = (socksStruct *)malloc(sizeof(socksStruct));
if (!s) {
free(socksBuffer);
return AG_NET_SOCKS_ERROR;
}
s->bytestosend = len;
s->bytessent = 0;
s->bytesread = 0;
s->data = socksBuffer;
soc->userData = (uint8 *)s;
return AG_NET_WOULDBLOCK;
}
}
/* If we are here and have no socks buffer, things have
gone very wrong, bail */
if (!soc->userData)
return AG_NET_SOCKS_ERROR;
s = (socksStruct *)soc->userData;
if (s->bytessent == s->bytestosend) {
int bytestoread = 8 - s->bytesread;
buf = s->data + s->bytesread;
rc = AGNETRECV(ctx, soc, buf, bytestoread, block);
if (rc == AG_NET_WOULDBLOCK)
return rc;
if (rc < 0) {
free(s->data);
free(s);
return AG_NET_SOCKS_ERROR;
}
s->bytesread += rc;
if (s->bytesread == 8) {
rc = AGSocksGetResponse((char*)s->data);
free(s->data);
free(s);
soc->userData = NULL;
return rc;
}
} else {
int32 bytestosend = s->bytestosend - s->bytessent;
buf = s->data + s->bytessent;
rc = AGNETSEND(ctx, soc, buf, bytestosend, block);
if (rc == AG_NET_WOULDBLOCK)
return rc;
if (rc < 0) {
free(s->data);
free(s);
return AG_NET_SOCKS_ERROR;
}
s->bytessent += rc;
return AG_NET_WOULDBLOCK;
}
return 0;
}
#endif /* (defined(macintosh) && !defined(__palmos__)) */
|