Permalink
Browse files

unet chat

  • Loading branch information...
vialamo committed Mar 26, 2017
1 parent 514c0f1 commit 41e3f042547851b8aa52210668f7cb5551254f9e
Showing with 107 additions and 16 deletions.
  1. +1 −1 source/kernel.c
  2. +1 −1 source/kernel.h
  3. +26 −11 source/net.c
  4. +79 −3 source/programs/unet.c
View
@@ -1277,7 +1277,7 @@ static void execute(uchar* str)
io_clear_screen();
io_hide_cursor();
putstr("Turn off computer\n\r");
return; /* Go to halt() */
halt(); /* Halt() */
} else if(argc == 2 && strcmp(argv[1], "reboot") == 0) {
reboot(); /* Reboot computer */
putstr("Reboot not supported\n\r");
View
@@ -7,7 +7,7 @@
#define OS_VERSION_HI 2
#define OS_VERSION_LO 0
#define OS_BUILD_NUM 19
#define OS_BUILD_NUM 20
/*
* Hardware related disk information is handled by the kernel module.
View
@@ -574,9 +574,19 @@ uint provide_mac_address(uint8_t* ip)
*/
uint udp_send(uint8_t* dst_ip, uint src_port, uint dst_port,
uint8_t protocol, uint8_t* data, uint len)
{
uint head_len = sizeof(struct UDPhdr);
struct UDPhdr* uh = &snd_buff[12];
{
struct UDPIPhdr {
uint8_t sender[4];
uint8_t recver[4];
uint8_t zero;
uint8_t protocol;
uint16_t len;
};
uint head_len = sizeof(struct UDPhdr);
uint iphead_len = sizeof(struct UDPIPhdr);
struct UDPIPhdr* ih = snd_buff;
struct UDPhdr* uh = &snd_buff[iphead_len];
uint checksum = 0;
/* Provide hw addresss before process */
@@ -586,20 +596,25 @@ uint udp_send(uint8_t* dst_ip, uint src_port, uint dst_port,
return 1;
}
/* Clamp len */
len = min(sizeof(snd_buff) - iphead_len-head_len -
sizeof(struct IPhdr) - sizeof(struct ethhdr),
len);
/* Generate UDP packet and send */
memset(snd_buff, 0, sizeof(snd_buff));
memcpy(&(snd_buff[12+head_len]), data, len);
memcpy(&(snd_buff[iphead_len+head_len]), data, len);
uh->srcPort = bswap_16(src_port);
uh->dstPort = bswap_16(dst_port);
uh->len = bswap_16(len+head_len);
memcpy(&snd_buff[0], local_ip, sizeof(local_ip));
memcpy(&snd_buff[4], dst_ip, sizeof(local_ip));
snd_buff[8] = 0;
snd_buff[9] = IP_PROTOCOL_UDP;
memcpy(&snd_buff[10], &uh->len, sizeof(uh->len));
checksum = net_checksum(snd_buff, 12+len+head_len);
memcpy(ih->sender, local_ip, sizeof(ih->sender));
memcpy(ih->recver, dst_ip, sizeof(ih->recver));
ih->zero = 0;
ih->protocol = IP_PROTOCOL_UDP;
ih->len = uh->len;
checksum = net_checksum(snd_buff, iphead_len+len+head_len);
uh->checksum = bswap_16(checksum);
return ip_send(dst_ip, IP_PROTOCOL_UDP, uh, head_len+len);
}
@@ -719,7 +734,7 @@ void ne2k_receive()
outb(0x12, base + NE2K_CR); /* Read and start */
for(i=0; i<4; i++) {
((uchar*)&info)[i] = inb(base + NE2K_DATA);
((uint8_t*)&info)[i] = inb(base + NE2K_DATA);
}
/* Get the data */
View
@@ -30,7 +30,7 @@ uint main(uint argc, uchar* argv[])
buff[result] = 0;
/* Show source address and contents */
putstr("Received %s from %d.%d.%d.%d\n\r", buff,
putstr("Received %s from %u.%u.%u.%u\n\r", buff,
src_ip[0], src_ip[1], src_ip[2], src_ip[3]);
}
} else if(argc == 4 && strcmp(argv[1], "send") == 0) {
@@ -44,14 +44,90 @@ uint main(uint argc, uchar* argv[])
/* If result == 0, then data was sent */
if(result == 0) {
putstr("Sent %s to %d.%d.%d.%d\n\r", argv[3],
putstr("Sent %s to %u.%u.%u.%u\n\r", argv[3],
dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3]);
} else {
/* Else, failed */
putstr("Failed to send\n\r");
}
/* Live node to node chat */
} else if(argc == 3 && strcmp(argv[1], "chat") == 0) {
uint8_t dst_ip[4];
uchar send_buff[256];
/* Initialize buffer */
memset(send_buff, 0, sizeof(send_buff));
/* Parse IP from string */
str_to_ip(dst_ip, argv[2]);
/* Show banner */
putstr("Chat with %u.%u.%u.%u. Press ESC to exit\n\r",
dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3]);
/* Main loop */
while(1) {
uint k;
uchar recv_buff[256];
uint8_t recv_ip[4];
/* Receive in buffer */
result = recv(recv_ip, recv_buff, sizeof(recv_buff));
/* If received something and source is desired source... */
if(result != 0 &&
memcmp(recv_ip, dst_ip, sizeof(dst_ip)) == 0) {
/* Show source address and contents */
putstr("\r%d.%d.%d.%d: %s\n\r",
dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3],
recv_buff);
}
/* Process keyboard input */
k = getkey(KM_NO_WAIT);
/* Exit if key ESC */
if(k == KEY_ESC) {
break;
}
/* Send if key RETURN */
else if(k == KEY_RETURN) {
result = send(dst_ip, send_buff, strlen(send_buff));
/* If result == 0, then data was sent */
if(result == 0) {
putstr("\rlocal: %s\n\r", send_buff);
} else {
/* Else, failed */
putstr("\rFailed to send\n\r");
}
/* Reset buffer */
memset(send_buff, 0, sizeof(send_buff));
}
/* Delete last char */
else if((k == KEY_BACKSPACE || k == KEY_DEL) &&
strlen(send_buff) > 0) {
send_buff[strlen(send_buff)-1] = 0;
putstr("\r \r%s", send_buff);
}
/* Append char */
else if(k >= ' ' && k <= '}') {
send_buff[min(sizeof(send_buff), strlen(send_buff))] = getLO(k);
putstr("\r%s", send_buff);
}
}
/* Finished */
putstr("-ESC-\n\r");
} else {
putstr("usage: %s <send <dst_ip> <word> | recv>\n\r", argv[0]);
putstr("usage: %s <send <dst_ip> <word> | recv | chat <dst_ip>>\n\r", argv[0]);
}
return result;

0 comments on commit 41e3f04

Please sign in to comment.