-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathip.c
104 lines (85 loc) · 2.47 KB
/
ip.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* ip.c
*
* 2006 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <arpa/inet.h>
#include <netpacket/packet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include "sys.h"
int ip_build_header(struct nc_buff *ncb)
{
struct iphdr *iph;
struct netchannel *nc = ncb->nc;
struct netchannel_addr *src = &nc->ctl.saddr;
struct netchannel_addr *dst = &nc->ctl.daddr;
ncb->nh.iph = iph = ncb_push(ncb, sizeof(struct iphdr));
if (!iph)
return -ENOMEM;
memcpy(&iph->saddr, src->addr, sizeof(iph->saddr));
memcpy(&iph->daddr, dst->addr, sizeof(iph->daddr));
iph->check = 0;
iph->tos = 0x10;
iph->tot_len = htons(ncb->len);
iph->ttl = 64;
iph->id = rand();
iph->frag_off = htons(0x4000);
iph->version = 4;
iph->ihl = 5;
iph->protocol = src->proto;
iph->check = in_csum((__u16 *)iph, iph->ihl*4);
return 0;
}
int ip_send_data(struct nc_buff *ncb)
{
int err;
err = ip_build_header(ncb);
if (err < 0)
return err;
return transmit_data(ncb);
}
int packet_ip_process(struct nc_buff *ncb)
{
struct iphdr *iph;
struct netchannel *nc = ncb->nc;
struct netchannel_addr *src = &nc->ctl.saddr;
struct netchannel_addr *dst = &nc->ctl.daddr;
ncb->nh.iph = iph = ncb_pull(ncb, sizeof(struct iphdr));
if (!iph)
return -ENOMEM;
ncb_pull(ncb, iph->ihl * 4 - sizeof(struct iphdr));
ncb_trim(ncb, ntohs(iph->tot_len) - iph->ihl * 4);
if (memcmp(&iph->saddr, dst->addr, 4))
return -EINVAL;
if (memcmp(&iph->daddr, src->addr, 4))
return -EINVAL;
ncb_queue_tail(&ncb->nc->recv_queue, ncb);
ncb->nc->hit++;
return 0;
}