💾 Archived View for gmi.noulin.net › gitRepositories › sodiumTest › file › server.c.gmi captured on 2024-08-31 at 13:31:30. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2024-07-09)
-=-=-=-=-=-=-
server.c (3206B)
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 // generate keys 17 keyst serverKeys = init0Var; 18 19 newKeysBuf(&serverKeys); 20 21 22 // start event loop 23 int sock; 24 struct sockaddr_in server; 25 int mysock; 26 char buf[128*1024]; 27 int rval; 28 29 sock = socket(AF_INET, SOCK_STREAM, 0); 30 if (sock < 0){ 31 perror("Failed to create socket"); 32 ret 1; 33 } 34 35 server.sin_family = AF_INET; 36 server.sin_addr.s_addr = INADDR_ANY; 37 server.sin_port = htons(5000); 38 39 if (bind(sock, (struct sockaddr *) &server, sizeof(server))){ 40 perror("bind failed"); 41 ret 1; 42 } 43 44 listen(sock, 5); 45 46 forever { 47 mysock = accept(sock, (struct sockaddr *)0, 0); 48 if (mysock == -1) 49 perror("accept failed"); 50 else { 51 52 bool snd(void *buf, size_t sz) { 53 logVarG(sz); 54 if(send(mysock, buf, sz, 0) < 0){ 55 perror("send failed"); 56 close(mysock); 57 ret no; 58 } 59 ret yes; 60 } 61 62 bool rcv(void *buf, size_t sz) { 63 rval = recv(mysock, buf, sizeof(buf), 0); 64 if (rval < 0) { 65 perror("reading message"); 66 ret no; 67 } 68 else if (rval == 0) { 69 logI("Ending connection"); 70 close(mysock); 71 ret no; 72 } 73 logVarG(rval); 74 ret yes; 75 } 76 77 /* memset(buf, 0, sizeof(buf)); */ 78 /* if ((rval = recv(mysock, buf, sizeof(buf), 0)) < 0) { */ 79 80 // store remote public key 81 if ((rval = recv(mysock, &serverKeys.remotePublicKey, sizeof(serverKeys.remotePublicKey), MSG_WAITALL)) < 0) { 82 perror("reading message"); 83 close(mysock); 84 continue; 85 } 86 else if (rval == 0) { 87 logI("Ending connection"); 88 close(mysock); 89 continue; 90 } 91 if (rval != sizeof(serverKeys.remotePublicKey)) { 92 logW("Didn't get complete client public key"); 93 } 94 95 logD("Remote public key"); 96 loghex(serverKeys.remotePublicKey, sizeof(serverKeys.remotePublicKey)); 97 put; 98 99 // send public key 100 if (!snd(&serverKeys.publicKey, sizeof(serverKeys.publicKey))) continue; 101 102 // get encrypted message 103 int len; 104 if (!rcv(&len, sizeof(len))) continue; 105 rval = recv(mysock, buf, len, MSG_WAITALL); 106 if (rval < 0) { 107 perror("reading message"); 108 close(mysock); 109 continue; 110 } 111 else if (rval == 0) { 112 logI("Ending connection"); 113 close(mysock); 114 continue; 115 } 116 logVarG(rval); 117 118 u8 decrypted[1000]; 119 len = selPublicDecrypt(decrypted, sizeof(decrypted), buf, rval, &serverKeys); 120 121 if (!len) { 122 logE("failed to decrypt"); 123 close(mysock); 124 continue; 125 } 126 127 decrypted[len] = 0; 128 129 logI("decrypted: %s", decrypted); 130 131 // send encrypted response 132 serverKeys.nonce[0] = 1; 133 134 char *msg = "OK"; 135 136 len = selPublicEncrypt(buf, sizeof(buf), msg, strlen(msg), &serverKeys); 137 138 logVarG(len); 139 140 snd(&len, sizeof(len)); 141 snd(buf, len); 142 143 close(mysock); 144 } 145 } 146 } 147 // vim: set expandtab ts=2 sw=2: