summaryrefslogtreecommitdiff
path: root/net/inet.c
blob: 88698c52e104b6d9869231802367a99f34dfdc59 (plain)
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
#include "inet.h"
#include "icmp.h"
#include "kmalloc.h"
#include "udp.h"
#include "ntp.h"

uint16_t ntohs(uint16_t val)
{
    return val<<8|val>>8;
}
uint16_t htons(uint16_t val)
{
    return val<<8|val>>8;
}

uint16_t checksum(void *addr, int count)
{
    /* Compute Internet Checksum for "count" bytes
     *         beginning at location "addr".
     * Taken from https://tools.ietf.org/html/rfc1071
     */

    register uint32_t sum = 0;
    uint16_t * ptr = addr;

    while( count > 1 )  {
        /*  This is the inner loop */
        //sum += ntohs(* ptr++);
        sum += *ptr++;
        count -= 2;
    }

    /*  Add left-over byte, if any */
    if( count > 0 )
        sum += * (uint8_t *) ptr;

    /*  Fold 32-bit sum to 16 bits */
    while (sum>>16)
        sum = (sum & 0xffff) + (sum >> 16);

    return ~sum;
}

bool net_packet(struct netdev *dev)
{
    uint32_t packet=kballoc(1)+4096; // we start one byte after the end;
    //
    uint32_t addr=(144)+(76<<8)+(106<<16)+(46<<24);
    uint32_t google_time=216+(239<<8)+(35<<16)+(0<<24);
    // this should of course be filled dynamically!
    //uint32_t pos=icmp_ping(dev,addr,packet,packet); // create ping packet and get new position;
    //uint32_t pos=udp_generic(dev, addr, 666, 123, packet,packet);
    uint32_t pos=ntp_generic(dev, google_time, 6666, 123, packet,packet);

    //
    dev->transmit(pos,(packet-pos));

    kbfree(packet-4096); // TODO: take care it is not overwritten before transmitted!
    return true;
}