Skip to content

Commit 7cfa100

Browse files
committed
add print info
1 parent 6460f58 commit 7cfa100

File tree

2 files changed

+511
-456
lines changed

2 files changed

+511
-456
lines changed

mysql_sniff.c

Lines changed: 47 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
TODO:
22-
main 函数 & 命令行参数处理
22+
2323
日志级别处理
2424
FIX 5.7 EOF 问题 conn_data->num_fields = conn_data->stmt_num_params;
2525
1. 处理 mysql 5.7 协议变更, 无 EOF packet
@@ -33,62 +33,6 @@ FIX 5.7 EOF 问题 conn_data->num_fields = conn_data->stmt_num_params;
3333
OK: 支持多mysql端口
3434
OK: 测试 新旧 两种协议 同时连接的场景
3535
用简单的 strmap 处理 ResultSet 结果集?! string <=> string
36-
37-
38-
// 以下 打印处理
39-
40-
decoding table: exec_flags
41-
static const value_string mysql_exec_flags_vals[] = {
42-
{MYSQL_CURSOR_TYPE_NO_CURSOR, "Defaults"},
43-
{MYSQL_CURSOR_TYPE_READ_ONLY, "Read-only cursor"},
44-
{MYSQL_CURSOR_TYPE_FOR_UPDATE, "Cursor for update"},
45-
{MYSQL_CURSOR_TYPE_SCROLLABLE, "Scrollable cursor"},
46-
{0, NULL}
47-
};
48-
49-
decoding table: new_parameter_bound_flag
50-
static const value_string mysql_new_parameter_bound_flag_vals[] = {
51-
{0, "Subsequent call"},
52-
{1, "First call or rebound"},
53-
{0, NULL}
54-
};
55-
56-
decoding table: exec_time_sign
57-
static const value_string mysql_exec_time_sign_vals[] = {
58-
{0, "Positive"},
59-
{1, "Negative"},
60-
{0, NULL}
61-
};
62-
63-
allowed MYSQL_SHUTDOWN levels
64-
static const value_string mysql_shutdown_vals[] = {
65-
{0, "default"},
66-
{1, "wait for connections to finish"},
67-
{2, "wait for transactions to finish"},
68-
{8, "wait for updates to finish"},
69-
{16, "wait flush all buffers"},
70-
{17, "wait flush critical buffers"},
71-
{254, "kill running queries"},
72-
{255, "kill connections"},
73-
{0, NULL}
74-
};
75-
76-
77-
allowed MYSQL_SET_OPTION values
78-
static const value_string mysql_option_vals[] = {
79-
{0, "multi statements on"},
80-
{1, "multi statements off"},
81-
{0, NULL}
82-
};
83-
84-
static const value_string mysql_session_track_type_vals[] = {
85-
{0, "SESSION_SYSVARS_TRACKER"},
86-
{1, "CURRENT_SCHEMA_TRACKER"},
87-
{2, "SESSION_STATE_CHANGE_TRACKER"},
88-
{0, NULL}
89-
};
90-
91-
9236
*/
9337

9438
/* thanks for https://github.com/wireshark/wireshark/blob/master/epan/dissectors/packet-mysql.c */
@@ -1208,16 +1152,15 @@ mysql_dissect_request(struct buffer *buf, mysql_conn_data_t *conn_data)
12081152
case MYSQL_SHUTDOWN:
12091153
{
12101154
uint8_t mysql_shutdown = buf_readInt8(buf);
1211-
UNUSED(mysql_shutdown);
1212-
LOG_INFO("Mysql Shutdown");
1155+
LOG_INFO("Mysql Shutdown %s(%d)", mysql_get_shutdown_val(mysql_shutdown, "未知"), mysql_shutdown);
12131156
}
12141157
mysql_set_conn_state(conn_data, RESPONSE_OK);
12151158
break;
12161159

12171160
case MYSQL_SET_OPTION:
12181161
{
12191162
uint16_t mysql_option = buf_readInt16LE(buf);
1220-
LOG_INFO("Mysql Set Option 0x%04x", mysql_option);
1163+
LOG_INFO("Mysql Set Option %s(0x%04x)", mysql_get_option_val(mysql_option, "未知"), mysql_option);
12211164
}
12221165
mysql_set_conn_state(conn_data, RESPONSE_OK);
12231166
break;
@@ -1265,8 +1208,11 @@ mysql_dissect_request(struct buffer *buf, mysql_conn_data_t *conn_data)
12651208
uint8_t exec_flags = buf_readInt8(buf);
12661209
uint32_t exec_iter = buf_readInt32LE(buf);
12671210

1268-
LOG_INFO("Mysql Statement Id %u, Flags 0x%02x, Iter %u",
1269-
stmt_id, exec_flags, exec_iter);
1211+
// 注意: 这里是+5.x协议, 不适用于4.x
1212+
const char *tofree = mysql_get_exec_flags(exec_flags);
1213+
LOG_INFO("Mysql Statement Id %u, Flags %s(0x%02x), Iter %u",
1214+
stmt_id, tofree, exec_flags, exec_iter);
1215+
free((void *)tofree);
12701216

12711217
khint_t k = kh_get(stmts, conn_data->stmts, stmt_id);
12721218
int is_missing = (k == kh_end(conn_data->stmts));
@@ -1289,6 +1235,8 @@ mysql_dissect_request(struct buffer *buf, mysql_conn_data_t *conn_data)
12891235
buf_retrieve(buf, n);
12901236

12911237
uint8_t stmt_bound = buf_readInt8(buf);
1238+
LOG_INFO("Mysql Parameter Bound: %s", mysql_get_parameter_bound_val(stmt_bound, "未知"));
1239+
12921240
if (stmt_bound == 1) // First Call Or Rebound
12931241
{
12941242
int stmt_pos;
@@ -1300,6 +1248,10 @@ mysql_dissect_request(struct buffer *buf, mysql_conn_data_t *conn_data)
13001248
break;
13011249
}
13021250
}
1251+
else
1252+
{
1253+
// TODO
1254+
}
13031255
}
13041256
}
13051257

@@ -1680,6 +1632,7 @@ mysql_dissect_session_tracker_entry(struct buffer *buf)
16801632

16811633
/* session tracker type */
16821634
uint8_t data_type = buf_readInt8(buf);
1635+
LOG_INFO("Mysql Session Tracker Type: %s(%d)", mysql_get_session_track_type(data_type, "未知"), data_type);
16831636
uint64_t length = buf_readFle(buf, &lenfle, NULL); /* complete length of session tracking entry */
16841637
int sz = 1 + lenfle + length;
16851638

