关于如何限制上网的问题

netreptile 2006-05-29 11:57:30
下面讨论实现方案。以一个局域网中的电脑为例,实现目标是:不能访问Internet,但可以访问局域网。我们知道,PC机上网有两种方式:A 通过网关(路由器)上网,B 通过代理服务器上网。
  对于A,则其访问Internet时的地址都是公网地址,我们直接通过自己的传输服务提供者直接过滤掉。如下:

int WSPAPI WSPSendTo(
SOCKET s,
LPWSABUF lpbuffer,
DWORD dwbuffercount,
LPDWORD lpnumberofbytessent,
DWORD dwflags,
const struct sockaddr FAR *lpto,
int itolen,
LPWSAOVERLAPPED lpoverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpcompletionroutine,
LPWSATHREADID lpthreadid,
LPINT lperrno)
{

struct sockaddr_in sin;
sin=*(const struct sockaddr_in *)lpto;

//过滤掉访问地址不是本地局域网的包,下面只是简单认为192.168.*.*是局域网的IP,具体需
//根据不同的局域网网络地址进行设置。

if (192 != sin.sin_addr.S_un.S_un_b.s_b1)
{
OutputDebugString(_T("WSPSendTo Tencent Filtered"));
return 0;
}
else
{
return nextproctable.lpWSPSendTo(s,
lpbuffer,
dwbuffercount,
lpnumberofbytessent,
dwflags,
lpto,
itolen,
lpoverlapped,
lpcompletionroutine,
lpthreadid,
lperrno);
}
}   对于B,由于PC机是通过局域网中的代理上网的,所以其所有的上网数据包通过先发给本地代理,然后代理将它请求的网页返回给它,所以其访问Internet是间接的,其访问Internet时发出的包都是局域网的IP。这样上面的过滤规则就不适用。怎么办?我们可以先取得IE的代理服务器IP地址和端口号,然后将包的地址和端口号与IE的代理IP地址和端口号比较,如果都相等,则过滤此包。这样就可以限制PC机通过代理上网,同时不影响它访问代理服务器的局域网资源。
  至于如何获取IE的代理服务器地址和端口号,可使用下面代码:
INTERNET_PROXY_INFO *pIEinfo = NULL;
DWORD dwSize = 0;
BOOL bRet = InternetQueryOption(NULL, INTERNET_OPTION_PROXY, pIEinfo, &dwSize);
pIEinfo = (INTERNET_PROXY_INFO*)new char[dwSize];
bRet = InternetQueryOption(NULL, INTERNET_OPTION_PROXY, pIEinfo, &dwSize);
以上是yy2better大大写的,对于以上的程序我不是很理解,大哥们是否能给出一个例子,教小弟使用呢。
...全文
1544 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacklzw88 2006-05-29
  • 打赏
  • 举报
回复
你去看看spi的方法
http://www.xfocus.net/articles/200304/518.html
netreptile 2006-05-29
  • 打赏
  • 举报
回复
jacklzw88(不可爱咯),您好,我还是个Windows编程的水手,您是否能给出一段程序呢。谢谢
jacklzw88 2006-05-29
  • 打赏
  • 举报
回复
首先一看就是用SPI截包,但是WSPSendTo只可以拦截udp数据包。。怎么可以阻止上网呢,要完全阻止上网还要要用WSPSend,拦截tcp包,还有不用考虑代理服务器还是路由器,内网机器还有一个ip的。
lwugui01 2006-05-29
  • 打赏
  • 举报
回复
学习
netreptile 2006-05-29
  • 打赏
  • 举报
回复
2.ipfilter.dll

#define UNICODE
#define _UNICODE

#include <ws2spi.h>
#include <tchar.h>

GUID filterguid={0x4d1e91fd,0x116a,0x44aa,{0x8f,0xd4,0x1d,0x2c,0xf2,0x7b,0xd9,0xa9}};

LPWSAPROTOCOL_INFOW protoinfo=NULL;
WSPPROC_TABLE nextproctable;
DWORD protoinfosize=0;
int totalprotos=0;

BOOL getfilter()
{
int errorcode;

protoinfo=NULL;
protoinfosize=0;
totalprotos=0;

if(WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode)==SOCKET_ERROR)
{
if(errorcode!=WSAENOBUFS)
{
OutputDebugString(_T("First WSCEnumProtocols Error!"));
return FALSE;
}
}

if((protoinfo=(LPWSAPROTOCOL_INFOW)GlobalAlloc(GPTR,protoinfosize))==NULL)
{
OutputDebugString(_T("GlobalAlloc Error!"));
return FALSE;
}

if((totalprotos=WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode))==SOCKET_ERROR)
{
OutputDebugString(_T("Second WSCEnumProtocols Error!"));
return FALSE;
}

return TRUE;
}

void freefilter()
{
GlobalFree(protoinfo);
}

