📄 addrule.c
字号:
#define _GNU_SOURCE#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <netdb.h>#include <string.h>#include <firewall.h>#include <errno.h>/* converts valid IP-Address string into IP-address in suitable format for kernel */IPAddressByteType convertIPAddress (char *IPAddressString, int *error) { *error = 0; struct hostent *IPAddress; IPAddressByteType IPAddressByte; IPAddress = gethostbyname (IPAddressString); if (!IPAddress) { *error = h_errno; return 0; } if (IPAddress->h_length != sizeof (IPAddressByteType)) { fprintf (stderr, "Wrong code!\n"); exit (1); } memcpy((char *)&IPAddressByte, (char *)IPAddress->h_addr, IPAddress->h_length); return IPAddressByte;}int main (int argc, char **argv) { IPAddressByteType IPAddress; int error; FILE *procFile; ssize_t noWritten; int bufferLength; int tmp; bufferLength = 2*sizeof (int) + strlen (argv[2]) + 1 + sizeof (IPAddressByteType); char buffer[bufferLength]; if (argc != 5) { fprintf (stderr, "Exactly four arguments required!\n"); exit (1); } procFile = fopen (argv[1], "w"); if (!procFile) { fprintf (stderr, "Opening failed!\n"); exit (1); } /* convert the IP-address into format suitable for kernel */ IPAddress = convertIPAddress (argv[3], &error); if (error) { fprintf (stderr, "Converision of IP-address failed \n"); } /* copy the port number into the buffer */ tmp = atoi (argv[4]); memcpy (buffer, &tmp, sizeof (int)); /* copy the length of the program string into the buffer */ tmp = strlen (argv[2]) + 1; memcpy (buffer + sizeof (int), &tmp, sizeof (int)); /* copy the program string into the buffer */ strncpy (buffer +2 * sizeof (int), argv[2], strlen (argv[2]) + 1); /* copy the IP-address into the buffer */ memcpy (buffer + 2 *sizeof (int) + strlen (argv[2]) + 1, &IPAddress, sizeof (IPAddressByteType)); /* write the buffer to the kernel */ noWritten = fwrite (buffer, 1, bufferLength, procFile); if (noWritten != bufferLength) { perror ("Couldn't write data!\n"); } fclose (procFile); exit (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -