📄 rt_user.c
字号:
//=====================================================================//// rtuser:// 1. User space application to demo how to use IOCTL function.// 2. Most of the IOCTL function is defined as "CHAR" type and return with string message.// 3. Use sscanf to get the raw data back from string message.// 4. The command format "parameter=value" is same as iwpriv command format.// 5. Remember to insert driver module and bring interface up prior execute rtuser.// change folder path to driver "Module"// dos2unix * ; in case the files are modified from other OS environment// chmod 644 *// chmod 755 Configure// make config// make// insmod RT273.ko// ifconfig mesh0 up//// Refer linux/if.h to have // #define ifr_name ifr_ifrn.ifrn_name /* interface name *///// Make:// cc -Wall -ortuser rtuser.c//// Run:// ./rtuser OID////=====================================================================#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <unistd.h> /* for close */#include <linux/wireless.h>//=============================================================================#if WIRELESS_EXT <= 11#ifndef SIOCDEVPRIVATE#define SIOCDEVPRIVATE 0x8BE0#endif#define SIOCIWFIRSTPRIV SIOCDEVPRIVATE#endif////SET/GET CONVENTION :// * ------------------// * Simplistic summary :// * o even numbered ioctls are SET, restricted to root, and should not// * return arguments (get_args = 0).// * o odd numbered ioctls are GET, authorised to anybody, and should// * not expect any arguments (set_args = 0).//#define RT_PRIV_IOCTL (SIOCIWFIRSTPRIV + 0x0E)#define RTPRIV_IOCTL_SET (SIOCIWFIRSTPRIV + 0x02)#define RTPRIV_IOCTL_BBP (SIOCIWFIRSTPRIV + 0x03)#define RTPRIV_IOCTL_MAC (SIOCIWFIRSTPRIV + 0x05)#define RTPRIV_IOCTL_E2P (SIOCIWFIRSTPRIV + 0x11)#define RTPRIV_IOCTL_STATISTICS (SIOCIWFIRSTPRIV + 0x09)#define RTPRIV_IOCTL_GSITESURVEY (SIOCIWFIRSTPRIV + 0x0D)#define OID_GET_SET_TOGGLE 0x8000#define RT_QUERY_ATE_TXDONE_COUNT 0x0401#define RT_QUERY_SIGNAL_CONTEXT 0x0402#define RT_SET_APD_PID (OID_GET_SET_TOGGLE + 0x0405)#define RT_SET_DEL_MAC_ENTRY (OID_GET_SET_TOGGLE + 0x0406)//---------------------------------------------------------// Mesh Extension#define OID_802_11_MESH_SECURITY_INFO 0x0651#define OID_802_11_MESH_ID 0x0652#define OID_802_11_MESH_AUTO_LINK 0x0653#define OID_802_11_MESH_LINK_STATUS 0x0654#define OID_802_11_MESH_LIST 0x0655#define OID_802_11_MESH_ROUTE_LIST 0x0656#define OID_802_11_MESH_ADD_LINK 0x0657#define OID_802_11_MESH_DEL_LINK 0x0658#define OID_802_11_MESH_MAX_TX_RATE 0x0659#define OID_802_11_MESH_CHANNEL 0x065A#define RT_OID_802_11_MESH_SECURITY_INFO (OID_GET_SET_TOGGLE + OID_802_11_MESH_SECURITY_INFO)#define RT_OID_802_11_MESH_ID (OID_GET_SET_TOGGLE + OID_802_11_MESH_ID)#define RT_OID_802_11_MESH_AUTO_LINK (OID_GET_SET_TOGGLE + OID_802_11_MESH_AUTO_LINK)#define RT_OID_802_11_MESH_ADD_LINK (OID_GET_SET_TOGGLE + OID_802_11_MESH_ADD_LINK)#define RT_OID_802_11_MESH_DEL_LINK (OID_GET_SET_TOGGLE + OID_802_11_MESH_DEL_LINK)#define RT_OID_802_11_MESH_MAX_TX_RATE (OID_GET_SET_TOGGLE + OID_802_11_MESH_MAX_TX_RATE)#define RT_OID_802_11_MESH_CHANNEL (OID_GET_SET_TOGGLE + OID_802_11_MESH_CHANNEL)//---------------------------------------------------------#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif#define MAC_ADDR_LEN 6#define ETH_LENGTH_OF_ADDRESS 6#define MAX_LEN_OF_MAC_TABLE 64#define MAX_MESH_ID_LEN 32#define MAX_NEIGHBOR_NUM 64#define MAX_MESH_LINK_NUM 4#define MAX_HOST_NAME_LEN 32#define PACKED __attribute__ ((packed))//---------------------------------------------------------// Mesh definition#define MESH_MAX_LEN_OF_FORWARD_TABLE 48typedef struct _RT_MESH_ROUTE_ENTRY{ unsigned char MeshDA[MAC_ADDR_LEN]; unsigned long Dsn; unsigned char NextHop[MAC_ADDR_LEN]; unsigned long Metric;} RT_MESH_ROUTE_ENTRY, *PRT_MESH_ROUTE_ENTRY;typedef struct _RT_MESH_ROUTE_TABLE{ unsigned char Num; RT_MESH_ROUTE_ENTRY Entry[MESH_MAX_LEN_OF_FORWARD_TABLE];} RT_MESH_ROUTE_TABLE, *PRT_MESH_ROUTE_TABLE;typedef struct PACKED _MESH_SECURITY_INFO{ unsigned long Length; // Length of this structure unsigned char EncrypType; // indicate Encryption Type unsigned char KeyIndex; // Default Key Index unsigned char KeyLength; // length of key in bytes unsigned char KeyMaterial[64]; // the key Material} MESH_SECURITY_INFO, *PMESH_SECURITY_INFO;typedef struct _MESH_NEIGHBOR_ENTRY_INFO{ char Rssi; unsigned char HostName[MAX_HOST_NAME_LEN]; unsigned char MacAddr[MAC_ADDR_LEN]; unsigned char MeshId[MAX_MESH_ID_LEN]; unsigned char Channel; unsigned char Status; //0:idle, 1:connected. unsigned char MeshEncrypType;} MESH_NEIGHBOR_ENTRY_INFO, *PMESH_NEIGHBOR_ENTRY_INFO;typedef struct _MESH_NEIGHBOR_INFO{ unsigned char num; MESH_NEIGHBOR_ENTRY_INFO Entry[MAX_NEIGHBOR_NUM];} MESH_NEIGHBOR_INFO, *PMESH_NEIGHBOR_INFO;typedef struct _MESH_LINK_ENTRY_INFO{ unsigned char Status; char Rssi; unsigned char CurTxRate; unsigned char PeerMacAddr[MAC_ADDR_LEN];} MESH_LINK_ENTRY_INFO, *PMESH_LINK_ENTRY_INFO;typedef struct _MESH_LINK_INFO{ MESH_LINK_ENTRY_INFO Entry[MAX_MESH_LINK_NUM];} MESH_LINK_INFO, *PMESH_LINK_INFO;//---------------------------------------------------------//=============================================================================int main( int argc, char ** argv ){#define DATA_BUFFER_SIZE 8192 char name[25]; int socket_id; struct iwreq wrq; int ret = 1, cmd = 0; char *base;//, *base1; char *data; // open socket based on address family: AF_NET ---------------------------- socket_id = socket(AF_INET, SOCK_DGRAM, 0); if(socket_id < 0) { printf("\nrtuser::error::Open socket error!\n\n"); return -1; } data = malloc(DATA_BUFFER_SIZE); if (data == NULL) { printf("unable to alloc data buffer, size (%d)\n", DATA_BUFFER_SIZE); return -1; } // set interface name as "mesh0" -------------------------------------------- sprintf(name, "mesh0"); memset(data, 0, DATA_BUFFER_SIZE); // //example of ioctl function ========================================== // base = argv[1]; //base1 = argv[2]; if(argc != 2) goto rtuser_exit; if(strstr(base, "OID_802_11_MESH_ROUTE_LIST")) cmd = OID_802_11_MESH_ROUTE_LIST; else if(strstr(base, "RT_OID_802_11_MESH_SECURITY_INFO")) cmd = RT_OID_802_11_MESH_SECURITY_INFO; else if(strstr(base, "OID_802_11_MESH_SECURITY_INFO")) cmd = OID_802_11_MESH_SECURITY_INFO; else if(strstr(base, "RT_OID_802_11_MESH_ID")) cmd = RT_OID_802_11_MESH_ID; else if(strstr(base, "OID_802_11_MESH_ID")) cmd = OID_802_11_MESH_ID; else if(strstr(base, "RT_OID_802_11_MESH_AUTO_LINK")) cmd = RT_OID_802_11_MESH_AUTO_LINK; else if(strstr(base, "OID_802_11_MESH_AUTO_LINK")) cmd = OID_802_11_MESH_AUTO_LINK; else if(strstr(base, "RT_OID_802_11_MESH_ADD_LINK")) cmd = RT_OID_802_11_MESH_ADD_LINK; else if(strstr(base, "RT_OID_802_11_MESH_DEL_LINK")) cmd = RT_OID_802_11_MESH_DEL_LINK; else if(strstr(base, "OID_802_11_MESH_LINK_STATUS")) cmd = OID_802_11_MESH_LINK_STATUS; else if(strstr(base, "OID_802_11_MESH_LIST")) cmd = OID_802_11_MESH_LIST; else if (strstr(base, "RT_OID_802_11_MESH_CHANNEL")) cmd = RT_OID_802_11_MESH_CHANNEL; else if (strstr(base, "OID_802_11_MESH_CHANNEL")) cmd = OID_802_11_MESH_CHANNEL; else if (strstr(base, "RT_OID_802_11_MESH_MAX_TX_RATE")) cmd = RT_OID_802_11_MESH_MAX_TX_RATE; else if (strstr(base, "OID_802_11_MESH_MAX_TX_RATE")) cmd = OID_802_11_MESH_MAX_TX_RATE; switch(cmd) { case OID_802_11_MESH_ROUTE_LIST: { RT_MESH_ROUTE_TABLE *sp; int i; //Get Mesh Route List printf("\nrtuser::get OID_802_11_MESH_ROUTE_LIST \n\n"); memset(data, 0, sizeof(RT_MESH_ROUTE_TABLE)); strcpy(wrq.ifr_name, name); wrq.u.data.length = sizeof(RT_MESH_ROUTE_TABLE); wrq.u.data.pointer = data; wrq.u.data.flags = OID_802_11_MESH_ROUTE_LIST; ret = ioctl(socket_id, RT_PRIV_IOCTL, &wrq); if(ret != 0) { printf("\nrtuser::error::get OID_802_11_MESH_ROUTE_LIST\n\n"); goto rtuser_exit; } sp = (RT_MESH_ROUTE_TABLE *)wrq.u.data.pointer; printf("\n===== MESH_ROUTE_TABLE =====\n\n"); for(i = 0; i < sp->Num; i++) { printf("Dest Mesh Addr = %02x:%02x:%02x:%02x:%02x:%02x\n", sp->Entry[i].MeshDA[0], sp->Entry[i].MeshDA[1], sp->Entry[i].MeshDA[2], sp->Entry[i].MeshDA[3], sp->Entry[i].MeshDA[4], sp->Entry[i].MeshDA[5]); printf("Dsn = %ld\n", sp->Entry[i].Dsn); printf("NextHop Addr = %02x:%02x:%02x:%02x:%02x:%02x\n", sp->Entry[i].NextHop[0], sp->Entry[i].NextHop[1], sp->Entry[i].NextHop[2], sp->Entry[i].NextHop[3], sp->Entry[i].NextHop[4], sp->Entry[i].NextHop[5]); printf("Metric = %ld\n\n", sp->Entry[i].Metric); } } break; case OID_802_11_MESH_SECURITY_INFO: { PMESH_SECURITY_INFO pMeshSecurity; // Get Mesh Security printf("\nrtuser::OID_802_11_MESH_SECURITY_INFO \n\n"); memset(data, 0, sizeof(MESH_SECURITY_INFO)); strcpy(wrq.ifr_name, name); wrq.u.data.length = sizeof(MESH_SECURITY_INFO); wrq.u.data.pointer = data; wrq.u.data.flags = OID_802_11_MESH_SECURITY_INFO; ret = ioctl(socket_id, RT_PRIV_IOCTL, &wrq); if(ret != 0) { printf("\nrtuser::error::get mesh security\n\n"); goto rtuser_exit; } pMeshSecurity = (PMESH_SECURITY_INFO)wrq.u.data.pointer; // Show Encryption Type switch(pMeshSecurity->EncrypType) { case 0: printf("Mesh Encryption is OPEN-NONE\n\n"); break; case 1: printf("Mesh Encryption is OPEN-WEP\n\n"); break; case 2: printf("Mesh Encryption is WPANONE-TKIP\n\n"); break; case 3: printf("Mesh Encryption is WPANONE-AES\n\n"); break; default: printf("Unsupported Mesh Encryption Type\n\n"); break; } // Show key, key length and key index if (pMeshSecurity->KeyLength > 0) { printf("Key=%s, length=%d\n", pMeshSecurity->KeyMaterial, pMeshSecurity->KeyLength); printf("The key Index = %d \n", pMeshSecurity->KeyIndex); } } break; case RT_OID_802_11_MESH_SECURITY_INFO: { MESH_SECURITY_INFO MeshSecurity; // Please modify the below parameter to set security // EncrypType, KeyIndex and MeshKey unsigned char EncrypType = 0; // 0:OPEN-NONE, 1:OPEN-WEP, 2:WPANONE-TKIP, 3:WPANONE-AES unsigned char KeyIndex = 1; // Default key index const char *MeshKey = "12345678"; // Key material // Set Mesh Security printf("\nrtuser::set mesh security \n\n"); memset(&MeshSecurity, 0, sizeof(MESH_SECURITY_INFO)); // OPEN-WEP mode if (EncrypType == 1) { if ((strlen(MeshKey) == 5) || (strlen(MeshKey) == 13) || (strlen(MeshKey) == 10) || (strlen(MeshKey) == 26)) { printf("set security as OPEN-WEP\n\n"); MeshSecurity.KeyLength = strlen(MeshKey); memcpy(MeshSecurity.KeyMaterial, MeshKey, MeshSecurity.KeyLength); } else { printf("set mesh security::invalid WEP key and set security as OPEN-NONE\n\n"); EncrypType = 0; } } else if ((EncrypType == 2) || (EncrypType == 3)) { // The WPA key must be 8~64 characters if ((strlen(MeshKey) >= 8) && (strlen(MeshKey) <= 64)) { printf("set security as %s\n\n", (EncrypType == 2) ? "WPANONE-TKIP" : "WPANONE-AES"); MeshSecurity.KeyLength = strlen(MeshKey); memcpy(MeshSecurity.KeyMaterial, MeshKey, MeshSecurity.KeyLength); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -