-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
97 lines (79 loc) · 2.18 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "contiki-net.h"
#include <stdio.h>
static struct psock ps;
static char buffer[100];
struct conn_state{
enum {send1, send2, send3, send4, send5, send6, send7} state;
};
PROCESS(example_psock_client_process, "Example protosocket client");
/*---------------------------------------------------------------------------*/
static int
handle_connection(struct psock *p)
{
PSOCK_BEGIN(p);
struct conn_state *s;
s->state = send1;
PSOCK_SEND_STR(p, "Connection Request");
while(1) {
if(s->state == send1)
{
memset(buffer,0,sizeof(buffer));
PSOCK_READTO(p, '\n');
printf("Got: %s", buffer);
printf("Server is Authenticated \n");
s->state = send2;
}
if(s->state == send2)
{
PSOCK_SEND_STR(p,"certificate\n");
s->state = send3;
}
if(s->state == send3)
{
memset(buffer,0,sizeof(buffer));
PSOCK_READTO(p, '\n');
printf("Got: %s", buffer);
printf("I received servers shared key \n");
s->state = send4;
}
if(s->state == send4)
{
printf("I accepted servers shared key \n");
s->state = send5;
PSOCK_SEND_STR(p,"shared key accepted\n");
}
if(s->state == send5)
{
memset(buffer,0,sizeof(buffer));
PSOCK_READTO(p, '\n');
printf("Got : %s", buffer);
printf("Secure Connection Established\n");
PSOCK_SEND_STR(p,"Connected\n");
printf("Connected Sent\n");
}
}
PSOCK_END(p);
}
/*---------------------------------------------------------------------------*/
AUTOSTART_PROCESSES(&example_psock_client_process);
PROCESS_THREAD(example_psock_client_process, ev, data)
{
uip_ipaddr_t addr;
PROCESS_BEGIN();
uip_ipaddr(&addr, 172,16,1,0);
tcp_connect(&addr, UIP_HTONS(5555), NULL);
printf("Connecting...\n");
PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
if(uip_aborted() || uip_timedout() || uip_closed()) {
printf("Could not establish connection...\n");
} else if(uip_connected()) {
printf("Connected\n");
PSOCK_INIT(&ps, buffer, sizeof(buffer));
do {
handle_connection(&ps);
PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
} while(!(uip_closed() || uip_aborted() || uip_timedout()));
printf("Connection closed.\n");
}
PROCESS_END();
}