Advertisement
FlyFar

knock.c

Jan 20th, 2024
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | Cybersecurity | 0 0
  1. /*
  2.  * knock.c
  3.  * Make a knock packet
  4.  * By J. Stuart McMurray
  5.  * Created 20160317
  6.  * Last Modified 20160317
  7.  */
  8.  
  9. #include "plainshell.h"
  10.  
  11. int make_knock(const char *address, const char *port) {
  12.         unsigned char a[4];
  13.         uint16_t p;
  14.         int ret;
  15.  
  16.         /* Convert the address */
  17.         if (0 == (ret = inet_pton(AF_INET, address, (void*)a))) {
  18.                 errx(1, "invalid address");
  19.         } else if (-1 == ret) {
  20.                 err(2, "address parse error");
  21.         }
  22.  
  23.  
  24.         /* Convert the port */
  25.         if (1 != sscanf(port, "%hu", &p)) {
  26.                 err(3, "port parse error");
  27.         }
  28.         p = htons(p);
  29.  
  30.         /* Write the bytes to stdout */
  31.         if (-1 == write(STDOUT_FILENO, a, sizeof(a))) {
  32.                 err(4, "write");
  33.         }
  34.         if (-1 == write(STDOUT_FILENO, &p, sizeof(p))) {
  35.                 err(5, "write");
  36.         }
  37.  
  38.         return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement