8 #include "io/files-util.h"
9 #include "util/angband-files.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;
38 /* プロキシサーバのアドレスををファイルから読んで設定する */
39 void set_proxy(char *default_url, int default_port)
46 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, "proxy.prf");
49 fp = angband_fopen(buf, "r");
53 /* ファイルが存在しない場合はデフォルトを設定 */
55 proxy_port = default_port;
59 while (angband_fgets(fp, buf, sizeof(buf)) == 0)
61 if (buf[0] != '#' && buf[0] != '\0') break;
69 /* "http://" から始まっている場合はその部分をカットする。 */
71 if (!strnicmp(s, "http://", 7))
76 if (!strncasecmp(s, "http://", 7))
82 /* 文字列の長さを調べ、必要なメモリを確保 */
84 proxy = malloc(len + 1);
86 /* ポート番号があるかどうかを調べ、あればproxy_portに設定。 */
88 while (len > 0 && isdigit((unsigned char)s[len]))
90 if (len > 0 && s[len] == ':' && s[len + 1] != '\0')
94 proxy_port = atoi(s + (len + 1));
99 proxy_port = default_port;
102 /* プロキシのアドレスをproxyにコピー */
109 /* ソケットにバッファの内容を書き込む */
110 int soc_write(int sd, char *buf, size_t sz)
117 nwritten = send(sd, buf, nleft, 0);
127 int soc_read(int sd, char *buf, size_t sz)
129 int nleft, nread = 0;
135 n = recv(sd, buf + nread, nleft, 0);
145 #if !defined(WINDOWS)
146 static sigjmp_buf env;
147 static void(*sig_int_saved)(int);
148 static void(*sig_alm_saved)(int);
151 static void restore_signal(void)
153 #if !defined(WINDOWS)
154 struct itimerval val0;
157 val0.it_interval.tv_sec = 0;
158 val0.it_interval.tv_usec = 0;
159 val0.it_value.tv_sec = 0;
160 val0.it_value.tv_usec = 0;
163 setitimer(ITIMER_REAL, &val0, NULL);
164 signal(SIGALRM, sig_alm_saved);
165 signal(SIGINT, sig_int_saved);
170 #if !defined(WINDOWS)
171 static void interrupt_report(int sig)
174 siglongjmp(env, sig);
180 int connect_server(int timeout, concptr host, int port)
183 struct sockaddr_in to;
187 struct itimerval val;
191 val.it_interval.tv_sec = 0;
192 val.it_interval.tv_usec = 0;
193 val.it_value.tv_sec = timeout;
194 val.it_value.tv_usec = 0;
196 /* タイムアウト、もしくは中断した時の処理。 */
197 if ((ret = sigsetjmp(env, 1)) != 0)
201 errstr = "エラー: タイムアウト";
203 errstr = "エラー: インタラプト";
206 errstr = "Error : time out";
208 errstr = "Error : interupted";
212 sig_int_saved = signal(SIGINT, interrupt_report);
213 sig_alm_saved = signal(SIGALRM, interrupt_report);
216 setitimer(ITIMER_REAL, &val, NULL);
218 /* Unused in Windows */
222 /* プロキシが設定されていればプロキシに繋ぐ */
223 if (proxy && proxy[0])
225 if ((hp = gethostbyname(proxy)) == NULL)
228 errstr = "エラー: プロキシのアドレスが不正です";
230 errstr = "Error : wrong proxy addres";
238 else if ((hp = gethostbyname(host)) == NULL)
241 errstr = "エラー: サーバのアドレスが不正です";
243 errstr = "Error : wrong server adress";
251 memset(&to, 0, sizeof(to));
252 memcpy(&to.sin_addr, hp->h_addr_list[0], hp->h_length);
254 to.sin_family = AF_INET;
256 if (proxy && proxy[0] && proxy_port)
257 to.sin_port = htons((unsigned short int)proxy_port);
259 to.sin_port = htons((unsigned short int)port);
262 if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
264 if ((sd = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
268 errstr = "エラー: ソケットを生成できません";
270 errstr = "Error : cannot create socket.";
276 if (connect(sd, (struct sockaddr *)&to, sizeof(to)) < 0)
279 errstr = "エラー: サーバに接続できません";
281 errstr = "Error : failed to connect server";
298 int disconnect_server(int sd)
301 return closesocket(sd);
312 #endif /* WORLD_SCORE */