Skip to content

Commit 150a6e3

Browse files
committed
Source formated.
1 parent 9a1e79d commit 150a6e3

File tree

4 files changed

+101
-156
lines changed

4 files changed

+101
-156
lines changed

h3c.c

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,35 @@ static struct bpf_program filter = {
7070
static struct sockaddr_ll addr;
7171
#endif /* AF_LINK */
7272

73-
static inline void set_eapol_header(unsigned char type, \
74-
unsigned short length)
75-
{
73+
static inline void set_eapol_header(unsigned char type, unsigned short length) {
7674
send_pkt->eapol_header.version = EAPOL_VERSION;
7775
send_pkt->eapol_header.type = type;
7876
send_pkt->eapol_header.length = length;
7977
}
8078

81-
static inline void set_eap_header(unsigned char code, \
82-
unsigned char id, unsigned short length)
83-
{
79+
static inline void set_eap_header(unsigned char code, unsigned char id,
80+
unsigned short length) {
8481
send_pkt->eap_header.code = code;
8582
send_pkt->eap_header.id = id;
8683
send_pkt->eap_header.length = length;
8784
}
8885

89-
static int sendout(int length)
90-
{
86+
static int sendout(int length) {
9187
#ifdef AF_LINK
9288
if (write(sockfd, send_buf, length) == -1)
9389
return SEND_ERR;
9490
else
9591
return SUCCESS;
9692
#else
97-
if (sendto(sockfd, send_buf, length, 0, (struct sockaddr*)&addr, \
93+
if (sendto(sockfd, send_buf, length, 0, (struct sockaddr*) &addr,
9894
sizeof(addr)) == -1)
9995
return SEND_ERR;
10096
else
10197
return SUCCESS;
10298
#endif /* AF_LINK */
10399
}
104100

105-
static int recvin(int length)
106-
{
101+
static int recvin(int length) {
107102
#ifdef AF_LINK
108103
if (read(sockfd, recv_buf, length) == -1)
109104
return RECV_ERR;
@@ -114,19 +109,19 @@ static int recvin(int length)
114109
socklen_t len;
115110
len = sizeof(addr);
116111

117-
if (recvfrom(sockfd, recv_buf, length, 0, \
118-
(struct sockaddr *)&addr, &len) == -1)
112+
if (recvfrom(sockfd, recv_buf, length, 0, (struct sockaddr *) &addr, &len)
113+
== -1)
119114
return RECV_ERR;
120115
else
121116
return SUCCESS;
122117
#endif /* AF_LINK */
123118
}
124119

125-
static int send_id(unsigned char packet_id)
126-
{
120+
static int send_id(unsigned char packet_id) {
127121
int username_length = strlen(username);
128-
unsigned short len = htons( sizeof(struct eap) + TYPE_LEN + \
129-
sizeof(VERSION_INFO) + username_length );
122+
unsigned short len = htons(
123+
sizeof(struct eap) + TYPE_LEN + sizeof(VERSION_INFO)
124+
+ username_length);
130125

131126
set_eapol_header(EAPOL_EAPPACKET, len);
132127
set_eap_header(EAP_RESPONSE, packet_id, len);
@@ -135,16 +130,16 @@ static int send_id(unsigned char packet_id)
135130
memcpy(eap_id_info(send_pkt), VERSION_INFO, sizeof(VERSION_INFO));
136131
memcpy(eap_id_username(send_pkt), username, username_length);
137132

138-
return sendout(sizeof(struct packet) + TYPE_LEN + \
139-
sizeof(VERSION_INFO) + username_length);
133+
return sendout(
134+
sizeof(struct packet) + TYPE_LEN + sizeof(VERSION_INFO)
135+
+ username_length);
140136
}
141137

142-
static int send_md5(unsigned char packet_id, unsigned char *md5data)
143-
{
138+
static int send_md5(unsigned char packet_id, unsigned char *md5data) {
144139
int username_length = strlen(username);
145140
unsigned char md5[MD5_LEN];
146-
unsigned short len = htons(sizeof(struct eap) + TYPE_LEN + \
147-
MD5_LEN_LEN + MD5_LEN + username_length);
141+
unsigned short len = htons(sizeof(struct eap) + TYPE_LEN +
142+
MD5_LEN_LEN + MD5_LEN + username_length);
148143

149144
memset(md5, 0, MD5_LEN);
150145
memcpy(md5, password, MD5_LEN);
@@ -161,31 +156,30 @@ static int send_md5(unsigned char packet_id, unsigned char *md5data)
161156
memcpy(eap_md5_data(send_pkt), md5, MD5_LEN);
162157
memcpy(eap_md5_username(send_pkt), username, username_length);
163158

164-
return sendout(sizeof(struct packet) + TYPE_LEN + MD5_LEN_LEN + \
165-
MD5_LEN + username_length);
159+
return sendout(sizeof(struct packet) + TYPE_LEN + MD5_LEN_LEN +
160+
MD5_LEN + username_length);
166161
}
167162

168-
static int send_h3c(unsigned char packet_id)
169-
{
163+
static int send_h3c(unsigned char packet_id) {
170164
int username_length = strlen(username);
171165
int password_length = strlen(password);
172-
unsigned short len = htons(sizeof(struct eap) + 1 + 1 + \
173-
password_length + username_length);
166+
unsigned short len = htons(
167+
sizeof(struct eap) + 1 + 1 + password_length + username_length);
174168

175169
set_eapol_header(EAPOL_EAPPACKET, len);
176170
set_eap_header(EAP_RESPONSE, packet_id, len);
177171
*eap_type(send_pkt) = EAP_TYPE_H3C;
178172

179-
*eap_h3c_length(send_pkt) = (unsigned char)password_length;
173+
*eap_h3c_length(send_pkt) = (unsigned char) password_length;
180174
memcpy(eap_h3c_password(send_pkt), password, password_length);
181175
memcpy(eap_h3c_username(send_pkt), username, username_length);
182176

183-
return sendout(sizeof(struct packet) + TYPE_LEN + H3C_LEN_LEN + \
184-
password_length + username_length);
177+
return sendout(
178+
sizeof(struct packet) + TYPE_LEN + H3C_LEN_LEN + password_length
179+
+ username_length);
185180
}
186181

187-
int h3c_init(char *_interface)
188-
{
182+
int h3c_init(char *_interface) {
189183
struct ifreq ifr;
190184

191185
/* Set destination mac address. */
@@ -202,7 +196,7 @@ int h3c_init(char *_interface)
202196

203197
do {
204198
sockfd = open(device, O_RDWR);
205-
} while ((sockfd == -1) && (errno == EBUSY) && (device[8]++ != '9'));
199+
}while ((sockfd == -1) && (errno == EBUSY) && (device[8]++ != '9'));
206200

207201
if (sockfd == -1)
208202
return BPF_OPEN_ERR;
@@ -239,7 +233,7 @@ int h3c_init(char *_interface)
239233
if ((sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_PAE))) == -1)
240234
return SOCKET_OPEN_ERR;
241235

242-
if (ioctl(sockfd,SIOCGIFINDEX,&ifr) == -1)
236+
if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1)
243237
return SOCKET_SET_IF_ERR;
244238
else
245239
addr.sll_ifindex = ifr.ifr_ifindex;
@@ -248,15 +242,13 @@ int h3c_init(char *_interface)
248242
return SOCKET_GET_HWADDR_ERR;
249243

250244
/* Set source mac address. */
251-
memcpy(send_pkt->eth_header.ether_shost, \
252-
ifr.ifr_hwaddr.sa_data, ETH_ALEN);
245+
memcpy(send_pkt->eth_header.ether_shost, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
253246
#endif /* AF_LINK */
254247

255248
return SUCCESS;
256249
}
257250

258-
int h3c_set_username(char *_username)
259-
{
251+
int h3c_set_username(char *_username) {
260252
int username_length = strlen(_username);
261253
if (username_length > USR_LEN - 1)
262254
return USR_TOO_LONG;
@@ -265,8 +257,7 @@ int h3c_set_username(char *_username)
265257
return SUCCESS;
266258
}
267259

268-
int h3c_set_password(char *_password)
269-
{
260+
int h3c_set_password(char *_password) {
270261
int password_length = strlen(_password);
271262
if (password_length > PWD_LEN - 1)
272263
return PWD_TOO_LONG;
@@ -275,75 +266,61 @@ int h3c_set_password(char *_password)
275266
return SUCCESS;
276267
}
277268

278-
int h3c_start()
279-
{
269+
int h3c_start() {
280270
set_eapol_header(EAPOL_START, 0);
281-
return sendout(sizeof(struct ether_header)+ sizeof(struct eapol));
271+
return sendout(sizeof(struct ether_header) + sizeof(struct eapol));
282272
}
283273

284-
int h3c_logoff()
285-
{
274+
int h3c_logoff() {
286275
set_eapol_header(EAPOL_LOGOFF, 0);
287-
return sendout(sizeof(struct ether_header)+ sizeof(struct eapol));
276+
return sendout(sizeof(struct ether_header) + sizeof(struct eapol));
288277
}
289278

290-
int h3c_response(int (*success_callback)(void), \
291-
int (*failure_callback)(void), \
292-
int (*unkown_eapol_callback)(void), \
293-
int (*unkown_eap_callback)(void), \
294-
int (*got_response_callback)(void))
295-
{
279+
int h3c_response(int (*success_callback)(void), int (*failure_callback)(void),
280+
int (*unkown_eapol_callback)(void), int (*unkown_eap_callback)(void),
281+
int (*got_response_callback)(void)) {
296282
if (recvin(BUF_LEN) == RECV_ERR)
297283
return RECV_ERR;
298284

299-
if (recv_pkt->eapol_header.type != EAPOL_EAPPACKET)
300-
{
285+
if (recv_pkt->eapol_header.type != EAPOL_EAPPACKET) {
301286
/* Got unknown eapol type. */
302287
if (unkown_eapol_callback != NULL)
303288
return unkown_eapol_callback();
304289
else
305290
return EAPOL_UNHANDLED;
306291
}
307-
if (recv_pkt->eap_header.code == EAP_SUCCESS)
308-
{
292+
if (recv_pkt->eap_header.code == EAP_SUCCESS) {
309293
/* Got success. */
310294
if (success_callback != NULL)
311295
return success_callback();
312296
else
313297
return SUCCESS_UNHANDLED;
314-
}
315-
else if (recv_pkt->eap_header.code == EAP_FAILURE)
316-
{
298+
} else if (recv_pkt->eap_header.code == EAP_FAILURE) {
317299
/* Got failure. */
318300
if (failure_callback != NULL)
319301
return failure_callback();
320302
else
321303
return FAILURE_UNHANDLED;
322-
}
323-
else if (recv_pkt->eap_header.code == EAP_REQUEST)
304+
} else if (recv_pkt->eap_header.code == EAP_REQUEST)
324305
/*
325306
* Got request.
326307
* Response according to request type.
327308
*/
328309
if (*eap_type(recv_pkt) == EAP_TYPE_ID)
329310
return send_id(recv_pkt->eap_header.id);
330311
else if (*eap_type(recv_pkt) == EAP_TYPE_MD5)
331-
return send_md5(recv_pkt->eap_header.id, \
332-
eap_md5_data(recv_pkt));
312+
return send_md5(recv_pkt->eap_header.id, eap_md5_data(recv_pkt));
333313
else if (*eap_type(recv_pkt) == EAP_TYPE_H3C)
334314
return send_h3c(recv_pkt->eap_header.id);
335315
else
336316
return SUCCESS;
337-
else if (recv_pkt->eap_header.code == EAP_RESPONSE)
338-
{
317+
else if (recv_pkt->eap_header.code == EAP_RESPONSE) {
339318
/* Got response. */
340319
if (got_response_callback != NULL)
341320
return got_response_callback();
342321
else
343322
return RESPONSE_UNHANDLED;
344-
}
345-
else
346-
{
323+
} else {
347324
/* Got unkown eap type. */
348325
if (unkown_eap_callback != NULL)
349326
return unkown_eap_callback();
@@ -352,7 +329,6 @@ int h3c_response(int (*success_callback)(void), \
352329
}
353330
}
354331

355-
void h3c_clean()
356-
{
332+
void h3c_clean() {
357333
close(sockfd);
358334
}

h3c.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,29 @@
114114
#define FAILURE_UNHANDLED 17
115115
#define RESPONSE_UNHANDLED 18
116116

117-
struct eapol{
117+
struct eapol {
118118
unsigned char version;
119119
unsigned char type;
120120
unsigned short length;
121121
}__attribute__ ((packed)) eapol;
122122

123-
struct eap{
123+
struct eap {
124124
unsigned char code;
125125
unsigned char id;
126126
unsigned short length;
127127
}__attribute__ ((packed)) eap;
128128

129-
struct packet{
129+
struct packet {
130130
struct ether_header eth_header;
131131
struct eapol eapol_header;
132132
struct eap eap_header;
133133
}__attribute__ ((packed)) packet;
134134

135-
const static char PAE_GROUP_ADDR[] = \
136-
{0x01, 0x80, 0xc2, 0x00, 0x00, 0x03};/* broadcast mac address */
135+
const static char PAE_GROUP_ADDR[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };/* broadcast mac address */
137136

138-
const static char VERSION_INFO[] = \
139-
{0x06, 0x07, 'b', 'j', 'Q', '7', 'S', 'E', '8', 'B', 'Z', '3', \
140-
'M', 'q', 'H', 'h', 's', '3', 'c', 'l', 'M', 'r', 'e', 'g', \
141-
'c', 'D', 'Y', '3', 'Y', '=',0x20,0x20};/* learned from yah3c */
137+
const static char VERSION_INFO[] = { 0x06, 0x07, 'b', 'j', 'Q', '7', 'S', 'E',
138+
'8', 'B', 'Z', '3', 'M', 'q', 'H', 'h', 's', '3', 'c', 'l', 'M', 'r',
139+
'e', 'g', 'c', 'D', 'Y', '3', 'Y', '=', 0x20, 0x20 };/* learned from yah3c */
142140

143141
/*
144142
* param _interface: ethernet device name, e.g. eth0
@@ -149,11 +147,9 @@ int h3c_init(char *_interface);
149147
int h3c_start();
150148
int h3c_logoff();
151149

152-
int h3c_response(int (*success_callback)(void), \
153-
int (*failure_callback)(void), \
154-
int (*unkown_eapol_callback)(void), \
155-
int (*unkown_eap_callback)(void), \
156-
int (*got_response_callback)(void));
150+
int h3c_response(int (*success_callback)(void), int (*failure_callback)(void),
151+
int (*unkown_eapol_callback)(void), int (*unkown_eap_callback)(void),
152+
int (*got_response_callback)(void));
157153

158154
int h3c_set_username(char *_username);
159155
int h3c_set_password(char *_password);

handler.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,31 @@
2424
#include "handler.h"
2525
#include "h3c.h"
2626

27-
int success_handler()
28-
{
27+
int success_handler() {
2928
printf("You are now ONLINE.\n");
3029
daemon(0, 0);
3130
return SUCCESS;
3231
}
3332

34-
int failure_handler()
35-
{
33+
int failure_handler() {
3634
printf("You are now OFFLINE.\n");
3735
return SUCCESS;
3836
}
3937

40-
int unkown_eapol_handler()
41-
{
38+
int unkown_eapol_handler() {
4239
return SUCCESS;
4340
}
4441

45-
int unkown_eap_handler()
46-
{
42+
int unkown_eap_handler() {
4743
return SUCCESS;
4844
}
4945

5046
/* We should NOT got response messages and we ignore them. */
51-
int got_response_handler()
52-
{
47+
int got_response_handler() {
5348
return SUCCESS;
5449
}
5550

56-
void exit_handler(int arg)
57-
{
51+
void exit_handler(int arg) {
5852
printf("\nExiting...\n");
5953
h3c_logoff();
6054
h3c_clean();

0 commit comments

Comments
 (0)