long SDMmessage::SendTCP(long ip_addr,long port) { int sock; long i; long result; char buf[3*BUFSIZE]; char ack[16]; short error = 0; //fill buffer i = Marshal(buf); if(i < 0) return SDM_MESSAGE_SEND_ERROR; //send message sock = TCPconnect(ip_addr,port); if (sock != IP_SOCK_INVALID) { result = TCPsend(sock,buf,i); if(buf[0] == SDM_xTEDS || buf[0] == SDM_CancelxTEDS) { TCPrecv(sock,ack,13); error = GET_SHORT(&ack[HEADER_SIZE]); if(error < 0) { result = error; } } TCPclose (sock); #ifdef BUILD_WITH_MESSAGE_LOGGING Logger.MessageSent(*this); #endif } return result; }
int chat_LST(int *nr_chats, char *mess, unsigned long myIP, unsigned short myport, connection **connections) { char *aux; unsigned long IP; unsigned short port; int fd; #ifdef DEBUG int err; printf("received connections list: %s", mess); #endif aux = mess; aux += strlen("LST\n"); while((*aux)!='\n') /* list ends in double \n */ { if(sscanf(aux, "%lu;%hu\n", &IP, &port)!=2) { #ifdef DEBUG puts("error while processing TCP LST"); #endif return -1; } #ifdef DEBUG printf("connecting to %08lX:%d\n", IP, port); #endif fd = TCPconnect(IP, port); /* connect to connection just discovered */ if(fd<0) { #ifdef DEBUG printf("error connecting in chat_LST(). err = %d\n", fd); #endif return -1; } #ifdef DEBUG err = #endif chat_send_ADD(fd, myIP, myport); /* ask to be added to their connections list */ #ifdef DEBUG if(err!=0) puts("failed to send ADD in chat_LST()"); #endif chat_add(fd, IP, port, connections); /* store new connection */ (*nr_chats)++; aux = strchr(aux, '\n'); aux++; /* move to next in list*/ } return 0; }
int LoginToAC() { Log( "\n connect ac "); usleep( 80*10000); Log( "."); afd = TCPconnect( ACADDRESS, ACPORT); if( afd <= 0 ) { close( afd); return 0; } Log( "."); usleep( 40*10000); return 1; }
static int checkCSconnect( void) { int num = 0; while( csd <= 0 && num < 10 ){ csd = TCPconnect(WGSAddress,WGSPort); if( csd <= 0 ) sleep( 6); num++; } if( csd <= 0 ){ char buf[256]; sprintf( buf, "Can't Not Connect %s:%d !\n", WGSAddress, WGSPort); Log( buf); return -1; } return 1; }
void StartGET(void) { if (get_sock.sock.state==TCP_STATE_CLOSED) { MP3fifo_pWR= MP3fifo_pRD= MP3fifo_pSTART; ETHfifo_pWR= ETHfifo_pRD= ETHfifo_pSTART; ETHfifo_CNT=0; GET_WINDOW_STATE=GET_WINDOW_STATE_UPDATE; PLAYER_STATE=PLAYER_STATE_STOPED; UINT32 IPsrc=station_list[stationNum].IP; UINT16 port = station_list[stationNum].port; if ((IP&MASK_IP)==(IPsrc&MASK_IP)) get_sock.sock.ACKNO=IPsrc;// без шлюза else get_sock.sock.ACKNO=OUTG_IP[1]; // идем через шлюз TCPconnect(&get_sock.sock,IPsrc,port); } }
int main(int argc, char *argv[]) { /* check command line args. */ if(argc < 4){ printf("usage : %s <RouterID> <LogFileName> <Initialization file> \n", argv[0]); exit(1); } char *router_id; char *log_filename; char *init_file; FILE *log_file; int nbytes; // Extract arguments router_id = argv[1]; log_filename = argv[2]; init_file = argv[3]; Router router; router = router_init(*router_id,init_file); //printf("Router init:\n"); //print_router(router); // Open log file log_file = fopen(log_filename, "wb"); if (!log_file) { printf("Failed open log file %s for router %s.\n", argv[2], argv[1]); exit(1); } router = initSock(router); //printf("Router after initSock:\n"); //print_router(router); // Initialize LSP of this router LSP_init(&router); print_LSP(&router.self_packet,log_filename); LSP lsp = router.self_packet; // Each router has an array of recently received packets // Receive buffer //LSP buffer_packet; LSP templsp; //LSP checklsp; char* buffer = malloc(sizeof(LSP)); //bzero(buffer,sizeof(buffer)); //memcpy(buffer,(char*) &lsp, sizeof(LSP)); //memcpy((char*) &checklsp, buffer, sizeof(LSP)); //print_LSP(&checklsp, log_file); //printf("router id: %c\nsequence number: %d\n sizeof: %d\n",checklsp.router.ID,checklsp.seq_num, (int) sizeof(lsp)); // initialize router routing table routing_table_init(&router); print_routing_table(&router, log_filename); fclose(log_file); // ?? int i, j; fd_set readSet; fd_set tempReadSet; FD_ZERO(&readSet); FD_ZERO(&tempReadSet); //first try to connect to neighbors for(i = 0; i< router.nbrs_count; i++) { if(TCPconnect(router.nbrs[i]) < 0) router.nbrs[i].connectedR = 0; else router.nbrs[i].connectedR = 1; } //then listen for(i = 0; i< router.nbrs_count; i++) { TCPlisten(router.nbrs[i]); FD_SET(router.nbrs[i].localSock,&readSet); } int highSock; highSock = router.nbrs[0].localSock; for(i = 0; i < router.nbrs_count-1; i++ ) { highSock = router.nbrs[i].localSock > router.nbrs[i+1].localSock? router.nbrs[i].localSock: router.nbrs[i+1].localSock; } int counter = 0; struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; // Select: while router is listening, it waits for one or more neighbors // to connect. for(;;) { tv.tv_sec = 5; tv.tv_usec = 0; tempReadSet = readSet; printf("- %d\n", counter); if(select(highSock+1, &tempReadSet, 0, 0, &tv) < 0) { printf("select error!\n"); } //CHECK FD for(i = 0; i< router.nbrs_count; i++) { // for select: Check if there's a connection or not if(FD_ISSET(router.nbrs[i].localSock, &tempReadSet)) { // Accept connections if( router.nbrs[i].connectedS == 0) { int newSock; if ((newSock = TCPaccept(router.nbrs[i])) > -1) { router.nbrs[i].connectedS= 1; router.nbrs[i].localSock = newSock; printf("sock: %d\n",router.nbrs[i].localSock); printf("connectedS: %d\n",router.nbrs[i].connectedS); FD_SET(router.nbrs[i].localSock,&readSet); if(highSock < newSock) highSock = newSock; } } // Receive LSPs else { bzero(buffer,sizeof(LSP)); socklen_t size = sizeof(&router.nbrs[i].localAddr); if(recvfrom(router.nbrs[i].localSock, buffer, sizeof(LSP), 0, (struct sockaddr *) &router.nbrs[i].localAddr, &size) <0) { printf("error receiving from node %s\n", router.nbrs[i].ID); } else { printf("LSP received from %s \n",router.nbrs[i].ID); memcpy((char *)&templsp, buffer, sizeof(LSP)); //print_LSP(&templsp,log_file); // Check if received lsp is new int check_lsp_flag = check_LSP(&router, &templsp); if (check_lsp_flag == 1 || check_lsp_flag == 2) { // Log if LSP caused change in routing table. if(check_lsp_flag == 2) { print_LSP(&templsp, log_filename); print_routing_table(&router,log_filename); } // 2. Flood to all links, except where it came from for(j = 0; j<router.nbrs_count; j++) { if((j != i) && router.nbrs[i].connectedR == 1) { //buffer = malloc(sizeof(LSP)); //bzero(buffer, sizeof(LSP)); //memcpy(buffer, (char*) &lsp, sizeof(LSP)); //memcpy((char *)&templsp, buffer, sizeof(LSP)); nbytes = sendto(router.nbrs[j].remoteSock, buffer, LSPSIZE, MSG_NOSIGNAL, (struct sockaddr*)&router.nbrs[j].remoteAddr, sizeof(router.nbrs[j].remoteAddr)); if (nbytes == -1) { printf("Failed to send on link: %s, %d, %s, %d\n", router.nbrs[i].src_ID, router.nbrs[i].send_port, router.nbrs[i].ID, router.nbrs[i].recv_port); } else { printf("Forward LSP from %s to %s\n", templsp.routerID, router.nbrs[j].ID); //print_LSP(&templsp, log_file); } } } } } } } } sleep(5); // Try to connect again. for(i = 0; i< router.nbrs_count; i++) { if(router.nbrs[i].connectedS == 1 &&router.nbrs[i].connectedR == 0) { if(TCPconnect(router.nbrs[i]) < 0) { printf("failed to connect to %s \n", router.nbrs[i].ID); router.nbrs[i].connectedR = 0; } else router.nbrs[i].connectedR = 1; } } // initial and periodic flooding: Send LSP to connected links. lsp.seq_num++; for(i = 0; i<router.nbrs_count; i++) { if(router.nbrs[i].connectedR == 1) { buffer = malloc(sizeof(LSP)); bzero(buffer, sizeof(LSP)); memcpy(buffer, (char*) &lsp, sizeof(LSP)); memcpy((char *)&templsp, buffer, sizeof(LSP)); nbytes = sendto(router.nbrs[i].remoteSock, buffer, sizeof(LSP), 0, (struct sockaddr*)&router.nbrs[i].remoteAddr, sizeof(router.nbrs[i].remoteAddr)); if (nbytes == -1) { printf("Failed to send on link: %s, %d, %s, %d\n", router.nbrs[i].src_ID, router.nbrs[i].send_port, router.nbrs[i].ID, router.nbrs[i].recv_port); } else { printf("Send LSP to: %s\n", router.nbrs[i].ID); //print_LSP(&templsp, log_file); } } } counter ++; } fclose(log_file); return 0; // Set router timer //time_t curr_time; //time(&router.timestamp); // some connection shit /* // check time, send self lsp on all ports with established link time(&curr_time); if (difftime(curr_time, router.timestamp) >= (double)5.0) { router.timestamp = curr_time; // update lsp seq router.self_packet.seq++; for (i=0 ; i<router.nbrs_count ; i++) { if (router.nbrs[i].connected) { nbytes = send(socket?, router.self_pakcet, sizeof(LSP), 0); if (nbytes == -1) { printf("Failed to send on link: %s, %d, %s, %d\n", router.nbrs[i].src_ID, router.nbrs[i].send_port, router.nbrs[i].ID, router.nbrs[i].recv_port); } else { printf("Send LSP to: %s\n", router.nbrs[i].ID); } } } } int lsp_update_flag; // Receive LSP from all ports with "connected" neighbors for (i=0 ; i<router.nbrs_count ; i++) { if (router.nbrs[i].connected) { nbytes = recv(router.nbrs[i].connect_fd, &buffer_packet, sizeof(LSP), 0); if (nbytes > 0) { printf("LSP received from ID %s, seq %d\n", buffer_packet.router.ID, buffer_lsp.seq_num); // store recvd lsp into database and determine if need forwarding // also determine if need update topology and recompute lsp_update_flag = update_LSP_list(&router, &buffer_lsp); if (lsp_update_flag == 1) { // 1. Run Dijkstra! printf("Update routing table...\n"); if (update_routing_table(&(router), &buffer_packet)) { time(&curr_time); sprintf(tmp_char_buffer, "UTC:\t%s", asctime(gmtime(&curr_time))); printf("%s",tmp_char_buffer); fwrite(tmp_char_buffer, sizeof(char), strlen(tmp_char_buffer), log_file); print_lsp(&buffer_packet, log_file); log_routing_table(&router, log_file); } // 2. Flood LSP to all outgoing links, except where it came from buffer_pacekt.ttl--; int j; for (j=0; j<router.nbrs_count; j++) { if ((j != i) && (router.nbrs[j].connected)) { nbytes = send(router.nbrs[j].connect_fd, &buffer_packet, sizeof(LSP), 0); if (nbytes == -1) { printf("Failed to send on link: %s, %d, %s, %d\n", router.nbrs[i].src_ID, router.nbrs[i].send_port, router.nbrs[i].ID, router.nbrs[i].recv_port); } else { printf("Forward LSP from %s to %s\n", buffer_packet.routerID, router.nbrs[j].ID); } } } } if(lsp_update_flag == 2) { // run dijkstra's algorithm printf("Update routing table...\n"); if (update_routing_table(&(router), &buffer_packet)) { time(&curr_time); sprintf(tmp_char_buffer, "UTC:\t%s", asctime(gmtime(&curr_time))); printf("%s",tmp_char_buffer); fwrite(tmp_char_buffer, sizeof(char), strlen(tmp_char_buffer), log_file); print_lsp(&buffer_packet, log_file); log_routing_table(&router, log_file); } }*/ //} //} //} // end if }
int main(int argc, char ** argv) { int sock; int length; char msg_buf[BUFSIZE]; SDMSubreqst sub_msg; SDMDeletesub delsub_msg; SDMCommand cmd_msg; SDMSerreqst ser_msg; SDMConsume cons_msg; SDMReady ready_msg; SDMCancel can_msg; char buf[11]; printf("\n**Make sure the DM and SM have debug levels at least one to see the messages received.**\n\n"); printf("Finding data manager..."); sock = TCPconnect("127.0.0.1", PORT_DM); if (sock > 0) printf("Done.\n"); else { printf("Error. Try again.\n"); return -1; } // // Send a consume message cons_msg.msg_id.setInterface(5); cons_msg.msg_id.setMessage(5); length = cons_msg.Marshal(msg_buf); printf("Sending SDMConsume message to DM..."); if (TCPsend (sock, msg_buf, length) > 0) printf("Done.\n"); else printf("Error.\n"); // // Send a ready message sock = TCPconnect("127.0.0.1", PORT_DM); length = ready_msg.Marshal(msg_buf); printf("Sending SDMReady message to DM..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); // // Send a ready message in segments sock = TCPconnect("127.0.0.1", PORT_DM); printf("Sending SDMReady message to DM in segments..."); if(TCPsend(sock, msg_buf, 8) > 0) printf("Sending header done. ..."); else printf("Error sending header. ..."); sleep(1); if(TCPsend(sock, msg_buf + 8, 13) >= 0) printf("Sending destination done.\n"); else printf("Error sending destination.\n"); sleep(1); if(TCPsend(sock, msg_buf + 21, 10) >= 0) printf("Sending destination done.\n"); else printf("Error sending destination.\n"); // // Send a command message sock = TCPconnect("127.0.0.1", PORT_DM); cmd_msg.command_id.setInterface(5); cmd_msg.command_id.setMessage(5); length = cmd_msg.Marshal(msg_buf); printf("Sending SDMCommand message..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); sock = TCPconnect("127.0.0.1", PORT_DM); can_msg.msg_id.setInterface(5); can_msg.msg_id.setMessage(5); length = can_msg.Marshal(msg_buf); printf("Sending SDMSerreqst message..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); fflush(NULL); //TEST SENSOR MANAGER's TCP LISTENER printf("Finding sensor manager..."); sock = TCPconnect("127.0.0.1", PORT_SM); if (sock >= 0) printf("Done.\n"); else { printf("Error. Try again.\n"); return 1; } sub_msg.msg_id.setInterface(5); sub_msg.msg_id.setMessage(5); sub_msg.source.setSensorID(1); length = sub_msg.Marshal(msg_buf); printf("Sending SDMSubreqst message to SM..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); sock = TCPconnect("127.0.0.1", PORT_SM); delsub_msg.msg_id.setInterface(5); delsub_msg.msg_id.setMessage(5); length = delsub_msg.Marshal(msg_buf); printf("Sending SDMDeletesub message to SM..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); sock = TCPconnect("127.0.0.1", PORT_SM); cmd_msg.command_id.setInterface(5); cmd_msg.command_id.setMessage(5); length = cmd_msg.Marshal(msg_buf); printf("Sending SDMCommand message to SM..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); sock = TCPconnect("127.0.0.1", PORT_SM); ser_msg.command_id.setInterface(5); ser_msg.command_id.setMessage(5); length = ser_msg.Marshal(msg_buf); printf("Sending SDMSerreqst message to SM..."); if (TCPsend (sock, msg_buf, length) >=0) printf("Done.\n"); else printf("Error.\n"); fflush(NULL); return 0; }
void sigusr1(int i) { if (csd>0) close(csd); csd=TCPconnect(WGSAddress,WGSPort); signal(SIGUSR1,sigusr1); }
static int doKN( char *id , char *pas , char *flag , char *process) #endif { char buffer[BUF_SIZE],idbuf[BUF_SIZE],pasbuf[BUF_SIZE],m[BUF_SIZE]; char *ptr; int ret, x, baka; struct timeval tv; fd_set rfds; int iProcess; int length; if (zap) return(0); again: if (csd<=0) csd=TCPconnect(WGSAddress,WGSPort); if (csd<=0) { sleep(1); return(-1); } else { tv.tv_sec=0; tv.tv_usec=10000; FD_ZERO(&rfds); FD_SET(csd,&rfds); x=select(1024,&rfds,0,0,&tv); if (x<0) return(-100); if (x>0) { ret=read(csd,buffer,BUF_SIZE-8); if (ret<=0) { close(csd); sleep(1); csd=0; goto again; } } strcpy(idbuf,xchange(id)); strcpy(pasbuf,xchange(pas)); iProcess = atoi( process ); switch (*flag) { case '1': // Login #ifdef _FIX_MESSAGE // WON ADD 修改封包內容 #ifdef _ADD_AC_IP // WON ADD 加AC_IP至通訊協定中 sprintf(buffer,"&;5;6;%s;%s;0;%s;",idbuf,pasbuf, AC_IP); #else sprintf(buffer,"&;5;6;%s;%s;0;",idbuf,pasbuf); #endif #else sprintf(buffer,"&;9999;5;J;%s;%s;",idbuf,pasbuf); #endif baka=90000+bakayaro(id); Log( buffer); break; case '4': // Delete #ifdef _FIX_MESSAGE // WON ADD 修改封包內容 #ifdef _ADD_AC_IP // WON ADD 加AC_IP至通訊協定中 sprintf(buffer,"&;5;6;%s;%s;0;%s;",idbuf,pasbuf, AC_IP); #else sprintf(buffer,"&;5;6;%s;%s;0;",idbuf,pasbuf); #endif #else sprintf(buffer,"&;9999;5;J;%s;%s;",idbuf,pasbuf); #endif baka=90000+bakayaro(id); break; case '2': // Game Start #ifdef _TIMEOUTKICK case '5': // recheck WGS deadline #endif #ifdef _FIX_MESSAGE // WON ADD 修改封包內容 #ifdef _ADD_AC_IP // WON ADD 加AC_IP至通訊協定中 sprintf(buffer,"&;4;6;%s;%s;%s;",idbuf,pasbuf, AC_IP); #else sprintf(buffer,"&;4;6;%s;%s;",idbuf,pasbuf); #endif #else sprintf(buffer,"&;9999;6;6;%s;%s;%d;",idbuf,pasbuf,iProcess+additional_process); #endif Log( buffer); baka=10000+bakayaro(id); break; case '3': // Game End #ifdef _FIX_MESSAGE // WON ADD 修改封包內容 #ifdef _ADD_AC_IP // WON ADD 加AC_IP至通訊協定中 sprintf(buffer,"&;3;7;%s;%s;",idbuf, AC_IP); #else sprintf(buffer,"&;3;7;%s;",idbuf); #endif #else sprintf(buffer,"&;9999;5;7;%s;%d;",idbuf,iProcess+additional_process); #endif Log( buffer); baka=20000+bakayaro(id); break; default: return(-100); } Log("<BW>"); Log(buffer); ret=write(csd,buffer,strlen(buffer)); Log("<AW>"); // Write error if (ret<0) { close(csd); sleep(1); csd=0; goto again; } // Line busy if (ret==0) { sleep(1); return(-103); } tv.tv_sec=5; tv.tv_usec=0; FD_ZERO(&rfds); FD_SET(csd,&rfds); Log ("<BS>"); x=select(1024,&rfds,0,0,&tv); Log ("<AS>"); if (x<0) return(-101); if (x==0) return(0); Log ("<BR>"); ret=read(csd,buffer,BUF_SIZE-8); Log ("<AR>"); snprintf( m, sizeof(m), "ret: %d ", ret ); Log( m ); // Read error if (ret<=0) { close(csd); sleep(1); csd=0; return(-104); } buffer[ret]=0; snprintf( m,sizeof(m) ," WGS: %s\n",buffer); Log( m ); strtok(buffer,";"); // Get & length = atoi( strtok(0,";")); // Get length ptr=strtok(0,";"); // Get Flag if (ptr==0) return(-105); if (ptr[0]=='K') { int payType; #ifndef _FIX_MESSAGE // WON ADD 修改封包內容 if (atoi(ptr+1)!=baka){ char buff1[256]; sprintf( buff1, "%d != %d\n", atoi(ptr+1), baka); Log(buff1); return(-100); } #endif #ifdef _TIMEOUTKICK payType = atoi( strtok(0,";")); if( *flag == '1' || *flag == '4' ) { // GameLogin or Char Delete strtok(0,";"); // get Point } if( strcmp( strtok(0,";"), idbuf)) { // get PlayerAccount //Log(" 回傳帳號不符! "); //return(-106); } switch( payType) { case 1: // 包月制 strcpy( deadline ,"0"); Log(" 包月 "); break; case 2: // 時段制 //strtok(0,";"); // get PlayerAccount strcpy( deadline ,strtok(0,";")); Log(" 時段 "); break; case 0: strcpy( deadline ,"0"); Log(" 結束 "); break; } #endif switch (*flag) { case '1': // List Log("User Login\n"); break; case '4': // Delete Log("User delete\n"); break; case '2': Log("Game start\n"); break; case '3': Log("Game end\n"); break; #ifdef _TIMEOUTKICK case '5': Log("Deadline recheck\n"); break; #endif default: Log("Strange but passed\n"); break; } return(0); // Authencation passed } #ifdef _FIX_MESSAGE // WON ADD 修改封包內容 // 回傳錯誤碼 else if (ptr[0]=='E'){ char err_code[10]; int i, err_num=0, login_star=-1; #ifdef _TIMEOUTKICK if( length != 3 ) strtok(0,";"); // Get 1 if( *flag != '1' && *flag != '4') { if( strcmp( strtok(0,";"), idbuf)) { // get PlayerAccount //Log(" 回傳帳號不符! "); //return(-106); } } strcpy( deadline ,"0"); #endif ptr=strtok(0,";"); for(i=0; i<9; i++){ if (ptr[i] == ' ' ) break; err_code[i]=ptr[i]; } err_num=atoi(err_code); if( err_num == 203 ){ // 203 不同星系 login_star=atoi(ptr+i); Log("203"); // 回傳已登入的星系代碼 if(login_star != -1) return(login_star+2030); } else if( err_num == 204 ){ // 204 同星系 Log("204"); if( *flag == '5') Log(" 時段未到 "); return(204); }else{ char msg[256]; sprintf( msg, "won test 1 ==> err_num(%d)", err_num); Log( msg ); return(err_num); } } #endif return(-106); } }