Annotation of q_a/samples/ddk/macaddr/macaddr.c, revision 1.1

1.1     ! root        1: /*++
        !             2: 
        !             3: Copyright (c) 1993  Microsoft Corporation
        !             4: 
        !             5: Module Name:
        !             6: 
        !             7:     macaddr.c
        !             8: 
        !             9: Abstract:
        !            10: 
        !            11:     This is a Win32 program used to access a MAC driver to query stats.
        !            12: 
        !            13: Author:
        !            14: 
        !            15:     Robert Nelson (RobertN) 22-Sep-1993.
        !            16: 
        !            17: Environment:
        !            18: 
        !            19:     Win32 user mode.
        !            20: 
        !            21: Notes:
        !            22: 
        !            23: Revision History:
        !            24: 
        !            25: --*/
        !            26: 
        !            27: 
        !            28: //
        !            29: // Include files
        !            30: //
        !            31: 
        !            32: #include <windows.h>
        !            33: #include <stdio.h>
        !            34: #include <stdlib.h>
        !            35: #include <winioctl.h>
        !            36: #include "ntddndis.h"        // This defines the IOCTL constants.
        !            37: 
        !            38: #define DEVICE_PREFIX   "\\\\.\\"
        !            39: 
        !            40: int
        !            41: main(
        !            42:     int argc,
        !            43:     char ** argv
        !            44:     )
        !            45: {
        !            46:     
        !            47:     UCHAR       LinkName[512];
        !            48:     UCHAR       DeviceName[80];
        !            49:     UCHAR       szMACFileName[80];
        !            50:     UCHAR       OidData[4096];
        !            51:     NDIS_OID    OidCode;
        !            52:     BOOLEAN     bCreatedDevice;
        !            53:     DWORD       ErrorNumber;
        !            54:     DWORD       ReturnedCount;
        !            55:     HANDLE      hMAC;
        !            56: 
        !            57:     //
        !            58:     // Check to make sure we got the right number of arguments before we
        !            59:     // create any devices.
        !            60:     //
        !            61: 
        !            62:     if (argc != 2)
        !            63:     {
        !            64:         printf("usage: macdmp <device>\n");
        !            65:         return(1);
        !            66:     }
        !            67: 
        !            68:     //
        !            69:     // Check to see if the DOS name for the MAC driver already exists.
        !            70:     // Its not created automatically in version 3.1 but may be later.
        !            71:     //
        !            72: 
        !            73:     if (QueryDosDevice(argv[1], LinkName, sizeof(LinkName)) == 0)
        !            74:     {
        !            75:         if ((ErrorNumber = GetLastError()) == ERROR_FILE_NOT_FOUND)
        !            76:         {
        !            77:             strcpy(DeviceName, "\\Device\\");
        !            78:             strcat(DeviceName, argv[1]);
        !            79: 
        !            80:             //
        !            81:             // It doesn't exist so create it.
        !            82:             //
        !            83:             if (!DefineDosDevice( DDD_RAW_TARGET_PATH, argv[1], DeviceName))
        !            84:             {
        !            85:                 printf(
        !            86:                     "DefineDosDevice returned an error creating the device = %d\n",
        !            87:                     GetLastError()
        !            88:                     );
        !            89:                 return(1);
        !            90:             }
        !            91:             bCreatedDevice = TRUE;
        !            92:         }
        !            93:         else
        !            94:         {
        !            95:             printf("QueryDosDevice returned an error = %d\n", GetLastError());
        !            96:             return(1);
        !            97:         }
        !            98:     }
        !            99:     else
        !           100:     {
        !           101:         bCreatedDevice = FALSE;
        !           102:     }
        !           103: 
        !           104:     //
        !           105:     // Construct a device name to pass to CreateFile
        !           106:     //
        !           107:     strcpy(szMACFileName, DEVICE_PREFIX);
        !           108:     strcat(szMACFileName, argv[1]);
        !           109: 
        !           110:     hMAC = CreateFile(
        !           111:                 szMACFileName,
        !           112:                 GENERIC_READ,
        !           113:                 FILE_SHARE_READ | FILE_SHARE_WRITE,
        !           114:                 NULL,
        !           115:                 OPEN_EXISTING,
        !           116:                 0,
        !           117:                 INVALID_HANDLE_VALUE
        !           118:                 );
        !           119: 
        !           120:     if (hMAC != INVALID_HANDLE_VALUE)
        !           121:     {
        !           122:         //
        !           123:         // We successfully opened the driver, format the IOCTL to pass the
        !           124:         // driver.
        !           125:         //
        !           126: 
        !           127:         OidCode = OID_802_3_CURRENT_ADDRESS;
        !           128: 
        !           129:         if (DeviceIoControl(
        !           130:                 hMAC,
        !           131:                 IOCTL_NDIS_QUERY_GLOBAL_STATS,
        !           132:                 &OidCode,
        !           133:                 sizeof(OidCode),
        !           134:                 OidData,
        !           135:                 sizeof(OidData),
        !           136:                 &ReturnedCount,
        !           137:                 NULL
        !           138:                 ))
        !           139:         {
        !           140:             if (ReturnedCount == 6)
        !           141:             {
        !           142:                 printf(
        !           143:                     "Mac address = %02.2X-%02.2X-%02.2X-%02.2X-%02.2X-%02.2X\n",
        !           144:                     OidData[0], OidData[1], OidData[2], OidData[3],
        !           145:                     OidData[4], OidData[5], OidData[6], OidData[7]
        !           146:                     );
        !           147:             }
        !           148:             else
        !           149:             {
        !           150:                 printf(
        !           151:                     "DeviceIoControl returned an invalid count = %d\n",
        !           152:                     ReturnedCount
        !           153:                     );
        !           154:             }
        !           155:         }
        !           156:         else
        !           157:         {
        !           158:             printf("DeviceIoControl returned an error = %d\n", GetLastError());
        !           159:         }
        !           160:     }
        !           161:     else
        !           162:     {
        !           163:         printf("CreateFile returned an error = %d\n", GetLastError());
        !           164:     }
        !           165: 
        !           166:     if (bCreatedDevice)
        !           167:     {
        !           168:         //
        !           169:         // The MAC driver wasn't visible in the Win32 name space so we created
        !           170:         // a link.  Now we have to delete it.
        !           171:         //
        !           172:         if (!DefineDosDevice(
        !           173:                 DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION |
        !           174:                     DDD_EXACT_MATCH_ON_REMOVE,
        !           175:                 argv[1],
        !           176:                 DeviceName)
        !           177:                 )
        !           178:         {
        !           179:             printf(
        !           180:                 "DefineDosDevice returned an error removing the device = %d\n",
        !           181:                 GetLastError()
        !           182:                 );
        !           183:             return(1);
        !           184:         }
        !           185:     }
        !           186:     
        !           187:     return(0);
        !           188: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.