The Wayback Machine - https://web.archive.org/web/20080420164448/http://www.codeguru.com:80/cpp/i-n/network/winsocksolutions/article.php/c6165/

Click Here

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Transfer Large Files
KVM Switches
Compare Prices
Logo Design
Economy Server Racks
Prepaid Phone Card
Shop Online
Corporate Awards
Dental Insurance
Desktop Computers
Domain registration
Online Shopping
Corporate Gifts
GPS


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Click Here
Home >> Visual C++ / C++ >> Internet & Networking >> Network Protocols >> Winsock Solutions

Deploying SharePoint 2007? F5�s BIG-IP LTM with WebAccelerator can speed it up by 8x! Read the Hewlett Packard white paper to learn how to avoid lost productivity caused by network performance issues.

Speeding Up Internet Browsing
Rating: none

Bill Nolde (view profile)
May 22, 2003

Environment:  VC6, WIN32


(continued)



  Heroes Happen Here
New products, new technologies: Microsoft SQL Server 2008, Microsoft Visual Studio 2008, and Windows Server 2008 create new opportunities to use your existing skills and to grow your business. »


  Tips for Growing Your Microsoft Practice
You can grow your practice by increasing business with existing clients or adding more customers, expanding your practice areas or expanding your business into hosted or managed services. Discover which approach to growing your business makes the most sense for you and your customers. »


  Drive Your Business with Microsoft
Explore the business opportunities available through the Microsoft Partner Program, which is designed to help you generate leads, drive customer demand and sales, increase your profitability, and assess your business performance. »
  Partner-to-Partner Networking: A New Paradigm in Relationship Building
Heres a great way to build your business: Team up with partners whose solutions and services work as an extension of your own offerings. View this Webcast to learn how building networks with other partners can help your business grow. »
  IT Provider Grows Profits 85% in One Year with Microsoft Technologies
Knight Enterprises Inc., a longtime Novell partner, chose to add Microsoft technologies to its offerings after attending a Microsoft partner event. The company's major accounts soon followed suit, opting for Microsoft solutions over those of the competition. Download this case study to discover how Knight now expects its revenue to double in two years time. »


(
Full Size Image)

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

  • DnsWatch20Src.zip - Download source code - 22 Kb
  • dnswatch20.zip - Download project files - 98 Kb

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed

    Is it time to make your move to the multi-threaded and parallel processing world? Find out!
    Learn Tools & Techniques to Justify and Fund Your IT Investments. Download Complimentary Report Now!
    Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
    Whitepaper: Embeddable Content Platform for OEM's
    Elementool PDF: Bug Tracking Saves Hundreds of Hours a Month in Software Development Time


  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    Excellent technical article !!!! - Legacy CodeGuru (05/27/2003)
    WARNING: Your Code Can Crash Computers! - Legacy CodeGuru (05/23/2003)
    Good Program - Legacy CodeGuru (02/24/2003)
    Still not working on Windows XP - Legacy CodeGuru (02/18/2003)
    Speed Issue - Legacy CodeGuru (01/14/2003)

    View All Comments
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)


    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Solutions
    Whitepapers and eBooks
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    Avaya Article: How to Begin Developing with Avaya's Event Processing Language
    Avaya Article: Deep Dive--Event Processor Offers Familiar Tools for Creating Event-Driven SOA Apps
    Intel Whitepaper: A Methodology for Threading Serial Applications (PDF)
    HP eBook: Storage Networking , Part 1
    ServerWatch.com Article: Tip of the Trade--IP Address Management
    ITChannelPlanet.com Article: Enterprise Fixed-Mobile Convergence Can Be Lucrative
    Intel Article: Transitioning Software to Future Generations of Multi-Core
    Hoover's Whitepaper Series: A Guide to Marketing in the Age of Social Media
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    Internet.com eBook: All About Botnets
    Intel Article: 8 Simple Rules for Designing Threaded Applications
    Intel Whitepaper: Best Practices for Developing and Optimizing Threaded Applications
    Intel Article: The Challenges of Developing Multithreaded Processing Pipelines
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Foundations of Parallel Programming, Part One
    HP Video: Page Cost Calculator
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Intel Video: Three Must-Knows for Parallelism
    Microsoft Partner Program Video: The Secrets to Partner Success
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    How-to-Article: How to Market Your Technology Solutions
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES