PC Review


Reply
Thread Tools Rate Thread

Configure ICS automatically

 
 
GoAheadTarCon
Guest
Posts: n/a
 
      28th Mar 2004
Hi all,

I'm developing a WinXP Setup for our production line.

Each computer has two NICs with fixed names:

1) External LAN
2) To RIP Server

I need to find a way to share the first adapter's Internet connection
with the second adapter (apply ICS).

The phase in which I will execute this configuration is when Windows
XP is already installed on the computer (the process/script should run
under Windows XP).

Any method is good: VBS, Win32 API, etc.

Can anyone help?

Thanx,
Ariel
 
Reply With Quote
 
 
 
 
GoAheadTarCon
Guest
Posts: n/a
 
      30th Mar 2004
for all the desperate people who will look for code sample, this will
give you a lead (unfortunately, you must download the platform SDK for
NetCon.h):

// main.cpp

#include <NetCon.h>

#include <cstdio>

//using namespace std;

//#include "stdafx.h"

// as in winsock.h
#define NAT_PROTOCOL_TCP 6
#define NAT_PROTOCOL_UDP 17

HRESULT DeletePortMapping (INetSharingManager * pNSM, UCHAR
ucIPProtocol, short usExternalPort)
{ // this is done in 2 parts:
// 1: enum connections until we get one that we can convert into
an INetSharingConfiguration
// 2: then, enum portmappings, and delete if we find a match.

// PART 1: find a valid connection
INetConnection * pNC = NULL; // fill this out for part 2 below

INetSharingEveryConnectionCollection * pNSECC = NULL;
HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);
if (!pNSECC)
printf ("failed to get EveryConnectionCollection!\r\n");
else {
// enumerate connections
IEnumVARIANT * pEV = NULL;
IUnknown * pUnk = NULL;
hr = pNSECC->get__NewEnum (&pUnk);
if (pUnk) {
hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
(void**)&pEV);
pUnk->Release();
}
if (pEV) {
VARIANT v;
VariantInit (&v);
BOOL bFoundIt = FALSE;
while (S_OK == pEV->Next (1, &v, NULL)) {
if (V_VT (&v) == VT_UNKNOWN) {
V_UNKNOWN (&v)->QueryInterface
(__uuidof(INetConnection),
(void**)&pNC);
if (pNC) {
INetConnectionProps * pNCP = NULL;
pNSM->get_NetConnectionProps (pNC, &pNCP);
if (!pNCP)
wprintf (L"failed to get
NetConnectionProps!\r\n");
else {
// check properties for firewalled or
shared connection
DWORD dwCharacteristics = 0;
pNCP->get_Characteristics
(&dwCharacteristics);
if (dwCharacteristics & (NCCF_SHARED |
NCCF_FIREWALLED)) {
NETCON_MEDIATYPE MediaType = NCM_NONE;
pNCP->get_MediaType (&MediaType);
if ((MediaType !=
NCM_SHAREDACCESSHOST_LAN) &&
(MediaType !=
NCM_SHAREDACCESSHOST_RAS) ){
// got a shared/firewalled
connection
bFoundIt = TRUE;
}
}
pNCP->Release();
}
if (bFoundIt == FALSE) {
pNC->Release();
pNC = NULL;
}
}
}
VariantClear (&v);
if (bFoundIt == TRUE)
break;
}
pEV->Release();
}
pNSECC->Release();
}
if (pNC == NULL) {
wprintf (L"failed to find a valid connection!\r\n");
return E_FAIL;
}
INetSharingConfiguration * pNSC = NULL;
hr = pNSM->get_INetSharingConfigurationForINetConnection (pNC,
&pNSC);
pNC->Release(); // don't need this anymore
if (!pNSC) {
wprintf (L"can't make INetSharingConfiguration object!\r\n");
return hr;
}

