Set network APIs in linux

Source: Internet
Author: User
Tags socket error usleep

Set network APIs in linux

Recently, in the project process, due to the use of the system function in multiple threads, sometimes an inexplicable program terminates abnormally and finally decides to replace all the system functions, however, the function for setting the mac address failed many times. The reason is as follows:

1. there is no delay in writing the function to disable/enable the NIC. (generally, you may need to initialize the NIC. Therefore, if the function is continuously disabled, you can try to quickly switch the NIC under the embedded Linux Command Line, but it does not respond)

2. type conversion problem. (For convenience, sscanf is used to extract mac strings. If 8bit is used to extract % x, conversion fails. In the end, only 32bit can be used, and values can be assigned one by one)

# Include <stdio. h> # include <stdlib. h> # include <sys/ioctl. h> # include <string. h> # include <sys/types. h> # include <sys/socket. h> # include <net/if. h> # include <netinet/in. h> typedef signed char INT8S; typedef unsigned char INT8U; typedef signed short INT16S; typedef unsigned short INT16U; typedef signed int INT32S; typedef unsigned int INT32U; // add by lightd, 2014-06-04 // ======================================== ========== ==================================================================/// Function: ifconfig_ethx_down_API // Description: Disable the specified local NIC-eg: ifconfig eth0 down // Input: // Output: // Return: // Others: none // =================================================== ================================================== INT8S ifconfig_ethx_down_API (const INT8U * interface_name) {INT32S sock_fd; struct ifreq ifr; int selector; // if (interface_name = NULL) {fprintf (stdout, "% s: % D: args invalid! ", _ FUNCTION __, _ LINE _); return-1;} // disable loop if (strncmp (char *) interface_name, (char *) "lo", 2) = 0) {fprintf (stdout, "% s: % d: You can't pull down interface lo! ", _ FUNCTION __, _ LINE _); return 0;} sock_fd = socket (AF_INET, SOCK_DGRAM, 0); if (sock_fd <0) {fprintf (stdout, "% s: % d: socket failed! ", _ FUNCTION __, _ LINE _); return-2;} sprintf (ifr. ifr_name, "% s", interface_name); if (ioctl (sock_fd, SIOCGIFFLAGS, & ifr) <0) {fprintf (stdout, "% s: % d: ioctl failed 1! ", _ FUNCTION __, _ LINE _); return-3;} selector = IFF_UP; ifr. ifr_flags & = ~ Selector; if (ioctl (sock_fd, SIOCSIFFLAGS, & ifr) <0) {fprintf (stdout, "% s: % d: ioctl failed 2! ", _ FUNCTION __, _ LINE _); return-4;} close (sock_fd); return 0;} // add by lightd, 2014-06-04 // ======================================== ========================================================== ===// Function: ifconfig_ethx_up_API // Description: Enable the specified local NIC-eg: ifconfig eth0 up // Input: // Output: // Return: // Others: none // =================================================== ================================================== INT8S ifconfig_ethx_up_API (c Onst INT8U * interface_name) {INT32S sock_fd; struct ifreq ifr; int selector; // if (interface_name = NULL) {fprintf (stdout, "% s: % d: args invalid! ", _ FUNCTION __, _ LINE _); return-1;} sock_fd = socket (AF_INET, SOCK_DGRAM, 0); if (sock_fd <0) {fprintf (stdout, "% s: % d: create socket failed! ", _ FUNCTION __, _ LINE _); return-2;} sprintf (ifr. ifr_name, "% s", interface_name); if (ioctl (sock_fd, SIOCGIFFLAGS, & ifr) <0) {fprintf (stdout, "% s: % d: ioctl error 1 ", _ FUNCTION __, _ LINE _); return-3;} selector = (IFF_UP | IFF_RUNNING); ifr. ifr_flags | = selector; if (ioctl (sock_fd, SIOCSIFFLAGS, & ifr) <0) {fprintf (stdout, "% s: % d: ioctl error 2 ", _ FUNCTION __, _ LINE _); return-4;} close (sock_fd); retur N 0;} // add by lightd, 2014-06-04 // ======================================== ========================================================== ===// Function: setLocalMACAddr_API // Description: Set the MAC // Input: // Output: // Return: // Others: None // Test result of the local Nic: test result-no error occurred for 20 consecutive calls // ============================ ========================================================== ======= INT8S SetLocalMACAddr_API (const INT8U * interface_name, const INT8U * str_macadd R) {int ret; int sock_fd; struct ifreq ifr; INT32U mac2bit [6]; // if (interface_name = NULL | str_macaddr = NULL) {fprintf (stdout, "% s: % d: args invalid! ", _ FUNCTION __, _ LINE _); return-1 ;}// extract the mac format sscanf (char *) str_macaddr," % 02X: % 02X: % 02X: % 02X: % 02X: % 02X ", (INT8U *) & mac2bit [0], (INT8U *) & mac2bit [1], (INT8U *) & mac2bit [2], (INT8U *) & mac2bit [3], (INT8U *) & mac2bit [4], (INT8U *) & mac2bit [5]); sock_fd = socket (PF_INET, SOCK_DGRAM, 0); if (sock_fd <0) {perror ("socket error"); return-2;} // before mac is set, you must disable the corresponding NIC-otherwise, the ret = ifconfig_ethx_down_API (inter Face_name); if (ret <0) {fprintf (stdout, "% s: % d: close eth0 error", _ FUNCTION __, _ LINE __); return-3;} sleep (1); // wait for the NIC to close OK sprintf (ifr. ifr_ifrn.ifrn_name, "% s", interface_name); ifr. ifr_ifru.ifru_hwaddr.sa_family = 1; ifr. ifr_ifru.ifru_hwaddr.sa_data [0] = mac2bit [0]; ifr. ifr_ifru.ifru_hwaddr.sa_data [1] = mac2bit [1]; ifr. ifr_ifru.ifru_hwaddr.sa_data [2] = mac2bit [2]; ifr. ifr_ifru.ifru_hwaddr.sa_data [3] = Mac2bit [3]; ifr. ifr_ifru.ifru_hwaddr.sa_data [4] = mac2bit [4]; ifr. ifr_ifru.ifru_hwaddr.sa_data [5] = mac2bit [5]; ret = ioctl (sock_fd, SIOCSIFHWADDR, & ifr); if (ret! = 0) {perror ("set mac address erorr"); return-4;} close (sock_fd); ret = ifconfig_ethx_up_API (interface_name); if (ret <0) {fprintf (stdout, "% s: % d: open eth0 error! ", _ FUNCTION __, _ LINE _); return-5;} sleep (2); // wait for the NIC to open OK return 0;} // add by lightd, 2014-06-04 // ======================================== ========================================================== ===// Function: getLocalMACAddr_API // Description: Obtain the MAC address of the local specified Nic // Input: // Output: // Return: // Others: none // =================================================== ================================================== INT8S getLocalMACAddr_API (const INT8U * interfa Ce_name, INT8U * str_macaddr) {INT32S sock_fd; struct ifreq ifr_mac; // if (interface_name = NULL | str_macaddr = NULL) {fprintf (stdout, "% s: % d: args invalid! ", _ FUNCTION __, _ LINE _); return-1;} sock_fd = socket (AF_INET, SOCK_STREAM, 0); if (sock_fd =-1) {perror ("create socket failed"); sprintf (char *) str_macaddr, "00: 00: 00: 00: 00: 00"); return-2 ;} // specify the NIC memset (& ifr_mac, 0, sizeof (ifr_mac); sprintf (ifr_mac.ifr_name, "% s", interface_name ); // obtain the mac address of the specified Nic if (ioctl (sock_fd, SIOCGIFHWADDR, & ifr_mac) <0) {perror ("mac ioctl error"); sprintf (char *) str_macaddr, "00: 00: 00: 00: 00: 00"); return-3;} close (sock_fd); sprintf (char *) str_macaddr, "% 02x: % 02x: % 02x: % 02x: % 02x: % 02x ", (unsigned char) Returns [0], (unsigned char) ifr_mac.ifr_hwaddr.sa_data [1], (unsigned char) parameters [2], (unsigned char) ifr_mac.ifr_hwaddr.sa_data [3], (unsigned char) parameters [4], (unsigned char) ifr_mac.ifr_hwaddr.sa_data [5]); printf ("local mac: <% s> \ n ", str_macaddr); return 0 ;}int main (void) {INT8U str_macaddr [20]; memset (str_macaddr, 0, sizeof (str_macaddr )); getLocalMACAddr_API ("eth0", str_macaddr); fprintf (stdout, "1 mac: % s \ n", str_macaddr); // ifconfig_ethx_down_API ("eth0 "); // system ("ifconfig eth0 down"); // usleep (500000); // system ("ifconfig eth0 up"); // usleep (500000 ); // sleep (1); // 10 ms // ifconfig_ethx_down_API ("eth0"); // sleep (1); SetLocalMACAddr_API ("eth0", "08: 00: 11: 22: 33: 44 "); // ifconfig_ethx_up_API (" eth0 "); // ifconfig_ethx_up_API (" eth0 "); // sleep (2); memset (str_macaddr, 0, sizeof (str_macaddr); GetLocalMACAddr_API ("eth0", str_macaddr); fprintf (stdout, "2 mac: % s \ n", str_macaddr); system ("ping signature "); // usleep (50000); // ifconfig_ethx_down_API ("eth0"); // reset ("eth0"); // memset (str_macaddr, 0, sizeof (str_macaddr )); // GetLocalMACAddr_API ("eth0", str_macaddr); fprintf (stdout, "2 mac: % s \ n", str_macaddr); return 0 ;}






