Speeding Up Internet Browsing

This article is based on another article I did, DNSWATCH, at http://www.codeguru.com. You might want to take a look at that before proceeding because this is a newer version with more features, and some new code. The program is based on DNS host name lookups done by programs, mainly browsers. It lists them and allows you to select some and save or append to a temporary file, and optionally update the host file. This allows you to block sites that are just advertisements, or tracking sites and eliminate the overhead of retrieving them. I basically wrote this program to try and watch who was watching me, and block annoying ads. It's also interesting just running it and see how many Web sites are looked up from one Web page.

Editing a system file (hosts) can be dangerous; however, most Windows users don't ever use it, so it's safe doing this. You can block yourself from some Web sites if you select the wrong URL. For example, if you selected "www.ebay.com," saved it, and updated the hosts file, you would not be able to access ebay at all. I added the "edit hosts file" option so you can remove any entries you want to.

I added some command line switches so I could start the program from startup. They are: "-s" to auto start the program, -b to turn off the beeping, and "-t tempfilename" to set the default temp file. After you add the program to the start menu, right-click it and select Properties. In the window will be a line marked Target and an EditBox next to it containing the full path and executable name. Just add the switches after the name. This is the one I use: C:\dev\NewDev\DNSWatch\Debug\DNSWatch.exe -s -b.

I also found a piece of code I have been looking for a long time. ListBoxes always scroll down and new entries can't be seen; the first entries are always seen. There may be other ways to do this, but by adding one line after adding an entry to the listbox it seems to work the opposite, newest at the bottom.

m_dns_name_list.AddString(name_to_add);

m_dns_name_list.PostMessage(WM_VSCROLL,SB_LINEDOWN);

You now can edit the temporary file. I also added some code that invokes the CFileDialog dialog so you can search for a temporary file. I added an On-Top button because that's what I needed once. You now can edit the hosts file. This is not recommended, but it's necessary sometimes (like just checking). You now can update the hosts file with the entries in the temporary file automatically. In WinXP and Win2K, the hosts file resides in \windows\system32\drivers\etc directory. The logic I used for this task is:

  1. Copy the hosts file to a file named hosts.bak and create it or replace it in the etc directory.
  2. Open the temporary file for append. Read in the hosts file and append it to existing entries.
  3. Sort the new file, remove duplicates, and write the results to the hosts file.
  4. Empty the temporary file.

This works and I haven't had any problems yet. Additional backups could be done and the logic could be changed to taste. Here is most of the code I've added. It reads better in the compiler.

