int main()

in EAP-TLS_Solution/EAP-TLS Client/main.c [134:254]


int main(int argc, char *argv[])
{
	Log_Debug("EAP-TLS Client starting...\n");

	struct sigaction action;
	memset(&action, 0, sizeof(struct sigaction));
	action.sa_handler = TerminationHandler;
	sigaction(SIGTERM, &action, NULL);


#ifdef RUN_TESTS

	ssize_t NetworkInterfaceCount = Networking_GetInterfaceCount();
	Networking_NetworkInterface *networkInterfaces = NULL;
	Networking_GetInterfaces(networkInterfaces, (size_t)NetworkInterfaceCount);

	exitCode = (sig_atomic_t)TestEapTlsLib_All(&eapTlsConfig, NetworkInterfaceType_Wifi);
	if (EapTlsResult_Success == exitCode)
	{
		exitCode = (sig_atomic_t)TestEapTlsLib_All(&eapTlsConfig, NetworkInterfaceType_Ethernet);
	}

#else // RUN_TESTS

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Initialize the library with minimal predefined test data (this must be customized with actual requirements).
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	{
		// Full-reset the configuration
		memset(&eapTlsConfig, 0, sizeof(EapTlsConfig));

		// Define the BOOTSTRAP network	
		EapTls_SetBootstrapNetworkInterfaceType(&eapTlsConfig, NetworkInterfaceType_Wifi);
		strncpy(eapTlsConfig.bootstrapNetworkName, g_bootstrapNetworkName, sizeof(eapTlsConfig.bootstrapNetworkName) - 1);
		strncpy(eapTlsConfig.bootstrapNetworkSsid, g_bootstrapNetworkSsid, sizeof(eapTlsConfig.bootstrapNetworkSsid) - 1);

		// Define the WebAPI Server certificate
		strncpy(eapTlsConfig.mdmWebApiInterfaceUrl, g_webApiInterfaceUrl, sizeof(eapTlsConfig.mdmWebApiInterfaceUrl) - 1);
		strncpy(eapTlsConfig.mdmWebApiRootCertificate.relativePath, g_webApiRootCaCertificatePath, sizeof(eapTlsConfig.mdmWebApiRootCertificate.relativePath) - 1);

		// Define the RADIUS network
		strncpy(eapTlsConfig.eapTlsNetworkName, g_eapTlsNetworkName, sizeof(eapTlsConfig.eapTlsNetworkName) - 1);

		// Define the RADIUS RootCA certificate
		strncpy(eapTlsConfig.eapTlsRootCertificate.id, g_eapTlsRootCaCertificateId, sizeof(eapTlsConfig.eapTlsRootCertificate.id) - 1);

		// Define the RADIUS Client certificate
		strncpy(eapTlsConfig.eapTlsClientCertificate.id, g_eapTlsClientCertificateId, sizeof(eapTlsConfig.eapTlsClientCertificate.id) - 1);
#	ifdef USE_CLIENT_CERT_PRIVATE_KEY_PASS_FROM_WEBAPI
		strncpy(eapTlsConfig.eapTlsClientCertificate.privateKeyPass, "", sizeof(eapTlsConfig.eapTlsClientCertificate.privateKeyPass) - 1);
#	else
		strncpy(eapTlsConfig.eapTlsClientCertificate.privateKeyPass, g_eapTlsClientPrivateKeyPassword, sizeof(eapTlsConfig.eapTlsClientCertificate.privateKeyPass) - 1);
#	endif // USE_CLIENT_CERT_PRIVATE_KEY_PASS_FROM_WEBAPI


#	ifdef START_FROM_CLEAN_DEVICE

		Log_Debug("Cleaning up all network configurations and certificate store...\n");
		{
			// Remove all networks
			int res = WifiConfig_ForgetAllNetworks();
			if (-1 == res)
			{
				Log_Debug("ERROR forgetting all network configurations: errno=%d (%s)\n", errno, strerror(errno));
			}

			// Remove all certificates from the CertStore
			int cnt;
			while ((cnt = CertStore_GetCertificateCount()) > 0)
			{
				CertStore_Identifier id;
				res = CertStore_GetCertificateIdentifierAt(0, &id);
				if (-1 == res)
				{
					Log_Debug("FATAL CERTSTORE ERROR finding certificate @ index[%d]/%d in the store: errno=%d (%s)\n", 0, cnt, errno, strerror(errno));

					break;
				}
				else
				{
					res = CertStore_DeleteCertificate(id.identifier);
					if (-1 == res)
					{
						Log_Debug("ERROR deleting certificate '%s' in the CertStore: errno=%d (%s)\n", id.identifier, errno, strerror(errno));
					}
					else
					{
						Log_Debug("Deleted certificate '%s' in the CertStore\n", id.identifier, errno, strerror(errno));
					}
				}
			}

			// Configure the Bootstrap network over WiFi
			if (NetworkInterfaceType_Wifi == eapTlsConfig.bootstrapNetworkInterfaceType)
			{
				EapTls_AddNetwork(eapTlsConfig.bootstrapNetworkName, g_bootstrapNetworkSsid, WifiConfig_Security_Wpa2_Psk, g_bootstrapNetworkPassword);
			}
		}
#	endif // START_FROM_CLEAN_DEVICE
	}

	InitHandlers();

	// Use event loop to wait for events and trigger handlers, until an error or SIGTERM happens
	while (exitCode == ExitCode_Success)
	{
		EventLoop_Run_Result result = EventLoop_Run(eventLoop, -1, true);

		// Continue if interrupted by signal, e.g. due to breakpoint being set.
		if (result == EventLoop_Run_Failed && errno != EINTR)
		{
			exitCode = ExitCode_Main_EventLoopFail;
		}
	}

	CloseHandlers();
#endif // RUN_TESTS

	Log_Debug("Application exiting.\n");
	return exitCode;
}