/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); MACInit(); #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__18CXX) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif // Seed the rand() function srand(GenerateRandomDWORD()); MACInit(); #if defined( ZG_CS_TRIS ) #if defined(ZG_CONFIG_LINKMGRII) ZGLibInitialize(); ZGLinkMgrInit(); #endif #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP_SERVER) || defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { static bool once = false; smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = true; #endif #if defined (WF_CS_TRIS) && defined (STACK_USE_DHCP_CLIENT) g_DhcpRenew = false; g_DhcpRetryTimer = 0; #endif if (!once) { // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); once = true; } MACInit(); #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__XC8) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_ANNOUNCE) AnnounceInit(); #endif #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif #if defined(STACK_USE_NBNS) NBNSInit(); #endif }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { static BOOL once = FALSE; smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif #if defined (WF_CS_TRIS) && defined (STACK_USE_DHCP_CLIENT) g_DhcpRenew = FALSE; g_DhcpRetryTimer = 0; #endif if (!once) { // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); once = TRUE; } MACInit(); #if defined (WF_AGGRESSIVE_PS) && defined (WF_CS_TRIS) WFEnableAggressivePowerSave(); #endif #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__18CXX) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif #if defined(STACK_USE_CCS_SMTP) SMTPInit(); #endif #if defined(STACK_USE_CCS_SNTP_CLIENT) NTPInit(); #endif #if defined(STACK_USE_CCS_GRATUITOUS_ARP) GratArpInit(); #endif #if defined(STACK_USE_CCS_HTTP1_SERVER) || defined(STACK_USE_CCS_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_CCS_TFTP_SERVER) TFTPSInit(); #endif }
int main_impl(const char *deviceName, const char *serverName, int port, const char *certFile, const char *keyFile, const char *caCertFile, const char *logFile) { struct hostent *srv; int dev = -1; FILE* log = NULL; SOCKET sock = SOCKET_ERROR; struct sockaddr_in srvaddr; int keepalive = 1, hasCert, retCode; SSL_CTX *sslContext = NULL; SSL *sslSession = NULL; int sslRead; char b[100]; // initial validation if (port < 1 || port > 65535) { printf("Port must be between 1 and 65535\n"); return 3; } srv = gethostbyname(serverName); if (srv == NULL) { srv = gethostbyaddr(serverName, 4, AF_INET); if (srv == NULL) { printf("Cannot resolve server address: %s\n", serverName); return 3; } } hasCert = HAS_STR(certFile) + HAS_STR(keyFile) + HAS_STR(caCertFile); if (hasCert != 0 && hasCert != 3) { printf("Either all or none certificates must be specified\n"); return 3; } // initial validation complete // connecting to server and opening device OpenLog(logFile); INFO("Opening device...\n"); dev = open(deviceName, O_BINARY | O_RDWR | O_NOCTTY); if (dev == -1) { ERR("Failed to open device: %s\n", strerror(errno)); RETURN(3); } DBG("Configuring device...\n"); ConfigureDevice(dev); DBG("Creating a socket...\n"); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == SOCKET_ERROR) { ERR("Cannot create a socket\n"); RETURN(3); } DBG("Enabling keep-alives on the socket...\n"); if (0 != setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive))) { WARN("Failed to enable keep-alives\n"); } srvaddr.sin_family = AF_INET; srvaddr.sin_port = htons((u_short) port); memcpy(&srvaddr.sin_addr, srv->h_addr_list[0], 4); INFO("Connecting to server...\n"); if (0 != connect(sock, (const struct sockaddr *) &srvaddr, sizeof(srvaddr))) { ERR("Cannot connect to %s:%d\n", serverName, port); RETURN(3); } if (0 != hasCert) { if (0 != SSLInit()) { RETURN(3); } sslContext = SSLCreateContext(certFile, keyFile, caCertFile); if (sslContext == NULL) { RETURN(3); } sslSession = SSLHandshake(sock, sslContext); if (sslSession == NULL) { RETURN(3); } sslRead = SSL_read(sslSession, b, sizeof(b)); switch (SSL_get_error(sslSession, sslRead)) { case SSL_ERROR_NONE: break; case SSL_ERROR_ZERO_RETURN: DBG("Server has closed the SSL connection\n"); SSLCloseSession(sslSession); sslSession = SSLHandshake(sock, sslContext); if (sslSession == NULL) { RETURN(3); } break; default: DEFAULT_HANDLER("Failed to receive server response", sslSession, sslRead); RETURN(3); } } IsTerminating = 0; signal(SIGTERM, sig_handler); signal(SIGINT, sig_handler); #ifdef WIN32 signal(SIGBREAK, sig_handler); #endif retCode = worker(dev, sock, sslSession, certFile, keyFile, caCertFile); RETURN(retCode); }