/** * 作用: 连接到FTP服务器 * 参数: hostname或IP,端口,用户名,密码 * 返回值: 已连接到FTP服务器的SOCKET -1 表示登录失败 * */ SOCKET ftp_connect(char *host, int port, char *user, char *pwd) { SOCKET sock; sock = connect_server(host, port); if (-1 == sock) { return -1; } if (-1 == login_server(sock, user, pwd)) { logger.error("socket登录失败"); closesocket(sock); return -1; } return sock; }
int main(int argc,char **argv,char **envp) { long command_id; char hlr_code[8]; char phone_no[20]; char command_code[8]; char op_code[8]; char imsi_no[32]; char new_phone[20]; char new_imsi[32]; char business_status[8]; struct timeval tt1, tt2; int ret; char inputstr[2048],ss[64]; int i,status,today; char srvip[32]; int srvport; char dbuser[32],dbpswd[32],dbname[32],tname[64]; char logfilename[256],logdir[256]; time_t t; struct tm *tv; char chhlr; int msglen,readlen,headlen,tcplen,bodylen; int transid; char order_fname[256],reply_fname[256],query_fname[256],cfginfo[1024],*p,*d; int line; int ordercode, etm; FILE *cfgfp; char cfgstr[256]; char mgr_user[20], mgr_pswd[20], mgr_term[10]; procname=argv[0]; logfp=NULL; memset(inputstr,0x0,sizeof(inputstr)); for(i=0;i<argc;i++) { strcat(inputstr,argv[i]); strcat(inputstr," "); } if(GenDaemon()<0) { printf("GenDaemon() failed!\n"); exit(1); } signal(SIGTERM,LogOut); signal(SIGPIPE,SIG_IGN); signal(SIGCLD,SIG_IGN); signal(SIGINT,SIG_IGN); memset(hlrcode,0x0,sizeof(hlrcode)); memset(hlrport,0x0,sizeof(hlrport)); memset(dbuser,0x0,sizeof(dbuser)); memset(dbpswd,0x0,sizeof(dbpswd)); memset(dbname,0x0,sizeof(dbname)); memset(logdir,0x0,sizeof(logdir)); while(--argc>0) { if(argv[argc][0]!='-') usage("Incorrect Parameter!"); printf("CMD:%c\n",argv[argc][1]); switch(argv[argc][1]) { case 'H': strncpy(hlrcode,argv[argc]+2,4); break; case 'N': strncpy(hlrport,argv[argc]+2,4); break; case 'U': strncpy(dbuser,argv[argc]+2,30); break; case 'P': strncpy(dbpswd,argv[argc]+2,30); break; case 'S': strncpy(dbname,argv[argc]+2,30); break; case 'L': strncpy(logdir,argv[argc]+2,240); break; case 'V': printf("\n\n version = %s\n\n",VERSION); exit(0); default: usage("Unknow Parameter!"); break; } } printf("hlrcode===%s[%s]~\n",hlrcode,hlrport); if(strlen(hlrcode)!=3) usage("Incorrect parameter [hlrcode]!\n"); if(strlen(hlrport)!=1) usage("Incorrect parameter [hlrport]!\n"); if(strlen(logdir)<1) strcpy(logdir,getenv("LOGDIR")); if(testdir(logdir)) usage("Incorrect parameter [logdir]!"); time(&t); tv=localtime(&t); memset(logfilename,0x0,sizeof(logfilename)); sprintf(logfilename,"%s/%s%04d%02d%02d.%s.%s",logdir, port_type, tv->tm_year+1900,tv->tm_mon+1,tv->tm_mday,hlrcode,hlrport); logfp=fopen(logfilename,"a"); if(logfp==NULL) { printf("can't open %s for LOG!\n",logfilename); exit(1); } fprintf(logfp,"INIT %04d/%02d/%02d %02d:%02d:%02d\n", tv->tm_year+1900,tv->tm_mon+1,tv->tm_mday, tv->tm_hour,tv->tm_min,tv->tm_sec); fprintf(logfp,"%s[%d]\n",inputstr,argc); fflush(logfp); today=tv->tm_mday; /************ 获取连接配置信息 ********************/ if(get_hlr_cfg(hlrcode, atoi(hlrport), gsmip, &gsmport, gsmuser, gsmpswd, order_fname,reply_fname,query_fname, srvip, &commport, NULL, mgr_user, mgr_pswd, mgr_term) != 0) { fprintf(logfp,"get_cfg_filename(%s) failed!\n",hlrcode); fclose(logfp); exit(1); } /************ GET ORDER INFO ***********************/ cfgfp=fopen(order_fname,"r"); if(cfgfp==NULL) { fprintf(logfp,"Can't open %s for read[%d]\n",order_fname,errno); fclose(logfp); exit(1); } line=0; memset(cfginfo,0x0,sizeof(cfginfo)); while(fgets(cfginfo,sizeof(cfginfo),cfgfp)) { line++; if(cfginfo[0]=='#' || cfginfo[0]=='\r' || cfginfo[0]=='\n') { memset(cfginfo,0x0,sizeof(cfginfo)); continue; } if(strncmp(cfginfo,"OP",2) || (p=strchr(cfginfo,':'))==NULL) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } orderptr=(struct order_info *)malloc(sizeof(struct order_info)); memset(orderptr,0x0,sizeof(struct order_info)); orderptr->ordercode=atoi(cfginfo+2); strcpy(orderptr->orderinfo,rlspace(p+1)); if(strlen(orderptr->orderinfo)==0 || strlen(orderptr->orderinfo)>1000) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } ordertail=&orderhead; while(ordertail->next) { if(ordertail->next->ordercode==orderptr->ordercode) { fprintf(logfp,"ORDER_CODE[%d] is exists[line=%d]\n", orderptr->ordercode,line); fclose(logfp); fclose(cfgfp); exit(1); } ordertail=ordertail->next; } ordertail->next=orderptr; memset(cfginfo,0x0,sizeof(cfginfo)); } fclose(cfgfp); /*fprintf(logfp,"ORDER INFO:\n"); orderptr=orderhead.next; while(orderptr) { fprintf(logfp,"%06d = [%s]\n",orderptr->ordercode,orderptr->orderinfo); orderptr=orderptr->next; } fflush(logfp);*/ /************ GET REPLY INFO ***********************/ cfgfp=fopen(reply_fname,"r"); if(cfgfp==NULL) { fprintf(logfp,"Can't open %s for read[%d]\n",reply_fname,errno); fclose(logfp); exit(1); } fprintf(logfp,"REPLY INFO:\n"); line=0; memset(cfginfo,0x0,sizeof(cfginfo)); while(fgets(cfginfo,sizeof(cfginfo),cfgfp)) { line++; if(cfginfo[0]=='#' || cfginfo[0]=='\r' || cfginfo[0]=='\n') { memset(cfginfo,0x0,sizeof(cfginfo)); continue; } /*fprintf(logfp,"%s",cfginfo); */ if(strncmp(cfginfo,"RETN:",5) || cfginfo[9]!=':') { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } replyptr=(struct reply_info *)malloc(sizeof(struct reply_info)); memset(replyptr,0x0,sizeof(struct reply_info)); replyptr->replycode=atoi(cfginfo+5); p=cfginfo+10; d=replyptr->replyinfo; if(*p!='[' || strchr(p,']')==NULL) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } while(*(++p)!=']') { if(*p=='\\') { p++; switch(*p) { case 'r': *d='\r'; break; case 'n': *d='\n'; break; case 't': *d='\t'; break; default: *d=*p; } } else *d=*p; d++; } if(d==replyptr->replyinfo) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } replyptr->next=replyhead.next; replyhead.next=replyptr; memset(cfginfo,0x0,sizeof(cfginfo)); } fclose(cfgfp); fflush(logfp); /************ GET QUERY INFO ***********************/ ret=get_query_cfg(query_fname); if(ret) { fprintf(logfp,"get_query_cfg()===%d\n",ret); fclose(logfp); exit(1); } /*disp_query_cfg(); */ connid = tcp_connect(srvip, commport); if(connid < 0) { fprintf(logfp,"Incorrect IPADDR in mcfghead[%s]\n",srvip); fclose(logfp); exit(1); } memset(dbuser,0x0,sizeof(dbuser)); memset(dbpswd,0x0,sizeof(dbpswd)); memset(tname,0x0,sizeof(tname)); if(login_server(connid,hlrcode,hlrport, mgr_user, mgr_pswd, mgr_term)) { fprintf(logfp,"login_server() failed!\n"); fclose(logfp); exit(1); } fprintf(logfp,"login_server(%s) success!\n",hlrcode); /*** CONNECT TO HLR ** */ gsmid = tcp_connect(gsmip, gsmport); if(gsmid < 0){ fprintf(logfp, "connect(%s:%d) failed, errno:%d\n", gsmip, gsmport, errno); fclose(logfp); exit(1); } sndid=gsmid; rcvid=gsmid; /*** LOGIN HLR ***/ sprintf(ss, "%s\r\n", gsmuser); ret = sndcmd(sndid, "\nENTER USERNAME < \08 ", ss, strlen(ss), "\nENTER PASSWORD < \08 ", replystr, sizeof(replystr)); if(ret <= 0){ fprintf(logfp, "sndcmd username failed, return:%d\n", ret); fclose(logfp); return 2; } sprintf(ss, "%s\r\n", gsmpswd); ret = sndcmd(sndid, "\nENTER PASSWORD < \08 ", ss, strlen(ss), "\nMAIN LEVEL COMMAND <___>", replystr, sizeof(replystr)); if(ret <= 0){ fprintf(logfp, "sndcmd password failed, return:%d\n", ret); fclose(logfp); return 3; } fprintf(logfp,"LOGIN HLR,SUCCESS![%ld]\n",time(NULL)); fflush(logfp); /*if(1) exit(0); */ /************** GET AN ORDER AND DEAL ********************/ waitnum=0; transid=2; cmdreqptr=(struct cmd_data_req *)tcpbuf.data; cmdreqptr->type=ONLY_GET; while(1) { if(send_req(connid,DECRYPT,TX_END,GETORDER,transid++,CMDREQLEN)<0) { fprintf(logfp,"send_req(%d) failed[%d]\n",connid,errno); break; } readlen=readnet(connid,(char *)&tcpbuf,8); if(readlen!=8) { fprintf(logfp,"readnet(%d,0,8)=%d failed\n",connid,readlen); break; } if(strncmp(tcpbuf.flag,MSGFLAG,4)) { tcpbuf.msglen[0]=0; fprintf(logfp,"MSGFLAG is incorrect [%s]!\n",tcpbuf.flag); break; } msglen=get_length(tcpbuf.msglen,4); if(msglen<32) { fprintf(logfp,"MSGLEN is incorrect[%04d]\n",msglen); break; } readlen=readnet(connid,(char *)&tcpbuf+8,msglen-8); if(readlen!=msglen-8) { fprintf(logfp,"readnet(%d,8,%d)=%d failed!\n",connid,msglen-8,readlen); break; } printf("RCV:%s~\n",(char *)&tcpbuf); ordercode = 0; etm = 0; ret = exec_cmd( &ordercode, &etm); fprintf(logfp, "EXEC_CMD===========%d ordercode=%04d tm=%dms\n",ret, ordercode, etm); if(ret<0) { fprintf(logfp,"exec_cmd() failed!\n"); break; } time(&t); tv=localtime(&t); if(today!=tv->tm_mday) { fclose(logfp); memset(logfilename,0x0,sizeof(logfilename)); sprintf(logfilename,"%s/%s%04d%02d%02d.%s.%s",logdir, port_type, tv->tm_year+1900,tv->tm_mon+1,tv->tm_mday,hlrcode,hlrport); logfp=fopen(logfilename,"a"); if(logfp==NULL) { printf("can't open %s for LOG!\n",logfilename); exit(1); } today=tv->tm_mday; } fflush(logfp); } fclose(logfp); close(connid); return 0; }
int main(int argc,char **argv,char **envp) { long command_id; char hlr_code[8]; char phone_no[20]; char command_code[8]; char op_code[8]; char imsi_no[32]; char new_phone[20]; char new_imsi[32]; char business_status[8]; char mgr_user[20], mgr_pswd[20], mgr_term[10]; int ret; char inputstr[2048],ss[64]; int i,status,today; char srvip[32]; int srvport; char logfilename[256],logdir[256]; time_t t; struct tm *tv; char chhlr; int msglen,readlen,headlen,tcplen,bodylen; int transid; char order_fname[256],reply_fname[256],query_fname[256],cfginfo[1024],*p,*d; int line; FILE *cfgfp; char cfgstr[256]; procname=argv[0]; logfp=NULL; memset(inputstr,0x0,sizeof(inputstr)); for(i=0;i<argc;i++) { strcat(inputstr,argv[i]); strcat(inputstr," "); } if(GenDaemon()<0) { printf("GenDaemon() failed!\n"); exit(1); } signal(SIGTERM,LogOut); signal(SIGPIPE,SIG_IGN); signal(SIGCLD,SIG_IGN); signal(SIGINT,SIG_IGN); memset(hlrcode,0x0,sizeof(hlrcode)); memset(hlrport,0x0,sizeof(hlrport)); memset(logdir,0x0,sizeof(logdir)); while(--argc>0) { if(argv[argc][0]!='-') usage("Incorrect Parameter!"); printf("CMD:%c\n",argv[argc][1]); switch(argv[argc][1]) { case 'H': strncpy(hlrcode,argv[argc]+2,4); break; case 'N': strncpy(hlrport,argv[argc]+2,4); break; case 'L': strncpy(logdir,argv[argc]+2,240); break; case 'V': printf("\n\n version = %s\n\n",VERSION); exit(0); case 't': case 'T': test_flag = 1; break; default: usage("Unknow Parameter!"); break; } } printf("hlrcode===%s[%s]~\n",hlrcode,hlrport); if(strlen(hlrcode)!=3) usage("Incorrect parameter [hlrcode]!\n"); if(strlen(hlrport)!=1) usage("Incorrect parameter [hlrport]!\n"); if(strlen(logdir)<1) strcpy(logdir,getenv("LOGDIR")); if(testdir(logdir)) usage("Incorrect parameter [logdir]!"); time(&t); tv=localtime(&t); memset(logfilename,0x0,sizeof(logfilename)); sprintf(logfilename,"%s/erc%04d%02d%02d.%s.%s",logdir, tv->tm_year+1900,tv->tm_mon+1,tv->tm_mday,hlrcode,hlrport); logfp=fopen(logfilename,"a"); if(logfp==NULL) { printf("can't open %s for LOG!\n",logfilename); exit(1); } fprintf(logfp,"INIT %04d/%02d/%02d %02d:%02d:%02d\n", tv->tm_year+1900,tv->tm_mon+1,tv->tm_mday, tv->tm_hour,tv->tm_min,tv->tm_sec); fprintf(logfp,"%s[%d]\n",inputstr,argc); fflush(logfp); today=tv->tm_mday; /************ 获取连接配置信息 ********************/ if(get_hlr_cfg(hlrcode, atoi(hlrport), gsmip, &gsmport, gsmuser, gsmpswd, order_fname,reply_fname,query_fname, srvip, &commport, NULL, mgr_user, mgr_pswd, mgr_term) != 0) { fprintf(logfp,"get_cfg_filename(%s) failed!\n",hlrcode); fclose(logfp); exit(1); } /************ GET ORDER INFO ***********************/ cfgfp=fopen(order_fname,"r"); if(cfgfp==NULL) { fprintf(logfp,"Can't open %s for read[%d]\n",order_fname,errno); fclose(logfp); exit(1); } line=0; memset(cfginfo,0x0,sizeof(cfginfo)); while(fgets(cfginfo,sizeof(cfginfo),cfgfp)) { line++; if(cfginfo[0]=='#' || cfginfo[0]=='\r' || cfginfo[0]=='\n') { memset(cfginfo,0x0,sizeof(cfginfo)); continue; } if(strncmp(cfginfo,"OP",2) || (p=strchr(cfginfo,':'))==NULL) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } orderptr=(struct order_info *)malloc(sizeof(struct order_info)); memset(orderptr,0x0,sizeof(struct order_info)); orderptr->ordercode=atoi(cfginfo+2); strcpy(orderptr->orderinfo,rlspace(p+1)); if(strlen(orderptr->orderinfo)==0 || strlen(orderptr->orderinfo)>1000) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } ordertail=&orderhead; while(ordertail->next) { if(ordertail->next->ordercode==orderptr->ordercode) { fprintf(logfp,"ORDER_CODE[%d] is exists[line=%d]\n", orderptr->ordercode,line); fclose(logfp); fclose(cfgfp); exit(1); } ordertail=ordertail->next; } ordertail->next=orderptr; memset(cfginfo,0x0,sizeof(cfginfo)); } fclose(cfgfp); fprintf(logfp,"ORDER INFO:\n"); orderptr=orderhead.next; while(orderptr) { fprintf(logfp,"%06d = [%s]\n",orderptr->ordercode,orderptr->orderinfo); orderptr=orderptr->next; } fflush(logfp); /************ GET REPLY INFO ***********************/ cfgfp=fopen(reply_fname,"r"); if(cfgfp==NULL) { fprintf(logfp,"Can't open %s for read[%d]\n",reply_fname,errno); fclose(logfp); exit(1); } fprintf(logfp,"REPLY INFO:\n"); line=0; memset(cfginfo,0x0,sizeof(cfginfo)); while(fgets(cfginfo,sizeof(cfginfo),cfgfp)) { line++; if(cfginfo[0]=='#' || cfginfo[0]=='\r' || cfginfo[0]=='\n') { memset(cfginfo,0x0,sizeof(cfginfo)); continue; } fprintf(logfp,"%s",cfginfo); if(strncmp(cfginfo,"RETN:",5) || cfginfo[9]!=':') { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } replyptr=(struct reply_info *)malloc(sizeof(struct reply_info)); memset(replyptr,0x0,sizeof(struct reply_info)); replyptr->replycode=atoi(cfginfo+5); p=cfginfo+10; d=replyptr->replyinfo; if(*p!='[' || strchr(p,']')==NULL) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } while(*(++p)!=']') { if(*p=='\\') { p++; switch(*p) { case 'r': *d='\r'; break; case 'n': *d='\n'; break; case 't': *d='\t'; break; default: *d=*p; } } else *d=*p; d++; } if(d==replyptr->replyinfo) { fprintf(logfp,"Incorrect cfg_info[line=%d]\n",line); fclose(cfgfp); fclose(logfp); exit(1); } replyptr->next=replyhead.next; replyhead.next=replyptr; memset(cfginfo,0x0,sizeof(cfginfo)); } fclose(cfgfp); fflush(logfp); /************ GET QUERY INFO ***********************/ ret=get_query_cfg(query_fname); if(ret) { fprintf(logfp,"get_query_cfg()===%d\n",ret); fclose(logfp); exit(1); } disp_query_cfg(); /************ INIT COMM_PORT ***********************/ connid=tcp_connect(srvip, commport); if(connid<0) { fprintf(logfp,"socket() failed[%d]\n",errno); fclose(logfp); exit(1); } if(login_server(connid, hlrcode, hlrport, mgr_user, mgr_pswd, mgr_term)) { fprintf(logfp,"login_server() failed!\n"); fclose(logfp); exit(1); } fprintf(logfp,"login_server(%s) success!\n",hlrcode); gsmid = tcp_connect(gsmip, gsmport); sndid=gsmid; rcvid=gsmid; /*** LOGIN HLR ***/ sleep(1); enter[0]='\r'; enter[1]=0x00; while(1) { RcvFrmFlag(); if(strstr(replystr+STATION,"<")) { break; } if(strstr(replystr+STATION,"USERCODE")) { cusnd(gsmuser,strlen(gsmuser)); cusnd(enter,1); continue; } if(strstr(replystr+STATION,"PASSWORD")) { cusnd(gsmpswd,strlen(gsmpswd)); cusnd(enter,1); continue; } cusnd(enter,1); } fprintf(logfp,"LOGIN HLR,SUCCESS![%ld]\n",time(NULL)); fflush(logfp); /************** GET AN ORDER AND DEAL ********************/ waitnum=0; transid=2; cmdreqptr=(struct cmd_data_req *)tcpbuf.data; cmdreqptr->type=ONLY_GET; yy_time.rec_time = time(NULL); strcpy(yy_time.hlrcode, hlrcode); strcpy(yy_time.hlrport, hlrport); while(1) { int data_flag = 0; gettimeofday(&t1, NULL); if(send_req(connid,DECRYPT,TX_END,GETORDER,transid++,CMDREQLEN)<0) { fprintf(logfp,"send_req(%d) failed[%d]\n",connid,errno); break; } readlen = read_ack(connid, &tcpbuf); if(readlen != 0) { fprintf(logfp,"readnet(%d,8,%d)=%d failed!\n",connid,msglen-8,readlen); break; } gettimeofday(&t2, NULL); cmdackptr=(struct cmd_data_ack *)tcpbuf.data; data_flag = (get_length(cmdackptr->retn,4) == 0001); if(data_flag){ yy_time.rows ++; yy_time.tm4get += time_diff1(t1, t2); } printf("RCV:%s~\n",(char *)&tcpbuf); ret=exec_cmd(); gettimeofday(&t1, NULL); if(data_flag){ yy_time.tm4exc += time_diff1(t2, t1); } printf("EXEC_CMD===========%d\n",ret); if(ret<0) { fprintf(logfp,"exec_cmd() failed!\n"); break; } time(&t); yy_time_log(t); tv=localtime(&t); if(today!=tv->tm_mday) { fclose(logfp); memset(logfilename,0x0,sizeof(logfilename)); sprintf(logfilename,"%s/erc%04d%02d%02d.%s.%s",logdir, tv->tm_year+1900,tv->tm_mon+1,tv->tm_mday,hlrcode,hlrport); logfp=fopen(logfilename,"a"); if(logfp==NULL) { printf("can't open %s for LOG!\n",logfilename); exit(1); } today=tv->tm_mday; } fflush(logfp); } fclose(logfp); close(connid); return 0; }
int ports_demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp) { return notice_process_died_msg(inp, outp) || login_server(inp, outp) || trivfs_demuxer(inp, outp); }