BOOL WINAPI DllMain(HINSTANCE hmodule,
DWORD reason,
LPVOID lpreserved)
{
TCHAR processname[MAX_PATH];
TCHAR showmessage[MAX_PATH+25];


if(reason==DLL_PROCESS_ATTACH)
{
GetModuleFileName(NULL,processname,MAX_PATH);
_tcscpy(showmessage,processname);
_tcscat(showmessage,_T(" Loading IPFilter ..."));
OutputDebugString(showmessage);
}
return TRUE;
}

int WSPAPI WSPSendTo(SOCKET s,
LPWSABUF lpbuffer,
DWORD dwbuffercount,
LPDWORD lpnumberofbytessent,
DWORD dwflags,
const struct sockaddr FAR *lpto,
int itolen,
LPWSAOVERLAPPED lpoverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpcompletionroutine,
LPWSATHREADID lpthreadid,
LPINT lperrno)
{

struct sockaddr_in sin;

sin=*(const struct sockaddr_in *)lpto;
if(sin.sin_port==htons(8000))
{
OutputDebugString(_T("WSPSendTo Tencent Filtered"));
return 0;
}
else
{
return nextproctable.lpWSPSendTo(s,lpbuffer,dwbuffercount,lpnumberofbytessent,dwflags,lpto,itolen,lpoverlapped,lpcompletionroutine,lpthreadid,lperrno);
}
}

int WSPAPI WSPStartup(
WORD wversionrequested,
LPWSPDATA lpwspdata,
LPWSAPROTOCOL_INFOW lpprotoinfo,
WSPUPCALLTABLE upcalltable,
LPWSPPROC_TABLE lpproctable
)
{
OutputDebugString(_T("IPFilter WSPStartup ..."));

int i;
int errorcode;
int filterpathlen;
DWORD layerid=0;
DWORD nextlayerid=0;
TCHAR *filterpath;
HINSTANCE hfilter;
LPWSPSTARTUP wspstartupfunc=NULL;

if(lpprotoinfo->ProtocolChain.ChainLen<=1)
{
OutputDebugString(_T("ChainLen<=1"));
return FALSE;
}

getfilter();

for(i=0;i<totalprotos;i++)
{
if(memcmp(&protoinfo[i].ProviderId,&filterguid,sizeof(GUID))==0)
{
layerid=protoinfo[i].dwCatalogEntryId;
break;
}
}

for(i=0;i<lpprotoinfo->ProtocolChain.ChainLen;i++)
{
if(lpprotoinfo->ProtocolChain.ChainEntries[i]==layerid)
{
nextlayerid=lpprotoinfo->ProtocolChain.ChainEntries[i+1];
break;
}
}

filterpathlen=MAX_PATH;
filterpath=(TCHAR*)GlobalAlloc(GPTR,filterpathlen);
for(i=0;i<totalprotos;i++)
{
if(nextlayerid==protoinfo[i].dwCatalogEntryId)
{
if(WSCGetProviderPath(&protoinfo[i].ProviderId,filterpath,&filterpathlen,&errorcode)==SOCKET_ERROR)
{
OutputDebugString(_T("WSCGetProviderPath Error!"));
return WSAEPROVIDERFAILEDINIT;
}
break;
}
}

if(!ExpandEnvironmentStrings(filterpath,filterpath,MAX_PATH))
{
OutputDebugString(_T("ExpandEnvironmentStrings Error!"));
return WSAEPROVIDERFAILEDINIT;
}

if((hfilter=LoadLibrary(filterpath))==NULL)
{
OutputDebugString(_T("LoadLibrary Error!"));
return WSAEPROVIDERFAILEDINIT;
}

if((wspstartupfunc=(LPWSPSTARTUP)GetProcAddress(hfilter,"WSPStartup"))==NULL)
{
OutputDebugString(_T("GetProcessAddress Error!"));
return WSAEPROVIDERFAILEDINIT;
}

if((errorcode=wspstartupfunc(wversionrequested,lpwspdata,lpprotoinfo,upcalltable,lpproctable))!=ERROR_SUCCESS)
{
OutputDebugString(_T("wspstartupfunc Error!"));
return errorcode;
}

nextproctable=*lpproctable;
lpproctable->lpWSPSendTo=WSPSendTo;

freefilter();
return 0;
}
这需要建立mfc的什么工程啊?谢谢指教
netreptile 2006-05-29
  • 打赏
  • 举报
回复
我看到那篇文章的源代码了,但是是真的不会使用,我的qq是9600163,msn是netreptile@hotmail.com,能不能加加,传个程序我研究一下,谢谢
jacklzw88 2006-05-29
  • 打赏
  • 举报
回复
那文章都有原代码了。。。。
WecanHuang 2006-05-29
  • 打赏
  • 举报
回复
up
netreptile 2006-05-29
  • 打赏
  • 举报
回复
这篇文章我看过,但是还是不会写这个程序,能不能给一个例子呢?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