/*
Ouverture d'une connexion RAS, essentiellement dans un fichier de commandes (batch)
Cela est déja prévu d'origine sous Windows NT et Windows 2000
(commande rasdial <nom_de_connexion> <username> <password>

Par contre, la commande rasdial n'existe pas sous Windows 9x
La dll rnaui.dll ne permet que d'ouvrir la boite de connexion, 
et nécessite d'appuyer sur <Entrée> 

*/
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "ras.h"
#include "raserror.h"
//--------------------------------------------------------------------	
void syntaxe() {
	printf("Dialup - JCB © 2000\n");
	printf("===================\n");
	printf("Syntaxe :\n");
	printf("---------\n");
	printf("1)Etablissement d'une connexion :\n");
	printf("  dialup /connect <nom_de_connexion> <username> <password>\n");
	printf("      <nom_de_connexion>: nom d'une connexion distante\n");
	printf("      (a encadrer par des guillemets si ce nom contient des espaces)\n"); 
	printf("      <username>        : nom du compte correspondant\n");
	printf("      <password>        : mot de passe associe\n");
	printf("  Ex.: dialup /connect \"Free RNIS\" jc.bellamy toto\n");
	printf("2)Enumeration des connexions actives :\n");
	printf("  dialup /enum\n");
	printf("3)Liste des connexions disponibles :\n");
	printf("  dialup /list\n");
	printf("4)Raccrochage d'une connexion active:\n");
	printf("  dialup /disconnect <nom_de_connexion>\n");
	printf("      <nom_de_connexion>: nom d'une connexion active\n");
	printf("  Ex.: dialup /disconnect Free\n");
	printf("5)Raccrochage de toutes les connexions actives:\n");
	printf("  dialup /disconnect\n");
	printf("6)Liste des peripheriques RAS disponibles:\n");
	printf("  dialup /devices\n");
	printf("7)Explication d'un code d'erreur RAS:\n");
	printf("  dialup /errcode <n° d'erreur>\n");
	printf("  Ex: dialup /errcode 623\n");
	}
//--------------------------------------------------------------------
DWORD raccroche(LPRASCONN lpRasConn){
	char name[80];
	DWORD nRet;
	strcpy(name,lpRasConn->szEntryName);
	printf("Raccrochage de %s en cours...\n",name);
	nRet=RasHangUp(lpRasConn->hrasconn);
	Sleep(3000);
	printf("Connexion %s raccrochee\n",name);
	return nRet;
	}
//--------------------------------------------------------------------
VOID printError(char* f,UINT err) {
	DWORD ls,nRet;
	LPSTR lpErrStr;
	char mess[256];
	lpErrStr= (LPSTR) GlobalAlloc(GPTR, 256);
	ls=256;
	nRet=RasGetErrorString(err,lpErrStr,ls);
	if (nRet==ERROR_INVALID_PARAMETER) {
		strcpy(mess,"Code inconnu");
		}
	else {CharToOem(lpErrStr,mess);}
	if (strlen(f)==0) {printf("%u : %s\n",err,mess);}
	else {printf("Erreur %s code : %u \n%s\n",f,err,mess);}
	}
//--------------------------------------------------------------------
VOID printIP(RASIPADDR ipaddr,char* mess){
	if (ipaddr.a+ipaddr.b+ipaddr.c+ipaddr.d==0) {
		printf("      %s:(DHCP)\n",mess);
		}
	else {
		printf("      %s:%u.%u.%u.%u\n",mess,
			ipaddr.a,ipaddr.b,ipaddr.c,ipaddr.d);
			}
	}
//--------------------------------------------------------------------
int main(int argc, char* argv[])
	{
	char job[80],device[80],DevType[80],username[80],password[80],errcode[80];
	int nRet;
	DWORD sizebuffer,nbconnexions,nbdevices,i,EntryInfoSize;
	RASDIALPARAMS RasDialParams;
	HRASCONN hRasConn;
	LPRASCONN lpRasConn;
	LPRASENTRYNAME lpRasEntryName;
	LPRASDEVINFO lpRasDevInfo;
	RASENTRY RasEntry;
	RASPROJECTION rasprojection;
	RASPPPIP rasinfo;
	bool trouve;
	
	if (argc<=1) {
		syntaxe();		
		return 1;
		}
	strcpy(job,CharLower(argv[1]));
//------ connect -------
	if (strcmp(job,"/connect")==0) {
		if (argc<3) {
			printf("Le nom de connexion est obligatoire!\n\n");
			syntaxe();
			return 2;}
		strcpy(username,"");
		strcpy(password,"");
		if (argc>3) {
			strcpy(username,argv[3]);
			if (argc>4) {strcpy(password,argv[4]);}
			}
   		RasDialParams.dwSize=sizeof(RASDIALPARAMS);
		strcpy(RasDialParams.szEntryName,argv[2]);
		strcpy(RasDialParams.szPhoneNumber,"");
		strcpy(RasDialParams.szCallbackNumber,"");
		strcpy(RasDialParams.szUserName,username);
		strcpy(RasDialParams.szPassword,password);
		strcpy(RasDialParams.szDomain,"");
		hRasConn=0;
		nRet=RasDial(NULL,NULL, &RasDialParams, 0,NULL,&hRasConn);
		if (nRet==0) {
			printf("Connexion %s etablie",argv[2]);
			}
		else {
			printError("RasDial",nRet);
			}
		return nRet;
		}
//------ enum -------
	else if (strcmp(job,"/enum")==0) {
		lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN));
		lpRasConn->dwSize = sizeof(RASCONN);
		sizebuffer=sizeof(RASCONN);
		nRet = RasEnumConnections(lpRasConn, &sizebuffer, &nbconnexions);
		if (nRet != 0)	{
			printError("RasEnumConnections",nRet);
			return nRet;
			}
		else {
			printf("%d connexion(s) active(s) actuellement\n",nbconnexions);
			for (i = 0; i < nbconnexions; i++) {
				printf("  %d : %s\n", i+1,lpRasConn->szEntryName);
				rasprojection=RASP_PppIp;
				sizebuffer=sizeof(RASPPPIP);
				rasinfo.dwSize=sizebuffer;
				RasGetProjectionInfo(lpRasConn->hrasconn,
					rasprojection,
					&rasinfo,
					&sizebuffer);
				printf("      Adr.IP client  = %s\n",rasinfo.szIpAddress); 
				printf("      Adr.IP serveur = %s\n",rasinfo.szServerIpAddress); 
				lpRasConn++;
				}
			return 0;
			}
		}
