Add stub network functions - headers need reorg

This commit is contained in:
K. Lange 2018-05-04 12:40:18 +09:00
parent 6bba5dd59e
commit 6d5d88cf53
3 changed files with 273 additions and 0 deletions

39
apps/nslookup.c Normal file
View File

@ -0,0 +1,39 @@
/* vim: ts=4 sw=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2016 Kevin Lange
*
* nslookup - perform nameserver lookups
*
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#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)));
}
int main(int argc, char * argv[]) {
if (argc < 2) return 1;
struct hostent * host = gethostbyname(argv[1]);
if (!host) {
fprintf(stderr, "%s: not found\n", argv[1]);
return 1;
}
char addr[16] = {0};
ip_ntoa(*(uint32_t *)host->h_addr_list[0], addr);
fprintf(stderr, "%s: %s\n", host->h_name, addr);
return 0;
}

View File

@ -0,0 +1,108 @@
#pragma once
#include <stdint.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#define AF_INET 1
#define AF_UNSPEC 0
#define SOCK_STREAM 1
#define SOCK_DGRAM 2
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
#define SOL_SOCKET 0
#define SO_KEEPALIVE 1
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 */
};
struct hostent * gethostbyname(const char * name);
typedef size_t socklen_t;
struct sockaddr {
unsigned short sa_family; // address family, AF_xxx
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;
int ai_socktype;
int ai_protocol;
socklen_t ai_addrlen;
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next;
};
struct iovec { /* Scatter/gather array items */
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
struct msghdr {
void *msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void *msg_control; /* ancillary data, see below */
size_t msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */
};
typedef uint32_t in_addr_t;
typedef uint16_t in_port_t;
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
int socket(int domain, int type, int protocol);
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int accept(int sockfd, struct sockaddr * addr, socklen_t * addrlen);
int listen(int sockfd, int backlog);
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
#ifdef __cplusplus
}
#endif

126
libc/sys/network.c Normal file
View File

@ -0,0 +1,126 @@
/*
* socket methods (mostly unimplemented)
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
static struct hostent _out_host = {0};
static struct in_addr * _out_host_vector[2] = {NULL, NULL};
static struct in_addr * _out_host_aliases[1] = {NULL};
static uint32_t _out_addr;
#define UNIMPLEMENTED fprintf(stderr, "[libnetwork] Unimplemented: %s\n", __FUNCTION__)
struct hostent * gethostbyname(const char * name) {
if (_out_host.h_name) free(_out_host.h_name);
_out_host.h_name = strdup(name);
int fd = open("/dev/net",O_RDONLY);
void * args[2] = {(void *)name,&_out_addr};
int ret = ioctl(fd, 0x5000, args);
close(fd);
if (ret) return NULL;
_out_host_vector[0] = (struct in_addr *)&_out_addr;
_out_host.h_aliases = (char **)&_out_host_aliases;
_out_host.h_addrtype = AF_INET;
_out_host.h_addr_list = (char**)_out_host_vector;
_out_host.h_length = sizeof(uint32_t);
return &_out_host;
}
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
UNIMPLEMENTED;
return -1;
}
/* All of these should just be reads. */
ssize_t recv(int sockfd, void *buf, size_t len, int flags) {
UNIMPLEMENTED;
return -1;
}
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) {
UNIMPLEMENTED;
return -1;
}
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) {
UNIMPLEMENTED;
return -1;
}
ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
UNIMPLEMENTED;
return len;
}
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) {
UNIMPLEMENTED;
return len;
}
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) {
UNIMPLEMENTED;
return -1;
}
int socket(int domain, int type, int protocol) {
UNIMPLEMENTED;
return -1;
}
uint32_t htonl(uint32_t hostlong) {
return ( (((hostlong) & 0xFF) << 24) | (((hostlong) & 0xFF00) << 8) | (((hostlong) & 0xFF0000) >> 8) | (((hostlong) & 0xFF000000) >> 24));
}
uint16_t htons(uint16_t hostshort) {
return ( (((hostshort) & 0xFF) << 8) | (((hostshort) & 0xFF00) >> 8) );
}
uint32_t ntohl(uint32_t netlong) {
return htonl(netlong);
}
uint16_t ntohs(uint16_t netshort) {
return htons(netshort);
}
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
UNIMPLEMENTED;
return -1;
}
int accept(int sockfd, struct sockaddr * addr, socklen_t * addrlen) {
UNIMPLEMENTED;
return -1;
}
int listen(int sockfd, int backlog) {
UNIMPLEMENTED;
return -1;
}
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
UNIMPLEMENTED;
return -1;
}
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
return -1;
}
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) {
UNIMPLEMENTED;
return -1;
}
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) {
UNIMPLEMENTED;
return -1;
}