16 #elif defined(MACINTOSH)
17 #include <OpenTransport.h>
18 #include <OpenTptInternet.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
32 static concptr errstr;
34 static int proxy_port;
37 static InetSvcRef inet_services = nil;
38 static EndpointRef ep = kOTInvalidEndpointRef;
41 #if 0 /* とりあえず現在は使わない。by Habu*/
50 homeurl = malloc(strlen(s) + 1);
67 static char buf[BUFSIZ];
69 if(proxy && proxy[0]){
71 snprintf(buf, sizeof(buf), "%s:%d", proxy, proxy_port);
73 _snprintf(buf, sizeof(buf), "%s:%d", proxy, proxy_port);
75 buf[sizeof(buf)-1] = '\0';
94 int soc_read(int sd, char *buf, size_t sz)
97 return read(sd, buf, sz);
99 return recv(sd, buf, sz, 0);
105 /* プロキシサーバのアドレスををファイルから読んで設定する */
106 void set_proxy(char *default_url, int default_port)
118 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, "proxy.prf");
121 fp = my_fopen(buf, "r");
125 /* ファイルが存在しない場合はデフォルトを設定 */
127 proxy_port = default_port;
131 while (my_fgets(fp, buf, sizeof(buf))==0)
133 if (buf[0] != '#' && buf[0] != '\0') break;
141 /* "http://" から始まっている場合はその部分をカットする。 */
143 if (!strnicmp(s, "http://", 7))
147 #elif defined(MACINTOSH)
148 strncpy( tmp , s , 7 );
149 for ( i = 0 ; i < 7 ; i++ )
151 if ( isalpha(tmp[i]) )
152 tmp[i]= tolower(tmp[i]);
154 if (!strncmp(s, "http://", 7))
159 if (!strncasecmp(s, "http://", 7))
165 /* 文字列の長さを調べ、必要なメモリを確保 */
167 proxy = malloc(len + 1);
169 /* ポート番号があるかどうかを調べ、あればproxy_portに設定。 */
171 while (len > 0 && isdigit((unsigned char)s[len]))
173 if (len > 0 && s[len] == ':' && s[len + 1] != '\0')
177 proxy_port = atoi(s + (len + 1));
182 proxy_port = default_port;
185 /* プロキシのアドレスをproxyにコピー */
192 /* ソケットにバッファの内容を書き込む */
193 int soc_write(int sd, char *buf, size_t sz)
201 nwritten = send(sd, buf, nleft, 0);
207 #else /* !MACINTOSH */
211 OTSnd(ep, (void *) buf, sz, 0);
217 int soc_read(int sd, char *buf, size_t sz)
220 int nleft, nread = 0;
226 n = recv(sd, buf + nread, nleft, 0);
232 #else /* !MACINTOSH */
236 OTSnd(ep, (void *)buf, sz, 0);
243 int soc_write_str(int sd, char *buf)
245 return soc_write(sd, buf, strlen(buf));
249 #if !defined(WINDOWS) && !defined(MACINTOSH)
250 static sigjmp_buf env;
251 static void (*sig_int_saved)(int);
252 static void (*sig_alm_saved)(int);
255 static void restore_signal(void)
257 #if !defined(WINDOWS) && !defined(MACINTOSH)
258 struct itimerval val0;
261 val0.it_interval.tv_sec = 0;
262 val0.it_interval.tv_usec = 0;
263 val0.it_value.tv_sec = 0;
264 val0.it_value.tv_usec = 0;
267 setitimer(ITIMER_REAL, &val0, NULL);
268 signal(SIGALRM, sig_alm_saved);
269 signal(SIGINT, sig_int_saved);
274 #if !defined(WINDOWS) && !defined(MACINTOSH)
275 static void interrupt_report(int sig)
278 siglongjmp(env, sig);
284 int connect_server(int timeout, concptr host, int port)
288 struct sockaddr_in to;
292 struct itimerval val;
296 val.it_interval.tv_sec = 0;
297 val.it_interval.tv_usec = 0;
298 val.it_value.tv_sec = timeout;
299 val.it_value.tv_usec = 0;
301 /* タイムアウト、もしくは中断した時の処理。 */
302 if ((ret = sigsetjmp(env,1)) != 0)
306 errstr = "エラー: タイムアウト";
308 errstr = "エラー: インタラプト";
311 errstr = "Error : time out";
313 errstr = "Error : interupted";
317 sig_int_saved = signal(SIGINT, interrupt_report);
318 sig_alm_saved = signal(SIGALRM, interrupt_report);
321 setitimer(ITIMER_REAL, &val, NULL);
323 /* Unused in Windows */
327 /* プロキシが設定されていればプロキシに繋ぐ */
328 if (proxy && proxy[0])
330 if ((hp = gethostbyname(proxy)) == NULL)
333 errstr = "エラー: プロキシのアドレスが不正です";
335 errstr = "Error : wrong proxy addres";
343 else if ((hp = gethostbyname(host)) == NULL)
346 errstr = "エラー: サーバのアドレスが不正です";
348 errstr = "Error : wrong server adress";
356 memset(&to, 0, sizeof(to));
357 memcpy(&to.sin_addr, hp->h_addr_list[0], hp->h_length);
359 to.sin_family = AF_INET;
361 if(proxy && proxy[0] && proxy_port)
362 to.sin_port = htons((unsigned short int)proxy_port);
364 to.sin_port = htons((unsigned short int)port);
367 if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
369 if ((sd = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
373 errstr = "エラー: ソケットを生成できません";
375 errstr = "Error : cannot create socket.";
381 if (connect(sd, (struct sockaddr *)&to, sizeof(to)) < 0)
384 errstr = "エラー: サーバに接続できません";
386 errstr = "Error : failed to connect server";
402 #else /* !MACINTOSH */
404 /* サーバにコネクトする関数。 Mac */
407 InetHostInfo response;
411 Boolean bind = false;
413 memset(&response, 0, sizeof(response));
415 #if TARGET_API_MAC_CARBON
416 inet_services = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, NULL);
418 inet_services = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err);
423 if (proxy && proxy[0])
425 err = OTInetStringToAddress(inet_services, proxy, &response);
429 err = OTInetStringToAddress(inet_services, (char *)host, &response);
434 host_addr = response.addrs[0];
438 errstr = "error: bad score server!\n";
441 #if TARGET_API_MAC_CARBON
442 ep = (void *)OTOpenEndpointInContext(OTCreateConfiguration(kTCPName), 0, nil, &err, NULL);
444 ep = (void *)OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, nil, &err);
449 err = OTBind(ep, nil, nil);
450 bind = (err == noErr);
454 if (proxy && proxy[0] && proxy_port)
455 OTInitInetAddress(&inAddr, proxy_port, host_addr);
457 OTInitInetAddress(&inAddr, port, host_addr);
459 sndCall.addr.len = sizeof(InetAddress);
460 sndCall.addr.buf = (unsigned char*) &inAddr;
461 sndCall.opt.buf = nil; /* no connection options */
463 sndCall.udata.buf = nil; /* no connection data */
464 sndCall.udata.len = 0;
465 sndCall.sequence = 0; /* ignored by OTConnect */
467 err = OTConnect(ep, &sndCall, NULL);
471 errstr = "error: cannot connect score server!\n";
483 if (ep != kOTInvalidEndpointRef)
488 if (inet_services != nil)
490 OTCloseProvider(inet_services);
502 int disconnect_server(int sd)
505 return closesocket(sd);
506 #elif defined(MACINTOSH)
507 if (ep != kOTInvalidEndpointRef)
512 if (inet_services != nil)
514 OTCloseProvider(inet_services);
526 #endif /* WORLD_SCORE */