author | viric@llimona |
Thu, 27 Sep 2007 00:25:54 +0200 | |
changeset 58 | 2cf8c513d18f |
parent 53 | 07500c5c53cb |
child 60 | 18c24be2b1a6 |
permissions | -rw-r--r-- |
53 | 1 |
/* |
2 |
Terminal Mixer - multi-point multi-user access to terminal applications |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
7 |
#include <netinet/in.h> |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
8 |
#include <string.h> |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
9 |
#include <stdio.h> |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
10 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
11 |
#include "eth_linux.h" |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
12 |
#include "main.h" |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
13 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
14 |
static struct |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
15 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
16 |
int socket; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
17 |
char partner[6]; |
44 | 18 |
int partner_set; |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
19 |
unsigned int seq_send; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
20 |
unsigned int seq_wait; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
21 |
unsigned int wrong_recv; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
22 |
} edata; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
23 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
24 |
enum Control{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
25 |
SEND, |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
26 |
ACK, |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
27 |
NAK |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
28 |
}; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
29 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
30 |
enum { |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
31 |
MAXPACKET = 1000, |
44 | 32 |
HEAD = 9 |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
33 |
}; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
34 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
35 |
static char eth_buffer[1000]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
36 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
37 |
static void eth_fill_mac(unsigned char *mac, const char *str); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
38 |
|
44 | 39 |
static int make_head(unsigned char *data, unsigned int seq, enum Control c, |
40 |
int size) |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
41 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
42 |
*((unsigned int *) data) = htonl(seq); |
44 | 43 |
data[4] = (unsigned char) c; |
44 |
*((unsigned int *)(data+5)) = htonl(size); |
|
45 |
return HEAD; |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
46 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
47 |
|
44 | 48 |
static int parse_head(unsigned char *data, unsigned int *seq, enum Control *c, |
49 |
int *size) |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
50 |
{ |
44 | 51 |
*seq = ntohl( *((unsigned int*) data) ); |
52 |
*c = data[4]; |
|
53 |
if (size) |
|
54 |
*size = ntohl(*((unsigned int *)(data+5))); |
|
55 |
return HEAD; |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
56 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
57 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
58 |
void eth_proto_init() |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
59 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
60 |
edata.socket = -1; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
61 |
edata.seq_send = 0; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
62 |
edata.seq_wait = 0; |
44 | 63 |
edata.partner_set = 0; |
64 |
if (command_line.c_param.server_address); |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
65 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
66 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
67 |
int eth_proto_open() |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
68 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
69 |
edata.socket = eth_open(command_line.eth_device); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
70 |
if (edata.socket == -1) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
71 |
error("Cannot open device %s", command_line.eth_device); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
72 |
return edata.socket; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
73 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
74 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
75 |
int eth_proto_recv(char *data, int size) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
76 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
77 |
int res; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
78 |
int seq; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
79 |
enum Control c; |
46
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
80 |
char partner[6]; |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
81 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
82 |
do { |
46
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
83 |
res = eth_recv(eth_buffer, sizeof(eth_buffer), partner); |
44 | 84 |
edata.partner_set = 1; |
85 |
} while(res < HEAD); |
|
86 |
parse_head(eth_buffer, &seq, &c, &res); |
|
46
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
87 |
/* We admit any first connection */ |
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
88 |
if (seq == 0) |
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
89 |
{ |
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
90 |
edata.seq_wait = 0; |
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
91 |
memcpy(edata.partner, partner, sizeof(edata.partner)); |
bb76f8ca177d
Allowing eth reconnections. Added DOCS.
lbatlle@npdl268.bpo.hp.com
parents:
44
diff
changeset
|
92 |
} |
48 | 93 |
/* |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
94 |
if (seq != edata.seq_wait) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
95 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
96 |
edata.wrong_recv++; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
97 |
return -1; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
98 |
} |
48 | 99 |
*/ |
44 | 100 |
/* res comes from parse_head. */ |
101 |
memcpy(data, eth_buffer + HEAD, min(size, res)); |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
102 |
|
48 | 103 |
/* |
44 | 104 |
make_head(eth_buffer, seq, ACK, 0); |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
105 |
eth_send(command_line.eth_device, edata.partner, eth_buffer, HEAD); |
48 | 106 |
*/ |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
107 |
edata.seq_wait++; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
108 |
|
44 | 109 |
return res; |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
110 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
111 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
112 |
int eth_proto_send(const char *data, int size) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
113 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
114 |
int total = size; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
115 |
|
44 | 116 |
if (!edata.partner_set) |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
117 |
{ |
44 | 118 |
if (edata.seq_send == 0 && !command_line.is_server |
119 |
&& command_line.c_param.server_address != 0) |
|
120 |
{ |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
121 |
eth_fill_mac(edata.partner, command_line.c_param.server_address); |
44 | 122 |
edata.partner_set = 1; |
123 |
} |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
124 |
else |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
125 |
return 0; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
126 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
127 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
128 |
do { |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
129 |
int once; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
130 |
int sent; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
131 |
int rseq; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
132 |
enum Control rc; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
133 |
|
44 | 134 |
once = min(sizeof(eth_buffer)-HEAD, size); |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
135 |
|
44 | 136 |
make_head(eth_buffer, edata.seq_send, SEND, once); |
137 |
memcpy(eth_buffer+HEAD, data, once); |
|
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
138 |
sent = eth_send(command_line.eth_device, edata.partner, |
44 | 139 |
eth_buffer, once+HEAD); |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
140 |
|
48 | 141 |
/* |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
142 |
eth_recv(eth_buffer, HEAD, 0); |
44 | 143 |
parse_head(eth_buffer, &rseq, &rc, 0); |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
144 |
if (rc != ACK || rseq != edata.seq_send) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
145 |
continue; |
48 | 146 |
*/ |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
147 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
148 |
edata.seq_send++; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
149 |
size -= sent; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
150 |
data += sent; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
151 |
} while(size > 0); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
152 |
return total; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
153 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
154 |
|
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
155 |
static void eth_fill_mac(unsigned char *mac, const char *str) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
156 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
157 |
int res; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
158 |
int imac[6]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
159 |
res = sscanf(str, "%x:%x:%x:%x:%x:%x", |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
160 |
&imac[0], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
161 |
&imac[1], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
162 |
&imac[2], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
163 |
&imac[3], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
164 |
&imac[4], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
165 |
&imac[5]); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
166 |
if (res != 6) |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
167 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
168 |
error("Error parsing mac: %02x:%02x:%02x:%02x:%02x:%02x\n", |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
169 |
imac[0], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
170 |
imac[1], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
171 |
imac[2], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
172 |
imac[3], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
173 |
imac[4], |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
174 |
imac[5]); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
175 |
} |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
176 |
mac[0] = imac[0]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
177 |
mac[1] = imac[1]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
178 |
mac[2] = imac[2]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
179 |
mac[3] = imac[3]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
180 |
mac[4] = imac[4]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
181 |
mac[5] = imac[5]; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff
changeset
|
182 |
} |