-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
218 lines (173 loc) · 4.79 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "test.h"
#include "sys\mbuf.h"
#include "net\slip.h"
#include "net\if.h"
#include "net\if_slvar.h"
#include "netinet\if_ether.h"
#include <stdio.h>
#include <string.h>
#include "hp300\dev\device.h"
#include "hp300\include\endian.h"
#include "sys\socketvar.h"
#include "sys\tty.h"
//#include "hp300\dev\device.h"
#define N 1024
#define CHAPTER1
#define CHAPTER2
#define CHAPTER3
#define CHAPTER3
#define CHAPTER4
#define CHAPTER5
#define CHAPTER6
#define CHAPTER7
#define CHAPTER8
#define CHAPTER9
#define CHAPTER10
#define CHAPTER11
#define CHAPTER12
#define CHAPTER13
#define CHAPTER14
int main(void)
{
char buf[N] = {'\0'};
char *p = "it's a test of mbuf on T460s, end...";
#ifdef CHAPTER2
{
struct mbuf *m1;
struct mbuf *m2;
MGET(m1, 0, MT_HEADER);
MGET(m2, 0, MT_DATA);
m_copyback(m1, 0, strlen(p), p);
print_mbuf(m1, 0, 0);
print_mbuf_content(m1);
m_copyback(m2, 4, strlen(p), p);
print_mbuf(m2, 4, 0);
print_mbuf_content(m2);
m_adj(m1, -5);
print_mbuf(m1, 0, 0);
print_mbuf_content(m1);
m_cat(m1, m2);
print_mbuf(m1, 0, 0);
print_mbuf_content(m1);
m_copydata(m1, 4, 5, buf);
print_mbuf(m1, 0, 0);
print_mbuf_content(m1);
printf("%s\n", buf);
struct mbuf *m3 = m_copym(m2, 15, 40, 0);
print_mbuf(m3, 0, 0);
print_mbuf_content(m3);
struct ifnet ifnet1;
struct mbuf *m4 = m_devget(buf, 20, 5, &ifnet1, NULL);
print_mbuf(m4, 0, 0);
print_mbuf_content(m4);
}
#endif // CHAPTER2
#ifdef CHAPTER3
{
struct hp_device dev;
dev.hp_addr = "hp_addr";
dev.hp_unit = 0;
leattach(&dev);
print_global_ifnet();
print_global_ifaddr();
loopattach(6);
print_global_ifnet();
print_global_ifaddr();
slattach();
print_global_ifnet();
print_global_ifaddr();
ifinit();
print_global_ifnet();
}
#endif // CHAPTER3
#ifdef CHAPTER4
{
int unit = 5;
char buf[1024];
int len = 100;
struct mbuf *m1;
struct ether_header eh;
// struct mbuf *m2;
// m1->m_flags = M_PKTHDR;
eh.ether_type = htons(ETHERTYPE_IP);
memcpy(eh.ether_dhost, etherbroadcastaddr, sizeof etherbroadcastaddr);
MGET(m1, 0, MT_HEADER);
// MGET(m2, 0, MT_DATA);
m_copyback(m1, 0, strlen(p), p);
print_mbuf(m1, 0, strlen(p));
memcpy(buf, &eh, sizeof eh);
memcpy(buf + sizeof eh, m1, sizeof *m1);
leread(unit, buf, len); // ether_input() is called, so no test for ether_input()
print_mbuf(m1, 0, len);
struct ifnet ifnet1;
memset(&ifnet1, 0, sizeof (ifnet1));
ifnet1.if_flags |= IFF_UP;
ifnet1.if_snd.ifq_maxlen = IFQ_MAXLEN;
// ifnet1.if_start = lestart;
struct rtentry rt;
rt.rt_flags = RTF_UP;
struct sockaddr sa;
sa.sa_family = AF_UNSPEC;
// ether_output(&ifnet1, &m1, &sa, &rt);
char data[1024] = "";
((struct ifconf *)data)->ifc_len = 1024;
((struct ifconf *)data)->ifc_req = (struct ifreq *)(data + sizeof(struct ifconf));
ifconf(0, data);
// print_ifconf((struct ifconf *)data);
struct socket so;
strcpy(((struct ifreq *)data)->ifr_name, "le0", strlen("le0"));
//((struct ifreq *)data)->ifr_addr = (struct sockaddr *)(data + offsetof(struct ifreq, ifr_ifru));
// ifioctl(&so, 0, data, NULL);
// if_down();
//if_up();
//leioctl();
}
#endif // CHAPTER4
#ifdef CHAPTER5
{
extern struct sl_softc sl_softc[];
extern struct ifnet *ifnet;
struct tty ty;
memset(&ty, 0, sizeof(ty));
ty.t_ospeed = 150;
ty.t_sc = &sl_softc[0];
ty.t_state = TS_CARR_ON;
dev_t dev = 0;
slopen(dev, &ty); // slinit() will be called, so no test for slinit();
int tt[] = {1, 0xdb, 0xdd, 0xdb, 2, 0xdb, 0xdc, 3, 4, 5, 6, 7, 0xc0};
*(((struct sl_softc*)ty.t_sc)->sc_buf) = 0x70;
int dd = 0x70 & 0xf0;
for (int i = 0; i < _countof(tt); i++)
slinput(tt[i], &ty);
struct sockaddr sa;
sa.sa_family = AF_INET;
struct mbuf *m1 = NULL;
MGET(m1, 0, 0);
extern int cfreecount;
cfreecount = 1500;
sloutput(&sl_softc[0].sc_if, m1, &sa, NULL);
cfreecount = 0;
// slclose(&ty);
sltioctl(&ty, SLIOCGUNIT, (caddr_t)&tt, 0);
printf("tt[0]: %d, if_unit: %d\n", tt[0],
((struct sl_softc*)ty.t_sc)->sc_if.if_unit);
struct rtentry rt;
rt.rt_flags = RTF_UP;
struct ifnet *ifp = ifnet;
m1->m_flags |= M_PKTHDR;
looutput(ifp, m1, &sa, &rt);
print_i_global_ifnet(ifp-ifnet);
}
#endif // CHAPTER5
#ifdef CHAPTER6
{
// all functions of chapter6 are copied from the book and simple,
// so no test for chapter6.......
}
#endif // CHAPTER6
#ifdef CHAPTER7
{
}
#endif // CHAPTER7
return 0;
}