<commit_before>/* minIP */
/* Written by <NAME> */

// Linux compile: gcc minIP.c -o minIP
// Linux usage: ./minIP eth1 192.168.0.99 255.255.255.0 192.168.0.1
// BareMetal compile:
// gcc -I newlib-2.4.0/newlib/libc/include/ -c minIP.c -o minIP.o -DBAREMETAL
// ld -T app.ld -o minIP.app crt0.o minIP.o libc.a libBareMetal.o

#define __USE_MISC

/* Global Includes */
#ifdef BAREMETAL
#include "libBareMetal.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <netpacket/packet.h>
#include <fcntl.h>
#endif

/* Typedefs */
#ifdef BAREMETAL
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
#else
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#endif

/* Global functions */
u16 checksum(u8* data, u16 bytes);
u16 checksum_tcp(u8* data, u16 bytes, u16 protocol, u16 length);
int net_init(char *interface);
int net_exit();
int net_send(unsigned char* data, unsigned int bytes);
int net_recv(unsigned char* data);
u16 swap16(u16 in);
u32 swap32(u32 in);
#ifdef BAREMETAL
void* memset( void* s, int c, int n );
void* memcpy( void* d, const void* s, int n );
int strlen( const char* s );
#endif

/* Global defines */
#undef ETH_FRAME_LEN
#define ETH_FRAME_LEN 1518
#define ETHERTYPE_ARP 0x0806
#define ETHERTYPE_IPv4 0x0800
#define ETHERTYPE_IPv6 0x86DD
#define ARP_REQUEST 1
#define ARP_REPLY 2
#define PROTOCOL_IP_ICMP 1
#define PROTOCOL_IP_TCP 6
#define PROTOCOL_IP_UDP 11
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
#define TCP_ACK 16
#define TCP_PSH 8
#define TCP_RST 4
#define TCP_SYN 2
#define TCP_FIN 1

/* Global variables */
u8 src_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 dst_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 dst_broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u8 src_IP[4] = {0, 0, 0, 0};
u8 src_SN[4] = {0, 0, 0, 0};
u8 src_GW[4] = {0, 0, 0, 0};
u8 dst_IP[4] = {0, 0, 0, 0};
unsigned char buffer[ETH_FRAME_LEN];
unsigned char tosend[ETH_FRAME_LEN];
int s; // Socket variable
int running = 1, c, retval;
unsigned int tint, tint0, tint1, tint2, tint3;
#ifndef BAREMETAL
struct sockaddr_ll sa;
struct ifreq ifr;
#endif

/* Global structs */
#pragma pack(1)
typedef struct eth_header {
	u8 dest_mac[6];
	u8 src_mac[6];
	u16 type;
} eth_header; // 14 bytes
typedef struct arp_packet {
	eth_header ethernet;
	u16 hardware_type;
	u16 protocol;
	u8 hardware_size;
	u8 protocol_size;
	u16 opcode;
	u8 sender_mac[6];
	u8 sender_ip[4];
	u8 target_mac[6];
	u8 target_ip[4];
} arp_packet; // 28 bytes
typedef struct ipv4_packet {
	eth_header ethernet;
	u8 version;
	u8 dsf;
	u16 total_length;
	u16 id;
	u16 flags;
	u8 ttl;
	u8 protocol;
	u16 checksum;
	u8 src_ip[4];
	u8 dest_ip[4];
} ipv4_packet; // 20 bytes since we don't support options
typedef struct icmp_packet {
	ipv4_packet ipv4;
	u8 type;
	u8 code;
	u16 checksum;
	u16 id;
	u16 sequence;
	u64 timestamp;
	u8 data[2]; // Set to 2 so can be used as pointer
} icmp_packet;
typedef struct udp_packet {
	ipv4_packet ipv4;
	u16 src_port;
	u16 dest_port;
	u16 length;
	u16 checksum;
	u8 data[2]; // Set to 2 so can be used as pointer
} udp_packet;
typedef struct tcp_packet {
	ipv4_packet ipv4;
	u16 src_port;
	u16 dest_port;
	u32 seqnum;
	u32 acknum;
	u8 data_offset;
	u8 flags;
	u16 window;
	u16 checksum;
	u16 urg_pointer;
	// Options and data
//	u8 data[2]; // Set to 2 so can be used as pointer
} tcp_packet;

/* Default HTTP page with HTTP headers */
char webpage[] =
"HTTP/1.0 200 OK\n"
"Server: BareMetal (http://www.returninfinity.com)\n"
"Content-type: text/html\n"
"\n"
"<!DOCTYPE html>\n"
"<html>\n"
"   <head>\n"
"       <title>minIP</title>\n"
"       <link rel=\"stylesheet\" href=\"http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css\">\n"
"       <link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>\n"
"       <style>\n"
"           body, h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {\n"
"           font-family: \"Avenir Next\", \"Roboto\", sans-serif;\n"
"           line-height: 1.5;\n"
"           }\n"
"       </style>\n"
"   </head>\n"
"   <body>\n"
"       <div class=\"container\">\n"
"           <h1>Hello, from minIP!</h1>\n"
"           <h3><a href=\"https://github.com/IanSeyler/minIP/\">minIP on GitHub</a></h3>\n"
"           <p>minIP is a tiny TCP/IP stack implementation in C.</p>\n"
"           <p>It cointains just enough code to serve this webpage.</p>\n"
"       </div>\n"
"   </body>\n"
"</html>\n";

/* Main code */
int main(int argc, char *argv[])
{
	#ifdef BAREMETAL
		b_output("minIP v0.5 (2017 04 18)\n");
	#else
	printf("minIP v0.5 (2017 04 18)\n");
	printf("Written by <NAME> @ Return Infinity\n\n");

	/* first argument needs to be a NIC */<commit_msg>Update for usage without newly on BareMetal<commit_after>/* minIP */
/* Written by <NAME> */

// Linux compile: gcc minIP.c -o minIP
// Linux usage: ./minIP eth1 192.168.0.99 255.255.255.0 192.168.0.1

// BareMetal compile (with newlib):
// gcc -I newlib-2.4.0/newlib/libc/include/ -c minIP.c -o minIP.o -DBAREMETAL
// ld -T app.ld -o minIP.app crt0.o minIP.o libc.a libBareMetal.o

// BareMetal compile (standalone):
// ./build.sh

#define __USE_MISC

/* Global Includes */
#ifdef BAREMETAL
#include "libBareMetal.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <netpacket/packet.h>
#include <fcntl.h>
#endif

/* Typedefs */
#ifdef BAREMETAL
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
#else
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#endif

/* Global functions */
u16 checksum(u8* data, u16 bytes);
u16 checksum_tcp(u8* data, u16 bytes, u16 protocol, u16 length);
int net_init(char *interface);
int net_exit();
int net_send(unsigned char* data, unsigned int bytes);
int net_recv(unsigned char* data);
u16 swap16(u16 in);
u32 swap32(u32 in);
#ifdef BAREMETAL
void* memset( void* s, int c, int n );
void* memcpy( void* d, const void* s, int n );
int strlen( const char* s );
#endif

/* Global defines */
#undef ETH_FRAME_LEN
#define ETH_FRAME_LEN 1518
#define ETHERTYPE_ARP 0x0806
#define ETHERTYPE_IPv4 0x0800
#define ETHERTYPE_IPv6 0x86DD
#define ARP_REQUEST 1
#define ARP_REPLY 2
#define PROTOCOL_IP_ICMP 1
#define PROTOCOL_IP_TCP 6
#define PROTOCOL_IP_UDP 11
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
#define TCP_ACK 16
#define TCP_PSH 8
#define TCP_RST 4
#define TCP_SYN 2
#define TCP_FIN 1

/* Global variables */
u8 src_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 dst_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 dst_broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u8 src_IP[4] = {0, 0, 0, 0};
u8 src_SN[4] = {0, 0, 0, 0};
u8 src_GW[4] = {0, 0, 0, 0};
u8 dst_IP[4] = {0, 0, 0, 0};
unsigned char buffer[ETH_FRAME_LEN];
unsigned char tosend[ETH_FRAME_LEN];
int s; // Socket variable
int running = 1, c, retval;
unsigned int tint, tint0, tint1, tint2, tint3;
#ifndef BAREMETAL
struct sockaddr_ll sa;
struct ifreq ifr;
#endif

/* Global structs */
#pragma pack(1)
typedef struct eth_header {
	u8 dest_mac[6];
	u8 src_mac[6];
	u16 type;
} eth_header; // 14 bytes
typedef struct arp_packet {
	eth_header ethernet;
	u16 hardware_type;
	u16 protocol;
	u8 hardware_size;
	u8 protocol_size;
	u16 opcode;
	u8 sender_mac[6];
	u8 sender_ip[4];
	u8 target_mac[6];
	u8 target_ip[4];
} arp_packet; // 28 bytes
typedef struct ipv4_packet {
	eth_header ethernet;
	u8 version;
	u8 dsf;
	u16 total_length;
	u16 id;
	u16 flags;
	u8 ttl;
	u8 protocol;
	u16 checksum;
	u8 src_ip[4];
	u8 dest_ip[4];
} ipv4_packet; // 20 bytes since we don't support options
typedef struct icmp_packet {
	ipv4_packet ipv4;
	u8 type;
	u8 code;
	u16 checksum;
	u16 id;
	u16 sequence;
	u64 timestamp;
	u8 data[2]; // Set to 2 so can be used as pointer
} icmp_packet;
typedef struct udp_packet {
	ipv4_packet ipv4;
	u16 src_port;
	u16 dest_port;
	u16 length;
	u16 checksum;
	u8 data[2]; // Set to 2 so can be used as pointer
} udp_packet;
typedef struct tcp_packet {
	ipv4_packet ipv4;
	u16 src_port;
	u16 dest_port;
	u32 seqnum;
	u32 acknum;
	u8 data_offset;
	u8 flags;
	u16 window;
	u16 checksum;
	u16 urg_pointer;
	// Options and data
//	u8 data[2]; // Set to 2 so can be used as pointer
} tcp_packet;

/* Default HTTP page with HTTP headers */
char webpage[] =
"HTTP/1.0 200 OK\n"
"Server: BareMetal (http://www.returninfinity.com)\n"
"Content-type: text/html\n"
"\n"
"<!DOCTYPE html>\n"
"<html>\n"
"   <head>\n"
"       <title>minIP</title>\n"
"       <link rel=\"stylesheet\" href=\"http://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">\n"
"       <link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>\n"
"       <style>\n"
"           body, h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {\n"
"           font-family: \"Avenir Next\", \"Roboto\", sans-serif;\n"
"           line-height: 1.5;\n"
"           }\n"
"       </style>\n"
"   </head>\n"
"   <body>\n"
"       <div class=\"container\">\n"
"           <h1>Hello, from minIP!</h1>\n"
"           <h3><a href=\"https://github.com/IanSeyler/minIP/\">minIP on GitHub</a></h3>\n"
"           <p>minIP is a tiny TCP/IP stack implementation in C.</p>\n"
"           <p>It cointains just enough code to serve this webpage.</p>\n"
"       </div>\n"
"   </body>\n"
"</html>\n";

/* Main code */
int main(int argc, char *argv[])
{
	#ifdef BAREMETAL
	b_output("minIP v0.5 (2017 04 18)\n");
	src_IP[0] = 10;
	src_IP[1] = 0;
	src_IP[2] = 0;
	src_IP[3] = 10;
	src_SN[0] = 255;
	src_SN[1] = 255;
	src_SN[2] = 255;
	src_SN[3] = 0;
	src_GW[0] = 10;
	src_GW[1] = 0;
	src_GW[2] = 0;
	src_GW[3] = 1;
	#else
	printf("minIP v0.5 (2017 04 18)\n");
	printf("Written by <NAME> @ Return Infinity\n\n");

	/* first argument needs to be a NIC */
{'fraction_non_alphanumeric': 0.15026266230548121, 'fraction_numerical': 0.06948161363861631, 'mean_word_length': 3.6178489702517163, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4562381', 'n_tokens_mistral': 4715, 'n_tokens_neox': 4264, 'n_words': 1198}