Using the CSA in a DHCP Network

677 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
677
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Using the CSA in a DHCP Network

  1. 1. Application Note 2004-014A Using the CSA in a DHCP Network Overview This application note describes how to use the CSA in a DHCP network. This document requires a minimal knowledge of DHCP networking in order to successfully configure the CSA. Summary The CSA supports a networking technology known as DHCP (Dynamic Host Configuration Protocol), which allows clients on the network to receive an IP address from the local network server. Most of today’s local area networks (LAN’s) use this type of protocol. This document will explain how to install, configure, and successfully use this feature of the CSA. A more technical explanation of how to broadcast on the network for DHCP CSAs is also included. Installation The dBm Optics CSA Locator is an optional Windows® application that can be installed to help communication between your CSA and computer. This application finds all the CSA’s that your computer has access to including GPIB, RS232 and any that would be connected via a LAN using TCP/IP. This application requires that the CSA has at least firmware version 1.6.5 and is available in the applications section of version 1.03+ of the Customer CD, and from the dBm Optics website (www.dbmoptics.com) under downloads. The install application for the CSA Locator will step you through the installation process for the Locator. Configuration After the firmware has been upgraded to version 1.6.5 or greater, the DHCP networking option must be activated before DHCP will function. The DHCP networking option is available by selecting the System | Communications Setup | Ethernet Setup panel. Once the DHCP option is selected the CSA will automatically contact the local DHCP server to obtain a valid IP address. If a DHCP server is not running on the network then the CSA will be unable to grab a valid IP address and the address will default to 192.168.0.222. The CSA Locator can be used from a PC in order to check that the CSA has properly obtained an IP address from the DHCP server. Usage DHCP communication is a basic component of modern networks, which reduces the amount of time IT professionals must spend assigning IP addresses. This will explain the concept of dynamic addresses, how to obtain a DHCP CSA’s IP address, and what will cause a DHCP server to change the CSA’s address. DHCP CSAs Have Dynamic Addresses CSAs that are configured to use DHCP have a dynamic IP address; this means that the IP address can change without any user input. Applications designed to contact a CSA on a DHCP network should broadcast for the CSA, use the Unique Identifier to pinpoint the requested CSA, and should not rely on a fixed address since the address could change. For networks that have infinite assignment times for IP addresses this is not necessary because the first address the CSA picks up from the DHCP server will remain in effect indefinitely.
  2. 2. Obtaining the DHCP CSA’s Address from a Computer In order to obtain the address of a CSA on a DHCP network you can use the CSA Locator to search the local computer and network for CSAs. If the CSA has not properly obtained an address then the CSA Locator will display the CSA as a grayed item unless the default address of 192.168.0.222 is valid on the local network. The address the CSA has obtained can be found in the “Address” column of the listing if the Details view mode is activated. Windows 2000 Example of the dBm Optics CSA Locator When the DHCP CSA’s Address Will Change The displayed address can be used as long as the CSA is assigned the same IP address from the DHCP server, on most networks this is between three and five days. If the address is obtained just before the turnover point or an application is used during the turnover, it is possible for communications to be lost since the address may become invalid. This will rarely be the case because the DHCP server usually has no need to change the address. The address will only change if network circumstances have changed since the last assignment. Some examples of circumstances that may trigger an address change are: • A DHCP incompatible device uses the address assigned to the CSA. • The DHCP server runs out of addresses in an address block and is forced to move devices into another block. • The DHCP server’s address table becomes corrupt and must be rebuilt. Application Note 2004-014A Page 2 of 2
  3. 3. • The DHCP server administrator clears the address table or removes the address block in which the CSA is located. • While there are many situations that can trigger an address change, it is actually quite rare. It is best to prepare the program to re-broadcast on the network for the CSA if communications are lost. Broadcasting on the Network for DHCP CSAs This portion of the document includes a brief overview of how to contact the CSA on a DHCP network and includes a C programming example that utilizes the Windows Sockets API to broadcast for a CSA. For a LabVIEW example of the same process the dBm Optics LabVIEW Driver includes a VI (ENET CSA Locator.vi) that shows how to broadcast for CSAs. How a Broadcast for CSAs is Performed Several steps are performed in order to obtain the IP address and unique information about a CSA on your network. First, an application sends out a broadcast message over the network that is formatted so that only CSAs will respond. CSAs that receive this message will send a broadcast message back across the network to the computer querying for CSAs. This message must be broadcast back instead of targeted because of a bug in the Windows Sockets API that reports the wrong IP address when the sending computer has more than one network card. This is all that is truly necessary to obtain the IP address and unique information from the CSA; however, it is a good idea to ensure that the CSA is on the same subnet as the sending computer. In order to check that the CSA is on the same subnet as the computer, a targeted message can be sent to the CSA IP address (found with the broadcast process). Targeted messages can only be sent to devices on the same subnet, therefore the CSA will only respond if it is on the same subnet as the computer. If the CSA does not respond an error message can be displayed to inform the user that the CSA is on the wrong subnet, most networks must have the first two or three fields of the IP address match (or be close) in order for the CSA to be contacted. On DHCP networks the DHCP server will handle the IP address so that it is within the acceptable subnet as the computer. A Simple Broadcast Example This example shows how the Windows Sockets API can be used to broadcast a locator message across the network. Please note that the example is multi-threaded and uses a window with the identifier ID_MAIN_DIALOG and a ListView control with identifier ID_CSAs_LISTVIEW. #include <windows.h> //windows functions #include <winsock.h> //windows socket functions (networking) #include <commctrl.h> //common controls functions (for ListView control) #include <time.h> //time functions (timeout check) #include "resource.h" //ID codes for resource items /* external functions (stdio.h) */ int _RTLENTRY _EXPFUNC sprintf(char * __buffer, const char * __format, ...); int _RTLENTRY _EXPFUNC sscanf(const char * __buffer, const char * __format, ...); /* local functions */ BOOL CALLBACK MainWindowProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam); DWORD WINAPI BroadcastForCSA(void *_m); DWORD WINAPI recv_threaded(void *_m); Application Note 2004-014A Page 3 of 3
  4. 4. HWND hwndMain; //main window HWND hwndListView; //ListView control int currItem = 0; //number of CSAs found #pragma argsused BOOL CALLBACK MainWindowProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) { DWORD nThreadID, nThreadID2; char mySN[1024]; if(uiMsg == WM_INITDIALOG) { return TRUE; } else if(uiMsg == WM_COMMAND) { if(wParam == 2) { // Escape key SendMessage(hDlg, WM_CLOSE, 0, 0); } } else if (uiMsg == WM_CLOSE) { DestroyWindow(hDlg); return TRUE; } else if (uiMsg == WM_DESTROY) { PostQuitMessage(0); } return FALSE; } #pragma argsused int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WSADATA stWSAData; WORD wVersionRequested = MAKEWORD(1, 1); u_long P; int nRet; MSG msg; INITCOMMONCONTROLSEX iccData = {sizeof(INITCOMMONCONTROLSEX), ICC_LISTVIEW_CLASSES}; HANDLE threadHandle; DWORD nThreadID; LVCOLUMN lvc; char VersionTitle[100]; /* INIT WINSOCK */ nRet = WSAStartup(wVersionRequested, &stWSAData); if(nRet) { char err[100]; sprintf(err, "WSAStartup failed with error #%d, execution terminated.n",nRet); MessageBox(0, err, "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } /* INIT COMMON WINDOWS CONTROLS */ InitCommonControlsEx(&iccData); /* INITIALIZE WINDOW */ hwndMain = CreateDialogParam(hInstance, MAKEINTRESOURCE(ID_MAIN_DIALOG), 0, (DLGPROC)MainWindowProc, (LPARAM) 0); Application Note 2004-014A Page 4 of 4
  5. 5. if(hwndMain == NULL) { MessageBox(0, "Error creating main window!", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } hwndListView = GetDlgItem(hwndMain, ID_CSAs_LISTVIEW); SetWindowLong(hwndListView, GWL_STYLE, (GetWindowLong(hwndListView, GWL_STYLE) & ~LVS_TYPEMASK) | LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SINGLESEL); SendMessage(hwndListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT); /* CREATE LISTVIEW NODES */ lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_ORDER; lvc.iSubItem = 0; lvc.iOrder = 0; lvc.cx = 80; lvc.pszText = "IP Address"; lvc.fmt = LVCFMT_LEFT; ListView_InsertColumn(hwndListView, 0, &lvc); lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_ORDER; lvc.iSubItem = 1; lvc.iOrder = 1; lvc.cx = 200; lvc.pszText = "Location"; lvc.fmt = LVCFMT_LEFT; ListView_InsertColumn(hwndListView, 1, &lvc); ShowWindow(hwndListView, TRUE); /* SET ICON */ SendMessage(hwndMain, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon(hInstance, "DBMLG")); SendMessage(hwndMain, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon(hInstance, "DBMSM")); /* SHOW WINDOW */ SetForegroundWindow(hwndMain); ShowWindow(hwndMain, TRUE); threadHandle = CreateThread(0, 0, BroadcastForCSA, (void*) NULL, 0, &nThreadID); /* wait for thread to start */ while(SetThreadPriority(threadHandle, THREAD_PRIORITY_NORMAL) == 0) {Sleep(100);} Sleep(100); /* HANDLE WINDOW MSGS */ while(GetMessage(&msg, NULL, 0, 0)) { if(!IsDialogMessage(hwndMain, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return 1; } Application Note 2004-014A Page 5 of 5
  6. 6. #define INSTRUMENT_PORT_SENDFROM 16207 #define INSTRUMENT_PORT_SENDTO 16208 #define INSTRUMENT_PORT_WAIT 16208 int Timeout = FALSE; #pragma argsused DWORD WINAPI BroadcastForCSA(void *_m) { struct sockaddr_in sndFrom, sndTo, addrWait; char sendbuffer[] = "LOCATE_INSTRUMENTrndBm Optics CSA Model 2004rn=>rn", myTotal[100]; SOCKET mySocket; time_t start, finish; double elapsed_time; HANDLE threadHandle; DWORD nThreadID; /* SENDING FROM SETTINGS */ sndFrom.sin_family = AF_INET; sndFrom.sin_addr.s_addr = INADDR_ANY; //Send from any (all) network card sndFrom.sin_port = htons(INSTRUMENT_PORT_SENDFROM); /* SENDING TO SETTINGS */ sndTo.sin_family = AF_INET; sndTo.sin_addr.s_addr = INADDR_BROADCAST; //Send to broadcast address (all computers) sndTo.sin_port = htons(INSTRUMENT_PORT_SENDTO); /* WAIT SETTINGS */ addrWait.sin_family = AF_INET; addrWait.sin_addr.s_addr = INADDR_ANY; //Wait for response from any address addrWait.sin_port = htons(INSTRUMENT_PORT_WAIT); mySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(mySocket == INVALID_SOCKET) { MessageBox(0, "Socket creation error! Search terminated.", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } if(bind(mySocket, (struct sockaddr *) &sndFrom, sizeof(sndFrom)) < 0) { MessageBox(0, "Socket bind() error! Search terminated.", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } if(connect(mySocket, (struct sockaddr *) &sndTo, sizeof(sndTo)) < 0) { MessageBox(0, "Socket connect() error! Search terminated.", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } if(send(mySocket, sendbuffer, strlen(sendbuffer) + 1, 0) < 0) { MessageBox(0, "Socket send() error! Search terminated.", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } closesocket(mySocket); mySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); Application Note 2004-014A Page 6 of 6
  7. 7. if(mySocket == INVALID_SOCKET) { MessageBox(0, "Socket creation error! Search terminated.", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } if(bind(mySocket, (struct sockaddr *) &addrWait, sizeof(addrWait)) < 0) { MessageBox(0, "Socket bind() error! Search terminated.", "FATAL ERROR!", MB_OK | MB_ICONSTOP); return FALSE; } threadHandle = CreateThread(0, 0, recv_threaded, (void*) mySocket, 0, &nThreadID); /* wait for thread to start */ while(SetThreadPriority(threadHandle, THREAD_PRIORITY_NORMAL) == 0) {Sleep(100);} Sleep(100); time(&start); while(!Timeout) { time(&finish); elapsed_time = difftime(finish, start); if(elapsed_time >= 5) { Timeout = TRUE; } } closesocket(mySocket); sprintf(myTotal, "%d CSA(s) locatedn", currItem); SetWindowText(GetDlgItem(hwndMain, ID_LOOK_TEXT), myTotal); return TRUE; } DWORD WINAPI recv_threaded(void *_m) { SOCKET mySocket = (SOCKET) _m; char *servaddr, buffer[1024], buffer2[100], trash[100]; struct sockaddr_in name; struct in_addr address; int n, namelen = sizeof(name); LVITEM lvI; while(!Timeout) { n = recvfrom(mySocket, buffer, sizeof(buffer), MSG_PARTIAL, (struct sockaddr *) &name, &namelen); if(n > 0) { memcpy(&address, &name.sin_addr, sizeof(address)); servaddr = inet_ntoa(address); if(servaddr == NULL) { //this msg is almost impossible to reach, the system must run out of memory first MessageBox(0, "An address was unobtainable (corrupt IP address structure).", "Winsock Bug!", MB_OK); } sscanf(buffer, "%[^n] %[^n] %s", &trash, &buffer2, &trash); if(buffer2[strlen(buffer2)-1] == 'r') { Application Note 2004-014A Page 7 of 7
  8. 8. buffer2[strlen(buffer2)-1] = '0'; } /* ADD LISTVIEW ITEM */ lvI.mask = LVIF_TEXT; lvI.iItem = currItem; lvI.iSubItem = 0; lvI.pszText = servaddr; ListView_InsertItem(hwndListView, &lvI); lvI.mask = LVIF_TEXT; lvI.iItem = currItem; lvI.iSubItem = 1; lvI.pszText = buffer2; SendMessage(hwndListView, LVM_SETITEM, 1, (LPARAM) (LPLVITEM) &lvI); currItem++; } } return TRUE; } © 2001, 2002, 2003 dBm Optics, Inc. dBm Optics, Inc. 300 South Public Road Lafayette, CO 80026 303-464-1919 www.dbmoptics.com Application Note 2004-014A Page 8 of 8

×