//------ list -------
	else if (strcmp(job,"/list")==0) {
		lpRasEntryName = (LPRASENTRYNAME)GlobalAlloc(GPTR, sizeof(RASENTRYNAME));
		lpRasEntryName->dwSize = sizeof(RASENTRYNAME);
		sizebuffer= sizeof(RASENTRYNAME);
		if ((nRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &sizebuffer, &nbconnexions))
			== ERROR_BUFFER_TOO_SMALL)
			{
			lpRasEntryName = (LPRASENTRYNAME)GlobalAlloc(GPTR, sizebuffer);
			lpRasEntryName->dwSize = sizeof(RASENTRYNAME);
			}
 		// Calling RasEnumEntries to enumerate the phone-book entries    
		nRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &sizebuffer, &nbconnexions);
		if (nRet != ERROR_SUCCESS)
			{
			printError("RasEnumEntries",nRet);
			return nRet;
			}
		else
			{
			printf("%d connexion(s) distante(s) disponible(s)\n",nbconnexions);
			for(i=0;i < nbconnexions;i++)
				{
				EntryInfoSize=0;
				RasGetEntryProperties(
					NULL,
					lpRasEntryName->szEntryName,
					NULL,
					&EntryInfoSize,	NULL,NULL);
				RasEntry.dwSize=EntryInfoSize;
				RasGetEntryProperties(
					NULL,
					lpRasEntryName->szEntryName,
					&RasEntry, 
					&EntryInfoSize, 
					NULL,
                    NULL);
				printf("  %d : %s (%s - avec %s)\n",i+1,lpRasEntryName->szEntryName,
					RasEntry.szDeviceType,
					RasEntry.szDeviceName);	
				printf("      Num.tel.:%s%s\n",RasEntry.szAreaCode,
					RasEntry.szLocalPhoneNumber);
				printIP(RasEntry.ipaddr,"Adr.IP  ");
				printIP(RasEntry.ipaddrDns,"Adr.DNS1");
				printIP(RasEntry.ipaddrDnsAlt,"Adr.DNS2");
				lpRasEntryName++;
				}
			return 0;
			}
		}
//------ disconnect -------
	else if (strcmp(job,"/disconnect")==0) {
		lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN));
		lpRasConn->dwSize = sizeof(RASCONN);
		sizebuffer=sizeof(RASCONN);
		nRet = RasEnumConnections(lpRasConn, &sizebuffer, &nbconnexions);
		if (nRet != 0)	{
			printError("RasEnumConnections", nRet);
			return nRet;
			}
		else {
			trouve=false;
			for (i = 0; i < nbconnexions; i++) {
				if (argc=3) {
					if (strcmp(argv[2],lpRasConn->szEntryName)==0) {
						trouve=true;
						nRet=raccroche(lpRasConn);}
					}
				else
					{nRet=raccroche(lpRasConn);}
				lpRasConn++;
				}
			if ((argc=3) && !trouve){
				printf("Connexion %s inexistante!",argv[2]);
				nRet= 2;
				}
			return nRet;
			}
		}
//------ devices -------
	else if (strcmp(job,"/devices")==0) {
		sizebuffer=0;
		RasEnumDevices(NULL, &sizebuffer, &nbdevices);
		lpRasDevInfo = (LPRASDEVINFO) GlobalAlloc(GPTR, sizebuffer);
		lpRasDevInfo->dwSize = sizeof(RASDEVINFO);
		
		nRet = RasEnumDevices(lpRasDevInfo, &sizebuffer, &nbdevices);
		if (nRet != 0)
			{
			printError("RasEnumDevices", nRet);
			}
		else
			{
			printf("%d peripherique(s) RAS disponible(s) :\n",nbdevices);
			printf("  Type     | Peripherique\n");
			printf("  ---------+----------------------------------\n");
			for (i=0; i < nbdevices; i++)
				{
				strcpy(DevType,lpRasDevInfo->szDeviceType);
				CharToOem(lpRasDevInfo->szDeviceName,device);
  				printf("  %8s | %s\n",DevType,device);
				lpRasDevInfo++;
				}
			}
		return 0;
		}
//------ autre -------	
	else if (strcmp(job,"/errcode")==0) {
		if (argc<3) {
			printf("Le n° de code d'erreur est obligatoire!\n\n");
			syntaxe();
			return 2;}
		strcpy(errcode,argv[2]);
		nRet=atoi(errcode);
		printError("",nRet);
		return 0;
		}
//------ autre -------	
	else {
		syntaxe();
		return 1;
		}
	}
