-
Notifications
You must be signed in to change notification settings - Fork 11
/
bank.h
57 lines (48 loc) · 1.03 KB
/
bank.h
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
/*
* bank.h
* Defines nessage structures and other things common to client and server
*
*/
#include <stdint.h>
#define VERSION 1
#define MAX_MSG 65536
#define MESSAGE_OVERHEAD 16
#define MAX_ERR MAX_MSG - MESSAGE_OVERHEAD
#define DEFAULT_PORT 8372
#define DEFAULT_HOST "localhost"
// typedef (unsigned long long) amt_t;
// typedef (unsigned long) acct_t;
typedef struct {
unsigned short version;
unsigned short opcode;
unsigned int acct;
unsigned long long amt;
char error[MAX_ERR];
} msg_t;
size_t msg_len(msg_t *msg)
{
return strlen(msg->error) + MESSAGE_OVERHEAD;
}
void hex_dump(msg_t *msg)
{
int i;
int len = msg_len(msg) / sizeof(unsigned short);
for (i = 0; i < len; i++) {
printf("%04x ", *((unsigned short *) msg + i));
}
printf("\n");
}
void clear_msg(msg_t *msg)
{
msg->version = 0x1;
msg->opcode = 0;
msg->acct = 0;
msg->amt = 0;
msg->error[0] = '\0';
}
msg_t *new_msg()
{
msg_t *msg = malloc(sizeof(msg_t));
clear_msg(msg);
return msg;
}