💾 Archived View for gmi.noulin.net › gitRepositories › sodiumTest › file › presharedServer.c.gmi captured on 2024-08-25 at 09:13:36. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
presharedServer.c (3057B)
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 int mysock; 40 char buf[128*1024]; 41 int rval; 42 43 sock = socket(AF_INET, SOCK_STREAM, 0); 44 if (sock < 0){ 45 perror("Failed to create socket"); 46 ret 1; 47 } 48 49 server.sin_family = AF_INET; 50 server.sin_addr.s_addr = INADDR_ANY; 51 server.sin_port = htons(5000); 52 53 if (bind(sock, (struct sockaddr *) &server, sizeof(server))){ 54 perror("bind failed"); 55 ret 1; 56 } 57 58 listen(sock, 5); 59 60 forever { 61 mysock = accept(sock, (struct sockaddr *)0, 0); 62 if (mysock == -1) 63 perror("accept failed"); 64 else { 65 66 bool snd(void *buf, size_t sz) { 67 logVarG(sz); 68 if(send(mysock, buf, sz, 0) < 0){ 69 perror("send failed"); 70 close(mysock); 71 ret no; 72 } 73 ret yes; 74 } 75 76 bool rcv(void *buf, size_t sz) { 77 memset(buf, 0, sizeof(buf)); 78 rval = recv(mysock, buf, sizeof(buf), 0); 79 if (rval < 0) { 80 perror("reading message"); 81 ret no; 82 } 83 else if (rval == 0) { 84 logI("Ending connection"); 85 close(mysock); 86 ret no; 87 } 88 logVarG(rval); 89 ret yes; 90 } 91 92 // get encrypted message 93 int len; 94 if (!rcv(&len, sizeof(len))) continue; 95 rval = recv(mysock, buf, len, MSG_WAITALL); 96 if (rval < 0) { 97 perror("reading message"); 98 close(mysock); 99 continue; 100 } 101 else if (rval == 0) { 102 logI("Ending connection"); 103 close(mysock); 104 continue; 105 } 106 logVarG(rval); 107 108 u8 decrypted[1000]; 109 len = selPublicDecrypt(decrypted, sizeof(decrypted), buf, rval, &serverKeys); 110 111 if (!len) { 112 logE("failed to decrypt"); 113 close(mysock); 114 continue; 115 } 116 117 decrypted[len] = 0; 118 119 logI("decrypted: %s", decrypted); 120 121 close(mysock); 122 } 123 } 124 } 125 // vim: set expandtab ts=2 sw=2: