error_code_t get(receiver_param *rp, const char *command) { char *file_name; unsigned char name_size; long file_size, available; error_code_t e; FILE *stream; char *cwd; struct statfs fs; if(command[3] != ' ' && command[3] != '\0') return INV_CMD; file_name = (char*)&command[4]; name_size = (unsigned char) strlen(file_name); if(name_size > DATA_MAX_SIZE) return BIG_NAME; e = remote_command(rp, 'G', file_name, name_size, &file_size); if(e != OK) return e; /* obtem espaco livre */ cwd = getcwd(NULL, 0); statfs(cwd, &fs); free(cwd); available = fs.f_bavail * fs.f_bsize; if( file_size >= available) { e = NO_SPACE; send_error(rp->source_sd, e,0); return e; } stream = fopen(file_name, "w"); if(!stream) { if (errno == EACCES) e = NO_PERMISSION; else if (errno == EISDIR) e = IS_DIR; send_error(rp->source_sd, e, 0); return e; } send_ack(rp->source_sd, 0); receiving_file(rp, fileno(stream), 1, file_size); fclose(stream); return OK; }
void __cdecl CAimProto::aim_dc_helper(void* param) //only called when we are initiating a direct connection with someone else { file_transfer *ft = (file_transfer*)param; sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, ft, 0); NETLIBPACKETRECVER packetRecv = {0}; packetRecv.cbSize = sizeof(packetRecv); packetRecv.dwTimeout = 350000; HANDLE hServerPacketRecver = (HANDLE) CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)ft->hConn, 2048 * 4); int result; if (ft->sending)//we are sending result = sending_file(ft, hServerPacketRecver, packetRecv); else result = receiving_file(ft, hServerPacketRecver, packetRecv); Netlib_CloseHandle(hServerPacketRecver); Netlib_CloseHandle(ft->hConn); ft->hConn = NULL; if (result == 0) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0); } else { if (!ft->requester && result == 1 && !Miranda_Terminated()) { ft->accepted = false; HANDLE hConn = aim_peer_connect(AIM_PROXY_SERVER, get_default_port()); if (hConn) { LOG("Connected to proxy ip because we want to use a proxy for the file transfer."); ft->requester = true; ft->hConn = hConn; ForkThread(&CAimProto::aim_proxy_helper, ft); return; } } aim_file_ad(hServerConn, seqno, ft->sn, ft->icbm_cookie, true, 0); sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); } ft_list.remove_by_ft(ft); }
void put_server(receiver_param *rp, struct frame_t *frame_info) { error_code_t e; FILE *stream; long available; struct statfs fs; char *cwd; long file_size; /* obtem espaco livre */ cwd = getcwd(NULL, 0); statfs(cwd, &fs); free(cwd); available = fs.f_bavail * fs.f_bsize; send_ack(rp->source_sd, 0); // ack de P e = receive_d(rp, available, 1, &file_size); if(e != OK) { fprintf(stderr, "Sem espaco para o arquivo\n"); return; } stream = fopen(frame_info->data, "w"); if (stream == NULL) { if (errno == EACCES) { send_error(rp->source_sd, NO_PERMISSION, 0); } else if (errno == EISDIR) { send_error(rp->source_sd, IS_DIR, 0); } return; } receiving_file(rp, fileno(stream), 2, file_size); fclose(stream); }
void __cdecl CAimProto::aim_proxy_helper(void* param) { file_transfer *ft = (file_transfer*)param; if (ft->requester) { if (proxy_initialize_send(ft->hConn, username, ft->icbm_cookie)) return;//error } else { if (proxy_initialize_recv(ft->hConn, username, ft->icbm_cookie, ft->port)) return;//error } //start listen for packets stuff NETLIBPACKETRECVER packetRecv = {0}; packetRecv.cbSize = sizeof(packetRecv); packetRecv.dwTimeout = INFINITE; HANDLE hServerPacketRecver = (HANDLE) CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)ft->hConn, 2048 * 4); for (;;) { int recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)hServerPacketRecver, (LPARAM)&packetRecv); if (recvResult == 0) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); break; } if (recvResult == SOCKET_ERROR) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); break; } if (recvResult > 0) { unsigned short length = _htons(*(unsigned short*)&packetRecv.buffer[0]); packetRecv.bytesUsed = length + 2; unsigned short type = _htons(*(unsigned short*)&packetRecv.buffer[4]); if (type == 0x0001) { unsigned short error = _htons(*(unsigned short*)&packetRecv.buffer[12]); switch (error) { case 0x000D: ShowPopup("Proxy Server File Transfer Error: Bad Request.", ERROR_POPUP); break; case 0x0010: ShowPopup("Proxy Server File Transfer Error: Initial Request Timed Out.", ERROR_POPUP); break; case 0x001A: ShowPopup("Proxy Server File Transfer Error: Accept Period Timed Out.", ERROR_POPUP); break; case 0x000e: ShowPopup("Proxy Server File Transfer Error: Incorrect command syntax.", ERROR_POPUP); break; case 0x0016: ShowPopup("Proxy Server File Transfer Error: Unknown command issued.", ERROR_POPUP); break; } } else if (type == 0x0003) { unsigned short port = _htons(*(unsigned short*)&packetRecv.buffer[12]); unsigned long ip = _htonl(*(unsigned long*)&packetRecv.buffer[14]); aim_send_file(hServerConn, seqno, ip, port, true, ft); LOG("Stage %d Proxy ft and we are not the sender.", ft->req_num); } else if (type == 0x0005) { if (!ft->requester) { aim_file_ad(hServerConn, seqno, ft->sn, ft->icbm_cookie, false, ft->max_ver); ft->accepted = true; } sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, ft, 0); int i; for (i = 21; --i; ) { if (Miranda_Terminated()) return; Sleep(100); if (ft->accepted) break; } if (i == 0) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); break; } packetRecv.dwTimeout = 350000; int result; if (ft->sending)//we are sending result = sending_file(ft, hServerPacketRecver, packetRecv); else result = receiving_file(ft, hServerPacketRecver, packetRecv); sendBroadcast(ft->hContact, ACKTYPE_FILE, result ? ACKRESULT_FAILED : ACKRESULT_SUCCESS, ft, 0); break; } } } Netlib_CloseHandle(hServerPacketRecver); Netlib_CloseHandle(ft->hConn); ft_list.remove_by_ft(ft); }