| 1 |
/** |
| 2 |
* @file netserver.c |
| 3 |
* @brief サーバ処理 |
| 4 |
* @author BananaJinn |
| 5 |
* @version $Id: netserver.c,v 1.9 2007/10/02 15:10:52 bananajinn Exp $ |
| 6 |
* 円盤複写屋 |
| 7 |
* Copyright (C) 2004-2006 BananaJinn<banana@mxh.mesh.ne.jp>. |
| 8 |
*/ |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <stdarg.h> |
| 13 |
#ifdef linux |
| 14 |
# include <fcntl.h> |
| 15 |
#endif |
| 16 |
#include <time.h> |
| 17 |
#if defined(WIN32) |
| 18 |
# include <io.h> |
| 19 |
#endif |
| 20 |
#include "mem.h" |
| 21 |
#include "aspi.h" |
| 22 |
#include "struct.h" |
| 23 |
#include "cmd.h" |
| 24 |
#include "ui.h" |
| 25 |
#include "copydisc.h" |
| 26 |
#include "cmdlog.h" |
| 27 |
#include "text.h" |
| 28 |
|
| 29 |
static int ReceiveNetCmd(CMDDRIVE *netdrive, CMDDRIVE *realdrive, |
| 30 |
BOOL enable_longread); |
| 31 |
|
| 32 |
/** |
| 33 |
* @brief リモート要求によるコマンド実行 |
| 34 |
* @param[in] netdrive ネットワークドライブ(リモートドライブ) |
| 35 |
* @param[in] realdrive 実ドライブ(ローカルドライブ) |
| 36 |
* @retval RET_OK 正常終了 |
| 37 |
* @retval RET_TIMEOUT タイムアウト |
| 38 |
* @retval RET_SOCKET ソケットエラー |
| 39 |
* @retval RET_NG エラー |
| 40 |
*/ |
| 41 |
static int ExecuteCommand(CMDDRIVE *netdrive, CMDDRIVE *realdrive) |
| 42 |
{ |
| 43 |
int ret; |
| 44 |
DWORD length; |
| 45 |
NETCMDHEADER cmdheader; |
| 46 |
BYTE netdata_common[8]; |
| 47 |
|
| 48 |
memset(netdata_common, 0, sizeof(netdata_common)); |
| 49 |
|
| 50 |
/* コマンド受信 */ |
| 51 |
ret = NAReceive(&netdrive->u.net, (char *)&cmdheader, sizeof(cmdheader), |
| 52 |
NETTIMEOUT); |
| 53 |
if(ret != RET_OK){ |
| 54 |
return ret; |
| 55 |
} |
| 56 |
length = Get4bytes(cmdheader.data_length); |
| 57 |
if(length > (DWORD)netdrive->bufsize){ |
| 58 |
length = (DWORD)netdrive->bufsize; |
| 59 |
} |
| 60 |
|
| 61 |
if((length > 0) && (cmdheader.reqflag==REQ_DATAOUT)){ |
| 62 |
/* コマンドデータ部を受信 */ |
| 63 |
ret = NAReceive(&netdrive->u.net, (char *)netdrive->data_buf, |
| 64 |
(int)length, NETTIMEOUT); |
| 65 |
if(ret != RET_OK){ |
| 66 |
return ret; |
| 67 |
} |
| 68 |
} |
| 69 |
ret = SendCmd(realdrive, cmdheader.cdb, length, cmdheader.reqflag); |
| 70 |
/* エラーが発生しても、エラー情報は返却する */ |
| 71 |
/* データ返送 */ |
| 72 |
if(cmdheader.reqflag == REQ_DATAIN){ |
| 73 |
Set4bytes(netdata_common, length+sizeof(NETCMDHEADER)); |
| 74 |
} |
| 75 |
else{ |
| 76 |
Set4bytes(netdata_common, sizeof(NETCMDHEADER)); |
| 77 |
} |
| 78 |
netdata_common[4] = NETDATATYPE_RESPONSE; |
| 79 |
ret = NASend(&netdrive->u.net, (char *)netdata_common, 8, |
| 80 |
NETTIMEOUT, FALSE); |
| 81 |
if(ret != RET_OK){ |
| 82 |
return ret; |
| 83 |
} |
| 84 |
memcpy(cmdheader.sense_data, realdrive->sense_data, |
| 85 |
sizeof(cmdheader.sense_data)); |
| 86 |
ret = NASend(&netdrive->u.net, (char *)&cmdheader, sizeof(cmdheader), |
| 87 |
NETTIMEOUT, FALSE); |
| 88 |
if(ret != RET_OK){ |
| 89 |
return ret; |
| 90 |
} |
| 91 |
if(cmdheader.reqflag == REQ_DATAIN){ |
| 92 |
ret = NASend(&netdrive->u.net, (char *)netdrive->data_buf, length, |
| 93 |
NETTIMEOUT, FALSE); |
| 94 |
if(ret != RET_OK){ |
| 95 |
return ret; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return RET_OK; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* @brief リモート要求による連続READ実行 |
| 106 |
* @param[in] netdrive ネットワークドライブ(リモートドライブ) |
| 107 |
* @param[in] realdrive 実ドライブ(ローカルドライブ) |
| 108 |
* @param[in] org_header 受けた要求のコマンドヘッダー |
| 109 |
* @retval RET_OK 正常終了 |
| 110 |
* @retval RET_TIMEOUT タイムアウト |
| 111 |
* @retval RET_SOCKET ソケットエラー |
| 112 |
* @retval RET_NG エラー |
| 113 |
*/ |
| 114 |
static int ExecuteLongRead(CMDDRIVE *netdrive, CMDDRIVE *realdrive) |
| 115 |
{ |
| 116 |
DWORD lba, start_lba; |
| 117 |
DWORD length, total_length; |
| 118 |
DWORD blocksize; |
| 119 |
int ret; |
| 120 |
BYTE netdata_common[8]; |
| 121 |
NETCMDHEADER org_header, header; |
| 122 |
BYTE work4[4]; |
| 123 |
|
| 124 |
/* コマンド受信 */ |
| 125 |
ret = NAReceive(&netdrive->u.net, (char *)&org_header, sizeof(org_header), |
| 126 |
NETTIMEOUT); |
| 127 |
if(ret != RET_OK){ |
| 128 |
return ret; |
| 129 |
} |
| 130 |
/* 合計ブロック数を受信 */ |
| 131 |
ret = NAReceive(&netdrive->u.net, (char *)work4, sizeof(work4), |
| 132 |
NETTIMEOUT); |
| 133 |
if(ret != RET_OK){ |
| 134 |
return ret; |
| 135 |
} |
| 136 |
total_length = Get4bytes(work4); |
| 137 |
start_lba = Get4bytes(org_header.cdb+2); |
| 138 |
|
| 139 |
memcpy(header.cdb, org_header.cdb, 12); |
| 140 |
lba = start_lba; |
| 141 |
if(header.cdb[0]==CMD_READ12){ |
| 142 |
length = Get4bytes(header.cdb+6); |
| 143 |
blocksize = 2048; |
| 144 |
} |
| 145 |
else if(header.cdb[0]==CMD_READ_CD){ |
| 146 |
length = Get3bytes(header.cdb+6); |
| 147 |
blocksize = 2352; |
| 148 |
} |
| 149 |
else{ |
| 150 |
return RET_NG; |
| 151 |
} |
| 152 |
|
| 153 |
while(total_length > 0){ |
| 154 |
if(UICheckAbort()){ |
| 155 |
return RET_ABORT; |
| 156 |
} |
| 157 |
if(length > total_length){ |
| 158 |
length = total_length; |
| 159 |
if(header.cdb[0]==CMD_READ12){ |
| 160 |
Set4bytes(header.cdb+2, lba); |
| 161 |
Set4bytes(header.cdb+6, length); |
| 162 |
} |
| 163 |
else /* CMD_READ_CD */{ |
| 164 |
Set4bytes(header.cdb+2, lba); |
| 165 |
Set3bytes(header.cdb+6, length); |
| 166 |
} |
| 167 |
} |
| 168 |
/* 実ドライブからREAD */ |
| 169 |
Set4bytes(header.cdb+2, lba); |
| 170 |
ret = SendCmd(realdrive, header.cdb, length*blocksize, REQ_DATAIN); |
| 171 |
if(ret != RET_OK){ |
| 172 |
return ret; |
| 173 |
} |
| 174 |
memcpy(header.sense_data, realdrive->sense_data, |
| 175 |
sizeof(header.sense_data)); |
| 176 |
|
| 177 |
/* READデータ返送 */ |
| 178 |
Set4bytes(netdata_common, length*blocksize+sizeof(header)); |
| 179 |
netdata_common[4] = NETDATATYPE_RESPONSE; |
| 180 |
while(1){ |
| 181 |
ret = NASend(&netdrive->u.net, (char *)netdata_common, 8, |
| 182 |
NETTIMEOUT, TRUE); |
| 183 |
if(ret==RET_OK){ |
| 184 |
break; |
| 185 |
} |
| 186 |
if(ret!=RET_READFD){ |
| 187 |
return ret; |
| 188 |
} |
| 189 |
ret = ReceiveNetCmd(netdrive, realdrive, FALSE); |
| 190 |
if(ret != RET_OK){ |
| 191 |
return ret; |
| 192 |
} |
| 193 |
} |
| 194 |
ret = NASend(&netdrive->u.net, (char *)&header, sizeof(header), |
| 195 |
NETTIMEOUT, FALSE); |
| 196 |
if(ret != RET_OK){ |
| 197 |
return ret; |
| 198 |
} |
| 199 |
ret = NASend(&netdrive->u.net, (char *)netdrive->data_buf, |
| 200 |
length*blocksize, NETTIMEOUT, FALSE); |
| 201 |
if(ret != RET_OK){ |
| 202 |
return ret; |
| 203 |
} |
| 204 |
|
| 205 |
lba += length; |
| 206 |
total_length -= length; |
| 207 |
} |
| 208 |
|
| 209 |
return RET_OK; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @brief リモート要求によるメッセージ表示 |
| 214 |
* @param[in] netdrive ネットワークドライブ(リモートドライブ) |
| 215 |
* @param[in] receive_bytes 受けた要求のバイト長 |
| 216 |
* @retval RET_OK 正常終了 |
| 217 |
* @retval RET_TIMEOUT タイムアウト |
| 218 |
* @retval RET_NG エラー |
| 219 |
* @retval RET_MEMERR メモリ不足 |
| 220 |
*/ |
| 221 |
static int DisplayMessage(CMDDRIVE *netdrive, DWORD receive_bytes) |
| 222 |
{ |
| 223 |
NETDISPHEADER dispheader; |
| 224 |
int ret; |
| 225 |
char *message=NULL; |
| 226 |
DWORD message_len, ignore_len=0; |
| 227 |
|
| 228 |
ret = NAReceive(&netdrive->u.net, (char *)&dispheader, sizeof(dispheader), |
| 229 |
NETTIMEOUT); |
| 230 |
if(ret != RET_OK){ |
| 231 |
return ret; |
| 232 |
} |
| 233 |
if(receive_bytes > (DWORD)sizeof(dispheader)){ |
| 234 |
message_len = receive_bytes-sizeof(dispheader); |
| 235 |
/* 最大255文字とする */ |
| 236 |
if(message_len > 255){ |
| 237 |
ignore_len = message_len-255; |
| 238 |
message_len = 255; |
| 239 |
} |
| 240 |
message = (char *)MemNew(message_len+1); |
| 241 |
if(message==NULL){ |
| 242 |
UIDispMessage(MSG_MEM_ALLOC_ERROR |
| 243 |
/*"メモリ確保に失敗しました。"*/, UIDMT_ERROR); |
| 244 |
return RET_MEMERR; |
| 245 |
} |
| 246 |
/* メッセージ文字列を受信 */ |
| 247 |
ret = NAReceive(&netdrive->u.net, message, message_len, NETTIMEOUT); |
| 248 |
if(ret != RET_OK){ |
| 249 |
MemFree(message); |
| 250 |
return ret; |
| 251 |
} |
| 252 |
ret = NAReceive(&netdrive->u.net, NULL, ignore_len, NETTIMEOUT); |
| 253 |
if(ret != RET_OK){ |
| 254 |
MemFree(message); |
| 255 |
return ret; |
| 256 |
} |
| 257 |
message[message_len] = '\0'; |
| 258 |
#if defined(linux) || defined(__MINGW32__) |
| 259 |
{ |
| 260 |
char *message_euc = ConvertCharcode(netdrive->u.net.iconv_desc, |
| 261 |
message); |
| 262 |
if(message_euc == NULL){ |
| 263 |
MemFree(message); |
| 264 |
UIDispMessage(MSG_CODE_CONV_ERROR |
| 265 |
/*"文字コード変換に失敗しました。"*/, UIDMT_ERROR); |
| 266 |
return RET_MEMERR; |
| 267 |
} |
| 268 |
MemFree(message); |
| 269 |
message = message_euc; |
| 270 |
} |
| 271 |
#endif |
| 272 |
} |
| 273 |
switch(dispheader.area){ |
| 274 |
case 0: /* 画面下メッセージ */ |
| 275 |
UIDispInfo(message); |
| 276 |
break; |
| 277 |
case 1: /* プログレスバー1 */ |
| 278 |
if(message!=NULL) |
| 279 |
UIMeter1Initialize(message); |
| 280 |
else |
| 281 |
UIMeter1Update((float)dispheader.percent); |
| 282 |
break; |
| 283 |
case 2: /* プログレスバー2 */ |
| 284 |
if(message!=NULL) |
| 285 |
UIMeter2Initialize(message); |
| 286 |
else |
| 287 |
UIMeter2Update((float)dispheader.percent); |
| 288 |
break; |
| 289 |
} |
| 290 |
MemFree(message); |
| 291 |
|
| 292 |
return RET_OK; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* @brief リモート要求処理 |
| 298 |
* @param[in] netdrive ネットワークドライブ(リモートドライブ) |
| 299 |
* @param[in] realdrive 実ドライブ(ローカルドライブ) |
| 300 |
* @param[in] enable_longread 連続READ許可 |
| 301 |
* @retval RET_OK 正常終了 |
| 302 |
* @retval RET_TIMEOUT タイムアウト |
| 303 |
* @retval RET_ABORT 処理中断 |
| 304 |
* @retval RET_COMPLETE 処理完了 |
| 305 |
* @retval RET_SOCKET ソケットエラー |
| 306 |
* @retval RET_NG エラー |
| 307 |
*/ |
| 308 |
static int ReceiveNetCmd(CMDDRIVE *netdrive, CMDDRIVE *realdrive, |
| 309 |
BOOL enable_longread) |
| 310 |
{ |
| 311 |
int ret; |
| 312 |
BYTE netdata_common[8]; |
| 313 |
DWORD receive_bytes; |
| 314 |
|
| 315 |
/* 共通部(データ長とデータタイプ)を受信する */ |
| 316 |
ret = NAReceive(&netdrive->u.net, (char *)netdata_common, 8, 1000); |
| 317 |
if(ret != RET_OK){ |
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
receive_bytes = Get4bytes(netdata_common); |
| 322 |
|
| 323 |
switch(netdata_common[4]){ |
| 324 |
case NETDATATYPE_COMMAND: |
| 325 |
ret = ExecuteCommand(netdrive, realdrive); |
| 326 |
if(ret != RET_OK){ |
| 327 |
return ret; |
| 328 |
} |
| 329 |
break; |
| 330 |
|
| 331 |
case NETDATATYPE_RESPONSE: |
| 332 |
/* あり得ない */ |
| 333 |
break; |
| 334 |
|
| 335 |
case NETDATATYPE_LONGREAD: |
| 336 |
if(enable_longread){ |
| 337 |
ret = ExecuteLongRead(netdrive, realdrive); |
| 338 |
if(ret != RET_OK){ |
| 339 |
return ret; |
| 340 |
} |
| 341 |
} |
| 342 |
else{ |
| 343 |
UIDispMessage(MSG_UNKNOWN_COMMAND |
| 344 |
/*"不正なコマンドを受信しました。"*/, UIDMT_ERROR); |
| 345 |
return RET_NG; |
| 346 |
} |
| 347 |
break; |
| 348 |
|
| 349 |
case NETDATATYPE_DISPLAY: |
| 350 |
ret = DisplayMessage(netdrive, receive_bytes); |
| 351 |
if(ret != RET_OK){ |
| 352 |
return ret; |
| 353 |
} |
| 354 |
break; |
| 355 |
|
| 356 |
case NETDATATYPE_ABORT: |
| 357 |
return RET_ABORT; |
| 358 |
|
| 359 |
case NETDATATYPE_COMPLETE: |
| 360 |
return RET_COMPLETE; |
| 361 |
|
| 362 |
default: |
| 363 |
/* 不正なコマンド */ |
| 364 |
UIDispMessage(MSG_UNKNOWN_COMMAND |
| 365 |
/*"不正なコマンドを受信しました。"*/, UIDMT_ERROR); |
| 366 |
return RET_NG; |
| 367 |
} |
| 368 |
return RET_OK; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
/** |
| 373 |
* @brief リモート要求受付処理 |
| 374 |
* @param[in] netdrive ネットワークドライブ(リモートドライブ) |
| 375 |
* @param[in] realdrive 実ドライブ(ローカルドライブ) |
| 376 |
* @retval RET_OK 正常終了 |
| 377 |
* @retval RET_ABORT 処理中断 |
| 378 |
* @retval RET_COMPLETE 処理完了 |
| 379 |
* @retval RET_SOCKET ソケットエラー |
| 380 |
* @retval RET_MEMERR メモリ不足 |
| 381 |
* @retval RET_NG エラー |
| 382 |
*/ |
| 383 |
int WaitingNetCmd(CMDDRIVE *netdrive, CMDDRIVE *realdrive) |
| 384 |
{ |
| 385 |
BOOL done=FALSE; |
| 386 |
int ret; |
| 387 |
|
| 388 |
while(!done){ |
| 389 |
if(UICheckAbort()){ |
| 390 |
return RET_ABORT; |
| 391 |
} |
| 392 |
ret = ReceiveNetCmd(netdrive, realdrive, TRUE); |
| 393 |
if(ret == RET_COMPLETE){ |
| 394 |
done=TRUE; |
| 395 |
} |
| 396 |
else if((ret != RET_OK) && (ret != RET_TIMEOUT)){ |
| 397 |
return ret; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
return RET_OK; |
| 402 |
} |
| 403 |
|