net: ongoing network work

This commit is contained in:
K. Lange 2021-09-09 19:40:43 +09:00
parent 530b9798af
commit c0cb37f971
11 changed files with 83 additions and 49 deletions

View File

@ -15,6 +15,9 @@
#include <unistd.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <toaru/hashmap.h>

View File

@ -15,6 +15,7 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
extern char * _argv_0;

View File

@ -14,6 +14,9 @@
#include <time.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/fswait.h>
#define _ITALIC "\033[3m"

View File

@ -12,14 +12,8 @@
#include <stdlib.h>
#include <getopt.h>
#include <sys/socket.h>
static void ip_ntoa(uint32_t src_addr, char * out) {
sprintf(out, "%d.%d.%d.%d",
(unsigned int)((src_addr & 0xFF000000) >> 24),
(unsigned int)((src_addr & 0xFF0000) >> 16),
(unsigned int)((src_addr & 0xFF00) >> 8),
(unsigned int)((src_addr & 0xFF)));
}
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char * argv[]) {
if (argc < 2) return 1;
@ -31,8 +25,7 @@ int main(int argc, char * argv[]) {
return 1;
}
char addr[16] = {0};
ip_ntoa(ntohl(*(uint32_t *)host->h_addr_list[0]), addr);
char * addr = inet_ntoa(*(struct in_addr*)host->h_addr_list[0]);
fprintf(stderr, "%s: %s\n", host->h_name, addr);
return 0;

View File

@ -31,6 +31,7 @@
#include <sys/fswait.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <toaru/yutani.h>
#include <toaru/graphics.h>

View File

@ -3,9 +3,20 @@
#include <_cheader.h>
#include <stdint.h>
#include <sys/types.h>
#include <netinet/in.h>
_Begin_C_Header
#define INADDR_ANY (unsigned long int)0x0
#define INADDR_BROADCAST (unsigned long int)0xFFffFFffUL
#ifndef _KERNEL_
extern uint32_t htonl(uint32_t hostlong);
extern uint16_t htons(uint16_t hostshort);
extern uint32_t ntohl(uint32_t netlong);
extern uint16_t ntohs(uint16_t netshort);
#endif
_End_C_Header

View File

@ -16,6 +16,20 @@ extern int getaddrinfo(const char *node, const char *service,
extern void freeaddrinfo(struct addrinfo *res);
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
};
extern struct hostent * gethostbyname(const char * name);
#ifndef _KERNEL_
#define h_addr h_addr_list[0]
#endif
#define NI_NUMERICHOST 1
#define NI_MAXHOST 255

View File

@ -0,0 +1,24 @@
#pragma once
#include <_cheader.h>
#include <stdint.h>
_Begin_C_Header
typedef uint32_t in_addr_t;
typedef uint16_t in_port_t;
struct in_addr {
in_addr_t s_addr;
};
struct sockaddr_in {
short sin_family; // e.g. AF_INET, AF_INET6
unsigned short sin_port; // e.g. htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
_End_C_Header

View File

@ -21,21 +21,8 @@ _Begin_C_Header
#define SO_KEEPALIVE 1
#define SO_REUSEADDR 2
#define SO_BINDTODEVICE 3
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
};
#ifndef _KERNEL_
#define h_addr h_addr_list[0]
#endif
typedef size_t socklen_t;
struct sockaddr {
@ -43,17 +30,6 @@ struct sockaddr {
char sa_data[14]; // 14 bytes of protocol address
};
struct in_addr {
unsigned long s_addr; // load with inet_pton()
};
struct sockaddr_in {
short sin_family; // e.g. AF_INET, AF_INET6
unsigned short sin_port; // e.g. htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};
struct addrinfo {
int ai_flags;
int ai_family;
@ -85,12 +61,7 @@ struct sockaddr_storage {
char _ss_pad[128];
};
typedef uint32_t in_addr_t;
typedef uint16_t in_port_t;
#ifndef _KERNEL_
extern struct hostent * gethostbyname(const char * name);
extern ssize_t recv(int sockfd, void *buf, size_t len, int flags);
extern ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
@ -101,11 +72,6 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
extern int socket(int domain, int type, int protocol);
extern uint32_t htonl(uint32_t hostlong);
extern uint16_t htons(uint16_t hostshort);
extern uint32_t ntohl(uint32_t netlong);
extern uint16_t ntohs(uint16_t netshort);
extern int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
extern int accept(int sockfd, struct sockaddr * addr, socklen_t * addrlen);
extern int accept4(int sockfd, struct sockaddr * addr, socklen_t * addrlen, int flags);

View File

@ -20,13 +20,14 @@
#include <kernel/net/ipv4.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...) if (_debug) printf(__VA_ARGS__)
//#define printf(...)
#endif
#define DEFAULT_TCP_WINDOW_SIZE 8000
#define DEFAULT_TCP_WINDOW_SIZE 65535
static int _debug __attribute__((unused)) = 0;
@ -222,7 +223,7 @@ static int tcp_ack(fs_node_t * nic, sock_t * sock, struct ipv4_packet * packet,
printf("tcp: their seq = %u our ack = %u\n",
ntohl(tcp->seq_number), sock->priv32[1]);
#endif
window_size = 300;
//window_size = 300;
retval = 0;
send_thrice = 1;
} else {

View File

@ -12,6 +12,9 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <syscall.h>
#include <syscall_nums.h>
@ -163,7 +166,7 @@ int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
return -1;
}
static uint32_t ip_aton(const char * in) {
in_addr_t inet_addr(const char * in) {
char ip[16];
char * c = ip;
uint32_t out[4];
@ -191,6 +194,20 @@ static uint32_t ip_aton(const char * in) {
return htonl((out[0] << 24) | (out[1] << 16) | (out[2] << 8) | (out[3]));
}
char * inet_ntoa(struct in_addr in) {
static char buf[17];
uint32_t hostOrder = ntohl(in.s_addr);
snprintf(buf,17,"%d.%d.%d.%d",
(hostOrder >> 24) & 0xFF,
(hostOrder >> 16) & 0xFF,
(hostOrder >> 8) & 0xFF,
(hostOrder >> 0) & 0xFF);
return buf;
}
static struct hostent _hostent = {0};
static uint32_t _hostent_addr = 0;
static char * _host_entry_list[1] = {0};
@ -231,7 +248,7 @@ struct hostent * gethostbyname(const char * name) {
_hostent.h_length = sizeof(uint32_t);
_hostent.h_addr_list = _host_entry_list;
_host_entry_list[0] = (char*)&_hostent_addr;
_hostent_addr = ip_aton(name);
_hostent_addr = inet_addr(name);
return &_hostent;
}
@ -258,7 +275,7 @@ struct hostent * gethostbyname(const char * name) {
}
/* Try to convert so we can connect... */
uint32_t ns_addr = ip_aton(tmp + strlen("nameserver "));
uint32_t ns_addr = inet_addr(tmp + strlen("nameserver "));
/* Form a DNS request */
char dat[256];