@@ -1786,35 +1739,37 @@ mysql_dissect_exec_time(struct buffer *buf, uint8_t param_unsigned, int *param_i
17861739
uint8_t param_len = buf_readInt8(buf);
17871740
*param_idx += sizeof(uint8_t);
17881741

1789-
uint8_t mysql_exec_field_time_sign = 0;
1790-
uint32_t mysql_exec_field_time_days = 0;
1791-
uint8_t mysql_exec_field_hour = 0;
1792-
uint8_t mysql_exec_field_minute = 0;
1793-
uint8_t mysql_exec_field_second = 0;
1794-
uint32_t mysql_exec_field_second_b = 0;
1742+
// TODO struct
1743+
uint8_t time_sign = 0;
1744+
uint32_t time_days = 0;
1745+
uint8_t hour = 0;
1746+
uint8_t minute = 0;
1747+
uint8_t second = 0;
1748+
uint32_t second_b = 0;
17951749

17961750
if (param_len >= 1)
17971751
{
1798-
mysql_exec_field_time_sign = buf_readInt8(buf);
1752+
time_sign = buf_readInt8(buf);
17991753
*param_idx += sizeof(uint8_t);
1754+
LOG_INFO("Mysql Time Sign: %s", mysql_get_time_sign(time_sign, "未知"));
18001755
}
18011756
if (param_len >= 5)
18021757
{
1803-
mysql_exec_field_time_days = buf_readInt32LE(buf);
1758+
time_days = buf_readInt32LE(buf);
18041759
*param_idx += 3;
18051760
}
18061761
if (param_len >= 8)
18071762
{
1808-
mysql_exec_field_hour = buf_readInt8(buf);
1809-
mysql_exec_field_minute = buf_readInt8(buf);
1810-
mysql_exec_field_second = buf_readInt8(buf);
1763+
hour = buf_readInt8(buf);
1764+
minute = buf_readInt8(buf);
1765+
second = buf_readInt8(buf);
18111766
*param_idx += sizeof(uint8_t);
18121767
*param_idx += sizeof(uint8_t);
18131768
*param_idx += sizeof(uint8_t);
18141769
}
18151770
if (param_len >= 12)
18161771
{
1817-
mysql_exec_field_second_b = buf_readInt32LE(buf);
1772+
second_b = buf_readInt32LE(buf);
18181773
*param_idx += sizeof(uint32_t);
18191774
}
18201775

@@ -1837,38 +1792,39 @@ mysql_dissect_exec_datetime(struct buffer *buf, uint8_t param_unsigned, int *par
18371792
uint8_t param_len = buf_readInt8(buf);
18381793
*param_idx += sizeof(uint8_t);
18391794

1840-
uint16_t mysql_exec_field_year = 0;
1841-
uint8_t mysql_exec_field_month = 0;
1842-
uint8_t mysql_exec_field_day = 0;
1843-
uint8_t mysql_exec_field_hour = 0;
1844-
uint8_t mysql_exec_field_minute = 0;
1845-
uint8_t mysql_exec_field_second = 0;
1846-
uint32_t mysql_exec_field_second_b = 0;
1795+
// TODO struct
1796+
uint16_t year = 0;
1797+
uint8_t month = 0;
1798+
uint8_t day = 0;
1799+
uint8_t hour = 0;
1800+
uint8_t minute = 0;
1801+
uint8_t second = 0;
1802+
uint32_t second_b = 0;
18471803

18481804
if (param_len >= 2)
18491805
{
1850-
mysql_exec_field_year = buf_readInt16LE(buf);
1806+
year = buf_readInt16LE(buf);
18511807
*param_idx += sizeof(uint16_t);
18521808
}
18531809
if (param_len >= 4)
18541810
{
1855-
mysql_exec_field_month = buf_readInt8(buf);
1856-
mysql_exec_field_day = buf_readInt8(buf);
1811+
month = buf_readInt8(buf);
1812+
day = buf_readInt8(buf);
18571813
*param_idx += sizeof(uint8_t);
18581814
*param_idx += sizeof(uint8_t);
18591815
}
18601816
if (param_len >= 7)
18611817
{
1862-
mysql_exec_field_hour = buf_readInt8(buf);
1863-
mysql_exec_field_minute = buf_readInt8(buf);
1864-
mysql_exec_field_second = buf_readInt8(buf);
1818+
hour = buf_readInt8(buf);
1819+
minute = buf_readInt8(buf);
1820+
second = buf_readInt8(buf);
18651821
*param_idx += sizeof(uint8_t);
18661822
*param_idx += sizeof(uint8_t);
18671823
*param_idx += sizeof(uint8_t);
18681824
}
18691825
if (param_len >= 11)
18701826
{
1871-
mysql_exec_field_second_b = buf_readInt32LE(buf);
1827+
second_b = buf_readInt32LE(buf);
18721828
*param_idx += sizeof(uint32_t);
18731829
}
18741830

@@ -2325,14 +2281,15 @@ int main(int argc, char **argv)
23252281
assert(opts->interface);
23262282
break;
23272283
case 'p':
2328-
// size_t needed = snprintf(NULL, 0, ,
2284+
// size_t needed = snprintf(NULL, 0, ,
23292285
i += snprintf(opts->expression + i, max_filter_sz - i - 1, "tcp and ( port 0 ");
23302286
assert(i <= max_filter_sz - 1);
23312287
while ((port = strsep(&optarg, ",")) != NULL)
23322288
{
23332289
if (port)
23342290
{
2335-
if (!atoi(port)) {
2291+
if (!atoi(port))
2292+
{
23362293
LOG_ERROR("端口号有误 %s", port);
23372294
goto free;
23382295
}

0 commit comments

Comments
 (0)