What are linux api functions? How can C and C ++ call APIs?

API is an application interface, so at the application level
Windows ddk, device driver development kit,
This development kit handles hardware.

For more details, see CSDN:
For example: dev.csdn.net/article/80266.shtm

VxD API Process

A VxD provides V86 mode and protection mode API process to allow applications and other software running in a virtual machine to access the features of the VxD. To make these optional processes valid, VxD must define them as parameters of the Declare_Virtual_Device macro. If not, VMM considers that the VxD has no API process.

Applications or other software running in a virtual machine can call the get device entry address function by setting the BX register to the VxD ID (INT 2FH 1684H function) obtain the entry address of the API process of a specific Virtual Machine. VMM returns this address so that the application can indirectly call this API process.

When an application calls this entry address, VMM saves the register of the application and calls the corresponding API process of VxD, save the handle of the current vm to the bx register and save the Client_Reg_Struc structure address to the EBP register. The API process must check the value of the customer register (using the Client_Reg_Struc structure) to determine the API call to run.

Generally, the main function number is specified using the AH register in most API processes, and the sub-function number is specified using the AL register. Other customer registers are used to add parameters. The API process modifies the return value of the customer register. The API process can modify the EAX, EBX, ECX, EDX, ESI, and EDI registers.

The following instance provides an instance API process-VSAMPLED_API_Get_Version:
BeginProc VSAMPLED_API_Get_Version
Movzx eax, [ebp. Client_AX]; function number

Or eax, eax
Jnz Undefined
Get_Version:
Mov [ebp. Client_AX], 030AH; Return Value in client register AX
And [ebp. Client_Flags], NOT CF_Mask; clear carry flag
Ret

Undefined:
Or [ebp. Client_Flags], CF_Mask; Set carry flag
Ret

EndProc VSAMPLED_API_Get_Version

Linux system APIs are the same as kernel APIs

Kernel is the core of Linux

Call the kernel api even if C is used.
Personal Understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.