static int login_finish(T_Connect *conn,T_NetHead *NetHead) { char *cp; T_SRV_Var *up=(T_SRV_Var *)conn->Var; GDA *gp=(GDA *)up->var; T_CLI_Var *clip; char tmp[30],tmp1[256]; unset_callback(up->TCB_no); // up->poolno=get_scpool_no(NetHead->D_NODE); /* 怎么认证还得想办法 cp=get_LABEL(up->poolno); if(!cp || strcmp(cp,logrec.label)) { sprintf(tmp1,"错误的DBLABEL %s",logrec.label); goto errret; } */ if(!gp->server) { sprintf(tmp1,"%s:connect to server fault,TCB:%d", __FUNCTION__,up->TCB_no); ShowLog(1,"%s:Error:%s",__FUNCTION__,tmp1); NetHead->ERRNO1=-1; NetHead->ERRNO2=-1; NetHead->PKG_REC_NUM=0; NetHead->data=tmp1; NetHead->PKG_LEN=strlen(NetHead->data); SendPack(conn,NetHead); return 0; // fail } clip=(T_CLI_Var *)gp->server->Var; if(clip) { // stptok(clip->UID,up->SQL_Connect.UID,sizeof(up->SQL_Connect.UID),0); // stptok(clip->DBOWN,up->SQL_Connect.DBOWN,sizeof(up->SQL_Connect.DBOWN),0); } ShowLog(5,"%s:TCB:%d,poolno=%d,Errno=%d",__FUNCTION__,up->TCB_no,up->poolno,clip->Errno); release_SC_connect(&gp->server,up->TCB_no,up->poolno); cp=tmp1; if(clip) cp+=sprintf(cp,"%s|%s|%s|%s|", gp->devid,gp->operid ,clip->UID,clip->DBOWN); else cp+=sprintf(cp,"%s|%s|||", gp->devid,gp->operid); cp+=sprintf(cp,"%s|%d|",rsecstrfmt(tmp,now_sec(),YEAR_TO_SEC),up->TCB_no); ShowLog(2,"%s:%s Login success",__FUNCTION__,tmp1); NetHead->data=tmp1; NetHead->PKG_LEN=strlen(NetHead->data); NetHead->ERRNO1=0; NetHead->ERRNO2=0; NetHead->PKG_REC_NUM=0; SendPack(conn,NetHead); return 1; }
uint8 SendByCSMA(u8 *buff,uint8 length) { uint8 cca_value = 0; uint32 wait_time = 0; uint32 q,w; //wait_time = ((MAX_DEVICE_NUM - EndPointDevice.free_node)*SLOT_LENGTH)/10; q=MAX_DEVICE_NUM - EndPointDevice.free_node; w=q*SLOT_LENGTH; wait_time=w/100; while(Frame_Time<=wait_time); //等待到达CSMA时隙 超帧内时间<=负载数*时隙长度 //TIME1_LOW; CSMABackOff(); //TIME1_HIGH; cca_value = (A7139_GetRSSI()+A7139_GetRSSI())/2; if(cca_value<CCA) { SendPack(); RXMode(); } else { CSMABackOff(); CSMA_BackOff_Count++; } if(CSMA_BackOff_Count>CSMA_BACKOFF_TIME) return CSMA_FAIL; else return CSMA_SUCCESS; }
BOOL CTcpAgent::Send(CONNID dwConnID, const BYTE* pBuffer, int iLength, int iOffset) { ASSERT(pBuffer && iLength > 0); if(iOffset != 0) pBuffer += iOffset; int result = NO_ERROR; TSocketObj* pSocketObj = FindSocketObj(dwConnID); if(!TSocketObj::IsValid(pSocketObj)) result = ERROR_OBJECT_NOT_FOUND; else { CCriSecLock locallock(pSocketObj->crisec); if(!TSocketObj::IsValid(pSocketObj)) result = ERROR_OBJECT_NOT_FOUND; else { switch(m_enSendPolicy) { case SP_PACK: result = SendPack(pSocketObj, pBuffer, iLength); break; case SP_SAFE: result = SendSafe(pSocketObj, pBuffer, iLength); break; case SP_DIRECT: result = SendDirect(pSocketObj, pBuffer, iLength); break; default: ASSERT(FALSE); result = ERROR_INVALID_INDEX; break; } } } if(result != NO_ERROR) ::SetLastError(result); return (result == NO_ERROR); }
int N_SQL_Exec(T_Connect *connect,char *cmd) { T_CLI_Var *clip=(T_CLI_Var *)connect->Var; T_NetHead nethead; int i; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_Exec"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } nethead.O_NODE=clip->ctx_id; nethead.D_NODE=get_d_node(); nethead.ERRNO1=0; nethead.ERRNO2=(connect->status>0)?PACK_STATUS:0; nethead.PKG_REC_NUM=0; nethead.data=cmd; nethead.PKG_LEN=strlen(nethead.data); i=SendPack(connect,&nethead); if(i)return i; i=RecvPack(connect,&nethead); //ShowLog(5,"SQL_Exec:ret=%d,errno1=%d,len=%d:%s",i,nethead.ERRNO1,nethead.PKG_LEN,nethead.data); if(i){ return i; } EventCatch(connect,nethead.PROTO_NUM); return GetError(connect,&nethead); }
int N_SQL_Select(T_Connect *connect,char *cmd,char **data,int num) { T_NetHead nethead; T_CLI_Var *clip=(T_CLI_Var *)connect->Var; int i; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_Select"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } nethead.ERRNO1=0; nethead.ERRNO2=(connect->status>0)?PACK_STATUS:0; nethead.O_NODE=clip->ctx_id; nethead.D_NODE=get_d_node(); nethead.PKG_REC_NUM=num; nethead.data=cmd; nethead.PKG_LEN=strlen(nethead.data); i=SendPack(connect,&nethead); if(i<0)return i; i=RecvPack(connect,&nethead); if(i<0)return i; EventCatch(connect,nethead.PROTO_NUM); *((T_User_Var *)connect->Var)->ErrMsg=0; ((T_User_Var *)connect->Var)->Errno=nethead.ERRNO1; *data=nethead.data; if(!nethead.ERRNO1) { /* column num */ ((T_User_Var *)connect->Var)->NativeError=nethead.ERRNO2; return nethead.PKG_REC_NUM; } else { return GetError(connect,&nethead); } }
int N_SQL_EndTran(T_Connect *connect,int TranFlag) { T_NetHead nethead; T_CLI_Var *clip=(T_CLI_Var *)connect->Var; int i; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_EndTran"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } nethead.O_NODE=clip->ctx_id; nethead.D_NODE=get_d_node(); if(TranFlag == TRANBEGIN) connect->status++; else connect->status--; nethead.ERRNO2=(connect->status>0)?PACK_STATUS:0; nethead.ERRNO1=TranFlag; nethead.PKG_REC_NUM=0; nethead.data=0; nethead.PKG_LEN=0; i=SendPack(connect,&nethead); if(i)return i; i=RecvPack(connect,&nethead); if(i)return i; EventCatch(connect,nethead.PROTO_NUM); return GetError(connect,&nethead); }
int N_SQL_Fetch(T_Connect *connect,int curno,char **data,int recnum) { T_CLI_Var *clip=(T_CLI_Var *)connect->Var; T_NetHead nethead; int i; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_Fetch"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } nethead.O_NODE=clip->ctx_id; nethead.D_NODE=get_d_node(); nethead.ERRNO2=PACK_STATUS; nethead.ERRNO1=curno; /* recnum:进入时每次Fetch的记录数,0=全部记录,null=1条 */ nethead.PKG_REC_NUM=recnum; nethead.data=0; nethead.PKG_LEN=0; i=SendPack(connect,&nethead); if(i<0)return i; i=RecvPack(connect,&nethead); if(i<0)return i; EventCatch(connect,nethead.PROTO_NUM); if(nethead.ERRNO1) return GetError(connect,&nethead); *data=nethead.data; *((T_User_Var *)connect->Var)->ErrMsg=0; ((T_User_Var *)connect->Var)->Errno=nethead.ERRNO1; /* colunm num */ ((T_User_Var *)connect->Var)->NativeError=nethead.ERRNO2; /* 返回实际得到的记录数 */ return nethead.PKG_REC_NUM; }
int CTcpServer::SendInternal(TSocketObj* pSocketObj, const WSABUF pBuffers[], int iCount) { int result = NO_ERROR; for(int i = 0; i < iCount; i++) { int iBufLen = pBuffers[i].len; if(iBufLen > 0) { BYTE* pBuffer = (BYTE*)pBuffers[i].buf; ASSERT(pBuffer); switch(m_enSendPolicy) { case SP_PACK: result = SendPack(pSocketObj, pBuffer, iBufLen); break; case SP_SAFE: result = SendSafe(pSocketObj, pBuffer, iBufLen); break; case SP_DIRECT: result = SendDirect(pSocketObj, pBuffer, iBufLen); break; default: ASSERT(FALSE); result = ERROR_INVALID_INDEX; break; } if(result != NO_ERROR) break; } } return result; }
//连接 static int sc_connect(pool *p1,resource *rs) { int ret=-1; T_NetHead Head; struct utsname ubuf; char buf[200],*p,addr[20]; log_stu logs; rs->Conn.timeout=-1; ret=Net_Connect(&rs->Conn,&rs->cli,*p1->log.family?p1->family:NULL); if(ret) { rs->cli.Errno=errno; stptok(strerror(errno),rs->cli.ErrMsg,sizeof(rs->cli.ErrMsg),0); return -1; } //login uname(&ubuf); p=buf; Head.O_NODE=LocalAddr(rs->Conn.Socket,addr); p+=sprintf(p,"%s|%s|%s,%s|||",p1->log.DEVID,p1->log.LABEL, ubuf.nodename,addr); rs->Conn.MTU=p1->log.MTU; Head.PROTO_NUM=0; Head.D_NODE=p1->log.NEXT_d_node; Head.ERRNO1=rs->Conn.MTU; Head.ERRNO2=Head.PKG_REC_NUM=0; Head.data=buf; Head.PKG_LEN=strlen(Head.data); ret=SendPack(&rs->Conn,&Head); ret=RecvPack(&rs->Conn,&Head); if(ret) { rs->cli.Errno=errno; stptok(strerror(errno),rs->cli.ErrMsg,sizeof(rs->cli.ErrMsg),0); disconnect(&rs->Conn); ShowLog(1,"%s:network error %d,%s",__FUNCTION__,rs->cli.Errno,rs->cli.ErrMsg); rs->cli.Errno=-1; return -2; } if(Head.ERRNO1 || Head.ERRNO2) { ShowLog(1,"%s:login error ERRNO1=%d,ERRNO2=%d,%s",__FUNCTION__, Head.ERRNO1,Head.ERRNO2,Head.data); disconnect(&rs->Conn); stptok(Head.data,rs->cli.ErrMsg,sizeof(rs->cli.ErrMsg),0); rs->cli.Errno=-1; return -3; } net_dispack(&logs,Head.data,log_tpl); strcpy(rs->cli.DBOWN,logs.DBOWN); strcpy(rs->cli.UID,logs.DBUSER); //取服务名 ret=init_svc_no(&rs->Conn); rs->cli.Errno=ret; *rs->cli.ErrMsg=0; return ret; }
int N_SQL_Prepare(T_Connect *connect,char *cmd,T_SqlDa *sqlda) { T_CLI_Var *clip=(T_CLI_Var *)connect->Var; T_NetHead nethead; int i; char *p; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_Prepare"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } nethead.O_NODE=clip->ctx_id; nethead.D_NODE=get_d_node(); nethead.ERRNO1=0; connect->status++; nethead.ERRNO2=PACK_STATUS; nethead.PKG_REC_NUM=0; nethead.data=cmd; nethead.PKG_LEN=strlen(nethead.data); i=SendPack(connect,&nethead); if(i) { connect->status--; return i; } i=RecvPack(connect,&nethead); if(i) { connect->status--; return i; } if((int)nethead.ERRNO1<0) { int ret=GetError(connect,&nethead); return ret; } sqlda->cursor_no=nethead.ERRNO1; sqlda->cols=nethead.PKG_REC_NUM; if(nethead.PKG_LEN>0) { sqlda->sqlvar=(T_SqlVar *)malloc(sizeof(T_SqlVar)*sqlda->cols); if(sqlda->sqlvar) { p=nethead.data; for(i=0;i<sqlda->cols;i++) { p+=net_dispack(&sqlda->sqlvar[i],p,SqlVarType); } } } else sqlda->sqlvar=0; ((T_CLI_Var *)connect->Var)->Errno=0; ((T_CLI_Var *)connect->Var)->NativeError=nethead.PKG_REC_NUM; *((T_CLI_Var *)connect->Var)->ErrMsg=0; EventCatch(connect,nethead.PROTO_NUM); return 0; }
/******************************************************* * 取得服务器端服务名列表 * 服务器端的0号函数必须是登录认证函数。登录认证完成后, * 必须把0号函数值换成 get_srvname(); * 1号函数必须是echo() * conn->freevar指向free_srv_list,如果需要其他善后操作 * 应重置,但最后应补做free_srv_list(T_CLI_Var *); *******************************************************/ int init_svc_no(T_Connect *conn) { T_CLI_Var *clip; T_NetHead head; int ret,i; char *p; srv_list *lp; svc_table *svcp; if(!conn || !conn->Var) return -1; clip=(T_CLI_Var *)conn->Var; if(!clip) return -1; svcp=clip->svc_tbl; if(!svcp) return -1; svcp->srvn=0; svcp->usage=-1; svcp->srv_hash=0; svcp->srvlist=0; head.PROTO_NUM=0; head.ERRNO1=head.ERRNO2=0; head.PKG_REC_NUM=0; head.O_NODE= clip->ctx_id; head.D_NODE=0; head.data=0; head.PKG_LEN=0; ret=SendPack(conn,&head); if(ret<0) return ret; ret=RecvPack(conn,&head); if(ret<0) return(ret); if(head.ERRNO1 && head.ERRNO2) return -2; svcp->srvn=head.PKG_REC_NUM; svcp->srvlist=malloc(sizeof(srv_list) * clip->svc_tbl->srvn); if(!svcp->srvlist) { clip->svc_tbl=NULL; return -1; } lp=(srv_list *)svcp->srvlist; p=head.data; //ShowLog(2,"%s:srvn=%d,data=%s",__FUNCTION__,svcp->srvn,p); for(i=0;i<svcp->srvn&&*p;i++) { p+=net_dispack(lp,p,slist_tpl); //ShowLog(5,"src_no=%d,srv_name=%s",lp->srv_no,lp->srv_name); lp++; } svcp->usage=0; svcp->srv_hash=mk_srv_hash((srv_list *)clip->svc_tbl->srvlist,clip->svc_tbl->srvn); conn->freevar=(void (*)(void *)) free_srv_list; return 0; }
int N_SQL_Close_RefCursor(T_Connect *connect,int ref_cursor) { T_CLI_Var *clip=(T_CLI_Var *)connect->Var; T_NetHead nethead; int i; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_Close"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } nethead.O_NODE=clip->ctx_id; nethead.D_NODE=0; connect->status--; nethead.ERRNO1=ref_cursor; nethead.PKG_REC_NUM=0; nethead.data=NULL; nethead.PKG_LEN=0; if(ref_cursor<0) { //终止中间件的状态 if(connect->status <= 0) { connect->status=0; //请求夭折状态 nethead.ERRNO2=PACK_NOANSER; SendPack(connect,&nethead); } return 0; } nethead.ERRNO2=(connect->status>0)?PACK_STATUS:0; i=SendPack(connect,&nethead); if(i)return i; i=RecvPack(connect,&nethead); if(i)return i; EventCatch(connect,nethead.PROTO_NUM); return GetError(connect,&nethead); }
void DataSend(void) { //before_slot_wake = (((EndPointDevice.cluster_innernum-1)*SLOT_LENGTH)-WAKE_TIME)+5000; //为什么写一起就不对!!! a = (EndPointDevice.cluster_innernum-1); b = a*SLOT_LENGTH; c = b + 5000; //每个时隙向后移5ms,让中继切换状态 before_slot_wake = (c-WAKE_TIME)/100; DIS_INT; while(Frame_Time<=before_slot_wake); EndPointDevice.data_ack = 0; A7139_Wake(); CreatSendData(); TIME1_LOW; SendPack(); RXMode(); ack_flag = RecvDataACK(); if(ack_flag == 1) { A7139_Sleep(); } else { EN_INT; //TIME1_HIGH; //delay_ms(5); //TIME1_LOW; //delay_ms(5); //TIME1_HIGH; } if((EndPointDevice.power == 0)) EN_INT; }
int N_SQL_RPC(T_Connect *connect,char *cmd,char **data,int *nrets,int st_lev) { T_NetHead nethead; int i; T_CLI_Var *clip; clip=(T_CLI_Var *)connect->Var; nethead.PROTO_NUM=get_srv_no(connect->Var,"SQL_RPC"); if(nethead.PROTO_NUM==1) { ShowLog(1,"%s:服务不存在",__FUNCTION__); return FORMATERR; } connect->status += st_lev; nethead.O_NODE=clip->ctx_id; nethead.D_NODE=get_d_node(); nethead.ERRNO1=0; nethead.ERRNO2=(connect->status>0)?PACK_STATUS:0; nethead.PKG_REC_NUM=0; nethead.data=cmd; nethead.PKG_LEN=strlen(nethead.data); i=SendPack(connect,&nethead); //free(buffer); if(i)return i; i=RecvPack(connect,&nethead); if(i)return i; EventCatch(connect,nethead.PROTO_NUM); *clip->ErrMsg=0; clip->Errno=nethead.ERRNO1; if(nrets) *nrets=0;*data=0; if(!nethead.ERRNO1) { if(nrets) *nrets=nethead.PKG_REC_NUM; //ncols *data=nethead.data; clip->NativeError=nethead.PKG_REC_NUM; //ncols return 0; } else { return GetError(connect,&nethead); } }
BOOL CUdpServer::Send(CONNID dwConnID, const BYTE* pBuffer, int iLength, int iOffset) { ASSERT(pBuffer && iLength > 0 && iLength <= (int)m_dwMaxDatagramSize); if(iOffset != 0) pBuffer += iOffset; if(!pBuffer || iLength <= 0 || iLength > (int)m_dwMaxDatagramSize) { ::SetLastError(ERROR_INVALID_PARAMETER); return FALSE; } int result = ERROR_OBJECT_NOT_FOUND; TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID); if(TUdpSocketObj::IsValid(pSocketObj)) { CCriSecLock locallock(pSocketObj->crisec); if(TUdpSocketObj::IsValid(pSocketObj)) { switch(m_enSendPolicy) { case SP_PACK: result = SendPack(pSocketObj, pBuffer, iLength); break; case SP_SAFE: result = SendSafe(pSocketObj, pBuffer, iLength); break; case SP_DIRECT: result = SendDirect(pSocketObj, pBuffer, iLength); break; default: ASSERT(FALSE); result = ERROR_INVALID_INDEX; break; } } } if(result != NO_ERROR) ::SetLastError(result); return (result == NO_ERROR); }
/* file_list: server */ int filels(T_Connect *conn,T_NetHead *NetHead) { char *buff,*env,*p; char *p1; char tmp[1000],dir[512],tmp1[2000]; FILE *fd; int ret; char buffer[SDBC_BLKSZ]; int Event_no=NetHead->PROTO_NUM; if(!NetHead->PKG_LEN) { ShowLog(1,"getlist what to list?"); NetHead->ERRNO1=-1; errret: NetHead->PROTO_NUM=PutEvent(conn,Event_no); NetHead->data=0; NetHead->PKG_LEN=0; NetHead->ERRNO2=-1; NetHead->PKG_REC_NUM=0; NetHead->O_NODE=LocalAddr(conn->Socket,0); NetHead->D_NODE=0; ret=SendPack(conn,NetHead); return 0; } buff=malloc(NetHead->PKG_LEN+200); if(!buff) { NetHead->ERRNO1=-2; goto errret; } strcpy(buff,NetHead->data); //ShowLog(5,"errno2=%d %s",NetHead->ERRNO2,buff); if(!strncmp(buff,"$/",2)) { env=getenv("SENDDIR"); p=buff+1; } else if(!strncmp(buff,"@/",2)) { env=getenv("HOME"); p=buff+1; } else if(*buff=='$') { p=stptok(buff+1,tmp,sizeof(tmp),"/"); env=getenv(tmp); } else { p=buff; } if(p>buff) { if(!env||!*env) env="."; if(env[strlen(env)-1]=='/') env[strlen(env)-1]=0; strcpy(tmp,env); //ShowLog(5,"%s:env=%s,buff=%s",__FUNCTION__,env,buff); strsubst(buff,p-buff,tmp); } ShowLog(5,"filelist:path=%s",buff); sprintf(tmp,"ls %s",buff); if(isdir(buff)>0) strcpy(dir,buff); else { p1=strrchr(buff,'/'); if(p1) { *p1=0; strcpy(dir,buff); } else strcpy(dir,"."); } if(dir[strlen(dir)-1]=='/') dir[strlen(dir)-1]=0; fd=popen(tmp,"r"); if(!fd) { ShowLog(1,"%s:err=%d",tmp,errno); free(buff); NetHead->ERRNO1=errno; goto errret; } //ShowLog(5,"tmp=%s",tmp); p=buffer; *p=0; while((ret=fscanf(fd,"%s",tmp))==1) { if(*buffer==0 && tmp[strlen(tmp)-1] == ':') {//没找到 break; } p1=sc_basename(tmp); ret=sprintf(tmp1,"%s/%s",dir,p1); if(isrfile(tmp1)<=0) continue; //not readble reg file /* send tmp1 to client */ if((int)(buffer+sizeof(buffer)-2-p)>ret) { p=stpcpy(p,tmp1); } else { //ShowLog(5,"%s",buffer); NetHead->PROTO_NUM=0; NetHead->data=buffer; NetHead->PKG_LEN=strlen(NetHead->data); NetHead->ERRNO2= PACK_CONTINUE; NetHead->O_NODE=LocalAddr(conn->Socket,0); NetHead->D_NODE=0; ret=SendPack(conn,NetHead); if(ret) break; p=buffer; p=stpcpy(p,tmp1); *p++ = ' '; *p=0; } } pclose(fd); if(*buffer) { //ShowLog(5,"final %s",buffer); NetHead->PROTO_NUM=0; NetHead->data=buffer; NetHead->PKG_LEN=strlen(NetHead->data); NetHead->ERRNO2= PACK_CONTINUE; NetHead->O_NODE=LocalAddr(conn->Socket,0); ret=SendPack(conn,NetHead); *buffer=0; } NetHead->PROTO_NUM=PutEvent(conn,Event_no); NetHead->data=0; NetHead->PKG_LEN=0; NetHead->ERRNO2=0; NetHead->PKG_REC_NUM=0; NetHead->O_NODE=LocalAddr(conn->Socket,0); NetHead->D_NODE=0; ret=SendPack(conn,NetHead); free(buff); return 0; }
int login(T_Connect *conn,T_NetHead *NetHead) { int ret,crc; char tmp[200]; char *cp,*key; char tmp1[1024],cliaddr[20]; DWS dw; struct login_s logrec; ENIGMA egm; FILE *fd; T_SRV_Var *up; GDA *gp; //u_int e[RSALEN],m[RSALEN]; up=(T_SRV_Var *)conn->Var; gp=(GDA *)up->var; StrAddr(NetHead->O_NODE,cliaddr); ShowLog(5,"%s:TCB:%d Client IP Addr=%s,Net_login %s",__FUNCTION__,up->TCB_no,cliaddr,NetHead->data); net_dispack(&logrec,NetHead->data,login_type); strcpy(gp->devid,logrec.devid); sprintf(gp->ShowID,"%s:%s:%d",logrec.devid,cliaddr,up->TCB_no); mthr_showid_add(up->tid,gp->ShowID); conn->MTU=NetHead->ERRNO1; cp=getenv("KEYFILE"); if(!cp||!*cp) { strcpy(tmp1,"缺少环境变量 KEYFILE"); errret: ShowLog(1,"%s:Error %s",__FUNCTION__,tmp1); NetHead->ERRNO1=-1; NetHead->ERRNO2=-1; NetHead->PKG_REC_NUM=0; NetHead->data=tmp1; NetHead->PKG_LEN=strlen(NetHead->data); SendPack(conn,NetHead); return 0; // fail } /* read key */ crc=0; reopen: ret=initdw(cp,&dw); if(ret) { if((errno==24)&& (++crc<5)) { sleep(15); goto reopen; } sprintf(tmp1,"Init dw error %d",ret); goto errret; } crc=ssh_crc32((unsigned char *)logrec.devid,strlen(logrec.devid)); key=getdw(crc,&dw); if(!key) { freedw(&dw); sprintf(tmp1,"无效的 DEVID"); goto errret; } //ShowLog(5,"getdw key=%s",key); enigma1_init(egm,key); /* check CA */ memset(gp->operid,0,sizeof(gp->operid)); cp=getenv("CADIR"); if(!cp||!*cp) cp="."; if(strcmp(gp->devid,"REGISTER")) { strncpy(gp->operid,logrec.uid,sizeof(gp->operid)-1); sprintf(tmp,"%s/%s.CA",cp,logrec.devid); //ShowLog(5,"CAfile=%s,key=%s",tmp,key); fd=fopen(tmp,"r"); if(!fd) { if(errno==2) { crc=strlen(logrec.CA); frenz_encode(egm,logrec.CA,crc); byte_a64(tmp1,logrec.CA,crc); //ShowLog(5,"CA=%s",tmp1); fd=fopen(tmp,"w"); if(!fd) { sprintf(tmp1,"write %s err=%d",tmp,errno); err1: freedw(&dw); goto errret; } fprintf(fd,"%s\n",tmp1); fclose(fd); } else { sprintf(tmp1,"open CAfile %s err=%d",tmp,errno); goto err1; } } else { fgets(tmp1,sizeof(logrec.CA),fd); fclose(fd); TRIM(tmp1); ret=a64_byte(tmp,tmp1); frenz_decode(egm,tmp,ret); tmp[ret]=0; if(strcmp(tmp,logrec.CA)) { sprintf(tmp1,"CA 错误"); ShowLog(1,"%s:%s CA=%s log=%s len=%d",__FUNCTION__,tmp1,tmp,logrec.CA,ret); goto err1; } } } else { //未注册客户端注册 char *p; char *keyD; /* REGISTER label|CA|devfile|CHK_Code| */ ShowLog(2,"REGISTER %s",logrec.uid); if(!*logrec.uid) { sprintf(tmp1,"REGSTER is empty!"); goto err1; } //uid=devfile crc=0xFFFF&gencrc((unsigned char *)logrec.uid,strlen(logrec.uid)); //pwd=CHK_Code sscanf(logrec.pwd,"%04X",&ret); ret &= 0xFFFF; if(ret != crc) { sprintf(tmp1,"REGISTER:devfile CHK Code error! ");//, crc,ret); goto err1; } p=stptok(logrec.uid,logrec.devid,sizeof(logrec.devid),".");//logrec.devid=准备注册的DEVID crc=ssh_crc32((unsigned char *)logrec.devid,strlen(logrec.devid)); keyD=getdw(crc,&dw); if(!keyD) { sprintf(tmp1,"注册失败,%s:没有这个设备!", logrec.devid); goto err1; } enigma1_init(egm,keyD); sprintf(tmp,"%s/%s.CA",cp,logrec.devid); ShowLog(5,"REGISTER:%s",tmp); if(0!=(fd=fopen(tmp,"r"))) { fgets(tmp1,81,fd); fclose(fd); TRIM(tmp1); ret=a64_byte(tmp,tmp1); frenz_decode(egm,tmp,ret); tmp[ret]=0; if(strcmp(tmp,logrec.CA)) { sprintf(tmp1,"注册失败,%s 已被注册,使用中。", logrec.devid); goto err1; } } else if(errno != 2) { sprintf(tmp1,"CA 错误"); goto err1; } /*把设备特征码写入文件*/ fd=fopen(tmp,"w"); if(fd) { int len=strlen(logrec.CA); frenz_encode(egm,logrec.CA,len); byte_a64(tmp1,logrec.CA,len); fprintf(fd,"%s\n",tmp1); fclose(fd); } else ShowLog(1,"net_login:REGISTER open %s for write,err=%d,%s", tmp,errno,strerror(errno)); freedw(&dw); sprintf(tmp,"%s/%s",cp,logrec.uid); fd=fopen(tmp,"r"); if(!fd) { sprintf(tmp1,"REGISTER 打不开文件 %s err=%d,%s", logrec.CA,errno,strerror(errno)); goto errret; } fgets(logrec.uid,sizeof(logrec.uid),fd); TRIM(logrec.uid); ShowLog(2,"REGISTER open %s",tmp); fclose(fd); cp=tmp1; cp+=sprintf(cp,"%s|%s|", logrec.devid,logrec.uid); cp+=sprintf(cp,"%s|",rsecstrfmt(tmp,now_sec(),YEAR_TO_SEC)); NetHead->data=tmp1; NetHead->PKG_LEN=strlen(NetHead->data); NetHead->ERRNO1=0; NetHead->ERRNO2=0; NetHead->PKG_REC_NUM=0; SendPack(conn,NetHead); return -1; } //未注册客户端注册完成 freedw(&dw); up->poolno=get_scpool_no(NetHead->D_NODE); if(up->poolno<0) { sprintf(tmp1,"非法的D_NODE %d",NetHead->D_NODE); goto errret; } ret=get_s_connect(up->TCB_no,up->poolno,&gp->server,login_finish); if(ret==0) return login_finish(conn,NetHead); else if(ret==1) return -5; sprintf(tmp1,"错误的参数"); goto errret; }
int getlist(T_Connect *conn,T_NetHead *NetHead) { char *buff,*env,*p; char *p1,*p2; char tmp[1000],dir[512],tmp1[2000]; FILE *fd; int ret,dirnum; char buffer[4096]; if(!NetHead->pkg_len) { ShowLog(1,"getlist what to list?"); NetHead->errno1=0; errret: NetHead->data=0; NetHead->pkg_len=0; NetHead->errno2=-1; NetHead->pkg_rec_num=0; NetHead->o_node=0; NetHead->d_node=0; ret=SendPack(conn,NetHead); return 0; } buff=malloc(NetHead->pkg_len+100); strcpy(buff,NetHead->data); ShowLog(5,"%s",buff); if(!strncmp(buff,"$/",2)) { env=getenv("SENDDIR"); p=buff+2; } else if(!strncmp(buff,"@/",2)) { env=getenv("HOME"); p=buff+2; } else if(*buff=='$') { p=stptok(buff+1,tmp,sizeof(tmp),"/"); env=getenv(tmp); } else { p=buff; } if(p>buff) { if(!env||!*env) env="."; sprintf(tmp,"%s/",env); strsubst(buff,p-buff,tmp); } //if(!*LOGFILE) p2="/dev/null"; //else p2=LOGFILE; p2="/dev/null"; sprintf(tmp,"ls -R %s 2>>%s ",buff,p2); ShowLog(5,tmp); dirnum=0; *dir=0; fd=popen(tmp,"r"); if(!fd) { ShowLog(1,"%s:err=%d",tmp,errno); free(buff); NetHead->errno1=errno; goto errret; } buffer[0]=0; while((ret=fscanf(fd,"%s",tmp))==1) { if(tmp[strlen(tmp)-1]==':') { // is dir if(!*dir) { tmp[strlen(tmp)-1]=0; strcpy(dir,tmp); continue; } else { ShowLog(5,tmp); break; // no -R } } if(*dir) { if(dir[strlen(dir)-1]=='/') sprintf(tmp1,"%s%s",dir,tmp); else sprintf(tmp1,"%s/%s",dir,tmp); } else { strcpy(tmp1,tmp); } if(isrfile(tmp1)<=0) continue; //not readble reg file /* send tmp1 to client */ ret=strlen(buffer); if((ret+strlen(tmp1)+2) < sizeof(buffer)) { sprintf(buffer+ret,"%s ",tmp1); } else { NetHead->data=buffer; NetHead->pkg_len=strlen(NetHead->data)+1; NetHead->errno2=0; NetHead->pkg_rec_num=0; NetHead->o_node=0; NetHead->d_node=0; ret=SendPack(conn,NetHead); if(ret) break; strcpy(buffer,tmp1); strcat(buffer," "); } } pclose(fd); if(*buffer) { NetHead->data=buffer; NetHead->pkg_len=strlen(NetHead->data)+1; NetHead->errno2=0; NetHead->pkg_rec_num=0; NetHead->o_node=0; NetHead->d_node=0; ret=SendPack(conn,NetHead); *buffer=0; } NetHead->data=0; NetHead->pkg_len=0; NetHead->errno2=100; NetHead->pkg_rec_num=0; NetHead->o_node=0; NetHead->d_node=0; ret=SendPack(conn,NetHead); free(buff); return 0; }
void ackpack(u8 *buff,u16 len) { SendPack(recvcmd,buff,len); AutoACK+=2; }