MissValeska 10 years ago
parent
commit
01fd7798ca
1 changed files with 322 additions and 0 deletions
  1. 322
    0
      src/ntk-console.c

+ 322
- 0
src/ntk-console.c View File

@@ -0,0 +1,322 @@
1
+#include "Netsukuku-Console.h"
2
+
3
+char response[BUFFER_LENGTH];
4
+
5
+void usage();
6
+
7
+void clean_up();
8
+
9
+int validity_check(char *request) {
10
+    
11
+        if(strncmp(request,"help", (int)strlen(request))  == 0)
12
+            return 1;
13
+        
14
+        else if(strncmp(request,"uptime", (int)strlen(request))  == 0)
15
+            return 0;
16
+        
17
+        else if(strncmp(request,"kill", (int)strlen(request))  == 0)
18
+            return 2;
19
+        
20
+        else if(strncmp(request,"version", (int)strlen(request))  == 0)
21
+            return 3;
22
+        
23
+        else if(strncmp(request,"console_uptime", (int)strlen(request))  == 0)
24
+            return 4;
25
+        
26
+        else if(strlen(request) > BUFFER_LENGTH)
27
+            return 5;
28
+        
29
+        else if(strncmp(request,"inet_connected", (int)strlen(request))  == 0)
30
+            return 0;
31
+        
32
+        else if(strncmp(request,"cur_ifs", (int)strlen(request))  == 0)
33
+            return 0;
34
+        
35
+        else if(strncmp(request,"cur_ifs_n", (int)strlen(request))  == 0)
36
+            return 0;
37
+        
38
+        else if(strncmp(request,"cur_qspn_id", (int)strlen(request))  == 0)
39
+            return 0;
40
+        
41
+        else if(strncmp(request,"cur_ip", (int)strlen(request))  == 0)
42
+            return 0;
43
+        
44
+        else if(strncmp(request,"cur_node", (int)strlen(request))  == 0)
45
+            return 0;
46
+        
47
+        else if(strncmp(request,"ifs", (int)strlen(request))  == 0)
48
+            return 0;
49
+        
50
+        else if(strncmp(request,"ifs_n", (int)strlen(request))  == 0)
51
+            return 0;
52
+        
53
+        else {
54
+            printf("Incorrect or unreadable command, Please correct it.\n");
55
+            return -1;
56
+        }
57
+    
58
+    return -2;
59
+    
60
+}
61
+
62
+void response_cleanup(char response[BUFFER_LENGTH]) {
63
+    
64
+    char remove = 'a';
65
+
66
+    char* c;
67
+    char* pPosition;
68
+    while((pPosition = strchr(response, 'a')) != NULL)  {
69
+        if ((c = index(response, remove)) != NULL) {
70
+        size_t len_left = sizeof(response) - (c+1-response);
71
+        memmove(c, c+1, len_left);
72
+        }
73
+    }
74
+    printf("Sent and received Successfully!\n The Response was: %s", response);
75
+}
76
+
77
+/* Sends and receives to ntkd */
78
+void ntkd_request(char *request) {
79
+
80
+    int request_length;
81
+    
82
+            rc = connect(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
83
+                if (rc < 0) {
84
+                    perror("connect() failed");
85
+                    exit(-1);
86
+                }
87
+    
88
+    
89
+            request_length = (int)strlen(request);
90
+            memset(request, 'a', BUFFER_LENGTH - request_length);
91
+            rc = send(sockfd, request, sizeof(request), 0);
92
+            if (rc < 0) {
93
+                perror("send() failed");
94
+                exit(-1);
95
+            }
96
+
97
+            bytesReceived = 0;
98
+            while (bytesReceived < BUFFER_LENGTH) {
99
+                rc = recv(sockfd, & response[bytesReceived],
100
+                   BUFFER_LENGTH - bytesReceived, 0);
101
+                if (rc < 0) {
102
+                    perror("recv() failed");
103
+                    exit(-1);
104
+                }
105
+            else if (rc == 0) {
106
+                printf("The server closed the connection\n");
107
+                exit(-1);
108
+            }
109
+
110
+            /* Increment the number of bytes that have been received so far  */
111
+            bytesReceived += rc;        
112
+            }
113
+            response_cleanup(response);
114
+}
115
+
116
+void opensocket(void) {
117
+    
118
+    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
119
+    if (sockfd < 0) {
120
+         perror("socket creation failed");
121
+         exit(-1);
122
+    }
123
+    
124
+    memset(&serveraddr, 0, sizeof(serveraddr));
125
+    serveraddr.sun_family = AF_UNIX;
126
+    strcpy(serveraddr.sun_path, SERVER_PATH);
127
+}
128
+
129
+void console_uptime(void) {
130
+    
131
+    int uptime_sec1;
132
+    int uptime_min1;
133
+    int uptime_hour1;
134
+    
135
+    int uptime_day1;
136
+    int uptime_month1;
137
+    int uptime_year1;
138
+    
139
+    time(&rawtime);
140
+    
141
+    timeinfo = localtime(&rawtime);
142
+    
143
+    uptime_sec1 = timeinfo->tm_sec;
144
+    uptime_min1 = timeinfo->tm_min;
145
+    uptime_hour1 = timeinfo->tm_hour;
146
+    
147
+    uptime_day1 = timeinfo->tm_mday;
148
+    uptime_month1 = timeinfo->tm_mon;
149
+    uptime_year1 = timeinfo->tm_year;
150
+    
151
+    uptime_sec1 -= uptime_sec;
152
+    uptime_min1 -= uptime_min;
153
+    uptime_hour1 -= uptime_hour;
154
+    
155
+    uptime_day1 -= uptime_day;
156
+    uptime_month1 -= uptime_month;
157
+    uptime_year1 -= uptime_year;
158
+    /* Checks if the date/time is negative,
159
+     * And makes it positive.
160
+     * e.g -11 is 1 because this is a signed variable
161
+     * at a modulus of 12. Thus after 12, It is -12
162
+     * which is zero, Every number after this is counting up
163
+     * to 12 again, Which is going to be 0 in this instance.
164
+     * -12 to 12 is 24 actual months.
165
+     * So -11 + 12 is 1, As it should be, And -12 + 12 is zero
166
+     * as it should be. The only difference is the modulus number,
167
+     * i.e: 12, 24, etc. */
168
+    if(uptime_month1 < 0)
169
+        uptime_month1 + 12;
170
+    if(uptime_day1 < 0)
171
+        uptime_day1 + 365;
172
+    if(uptime_hour1 < 0)
173
+        uptime_hour1 + 24;
174
+    if(uptime_min1 < 0)
175
+        uptime_min1 + 60;
176
+    if(uptime_sec1 < 0)
177
+        uptime_sec1 + 60;
178
+    /* Checks if the date/time is the modulus, And resets it to zero.
179
+     * e.g: 12 months is a year, Listing 12 months and one year
180
+     * is confusing,
181
+     * And implies that it has been two years. */
182
+    if(uptime_month1 == 12)
183
+        uptime_month1 = 0;
184
+    if(uptime_day1 == 365)
185
+        uptime_day1 = 0;
186
+    if(uptime_hour1 == 24)
187
+        uptime_hour1 = 0;
188
+    if(uptime_min1 == 60)
189
+        uptime_min1 = 0;
190
+    if(uptime_sec1 == 60)
191
+        uptime_sec1 = 0;
192
+    
193
+    printf("Total Uptime is: %i Year(s), %i Month(s), %i Day(s), %i Hour(s), %i Minute(s), %i Second(s)\n",uptime_year1, uptime_month1, uptime_day1, uptime_hour1, uptime_min1, uptime_sec1);
194
+    
195
+}
196
+
197
+int millisleep(unsigned ms)
198
+{
199
+  return usleep(1000 * ms);
200
+}
201
+
202
+void console(char * request) {
203
+    
204
+    if(validity_check(request) == -2)
205
+            printf("Error: Command has not been processed!\n");
206
+        
207
+    if(validity_check(request) == -1)
208
+            usage();
209
+        
210
+    if(strncmp(request,"quit", (int)strlen(request)) == 0) {
211
+            clean_up();
212
+            exit(0);
213
+        }
214
+    
215
+    if(validity_check(request) == 0) {
216
+            ntkd_request(request);
217
+            millisleep(200);
218
+    }
219
+        
220
+    if(validity_check(request) == 1)
221
+            usage();
222
+        
223
+    if(validity_check(request) == 2)
224
+            system("ntkd -k");
225
+        
226
+    if(validity_check(request) == 3) {
227
+            printf("Ntk-Console Version: %s\n", VERSION_STR);
228
+            ntkd_request(request);
229
+        }
230
+
231
+    if(validity_check(request) == 4)
232
+            console_uptime();
233
+    
234
+    if(validity_check(request) == 5)
235
+        printf("Error: Command longer than 250 bytes.\n");
236
+}
237
+
238
+int main(void) {
239
+    
240
+    time(&rawtime);
241
+    
242
+    timeinfo = localtime(&rawtime);
243
+    
244
+    uptime_sec = timeinfo->tm_sec;
245
+    uptime_min = timeinfo->tm_min;
246
+    uptime_hour = timeinfo->tm_hour;
247
+    uptime_day = timeinfo->tm_mday;
248
+    uptime_month = timeinfo->tm_mon;
249
+    uptime_year = timeinfo->tm_year;
250
+    
251
+    opensocket();
252
+    
253
+    printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
254
+    
255
+    char request;
256
+
257
+    char request1;
258
+    
259
+    request = (char)malloc(BUFFER_LENGTH);
260
+    
261
+    request1 = (char)malloc(BUFFER_LENGTH);
262
+    
263
+    do {
264
+    
265
+    printf("\n>");
266
+        
267
+    fgets(request1, 16, stdin);
268
+    
269
+    fflush(stdin);
270
+    
271
+    request1 = request;
272
+    
273
+    console(request);
274
+    } while(FALSE);
275
+    
276
+clean_up(); 
277
+return 0;
278
+}
279
+
280
+void usage(void) {
281
+    
282
+    	printf("Usage\n"
283
+		" uptime    Returns the time when ntkd finished the hooking," 
284
+					  "to get the the actual uptime just do) "
285
+					  "time(0)-me.uptime \n"
286
+		" help	Shows this\n"
287
+		" kill	Kills the running instance of netsukuku with SIGINT\n\n"
288
+		" version   Shows the running version of the ntk-console, and ntkd.\n"
289
+		" inet_connected    If it is 1, Ntkd is connected to the Internet\n"
290
+		"\n"
291
+		" cur_ifs   Lists all of the interfaces in cur_ifs\n"
292
+		" cur_ifs_n Lists the number of interfaces present in `cur_ifs'\n"
293
+		"\n");
294
+        printf(" cur_qspn_id   The current qspn_id we are processing. "
295
+                                "It is cur_qspn_id[levels] big\n"
296
+		" cur_ip    Current IP address\n"
297
+		"\n"
298
+		" cur_node  Current Node\n"
299
+		" ifs   Lists all of the interfaces in server_opt.ifs\n"
300
+                " ifs_n Lists the number of interfaces present in server_opt.ifs\n"
301
+                " quit Exits this program\n"
302
+		" console_uptime    Gets the uptime of this console\n");
303
+    
304
+}
305
+
306
+void clean_up(void) {
307
+    
308
+    const int optVal = 1;
309
+    const socklen_t optLen = sizeof(optVal);
310
+    
311
+    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
312
+    setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
313
+    
314
+    if (sockfd != -1)
315
+        close(sockfd);
316
+
317
+    if (sockfd1 != -1)
318
+        close(sockfd1);
319
+   
320
+   unlink(SERVER_PATH);
321
+    
322
+}

Loading…
Cancel
Save