Bläddra i källkod

Finished chat loop

thajohns 11 månader sedan
förälder
incheckning
98388bf803
4 ändrade filer med 86 tillägg och 6 borttagningar
  1. 31
    2
      client.c
  2. 21
    1
      comm.c
  3. 2
    0
      comm.h
  4. 32
    3
      server.c

+ 31
- 2
client.c Visa fil

@@ -5,13 +5,19 @@
5 5
 #include "comm.h"
6 6
 #include "aes.h"
7 7
 
8
-int main()
8
+int main(int argc, char **argv)
9 9
 {
10 10
   struct session sess;
11 11
   initializeSBox();
12 12
   sess_init(&sess);
13 13
 
14
-  if (do_resolve("127.0.0.1", NULL, &sess.params))
14
+  if (argc != 3)
15
+  {
16
+    printf("Usage: %s <bind address> <port>\n", argv[0]);
17
+    return 0;
18
+  }
19
+
20
+  if (do_resolve(argv[1], argv[2], &sess.params))
15 21
   {
16 22
     perror("could not open socket");
17 23
     return 1;
@@ -23,6 +29,29 @@ int main()
23 29
     return 1;
24 30
   }
25 31
 
32
+  int byte;
33
+  while (1)
34
+  {
35
+    byte = getchar();
36
+    if (byte == EOF)
37
+      break;
38
+    send_encrypted_byte(&sess, byte);
39
+
40
+    if (byte == '\n')
41
+    {
42
+      send_encrypted_byte(&sess, 0);
43
+      while ((byte = recv_encrypted_byte(&sess)))
44
+      {
45
+        if (byte == EOF)
46
+        {
47
+          goto break2;
48
+        }
49
+        printf("%c", byte);
50
+      }
51
+    }
52
+  }
53
+break2:
54
+
26 55
   do_unresolve(sess.params);
27 56
   sess_destroy(&sess);
28 57
 }

+ 21
- 1
comm.c Visa fil

@@ -153,7 +153,8 @@ int send_data(char data[16], struct session *sess)
153 153
 
154 154
 int recv_data(char data[16], struct session *sess)
155 155
 {
156
-  read(sess->params.sock, data, 16);
156
+  if (!read(sess->params.sock, data, 16))
157
+    return 1;
157 158
   return 0;
158 159
 }
159 160
 
@@ -262,3 +263,22 @@ int do_receive(struct session *sess)
262 263
   return 0;
263 264
 }
264 265
 
266
+void send_encrypted_byte(struct session *sess, char byte)
267
+{
268
+  fill_random(sess->last_sent_enc, 15);
269
+  sess->last_sent_enc[15] ^= byte;
270
+  send_data(sess->last_sent_enc, sess);
271
+  AESRound_data(sess->last_sent_enc, sess->key);
272
+}
273
+
274
+int recv_encrypted_byte(struct session *sess)
275
+{
276
+  char buffer[16];
277
+  memcpy(buffer, sess->last_recv_enc, 16);
278
+  if (recv_data(sess->last_recv_enc, sess))
279
+    return EOF;
280
+  buffer[15] ^= sess->last_recv_enc[15];
281
+  AESRound_data(sess->last_recv_enc, sess->key);
282
+  return buffer[15];
283
+}
284
+

+ 2
- 0
comm.h Visa fil

@@ -38,6 +38,8 @@ int do_resolve(char *address, char *portstr, struct sock_params *params);
38 38
 void do_unresolve(struct sock_params params);
39 39
 int do_connect(struct session *sess);
40 40
 int do_receive(struct session *sess);
41
+void send_encrypted_byte(struct session *sess, char byte);
42
+int recv_encrypted_byte(struct session *sess);
41 43
 
42 44
 #endif
43 45
 

+ 32
- 3
server.c Visa fil

@@ -5,13 +5,19 @@
5 5
 #include "comm.h"
6 6
 #include "aes.h"
7 7
 
8
-int main()
8
+int main(int argc, char **argv)
9 9
 {
10 10
   struct session sess;
11 11
   initializeSBox();
12 12
   sess_init(&sess);
13 13
 
14
-  FILE *keyf = fopen("key.out", "r");
14
+  if (argc != 4)
15
+  {
16
+    printf("Usage: %s <key file> <bind address> <port>\n", argv[0]);
17
+    return 0;
18
+  }
19
+
20
+  FILE *keyf = fopen(argv[1], "r");
15 21
   if (keyf == NULL)
16 22
   {
17 23
     perror("could not open key file");
@@ -26,7 +32,7 @@ int main()
26 32
 
27 33
   fclose(keyf);
28 34
 
29
-  if (do_resolve("127.0.0.1", NULL, &sess.params))
35
+  if (do_resolve(argv[2], argv[3], &sess.params))
30 36
   {
31 37
     perror("could not open socket");
32 38
     return 1;
@@ -37,6 +43,29 @@ int main()
37 43
     perror("could not accept");
38 44
     return 1;
39 45
   }
46
+  
47
+  int byte;
48
+  while (1)
49
+  {
50
+    byte = getchar();
51
+    if (byte == EOF)
52
+      break;
53
+    send_encrypted_byte(&sess, byte);
54
+
55
+    if (byte == '\n')
56
+    {
57
+      send_encrypted_byte(&sess, 0);
58
+      while ((byte = recv_encrypted_byte(&sess)))
59
+      {
60
+        if (byte == EOF)
61
+        {
62
+          goto break2;
63
+        }
64
+        printf("%c", byte);
65
+      }
66
+    }
67
+  }
68
+break2:
40 69
 
41 70
   do_unresolve(sess.params);
42 71
   sess_destroy(&sess);

Laddar…
Avbryt
Spara