// PART 2: enum port mappings, deleting match, if any
INetSharingPortMappingCollection * pNSPMC = NULL;
hr = pNSC->get_EnumPortMappings (ICSSC_DEFAULT, &pNSPMC);
if (!pNSPMC)
wprintf (L"can't get PortMapping collection!\r\n");
else {

// this is the interface to be filled out by the code below
INetSharingPortMapping * pNSPM = NULL;

IEnumVARIANT * pEV = NULL;
IUnknown * pUnk = NULL;
hr = pNSPMC->get__NewEnum (&pUnk);
if (pUnk) {
hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
(void**)&pEV);
pUnk->Release();
}
if (pEV) {
VARIANT v;
VariantInit (&v);
BOOL bFoundIt = FALSE;
while (S_OK == pEV->Next (1, &v, NULL)) {
if (V_VT (&v) == VT_DISPATCH) {
V_DISPATCH (&v)->QueryInterface
(__uuidof(INetSharingPortMapping),
(void**)&pNSPM);
if (pNSPM) {
INetSharingPortMappingProps * pNSPMP = NULL;
hr = pNSPM->get_Properties (&pNSPMP);
if (pNSPMP) {
UCHAR uc = 0;
pNSPMP->get_IPProtocol (&uc);
long usExternal = 0;
pNSPMP->get_ExternalPort (&usExternal);
if ((uc == ucIPProtocol) &&
(usExternal == usExternalPort))
bFoundIt = TRUE;

pNSPMP->Release();
}
if (bFoundIt == FALSE) { // hang onto
reference to pNSPM iff found (used below)
pNSPM->Release();
pNSPM = NULL;
}
}
}
VariantClear (&v);
if (bFoundIt == TRUE)
break; // bail out if we've found one
}
pEV->Release();
}

if (pNSPM) {
hr = pNSPM->Delete(); // or pNSC->RemovePortMapping
(pNSPM);
wprintf (L"just deleted a port mapping!\r\n");

pNSPM->Release();
}
pNSPMC->Release();
}
pNSC->Release();
return hr;
}

//-------------------------------

HRESULT CICS(INetSharingManager * pNSM, UCHAR ucIPProtocol, short
usExternalPort)
{

// this is done in 2 parts:
// 1: enum connections until we get one that we can convert into
an INetSharingConfiguration
// 2: then, enum portmappings, and delete if we find a match.

// PART 1: find a valid connection

INetConnection * pNC = NULL; // fill this out for part 2 below

INetSharingEveryConnectionCollection * pNSECC = NULL;
HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);
if (!pNSECC)
printf ("failed to get EveryConnectionCollection!\r\n");
else {
// enumerate connections
IEnumVARIANT * pEV = NULL;
IUnknown * pUnk = NULL;
hr = pNSECC->get__NewEnum (&pUnk);
if (pUnk) {
hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
(void**)&pEV);
pUnk->Release();
}
if (pEV) {
VARIANT v;
VariantInit (&v);
BOOL bFoundIt = FALSE;
while (S_OK == pEV->Next (1, &v, NULL)) {
if (V_VT (&v) == VT_UNKNOWN) {
V_UNKNOWN (&v)->QueryInterface
(__uuidof(INetConnection),
(void**)&pNC);
if (pNC) {
INetConnectionProps * pNCP = NULL;
pNSM->get_NetConnectionProps (pNC, &pNCP);
if (!pNCP)
wprintf (L"failed to get
NetConnectionProps!\r\n");
else
{

/*

// check properties for firewalled or
shared connection

DWORD dwCharacteristics = 0;
pNCP->get_Characteristics
(&dwCharacteristics);
if (dwCharacteristics & (NCCF_SHARED |
NCCF_FIREWALLED)) {
NETCON_MEDIATYPE MediaType = NCM_NONE;
pNCP->get_MediaType (&MediaType);
if ((MediaType !=
NCM_SHAREDACCESSHOST_LAN) &&
(MediaType !=
NCM_SHAREDACCESSHOST_RAS) ){
// got a shared/firewalled
connection
bFoundIt = TRUE;
}
}

*/
BSTR name;

pNCP->get_Name(&name);

wprintf( L"%s" , name );

if( wcscmp( name , L"External LAN" ) == 0 )
{
bFoundIt = TRUE;
}

pNCP->Release();

}
if (bFoundIt == FALSE) {
pNC->Release();
pNC = NULL;
}
}
}
VariantClear (&v);
if (bFoundIt == TRUE)
break;
}
pEV->Release();
}
pNSECC->Release();
}
if (pNC == NULL) {
wprintf (L"failed to find a valid connection!\r\n");
return E_FAIL;
}

INetSharingConfiguration * pNSC = NULL;

hr = pNSM->get_INetSharingConfigurationForINetConnection ( pNC ,
&pNSC );

pNC->Release(); // don't need this anymore
if (!pNSC) {
wprintf (L"can't make INetSharingConfiguration object!\r\n");
return hr;
}

pNSC->EnableSharing(ICSSHARINGTYPE_PUBLIC);

/*

// PART 2: enum port mappings, deleting match, if any
INetSharingPortMappingCollection * pNSPMC = NULL;
hr = pNSC->get_EnumPortMappings (ICSSC_DEFAULT, &pNSPMC);
if (!pNSPMC)
wprintf (L"can't get PortMapping collection!\r\n");
else {

// this is the interface to be filled out by the code below
INetSharingPortMapping * pNSPM = NULL;

IEnumVARIANT * pEV = NULL;
IUnknown * pUnk = NULL;
hr = pNSPMC->get__NewEnum (&pUnk);
if (pUnk) {
hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
(void**)&pEV);
pUnk->Release();
}
if (pEV) {
VARIANT v;
VariantInit (&v);
BOOL bFoundIt = FALSE;
while (S_OK == pEV->Next (1, &v, NULL)) {
if (V_VT (&v) == VT_DISPATCH) {
V_DISPATCH (&v)->QueryInterface
(__uuidof(INetSharingPortMapping),
(void**)&pNSPM);
if (pNSPM) {
INetSharingPortMappingProps * pNSPMP = NULL;
hr = pNSPM->get_Properties (&pNSPMP);
if (pNSPMP) {
UCHAR uc = 0;
pNSPMP->get_IPProtocol (&uc);
long usExternal = 0;
pNSPMP->get_ExternalPort (&usExternal);
if ((uc == ucIPProtocol) &&
(usExternal == usExternalPort))
bFoundIt = TRUE;

pNSPMP->Release();
}
if (bFoundIt == FALSE) { // hang onto
reference to pNSPM iff found (used below)
pNSPM->Release();
pNSPM = NULL;
}
}
}
VariantClear (&v);
if (bFoundIt == TRUE)
break; // bail out if we've found one
}
pEV->Release();
}

if (pNSPM) {
hr = pNSPM->Delete(); // or pNSC->RemovePortMapping
(pNSPM);
wprintf (L"just deleted a port mapping!\r\n");

pNSPM->Release();
}
pNSPMC->Release();
}

*/

pNSC->Release();

return hr;
}


//-----------------------------

