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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read