void load_bans(void) { FILE *fp = NULL; int i; unload_bans(); log_boot("- Loading banned name list from %s", BAN_FILE); if ((fp = fopen(BAN_FILE, "r"))) { banned_names_count = fread_number(fp); banned_names = calloc(banned_names_count, sizeof(char *)); for (i = 0; i < banned_names_count; i++) { banned_names[i] = strdup(new_fread_string(fp)); } log_boot("- Loading banned ip list from %s", BAN_FILE); banned_ips_count = fread_number(fp); banned_ips = calloc(banned_ips_count, sizeof(char *)); for (i = 0; i < banned_ips_count; i++) { banned_ips[i] = strdup(new_fread_string(fp)); } log_boot("- Loading banned name@ip list from %s", BAN_FILE); banned_at_count = fread_number(fp); banned_at_names = calloc(banned_at_count, sizeof(char *)); banned_at_ips = calloc(banned_at_count, sizeof(char *)); for (i = 0; i < banned_at_count; i++) { banned_at_names[i] = strdup(new_fread_string(fp)); banned_at_ips[i] = strdup(new_fread_string(fp)); } fclose(fp); } }
int CCSocketHandler::handle_output() { log_debug("*STEP: send data, len:[%d] netfd[%d]", _w.data_len(), netfd); if (_w.data_len() != 0) { int ret = ::send (netfd, _w.data(), _w.data_len(), 0); if(-1 == ret) { if(errno == EINTR || errno == EAGAIN || errno == EINPROGRESS) { log_boot("sending,INTR|EAGAIN|EINPROGRESS,errno:[%d]", errno); EnableOutput (); ApplyEvents (); _stage = CONN_DATA_SENDING; return _stage; } log_boot ("sending package to client failed, ret[%d]", ret); DisableInput (); DisableOutput (); ApplyEvents (); _stage = CONN_FATAL_ERROR; return _stage; } if(ret == (int)_w.data_len()) { log_debug("send complete, send len=[%d]",ret); DisableOutput(); ApplyEvents (); _w.skip(ret); _stage = CONN_SEND_DONE; return _stage; } else if (ret < (int)_w.data_len()) { log_debug("had sent part of data, send len=[%d]",ret); EnableOutput (); ApplyEvents (); _w.skip(ret); _stage = CONN_DATA_SENDING; return _stage; } } DisableOutput(); ApplyEvents (); _stage = CONN_FATAL_ERROR; log_debug("send process failure"); return _stage; }
static ssize_t mt_bootprof_write(struct file *filp, const char *ubuf, size_t cnt, loff_t *data) { char buf[BOOT_STR_SIZE]; size_t copy_size = cnt; if (cnt >= sizeof(buf)) copy_size = BOOT_STR_SIZE - 1; if (copy_from_user(&buf, ubuf, copy_size)) return -EFAULT; if(cnt==1) { if(buf[0] == '0') mt_bootprof_switch(0); else if(buf[0] == '1') mt_bootprof_switch(1); } buf[copy_size] = 0; log_boot(buf); return cnt; }
void unload_bans(void) { int i; if (banned_names) { log_boot("- Unloading banned name list"); for (i = 0; i < banned_names_count; i++) if (banned_names[i]) free(banned_names[i]); free(banned_names); banned_names = NULL; banned_names_count = 0; } if (banned_ips) { log_boot("- Unloading banned ip list"); for (i = 0; i < banned_ips_count; i++) if (banned_ips[i]) free(banned_ips[i]); free(banned_ips); banned_ips = NULL; banned_ips_count = 0; } if (banned_at_names && banned_at_ips) { log_boot("- Unloading banned name@ip list"); for (i = 0; i < banned_at_count; i++) { if (banned_at_names[i]) free(banned_at_names[i]); if (banned_at_ips[i]) free(banned_at_ips[i]); } free(banned_at_names); free(banned_at_ips); banned_at_names = NULL; banned_at_ips = NULL; banned_at_count = 0; } }
void save_bans(void) { FILE *fp = NULL; int i; if(!(fp = fopen(BAN_FILE, "w"))) { log_error("Cannot open %s for writing!\n", BAN_FILE); return; } log_boot("- Saving ban data to %s", BAN_FILE); fprintf(fp, "%d\n", banned_names_count); for (i = 0; i < banned_names_count; i++) { fprintf(fp, "%s~\n", banned_names[i]); } fprintf(fp, "%d\n", banned_ips_count); for (i = 0; i < banned_ips_count; i++) { fprintf(fp, "%s~\n", banned_ips[i]); } fprintf(fp, "%d\n", banned_at_count); for (i = 0; i < banned_at_count; i++) { fprintf(fp, "%s~\n%s~\n", banned_at_names[i], banned_at_ips[i]); } fclose(fp); }
int CCSocketHandler::Send(const char * buff, int len) { if(len>0) { if (GetReconnectFlag()==true){ Connect(); } const char* sendbuff = buff; int sendlen = len; if(this->_w.data_len()==0) { int ret = ::send (netfd,buff, len, 0); log_debug("Send Fd[%d]", netfd); if(-1 == ret) { if(errno == EINTR || errno == EAGAIN || errno == EINPROGRESS) { log_boot("sending,INTR|EAGAIN|EINPROGRESS,errno:[%d]", errno); this->_w.append(sendbuff, sendlen); EnableOutput (); ApplyEvents (); _stage = CONN_DATA_SENDING; return 0; } else { log_boot ("sending package to client failed, ret[%d]", ret); _stage = CONN_FATAL_ERROR; this->OnClose(); if(GetReconnectFlag()==false){//����Ҫ���� delete this; } else{ Reset(); } return -1; } } else if(ret<len) { sendbuff += ret; sendlen -= ret; this->_w.append(sendbuff, sendlen); EnableOutput (); ApplyEvents (); _stage = CONN_DATA_SENDING; log_debug("had sent part of data, send len=[%d]",ret); return ret; } else if(ret==len) { log_debug("send complete, send len=[%d]",len); _stage = CONN_SEND_DONE; return ret; } } else { this->_w.append(sendbuff, sendlen); if( handle_output() ==CONN_FATAL_ERROR ) { this->OnClose(); if(GetReconnectFlag()==false){//����Ҫ���� delete this; } else{ Reset(); } return -1; } else return len; } } return len; }
int CCSocketHandler::handle_input() { int ret = 0 ; int packet_len = 0 ; int curr_recv_len = 0; char curr_recv_buf[MAX_WEB_RECV_LEN] = {'\0'}; curr_recv_len = ::recv (netfd, curr_recv_buf, MAX_WEB_RECV_LEN, 0); log_debug ("*STEP: receiving data, length[%d]", curr_recv_len); if(-1 == curr_recv_len)//接收数据有误 { if(errno != EAGAIN && errno != EINTR && errno != EINPROGRESS) { DisableInput(); _stage = CONN_FATAL_ERROR; log_boot ("recv failed from fd[%d], msg[%s]", netfd, strerror(errno)); } else _stage = CONN_DATA_RECVING; } else if( 0 == curr_recv_len )//客户端关闭连接 { DisableInput (); _stage = CONN_DISCONNECT; log_boot ("connection disconnect by user fd[%d], msg[%s]", netfd, strerror(errno)); } else { if(curr_recv_len==23 && curr_recv_buf[0]=='<' && curr_recv_buf[1]=='p') { std::string policy = "<policy-file-request/>"; for(int i=0; i<23; ++i) { if(curr_recv_buf[i] != policy[i]) { _stage = CONN_DATA_ERROR; return _stage; } } std::string resPolicy ="<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\0"; this->Send(resPolicy.c_str(), resPolicy.size()); _stage = CONN_XML_POLICY; log_notice ("flash policy-file-request"); return _stage; } log_debug ("1111111111111111111"); _r.append(curr_recv_buf, curr_recv_len); while(_r.data_len() > 0) { log_debug ("2222222222222"); packet_len = this->_decode->ParsePacket(_r.data(), _r.data_len()); log_debug ("333333333333"); if(packet_len == -1) //数据错误 { DisableInput (); _stage = CONN_DATA_ERROR; break ; } else if(packet_len == 0) //包头解析完,等待包体 { _stage = CONN_DATA_RECVING; break; } else //解析到完整包 inputRet=完整包长度 { log_debug ("55555555555555"); ret = this->OnPacketComplete(_r.data(), packet_len); log_debug ("66666666666666 ret = %d",ret); if( ret < 0 ) { _stage = CONN_FATAL_ERROR; break; } _stage = CONN_RECV_DONE; _r.skip(packet_len); } } } return _stage; }