HRESULT AddAsymmetricPortMapping (INetSharingConfiguration * pNSC) {
HRESULT hr = S_OK;
VARIANT_BOOL vb1 = VARIANT_FALSE;
VARIANT_BOOL vb2 = VARIANT_FALSE;
pNSC->get_SharingEnabled (&vb1);
pNSC->get_InternetFirewallEnabled (&vb2);
if ((vb1 == VARIANT_FALSE) &&
(vb2 == VARIANT_FALSE))
wprintf (L"sharing and/or firewall not enabled on this
connection!\r\n");
else {
INetSharingPortMapping * pNSPM = NULL;
hr = pNSC->AddPortMapping (L"Ben's Port Mapping",
NAT_PROTOCOL_TCP,
555,
444,
0,
L"192.168.0.2",
ICSTT_IPADDRESS,
&pNSPM);
if (pNSPM) {
wprintf (L"just added NAT_PROTOCOL_TCP, 555, 444!\r\n");

hr = pNSPM->Enable();
wprintf (L"just enabled port mapping!\r\n");

pNSPM->Release();
} else
wprintf (L"failed to add asymmetric port mapping!\r\n");
}
return hr;
}

HRESULT DoTheWork (INetSharingManager * pNSM)
{ // add a port mapping to every firewalled or shared connection

INetSharingEveryConnectionCollection * pNSECC = NULL;
HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);
if (!pNSECC)
wprintf (L"failed to get EveryConnectionCollection!\r\n");
else {

// enumerate connections
IEnumVARIANT * pEV = NULL;
IUnknown * pUnk = NULL;
hr = pNSECC->get__NewEnum (&pUnk);
if (pUnk) {
hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
(void**)&pEV);
pUnk->Release();
}
if (pEV) {
VARIANT v;
VariantInit (&v);
while (S_OK == pEV->Next (1, &v, NULL)) {
if (V_VT (&v) == VT_UNKNOWN) {
INetConnection * pNC = NULL;
V_UNKNOWN (&v)->QueryInterface
(__uuidof(INetConnection),
(void**)&pNC);
if (pNC) {
INetConnectionProps * pNCP = NULL;
pNSM->get_NetConnectionProps (pNC, &pNCP);
if (!pNCP)
wprintf (L"failed to get
NetConnectionProps!\r\n");
else {
// check properties for firewalled or
shared connection
DWORD dwCharacteristics = 0;
pNCP->get_Characteristics
(&dwCharacteristics);
if (dwCharacteristics & (NCCF_SHARED |
NCCF_FIREWALLED)) {
NETCON_MEDIATYPE MediaType = NCM_NONE;
pNCP->get_MediaType (&MediaType);
if ((MediaType !=
NCM_SHAREDACCESSHOST_LAN) &&
(MediaType !=
NCM_SHAREDACCESSHOST_RAS) ){
// got a shared/firewalled
connection
INetSharingConfiguration * pNSC =
NULL;
hr =
pNSM->get_INetSharingConfigurationForINetConnection (pNC, &pNSC);
if (!pNSC)
wprintf (L"can't make
INetSharingConfiguration object!\r\n");
else {
hr = AddAsymmetricPortMapping
(pNSC);
pNSC->Release();
}
}
}
pNCP->Release();
}
pNC->Release();
}
}
VariantClear (&v);
}
pEV->Release();
}
pNSECC->Release();
}
return hr;
}

//---------------------------

int main(int argc, char* argv[])
{
CoInitialize (NULL);



// init security to enum RAS connections

CoInitializeSecurity (NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, NULL);



INetSharingManager * pNSM = NULL;
HRESULT hr = ::CoCreateInstance (__uuidof(NetSharingManager),
NULL,
CLSCTX_ALL,
__uuidof(INetSharingManager),
(void**)&pNSM);
if (!pNSM)
wprintf (L"failed to create NetSharingManager object\r\n");
else {

// in case it exists already

CICS( pNSM , NAT_PROTOCOL_TCP , 555 );

/*

// add a port mapping to every shared or firewalled
connection.
hr = DoTheWork (pNSM);
if (SUCCEEDED(hr)) {

// do other work here.
// when you're done,
// clean up port mapping

hr = DeletePortMapping (pNSM, NAT_PROTOCOL_TCP, 555);
}

*/

pNSM->Release();
}

CoUninitialize ();
return (int)hr;
}



(E-Mail Removed) (GoAheadTarCon) wrote in message news:<(E-Mail Removed)>...
> Hi all,
>
> I'm developing a WinXP Setup for our production line.
>
> Each computer has two NICs with fixed names:
>
> 1) External LAN
> 2) To RIP Server
>
> I need to find a way to share the first adapter's Internet connection
> with the second adapter (apply ICS).
>
> The phase in which I will execute this configuration is when Windows
> XP is already installed on the computer (the process/script should run
> under Windows XP).
>
> Any method is good: VBS, Win32 API, etc.
>
> Can anyone help?
>
> Thanx,
> Ariel

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to configure automatically replied. =?Utf-8?B?UyBNdXJhbGkgS3Jpc2huYW4=?= Microsoft Outlook Discussion 2 19th Jul 2007 12:27 PM
How can I configure Time Zone Automatically? =?Utf-8?B?Si4gTmFnYXNl?= Windows XP Security 2 14th Oct 2006 05:27 PM
Automatically configure new accounts. =?Utf-8?B?QWxiZXJ0IEJyaWNrZXI=?= Microsoft Outlook Installation 1 7th Jul 2006 05:54 PM
automatically configure outlook =?Utf-8?B?QWxiZXJ0IEJyaWNrZXI=?= Windows XP General 2 7th Jul 2006 05:21 PM
Automatically Configure Outlook Profile Keith Microsoft Outlook 1 4th May 2005 03:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:40 AM.