Note: You may have to delete and re-add ws2_lib.dll and iphlpapi.lib with the path of your SDK lib directory.
//---------------------------------------------------------
int CompHostNames(const void *s1, const void *s2)
{
return strcmp((const char *) s1, (const char *) s2);
}
//--------------------------------------------------------
int MergeSortAndDeleteDups()
{
FILE *fh;
int count=0;
char *host_array = (char *)calloc(1000*256,1);    //1000 entries
char ctemp[256], *cptr;


//read in hosts file
fh = fopen (hosts_file_name,"r");
if ( fh == NULL ) return 1;    //No default param file
while(true) {
cptr = fgets((char *)&ctemp,256,fh);
if (cptr == NULL ) break;
strcpy(&host_array[count*256],ctemp);
++count;
if (count >=1000) break;
}
fclose(fh);

//Now read and append the temporary file
fh = fopen (temp_file_name,"r");
if ( fh == NULL ) return 2;    //No default param file
while(true) {
cptr = fgets((char *)&ctemp,256,fh);
if (cptr == NULL ) break;
strcpy(&host_array[count*256],ctemp);
++count;
if (count >=1000) break;
}
fclose(fh);

//We have all the hosts, now sort the array
qsort(host_array,count,256,CompHostNames);

//Now remove duplicates and save in temp file
fh = fopen (temp_file_name,"w");
if ( fh == NULL ) {
free(host_array);
return 3;    //???
}
char last_host[256] = "";
for(int i=0; i< count; i++){
strcpy(ctemp, &host_array[i*256]);
if (strcmp(last_host,ctemp) != 0) {
fputs(ctemp,fh);
strcpy(last_host,ctemp);
}
}
fclose(fh);
free(host_array);    //no longer needed

//Now copy temp to hosts
FILE *fhr = fopen (temp_file_name,"r");
if ( fhr == NULL ) {
return 4; //???
}

FILE *fhw = fopen (hosts_file_name,"w+");
if ( fh == NULL ) {
return 5;    //???
}
while(true) {
cptr = fgets((char *)&ctemp,256,fhr);
if (cptr == NULL ) break;
fputs(ctemp,fhw);
}
fclose(fh);
fclose(fhw);
return 0;
}
//---------------------------------------------------------
void CDNSWatchDlg::OnUpdateButtonButton()
{
//Save a copy of current host file
int rc = CopyFile(hosts_file_name, hosts_file_name+".bak", FALSE);
temp_file_name = m_save_filename;
//Merge temp and host files, sort,remove duplicates, and update
//host file
rc = MergeSortAndDeleteDups();
if ( rc == 0) {    ///empty temp file
FILE *fh;
fh = fopen (temp_file_name,"w");
if ( fh == NULL ) return;    //??
fseek(fh,0,0);
fclose(fh);
}
m_status = "Hosts File Updated";
UpdateData(FALSE);
}
//---------------------------------------------------------
// I simply exec notepad from SystemRoot and edit hosts file
void CDNSWatchDlg::OnEditHostsButton()
{
char ctemp[256], sysroot[256];
GetEnvironmentVariable("SystemRoot",sysroot,256);
strcpy(ctemp,sysroot);
strcat(ctemp,"\\notepad ");
strcat(ctemp,sysroot);
strcat(ctemp,"\\system32\\drivers\\etc\\hosts");
WinExec(ctemp,SW_SHOWNORMAL);

}
//------------------------------------------------------
// I exec notepad from SystemRoot and edit temp file
void CDNSWatchDlg::OnEditTempFile()
{
char sysroot[256], tstr[256];;
CString Newstr,Srchstr,Srchaddstr="\\",Repstr="\\\\",ctemp;

//this function adds a '\' to each '\' in the temp file display
//name
CString Chstr = ConvertAndInsert(Newstr,m_save_filename,
                                 Srchaddstr,Repstr);

//check if temp file doesn't exist, create it
FILE *fhr = fopen (temp_file_name,"a+");    //check if exists,
                                            //create else
if ( fhr == NULL ) {
m_status = "Error opening temp file";    //Error ???
UpdateData(FALSE);
}
fclose(fhr);

GetEnvironmentVariable("SystemRoot",sysroot,256);
strcpy(tstr,sysroot);
strcat(tstr,"\\notepad ");
strcat(tstr,Chstr);
WinExec(tstr,SW_SHOWNORMAL);
}
//------------------------------------------------------
//This function takes a string, searches for another string in it,
//and inserts a string after the string found. I couldn't seen to
//find an easier way for a CString
CString ConvertAndInsert(CString Newstr,CString Srchstr,
                         CString Srchaddstr,CString Repstr)
{
int ndx = 0;
Newstr = Srchstr;
while(true) {
ndx = Srchstr.Find(Srchaddstr, ndx);
if (ndx == -1 ) break;
++ndx;
Newstr.Insert(ndx,Srchaddstr);
Srchstr = Newstr;
++ndx;
}
return Newstr;
}
//-----------------------------------------------------
//This is a function to toggle whether the dialog is always on
//top or not
void CDNSWatchDlg::OnOntopbutton()
{
int rc;
HWND hWandle;

m_on_top_button.SetButtonStyle(BS_DEFPUSHBUTTON);
if ( OnTop == TRUE ) {
OnTop = FALSE;
hWandle = HWND_NOTOPMOST;
m_on_top_button.SetWindowText("Not On Top");

}
else {
OnTop = TRUE;
hWandle = HWND_TOPMOST;
m_on_top_button.SetWindowText("On Top");
}
rc = ::SetWindowPos ( ::GetActiveWindow(), hWandle ,
0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
if ( rc == 1) return;
rc = GetLastError ();
}
//-----------------------------------------------------
//This function allows you to search for a temp file using system
//dialog
void CDNSWatchDlg::OnSearchBUTTON()
{
static LPCTSTR kpszFileTypes = _T(" (*.*)||");

CFileDialog fd(TRUE, _T(".acs"), NULL, OFN_HIDEREADONLY,
               kpszFileTypes, this);
if (fd.DoModal() == IDCANCEL) return;    //nothing selected or
                                         //cancelled
m_save_filename = fd.GetPathName();      //get new temp file name
UpdateData(FALSE);
} 

Known Quirks

  • On multi-homed systems where the first interface listed isn't active or is disabled, a bind error will occur.
  • If no interface is enabled, it will default to the loop back address.
  • If you use a proxy server for your browser, the hosts file is bypassed and no DNS lookups will show.

History

Date Posted: 09-28-2002
Modified 02-13-2003



Downloads

Comments

  • Download Movie DVD

    Posted by gd3cbtkh538bh on 11/01/2012 01:02pm

    http://wpshower.com/forums/showthread.php?t=13874&p=23377 - 10 Years Movie Movie Download For HTC, 10 Years film download link, 10 Years film money 9445735, http://wpshower.com/forums/showthread.php?t=13864&p=23365 - Download Lawless Movie For iPhone, Download Lawless Movie Movie In Hd Formats, Lawless Movie Download Link :-)))

    Reply
  • Excellent technical article !!!!

    Posted by Legacy on 05/27/2003 12:00am

    Originally posted by: USA

    We learn a lot from your article, your solution is the best solution in the world.

    regards,

    USA

    Reply
  • WARNING: Your Code Can Crash Computers!

    Posted by Legacy on 05/23/2003 12:00am

    Originally posted by: Bill SerGio

    You fail to address several significant issues related to the hosts file that makes your suggested not practical.

    First, this idea is not new in terms of using the hosts file to block ads and it goes back as far as 1991 and probably earlier.

    Second, there is a physical limite to the number of entries that can be posted to the hosts file that makes your approach useless. In a simple test it is easy to get a list of over 13,000 ad sites in common usage and which I posted in a previous article posted here on your approach.

    If you put 13,000 which is really the MINIMUM number of sites needed to get any significant blocking of ads many computers will completely LOCK UP and require a reboot of the system and will STOP working completelty! YOU FAIL TO POINT THIS OUT.

    Why does this happen? It is due tothe fact there several of those annoying spyware programs open the hosts file and LOCK IT to prevent ad blockage dude!

    While abstarct approaches likes yours are interesting, your approach fails to work in any practical manner because of the reality of the Internet the the way the hosts file is used by many programs. Anything over a few hundred lines will CRASH most computers and you should WARN people about this!

    Again, while you idea to use the hosts file is not original your Winsock code is excellent but unfortunately your appraoch fails to achieve its goal because of the limitation on the size of the hosts file.

    Like I said, it would take AT LEAST 13,000 entries in the hosts file to achieve any useful ad blocking results!

    Reply
  • Good Program

    Posted by Legacy on 02/24/2003 12:00am

    Originally posted by: Frank Jenson

    I used you program and got a good list of hosts NOT to go to. Surfing is definately faster. Here is the list:
    127.0.0.1 banner.linksynergy.com
    127.0.0.1 ad.doubleclick.net
    127.0.0.1 doubleclick.net
    127.0.0.1 m.doubleclick.net
    127.0.0.1 tribalfusion.com
    127.0.0.1 netidentity.com
    127.0.0.1 pro-market.net
    127.0.0.1 ke2.informative.com
    127.0.0.1 www.aadcom.com
    127.0.0.1 ehg-pennwell.hitbox.com
    127.0.0.1 trafficmp.com
    127.0.0.1 pro-market.net
    127.0.0.1 www.netster.com
    127.0.0.1 auto.search.msn.com
    127.0.0.1 statse.webtrendslive.com
    127.0.0.1 spylog.com
    127.0.0.1 fastclick.net
    127.0.0.1 www.cyblime.com
    127.0.0.1 capitalone.com
    127.0.0.1 lhg1.hitbox.com
    127.0.0.1 ru4.com
    127.0.0.1 servedby.advertising.com
    127.0.0.1 advertising.com
    127.0.0.1 switchboard.com
    127.0.0.1 toolbar.netscape.com
    127.0.0.1 ar.atwola.com
    127.0.0.1 www.qksrv.net
    127.0.0.1 cl.zedo.net
    127.0.0.1 gator.com
    127.0.0.1 www.nwsource.com
    127.0.0.1 adcoltroller.unicast.com
    127.0.0.1 adsl.intelliads.com
    127.0.0.1 barconet.com
    127.0.0.1 www.moreover.com
    127.0.0.1 ad.trafficmp.com
    127.0.0.1 ad.trafficmp.com
    127.0.0.1 adremote.pathfinder.com
    127.0.0.1 localhost
    127.0.0.1 stats.surfaid.ihost.com
    127.0.0.1 track.directleads.com
    127.0.0.1 oz.valueclick.com
    127.0.0.1 ads.adorigin.com
    127.0.0.1 ads.clickagents.com
    127.0.0.1 st.clickagents.com
    127.0.0.1 ie.search.msn.com
    127.0.0.1 ads.adorigin.com
    127.0.0.1 banners.pennyweb.com
    127.0.0.1 ads.addynamix.com
    127.0.0.1 focusin.ads.targetnet.com
    127.0.0.1 toolbar.aol.com
    127.0.0.1 stats.surfaid.ihost.com
    127.0.0.1 adfarm.mediaplex.com
    127.0.0.1 mediaplex.com
    127.0.0.1 www.looksmart.com
    127.0.0.1 zedo.net 11.zedo.net
    127.0.0.1 media.fastclick.net
    127.0.0.1 ads.x10.com
    127.0.0.1 www.netster.com
    127.0.0.1 endingtree.com
    127.0.0.1 srd.yahoo.com
    127.0.0.1 babel.altavista.com
    127.0.0.1 us.adserver.yahoo.com
    127.0.0.1 tscnads2.telescan.com
    127.0.0.1 view.atdmt.com
    127.0.0.1 spd.atdmt.com
    127.0.0.1 ads-direct.prodigy.net
    127.0.0.1 adserver.prodigy.net
    127.0.0.1 pclick.yahoo.com
    127.0.0.1 myclicks.prodigy.net
    127.0.0.1 chkpt.zdnet.com
    127.0.0.1 servedfor.valuead.com
    127.0.0.1 banners.valuead.com
    127.0.0.1 z1.adserver.com
    127.0.0.1 ads.ad-flow.com
    127.0.0.1 citi.bridgetrack.com
    127.0.0.1 mds.centrport.net
    127.0.0.1 wb.coolsavings.com
    127.0.0.1 c1.zedo.com
    127.0.0.1 l5.zedo.com
    127.0.0.1 c4.zedo.com
    127.0.0.1 ad.linksynergy.com
    127.0.0.1 ads.web.aol.com
    127.0.0.1 sc.musicmatch.com
    127.0.0.1 sc.musicmatch.com
    127.0.0.1 windowsmedia.com
    127.0.0.1 rd.intl.doubleclick.net
    127.0.0.1 ad.uk.doubleclick.net
    127.0.0.1 adlog.com.com
    127.0.0.1 www.vibrantmedia.com
    127.0.0.1 promo.search.com.com
    127.0.0.1 ads.com.com
    127.0.0.1 tribalfusion.speedera.net
    127.0.0.1 ads.specificpop.com
    127.0.0.1 static.smni.com
    127.0.0.1 ad.smni.com

    Reply
  • Still not working on Windows XP

    Posted by Legacy on 02/18/2003 12:00am

    Originally posted by: Jase Jennings

    Despite the recent code update, this still does not work on Windows XP (at least not on mine anyway) ...

    Error :

    Ioctl() failed, errno=10022

    Which is a shame, because i found the article interesting and was looking forward to getting my hands dirty with this. Is it fixable ?

    Jase

    Reply
  • Speed Issue

    Posted by Legacy on 01/14/2003 12:00am

    Originally posted by: Sirsha

    How to increase the speed of modem booster

    Reply
  • Give an example of a filter--do you mean this?

    Posted by Legacy on 10/16/2002 12:00am

    Originally posted by: Bill SerGio

    // Your Filter goes here. Don't do too much processing,
    // or you may drop packets

    If you are adding these domains to the local hosts file, WHY would you add any filter here? Please give an example of a filter you might add??????

    Thanks!

    Reply
  • Easy fix in most cases

    Posted by Legacy on 10/08/2002 12:00am

    Originally posted by: Pete

    There are a few ad sites that make up the majority. A real easy way to address this problem is to look at the source for the web pages. Figure out where they're getting the ads from, such as ads.doubleclick.net, various akamai.net servers, etc.

    Next, open up the hosts file (on my system it's in c:\winnt\system32\drivers\etc\). Ad each of these DNS entries and map it to 127.0.0.1

    Goodbye ads, hello speed. I have maybe 12 entries and it's made a huge difference. Cool program, though.

    Reply
  • Good.

    Posted by Legacy on 10/07/2002 12:00am

    Originally posted by: Kandukondein

    Very informative.

    Reply
  • drop packets

    Posted by Legacy on 10/05/2002 12:00am

    Originally posted by: Nathan

    How do i know if packets are being dropped?

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

  • A help desk is critical to the operations of an IT services business. As a centralized intake location for technical issues, it allows for a responsive and timely solution to get clients and their staff back to business as usual. In addition to handling immediate IT issues, a help desk performs several proactive tasks to ensure clients' IT systems remain operational and downtime is minimized. Thus, utilizing a help desk and following best practices can improve the productivity, efficiency and satisfaction of …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds