-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
623 lines (567 loc) · 24.5 KB
/
Copy pathcli.c
File metadata and controls
623 lines (567 loc) · 24.5 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#include "cli.h"
#include "libc.h"
#include "e1000.h"
#include "kernel.h"
#include "libc.h"
#include "vfs.h"
#include "edit.h"
#include "nat.h"
extern void puts(const char *s);
extern void puts_serial(const char *s);
void (*cli_puts)(const char *s) = puts;
void (*cli_set_color)(uint8_t fg, uint8_t bg) = set_color;
// We need access to route_entry to print routes
struct route_entry {
uint32_t dest_ip;
uint32_t netmask;
uint8_t next_hop_mac[6];
};
#define MAX_ROUTES 16
extern struct route_entry routing_table[MAX_ROUTES];
extern int num_routes;
// Helper to print IP in x.x.x.x format (assuming Little Endian uint32_t)
static void print_ip(uint32_t ip) {
uint8_t *p = (uint8_t *)&ip;
char buf[16];
// Simple custom itoa for IP parts
for(int i=0; i<4; i++) {
uint8_t v = p[i];
if (v >= 100) {
char str[2] = { '0' + (v / 100), '\0' };
cli_puts(str);
v %= 100;
str[0] = '0' + (v / 10);
cli_puts(str);
v %= 10;
} else if (v >= 10) {
char str[2] = { '0' + (v / 10), '\0' };
cli_puts(str);
v %= 10;
}
char str[2] = { '0' + v, '\0' };
cli_puts(str);
if (i < 3) cli_puts(".");
}
}
static void print_mac(const uint8_t *mac) {
const char hex[] = "0123456789ABCDEF";
for(int i=0; i<6; i++) {
char str[3] = { hex[mac[i] >> 4], hex[mac[i] & 0x0F], '\0' };
cli_puts(str);
if (i < 5) cli_puts(":");
}
}
void cli_execute(const char *cmd) {
if (strcmp(cmd, "") == 0) {
return; // Empty command
}
if (strncmp(cmd, "help", 4) == 0) {
cli_set_color(VGA_YELLOW, VGA_BLACK);
cli_puts("Available commands:\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" help ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Show this message\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" ifconfig ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- List all E1000 NICs\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" route print ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Show Fast-Path routing table\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" info ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Show system info\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" ntop ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Network topology map\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" nat show ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Show NAT mapping table\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" ls ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- List files in VFS\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" cat <file> ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Print file contents\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" edit <file> ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Open text editor\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" save / load ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Save/load config to disk\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" clear ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Clear screen\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" exit ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("- Halt system\n");
}
else if (strncmp(cmd, "info", 4) == 0) {
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
cli_puts("NKernel (Network Kernel) - Unikernel Router\n");
cli_set_color(VGA_LIGHT_BLUE, VGA_BLACK);
cli_puts("Architecture: x86_64\n");
}
else if (strncmp(cmd, "ifconfig", 8) == 0) {
if (num_e1000_devices == 0) {
cli_set_color(VGA_LIGHT_RED, VGA_BLACK);
cli_puts("No NICs found.\n");
}
for (int i = 0; i < num_e1000_devices; i++) {
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
cli_puts("en");
char id[2] = { '0' + i, '\0' };
cli_puts(id);
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts(": MAC=");
cli_set_color(VGA_YELLOW, VGA_BLACK);
print_mac(e1000_devs[i].mac);
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" (E1000)\n");
}
}
else if (strncmp(cmd, "route print", 11) == 0) {
cli_set_color(VGA_YELLOW, VGA_BLACK);
cli_puts("Destination Netmask Next-Hop MAC\n");
cli_puts("--------------------------------------------------\n");
for (int i = 0; i < num_routes; i++) {
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
print_ip(routing_table[i].dest_ip);
cli_puts("\t");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
print_ip(routing_table[i].netmask);
cli_puts("\t");
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
print_mac(routing_table[i].next_hop_mac);
cli_puts("\n");
}
}
else if (strncmp(cmd, "save", 4) == 0) {
// Save routing table to sector 1
uint8_t buffer[512] = {0};
extern void ata_write_sector(uint32_t lba, const uint8_t *buffer);
// First 4 bytes: num_routes
*(uint32_t *)buffer = num_routes;
// Next bytes: routing_table array
memcpy(buffer + 4, routing_table, sizeof(routing_table));
ata_write_sector(1, buffer);
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
cli_puts("Configuration saved to disk (ATA PIO Sector 1).\n");
}
else if (strncmp(cmd, "load", 4) == 0) {
// Load routing table from sector 1
uint8_t buffer[512] = {0};
extern void ata_read_sector(uint32_t lba, uint8_t *buffer);
ata_read_sector(1, buffer);
num_routes = *(uint32_t *)buffer;
if (num_routes > MAX_ROUTES) num_routes = 0; // sanity check
memcpy(routing_table, buffer + 4, sizeof(routing_table));
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
cli_puts("Configuration loaded from disk.\n");
}
else if (strncmp(cmd, "ls", 2) == 0) {
extern void vfs_list(void);
vfs_list();
}
else if (strncmp(cmd, "exit", 4) == 0) {
cli_set_color(VGA_YELLOW, VGA_BLACK);
cli_puts("Halting system. Use Ctrl-A X to exit QEMU.\n");
__asm__ volatile ("cli; hlt");
}
else if (strncmp(cmd, "clear", 5) == 0) {
clear_screen();
}
else if (strncmp(cmd, "ntop", 4) == 0) {
// Scan NAT table for active hosts first
extern struct nat_entry nat_table[MAX_NAT_ENTRIES];
uint32_t active_hosts[16];
uint16_t host_ports[16]; // one sample port per host
uint8_t host_protos[16]; // protocol of sample
int host_count = 0;
int total_sessions = 0;
int tcp_sessions = 0;
int udp_sessions = 0;
for (int i = 0; i < MAX_NAT_ENTRIES; i++) {
if (nat_table[i].in_use) {
total_sessions++;
if (nat_table[i].protocol == NAT_PROTO_TCP) tcp_sessions++;
else udp_sessions++;
int found = 0;
for (int j = 0; j < host_count; j++) {
if (active_hosts[j] == nat_table[i].internal_ip) {
found = 1; break;
}
}
if (!found && host_count < 16) {
active_hosts[host_count] = nat_table[i].internal_ip;
host_ports[host_count] = nat_table[i].internal_port;
host_protos[host_count] = nat_table[i].protocol;
host_count++;
}
}
}
// === Header ===
cli_set_color(VGA_YELLOW, VGA_BLACK);
cli_puts("=== NKernel Network Topology ===\n\n");
// === WAN Cloud ===
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" .--------. \n");
cli_puts(" ( INTERNET ) \n");
cli_puts(" '--------' \n");
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" | \n");
cli_puts(" | WAN \n");
// === WAN Interface ===
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts(" [");
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
cli_puts("10.0.2.15");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("] \n");
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" | \n");
// === Router Box ===
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
cli_puts(" +=====================+ \n");
cli_puts(" || ");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("NKernel Router");
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
cli_puts(" || \n");
cli_puts(" || ");
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" NAT | Firewall ");
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
cli_puts(" || \n");
cli_puts(" +=====================+ \n");
// === NAT stats inside router ===
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" Sessions: ");
cli_set_color(VGA_YELLOW, VGA_BLACK);
// Print total_sessions
if (total_sessions >= 100) {
char d[2] = { '0' + (total_sessions / 100), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((total_sessions / 10) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + (total_sessions % 10), '\0' }; cli_puts(d3);
} else if (total_sessions >= 10) {
char d[2] = { '0' + (total_sessions / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (total_sessions % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + total_sessions, '\0' }; cli_puts(d);
}
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" (TCP:");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
if (tcp_sessions >= 10) {
char d[2] = { '0' + (tcp_sessions / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (tcp_sessions % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + tcp_sessions, '\0' }; cli_puts(d);
}
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" UDP:");
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
if (udp_sessions >= 10) {
char d[2] = { '0' + (udp_sessions / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (udp_sessions % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + udp_sessions, '\0' }; cli_puts(d);
}
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(")\n");
// === LAN Interface ===
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" | \n");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts(" [");
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
cli_puts("192.168.1.1");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("] \n");
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" | LAN \n");
// === Switch representation ===
cli_set_color(VGA_LIGHT_BLUE, VGA_BLACK);
cli_puts(" +-----------+ \n");
cli_puts(" | Switch | \n");
cli_puts(" +--+--+--+--+ \n");
// === Host nodes ===
if (host_count == 0) {
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" | \n");
cli_puts(" [No active hosts] \n");
} else {
// Draw branch lines
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" ");
for (int i = 0; i < host_count && i < 4; i++) {
cli_puts(" | ");
}
cli_puts("\n");
// Draw host boxes
for (int i = 0; i < host_count && i < 4; i++) {
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
cli_puts(" [");
print_ip(active_hosts[i]);
cli_puts("]\n");
}
// If more than 4 hosts
if (host_count > 4) {
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" ... and more\n");
}
}
// === NIC Info ===
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts("\n--- Interfaces ---\n");
for (int i = 0; i < num_e1000_devices; i++) {
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts(" en");
char id[2] = { '0' + i, '\0' };
cli_puts(id);
cli_puts(": ");
cli_set_color(VGA_YELLOW, VGA_BLACK);
print_mac(e1000_devs[i].mac);
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" (E1000)\n");
}
}
else if (strncmp(cmd, "nat show", 8) == 0) {
extern struct nat_entry nat_table[MAX_NAT_ENTRIES];
cli_set_color(VGA_YELLOW, VGA_BLACK);
cli_puts("=== NAT Mapping Table ===\n");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
cli_puts(" # Proto Internal IP Int.Port Ext.Port Age\n");
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts("--- ----- ---------------- -------- -------- ----\n");
int count = 0;
for (int i = 0; i < MAX_NAT_ENTRIES; i++) {
if (!nat_table[i].in_use) continue;
count++;
// Index number
cli_set_color(VGA_WHITE, VGA_BLACK);
if (count >= 100) {
char d[2] = { '0' + (count / 100), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((count / 10) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + (count % 10), '\0' }; cli_puts(d3);
} else if (count >= 10) {
cli_puts(" ");
char d[2] = { '0' + (count / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (count % 10), '\0' }; cli_puts(d2);
} else {
cli_puts(" ");
char d[2] = { '0' + count, '\0' }; cli_puts(d);
}
// Protocol
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
if (nat_table[i].protocol == NAT_PROTO_TCP) {
cli_puts(" TCP ");
} else {
cli_puts(" UDP ");
}
// Internal IP
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
print_ip(nat_table[i].internal_ip);
cli_puts(" ");
// Internal Port (big-endian -> host)
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
uint16_t iport = nat_table[i].internal_port;
uint16_t iport_h = (iport << 8) | (iport >> 8);
if (iport_h >= 10000) {
char d[2] = { '0' + (iport_h / 10000), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((iport_h / 1000) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + ((iport_h / 100) % 10), '\0' }; cli_puts(d3);
char d4[2] = { '0' + ((iport_h / 10) % 10), '\0' }; cli_puts(d4);
char d5[2] = { '0' + (iport_h % 10), '\0' }; cli_puts(d5);
} else if (iport_h >= 1000) {
char d[2] = { '0' + (iport_h / 1000), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((iport_h / 100) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + ((iport_h / 10) % 10), '\0' }; cli_puts(d3);
char d4[2] = { '0' + (iport_h % 10), '\0' }; cli_puts(d4);
} else if (iport_h >= 100) {
char d[2] = { '0' + (iport_h / 100), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((iport_h / 10) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + (iport_h % 10), '\0' }; cli_puts(d3);
} else if (iport_h >= 10) {
char d[2] = { '0' + (iport_h / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (iport_h % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + iport_h, '\0' }; cli_puts(d);
}
cli_puts(" ");
// External Port
cli_set_color(VGA_YELLOW, VGA_BLACK);
uint16_t eport = nat_table[i].external_port;
uint16_t eport_h = (eport << 8) | (eport >> 8);
if (eport_h >= 10000) {
char d[2] = { '0' + (eport_h / 10000), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((eport_h / 1000) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + ((eport_h / 100) % 10), '\0' }; cli_puts(d3);
char d4[2] = { '0' + ((eport_h / 10) % 10), '\0' }; cli_puts(d4);
char d5[2] = { '0' + (eport_h % 10), '\0' }; cli_puts(d5);
} else if (eport_h >= 1000) {
char d[2] = { '0' + (eport_h / 1000), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((eport_h / 100) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + ((eport_h / 10) % 10), '\0' }; cli_puts(d3);
char d4[2] = { '0' + (eport_h % 10), '\0' }; cli_puts(d4);
} else if (eport_h >= 100) {
char d[2] = { '0' + (eport_h / 100), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((eport_h / 10) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + (eport_h % 10), '\0' }; cli_puts(d3);
} else if (eport_h >= 10) {
char d[2] = { '0' + (eport_h / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (eport_h % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + eport_h, '\0' }; cli_puts(d);
}
cli_puts(" ");
// Age (last_active tick)
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
uint32_t age = nat_table[i].last_active;
if (age >= 1000) {
char d[2] = { '0' + ((age / 1000) % 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((age / 100) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + ((age / 10) % 10), '\0' }; cli_puts(d3);
char d4[2] = { '0' + (age % 10), '\0' }; cli_puts(d4);
} else if (age >= 100) {
char d[2] = { '0' + (age / 100), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((age / 10) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + (age % 10), '\0' }; cli_puts(d3);
} else if (age >= 10) {
char d[2] = { '0' + (age / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (age % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + age, '\0' }; cli_puts(d);
}
cli_puts("\n");
}
if (count == 0) {
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts(" (No active NAT sessions)\n");
} else {
cli_set_color(VGA_DARK_GRAY, VGA_BLACK);
cli_puts("---\n");
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("Total active mappings: ");
cli_set_color(VGA_YELLOW, VGA_BLACK);
if (count >= 100) {
char d[2] = { '0' + (count / 100), '\0' }; cli_puts(d);
char d2[2] = { '0' + ((count / 10) % 10), '\0' }; cli_puts(d2);
char d3[2] = { '0' + (count % 10), '\0' }; cli_puts(d3);
} else if (count >= 10) {
char d[2] = { '0' + (count / 10), '\0' }; cli_puts(d);
char d2[2] = { '0' + (count % 10), '\0' }; cli_puts(d2);
} else {
char d[2] = { '0' + count, '\0' }; cli_puts(d);
}
cli_puts("\n");
}
}
else if (strncmp(cmd, "cat ", 4) == 0) {
const char *arg = cmd + 4;
// Keep the mocked config loading functionality as fallback/alias for old tests
if (strcmp(arg, "running-config") == 0) {
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("Current Routing Table (in RAM):\n");
cli_execute("route print");
}
else if (strcmp(arg, "startup-config") == 0) {
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts("Saved Routing Table (in ATA Disk):\n");
uint8_t buffer[512] = {0};
extern void ata_read_sector(uint32_t lba, uint8_t *buffer);
ata_read_sector(1, buffer);
uint32_t saved_num_routes = *(uint32_t *)buffer;
if (saved_num_routes > MAX_ROUTES) saved_num_routes = 0;
struct route_entry saved_routes[MAX_ROUTES];
memcpy(saved_routes, buffer + 4, sizeof(saved_routes));
cli_set_color(VGA_YELLOW, VGA_BLACK);
cli_puts("Destination Netmask Next-Hop MAC\n");
cli_puts("--------------------------------------------------\n");
for (uint32_t i = 0; i < saved_num_routes; i++) {
cli_set_color(VGA_LIGHT_GREEN, VGA_BLACK);
print_ip(saved_routes[i].dest_ip);
cli_puts("\t");
cli_set_color(VGA_LIGHT_CYAN, VGA_BLACK);
print_ip(saved_routes[i].netmask);
cli_puts("\t");
cli_set_color(VGA_LIGHT_MAGENTA, VGA_BLACK);
print_mac(saved_routes[i].next_hop_mac);
cli_puts("\n");
}
}
else {
// Check VFS
struct vfs_file *f = vfs_open(arg);
if (f) {
cli_set_color(VGA_WHITE, VGA_BLACK);
cli_puts(f->data);
if (f->size > 0 && f->data[f->size-1] != '\n') cli_puts("\n");
} else {
cli_set_color(VGA_LIGHT_RED, VGA_BLACK);
cli_puts("cat: ");
cli_puts(arg);
cli_puts(": No such file or directory\n");
}
}
}
else if (strncmp(cmd, "edit ", 5) == 0) {
const char *arg = cmd + 5;
editor_start(arg);
}
else {
cli_set_color(VGA_LIGHT_RED, VGA_BLACK);
cli_puts("Unknown command: ");
cli_puts(cmd);
cli_puts("\n");
}
cli_set_color(VGA_WHITE, VGA_BLACK); // Reset to default
}
void cli_autocomplete(char *buf, int *idx, int max_size) {
if (*idx == 0) return;
buf[*idx] = '\0';
// List of known commands
const char *commands[] = {"help", "ifconfig", "route print", "info", "save", "load", "ls", "exit", "clear", "ntop", "nat show", "cat ", "edit "};
int num_cmds = sizeof(commands)/sizeof(commands[0]);
// Check if it's a command
for (int i = 0; i < num_cmds; i++) {
if (strncmp(buf, commands[i], *idx) == 0) {
// Found a match
int cmd_len = strlen(commands[i]);
for (int j = *idx; j < cmd_len && *idx < max_size - 1; j++) {
buf[(*idx)++] = commands[i][j];
char str[2] = {commands[i][j], '\0'};
cli_puts(str);
}
return;
}
}
// If it starts with "cat " or "edit ", try to autocomplete from VFS
if (strncmp(buf, "cat ", 4) == 0 || strncmp(buf, "edit ", 5) == 0) {
int prefix_len = (strncmp(buf, "cat ", 4) == 0) ? 4 : 5;
const char *arg = buf + prefix_len;
int arg_len = *idx - prefix_len;
for (int i = 0; i < MAX_VFS_FILES; i++) {
if (vfs_nodes[i].is_used && strncmp(vfs_nodes[i].name, arg, arg_len) == 0) {
int name_len = strlen(vfs_nodes[i].name);
for (int j = arg_len; j < name_len && *idx < max_size - 1; j++) {
buf[(*idx)++] = vfs_nodes[i].name[j];
char str[2] = {vfs_nodes[i].name[j], '\0'};
cli_puts(str);
}
return;
}
}
}
}