-
Notifications
You must be signed in to change notification settings - Fork 0
/
talk.c
218 lines (192 loc) · 4.33 KB
/
talk.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 <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define SW_INT 0x60
//change Destination MAC address before compiling
unsigned char dest_mac[] = "\x08\x00\x27\x52\xbf\x0f";
unsigned char data[64];
unsigned char c[2];
int handle,packet_len,i;
unsigned char e[200];
unsigned char from_mac[6];
unsigned char type[] = "\xff\xff";
unsigned char broadcastAdd[] = "\xff\xff\xff\xff\xff\xff";
unsigned char buffer[100];
int bufferCounter;
unsigned char temp;
int exitCounter=0;
void get_mac_address();
void fill_headers();
void fill_data(unsigned char *msg, int length);
void send_packet();
int getHandle();
void releaseType();
void interrupt receiver(bp, di, si, ds, es, dx, cx, bx, ax, ip, cs, flags)
unsigned short bp, di, si, ds, es, dx, cx, bx, ax, ip, cs, flags;
{
//printf("Reciever ax=%d\n",ax);
if ( ax == 0)
{
//discard packets of size greater than 200
if(cx>200)
{
es = 0;
di = 0;
return;
}
es=FP_SEG(e);
di=FP_OFF(e);
packet_len=cx;
}
if(ax==1)
{
if(memcmp(e,broadcastAdd,6) == 0)
{
//broadcast packet received discard it
return;
}
for(i=6;i<12;i++)
{
cprintf("%02x:",e[i]);
}
cprintf(" > ");
//skip type
for (i=14;i<packet_len;i++)
{
if(e[i] == 0x00)
{
break;
}
putch(e[i]);
}
putch('\r');
putch('\n');
}
}
int main()
{
get_mac_address();
fill_headers();
if(getHandle() != 0)
printf("Error getting Handle, Exitting\r\n");
bufferCounter = 0;
while(1)
{
temp = getchar();
if(temp==0x0D || temp ==0x0A)
{
if(exitCounter >= 1)
{
break;
}
if(bufferCounter ==0)
exitCounter++;
fill_data(buffer,bufferCounter);
bufferCounter = 0;
send_packet();
}
else
{
exitCounter = 0;
buffer[bufferCounter] = temp;
bufferCounter++;
}
}
releaseType();
printf("Bye Bye\n");
return 0;
}
int getHandle()
{
union REGS inregs,outregs;
struct SREGS segregs;
inregs.h.al=1;
inregs.x.bx=-1;
inregs.h.dl=0;
inregs.x.cx=0;
inregs.h.ah=2;
segregs.es=FP_SEG(receiver);
inregs.x.di=FP_OFF(receiver);
c[0]=0xff;
c[1]=0xff;
segregs.ds=FP_SEG(c);
inregs.x.si=FP_OFF(c);
int86x(SW_INT,&inregs,&outregs,&segregs);
printf("Carry Flag Access Type %d\n",outregs.x.cflag);
printf("Handle %d\n",outregs.x.ax);
handle = outregs.x.ax;
return outregs.x.cflag;
}
void releaseType()
{
union REGS inregs,outregs;
struct SREGS segregs;
inregs.h.ah=3;
inregs.x.bx=handle;
int86x(SW_INT,&inregs,&outregs,&segregs);
printf("Carry Flag Release Type %d\n",outregs.x.cflag);
}
void printReceiveMode()
{
union REGS inregs,outregs;
struct SREGS segregs;
inregs.h.ah=21;
inregs.x.bx=handle;
int86x(SW_INT,&inregs,&outregs,&segregs);
printf("Carry Flag Get Recieve Mode %d\n",outregs.x.cflag);
printf("Get Recieve Mode %d\n",outregs.x.ax);
}
int setReceiveMode(int mode)
{
union REGS inregs,outregs;
struct SREGS segregs;
inregs.h.ah=20;
inregs.x.bx=handle;
inregs.x.cx=mode;
int86x(SW_INT,&inregs,&outregs,&segregs);
printf("Carry Flag Set Recieve Mode %d\n",outregs.x.cflag);
return outregs.x.cflag;
}
void get_mac_address()
{
union REGS inregs,outregs;
struct SREGS segregs;
char far *bufptr;
segread(&segregs);
bufptr = (char far *)from_mac;
segregs.es = FP_SEG(bufptr);
inregs.x.di = FP_OFF(from_mac);
inregs.x.cx = 6;
inregs.h.ah = 6;
int86x(SW_INT, &inregs, &outregs, &segregs);
}
//To be called after calling the get_mac method
void fill_headers()
{
memcpy(data, dest_mac, 6); //set the destination mac
memcpy(data+6, from_mac, 6); //set the source mac
memcpy(data+12, type, 2); //set the type
}
void fill_data(unsigned char *msg, int length)
{
if(length+14 > 64)
length = 64-14;
memcpy(data+14, msg, length);
for(i=length+14; i<64; i++)
{
data[i] = 0;
}
}
void send_packet()
{
union REGS inregs,outregs;
struct SREGS segregs;
segread(&segregs);
inregs.h.ah = 4;
segregs.ds = FP_SEG(data);
inregs.x.si = FP_OFF(data);
inregs.x.cx = 64;
int86x(SW_INT,&inregs,&outregs,&segregs);
}