-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGNTP.cpp
190 lines (176 loc) · 4.54 KB
/
GNTP.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "GNTP.h"
#include "../MD5/MD5.h"
GNTPNotification::GNTPNotification(char *name, char *title, char *description)
{
this->title = title;
this->description = description;
this->name = name;
}
GNTP::GNTP(char *appName, IPAddress server)
{
this->client= EthernetClient();
this->server=server;
this->appName = appName;
this->myNotifications = (notificationsList*) malloc(sizeof(notificationsList));
this->myNotifications->nextNotification = NULL;
this->port = DEFAULT_PORT;
this->debugging_enabled = false;
}
void GNTP::begin(void)
{
begin("hello world");
}
void GNTP::begin(char *pass)
{
salt = generate_salt();
char* salted_string = season_string(pass, salt);
digest =MD5::make_hash(salted_string);
char *md5str = MD5::make_digest(digest, 16);
digest = MD5::make_hash((char*) digest);
digest = (unsigned char*) MD5::make_digest(digest, 16);
salt = (unsigned char*) MD5::make_digest(salt, 8);
// Serial.println((char*) digest);
// Serial.println((char*) salt);
}
void GNTP::checkConnection()
{
// if there are incoming bytes available
// from the server, read them and print them:
// Serial.println("Checking connection");
delay(200);
while (client.available())
{
char c = client.read();
DBG(Serial.print(c););
}
// if the server's disconnected, stop the client:
if (!client.connected())
{
DBG(Serial.println(););
DBG(Serial.println("disconnecting."););
client.stop();
}
}
void GNTP::sendNotification(char* name)
{
notificationsList* lastItem = myNotifications;
while(lastItem->nextNotification != NULL)
{
lastItem = lastItem->nextNotification;
if(!strcmp(lastItem->notification.name, name))
{
sendNotification(lastItem->notification);
break;
}
}
}
void GNTP::sendNotification(GNTPNotification notification)
{
if(client.connect(server, this->port))
{
client.print("GNTP/1.0 NOTIFY NONE MD5 ");
client.print((char*) digest);
client.print(".");
client.println((char*) salt);
client.print("Application-Name: ");
client.println(appName);
client.print("Notification-Name: ");
client.println(notification.name);
client.print("Notification-Text: ");
client.println(notification.description);
client.print("Notification-Title: ");
client.println(notification.title);
client.println();
client.println();
checkConnection();
}
}
void GNTP::registerNotifications()
{
DBG(Serial.println("connecting..."););
// Make a HTTP request:
if(client.connect(server, this->port))
{
DBG(Serial.println("connected"););
client.print("GNTP/1.0 REGISTER NONE MD5 ");
client.print((char*) digest);
client.print(".");
client.println((char*) salt);
client.print("Application-Name: ");
client.println(appName);
int bla = count();
client.print("Notifications-Count: ");
client.println(bla);
// Serial.println("registration head sent");
notificationsList* lastItem = myNotifications;
while(lastItem->nextNotification != NULL)
{
lastItem = lastItem->nextNotification;
client.println();
client.print("Notification-Name: ");
client.println(lastItem->notification.name);
client.println("Notification-Enabled: True");
}
client.println();
client.println();
// Serial.println("registration sent");
checkConnection();
}
else
{
// kf you didn't get a connection to the server:
DBG(Serial.println("connection failed"););
}
}
void GNTP::add(GNTPNotification notification)
{
notificationsList* lastItem = myNotifications;
while(lastItem->nextNotification != NULL)
{
lastItem = lastItem->nextNotification;
}
notificationsList* newNotification = (notificationsList*) malloc(sizeof(notificationsList));
newNotification->nextNotification = NULL;
newNotification->notification = notification;
lastItem->nextNotification = newNotification;
}
int GNTP::count(void)
{
notificationsList* lastItem = myNotifications;
int count = 0;
while(lastItem->nextNotification != NULL)
{
count++;
lastItem = lastItem->nextNotification;
}
return count;
}
unsigned char* GNTP::generate_salt(void)
{
randomSeed(analogRead(A0));
unsigned char* salt = (unsigned char*) malloc(sizeof(unsigned char) * 9);
for(int i = 0; i < 8; i++)
{
salt[i] = random(1, 256);
}
salt[8] = '\0';
return salt;
}
char* GNTP::season_string(char* arg, unsigned char* salt)
{
char *challenge = (char*) malloc(sizeof(byte)*(strlen(arg)+9));
for(int i =0; i< strlen(arg); i++)
{
challenge[i] = arg[i];
}
for(int i = 0; i < 8 ; i++)
{
challenge[i+strlen(arg)] = salt[i];
}
challenge[strlen(arg)+8] = '\0';
return challenge;
}
void GNTP::setDebugging(bool dbg)
{
debugging_enabled = dbg;
}