💾 Archived View for gmi.noulin.net › gitRepositories › sodiumTest › file › presharedServerudp.c.gmi captured on 2024-08-25 at 09:13:36. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

sodiumTest

Log

Files

Refs

README

presharedServerudp.c (2789B)

     1 #! /usr/bin/env sheepy
     2 
     3 #include "libsheepyObject.h"
     4 
     5 #include <sys/socket.h>
     6 #include <netinet/in.h>
     7 
     8 #include "sel.h"
     9 
    10 int main(int ac, char **av){
    11 
    12   setLogMode(LOG_FUNC);
    13 
    14   if (not selInit()) ret 1;
    15 
    16   // load keys
    17   const char* clientSecretFilename = "presharedClientSecret.bin";
    18   const char* clientPublicFilename = "presharedClientPublic.bin";
    19   const char* serverSecretFilename = "presharedServerSecret.bin";
    20   const char* serverPublicFilename = "presharedServerPublic.bin";
    21   keyst clientKeys                 = init0Var;
    22   keyst serverKeys                 = init0Var;
    23 
    24   if (isPath(serverPublicFilename) and isPath(serverSecretFilename) and isPath(clientPublicFilename)) {
    25     logI("Loading server keys");
    26     pError0(bLReadFile(serverSecretFilename, serverKeys.secretKey, sizeof(serverKeys.secretKey)));
    27     pError0(bLReadFile(serverPublicFilename, serverKeys.publicKey, sizeof(serverKeys.publicKey)));
    28     pError0(bLReadFile(clientPublicFilename, serverKeys.remotePublicKey, sizeof(serverKeys.remotePublicKey)));
    29   }
    30   else {
    31     logE("Failed to load the server keys. Stop. Run ./presharedClient.c generate keys");
    32     ret 1;
    33   }
    34 
    35 
    36   // start event loop
    37   int sock;
    38   struct sockaddr_in server;
    39   typ struct PACKED {
    40     u64 time;
    41     char buf[64*1024];
    42   } payloadt;
    43   typ struct PACKED {
    44     u8 nonce[crypto_box_NONCEBYTES];
    45     i32 len;
    46     payloadt payload;
    47   } packett;
    48   packett data = init0Var;
    49   int rval;
    50 
    51   sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    52   if (sock < 0){
    53     perror("Failed to create socket");
    54     ret 1;
    55   }
    56 
    57   server.sin_family = AF_INET;
    58   server.sin_addr.s_addr = INADDR_ANY;
    59   server.sin_port = htons(5000);
    60 
    61   if (bind(sock, (struct sockaddr *) &server, sizeof(server))){
    62     perror("bind failed");
    63     ret 1;
    64   }
    65 
    66   listen(sock, SOMAXCONN);
    67 
    68   struct sockaddr_in addr;
    69 
    70   forever {
    71     socklen_t ln = sizeof(addr);
    72     ssize_t r = recvfrom(sock, &data, sizeof(data), 0, (struct sockaddr *) &addr, &ln);
    73     if (r == -1)
    74       perror("recvfrom failed");
    75     elif (r != data.len + sizeof(data.len) + sizeof(data.nonce)) {
    76       logE("Wrong size");
    77       continue;
    78     }
    79 
    80     memcpy(serverKeys.nonce, data.nonce, sizeof(serverKeys.nonce));
    81 
    82     payloadt decrypted;
    83     int len = selPublicDecrypt((u8*)&decrypted, sizeof(decrypted), (u8*)&data.payload, data.len, &serverKeys);
    84 
    85     if (!len) {
    86       logE("failed to decrypt");
    87       continue;
    88     }
    89 
    90     u64 now = getCurrentUnixTime();
    91     if (decrypted.time < now - 1 or decrypted.time > now + 1) {
    92       logW("Dropping packet. Wrong timestamp %"PRIu64" now %"PRIu64" diff %"PRIi64, decrypted.time, now, (i64)now - (i64)decrypted.time);
    93       continue;
    94     }
    95 
    96     decrypted.buf[len - sizeof(decrypted.time)] = 0;
    97 
    98     logI("decrypted: %s", decrypted.buf);
    99   }
   100 }
   101 // vim: set expandtab ts=2 sw=2: