Skip to content

Commit a09168e

Browse files
committed
fix #44 handle fin packet, remove slot quickly
1 parent d390478 commit a09168e

File tree

4 files changed

+106
-33
lines changed

4 files changed

+106
-33
lines changed

packet.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ start_packet(MysqlPcap *mp) {
104104
ASSERT(ret >= 0);
105105

106106
snprintf(mp->filter, sizeof(mp->filter),
107-
"tcp port %d and tcp[tcpflags] & (tcp-push|tcp-ack) != 0", mp->mysqlPort);
107+
"tcp port %d and tcp[tcpflags] & (tcp-push|tcp-ack|tcp-fin) != 0", mp->mysqlPort);
108108

109109
if (pcap_compile(mp->pd, &fcode, mp->filter, 0, mp->netmask) < 0) {
110110
dump(L_ERR, "pcap_compile failed: %s", pcap_geterr(mp->pd));
@@ -302,9 +302,14 @@ process_ip(MysqlPcap *mp, const struct ip *ip, struct timeval tv) {
302302
#endif
303303
ASSERT((sport > 0) && (dport > 0));
304304

305-
// Capture only "data" packets, ignore TCP control
306-
if (datalen == 0)
305+
/* sack datalen > 0 continue
306+
fin possible ack = 1 continue
307+
only filter pure ack packet
308+
*/
309+
if ((tcp->ack) && (datalen == 0) && (!tcp->fin)) {
310+
dump(L_DEBUG, "skip a packet");
307311
break;
312+
}
308313
/*
309314
* for loopback, dst & src are all local_address
310315
* so use port to distinguish
@@ -317,7 +322,8 @@ process_ip(MysqlPcap *mp, const struct ip *ip, struct timeval tv) {
317322
}
318323

319324
char *data = (char*) ((uchar *) tcp + tcp->doff * 4);
320-
dump(L_DEBUG, "is_in:%c-datalen:%d-tcp:%u [%u]-[%u]", incoming, datalen, ntohl(tcp->seq), dport,sport);
325+
dump(L_DEBUG, "is_in:%c-datalen:%d-tcp:%u [%u]-[%u] [%d-%d-%d]", incoming, datalen, ntohl(tcp->seq), dport,sport
326+
,tcp->psh, tcp->ack, tcp->fin);
321327
addPacketInfo(incoming, datalen, ntohl(tcp->seq), dport,sport, data);
322328

323329
if (incoming == '1') {
@@ -386,7 +392,6 @@ inbound(MysqlPcap *mp, char* data, uint32 datalen,
386392
uint32 sqlSaveLen = 0;
387393
uint32_t *tcp_seq = NULL;
388394

389-
ASSERT(datalen > 0);
390395
ASSERT(data);
391396
ASSERT(mp && mp->hash && mp->pd);
392397

@@ -395,6 +400,14 @@ inbound(MysqlPcap *mp, char* data, uint32 datalen,
395400
lport = dport;
396401
rport = sport;
397402

403+
if (tcp->fin) {
404+
ASSERT(datalen == 0);
405+
dump(L_DEBUG, "fin");
406+
hash_get_rem(mp->hash, dst, src, lport, rport);
407+
return OK;
408+
}
409+
ASSERT(datalen > 0);
410+
398411
status = hash_get_status(mp->hash, dst, src,
399412
lport, rport, &sql, &sqlSaveLen, &tcp_seq, &cmd);
400413

@@ -548,7 +561,7 @@ inbound(MysqlPcap *mp, char* data, uint32 datalen,
548561

549562
if (param_count != ERR) {
550563
ASSERT(param_count >= 0);
551-
ASSERT(100 > param_count);
564+
ASSERT(1000 > param_count);
552565
/* prepare cant find, param_type in payload */
553566
if ((param_count > 0)
554567
&& (datalen < sizeof(param) - 200 )) {
@@ -708,6 +721,13 @@ outbound(MysqlPcap *mp, char *data, uint32 datalen,
708721
lport = sport;
709722
rport = dport;
710723

724+
if (tcp->fin) {
725+
ASSERT(datalen == 0);
726+
dump(L_DEBUG, "fin");
727+
hash_get_rem(mp->hash, src, dst, lport, rport);
728+
return OK;
729+
}
730+
ASSERT(datalen > 0);
711731
struct timeval tv2;
712732
time_t tv_t;
713733
struct tm *tm;
@@ -910,7 +930,7 @@ outbound(MysqlPcap *mp, char *data, uint32 datalen,
910930
if (ret == 0) {
911931
ASSERT(stmt_id > 0);
912932
ASSERT(param_count >= 0);
913-
ASSERT(100 > param_count);
933+
ASSERT(1000 > param_count);
914934
hash_set_param_count(mp->hash, src, dst,
915935
lport, rport, stmt_id, param_count);
916936
dump(L_DEBUG, "prepare ok packet %d %d", stmt_id, param_count);

stat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void addPacketInfo(char incoming, uint32 datalen, uint32 tcp_seq,
2727
uint16 dport, uint16 sport, char* payload) {
2828
ASSERT(G_pos <= LAST_PACKETS_NUM);
2929
ASSERT((incoming == '1') || (incoming == '0'));
30-
ASSERT((datalen > 0) && (dport > 0) && (sport > 0));
30+
ASSERT((datalen >= 0) && (dport > 0) && (sport > 0));
3131

3232
if (G_pos == LAST_PACKETS_NUM) {
3333
G_pos = 0;

test/Makefile

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/p22_blob.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <stdio.h>
2+
#include <mysql.h>
3+
#include <unistd.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include "c.h"
7+
8+
int main(int argc, char* argv[])
9+
{
10+
MYSQL *mysql,*sock;
11+
MYSQL_ROW row;
12+
MYSQL_RES *result;
13+
14+
mysql = mysql_init(NULL);
15+
if (!(sock = CONN(CLIENT_MULTI_STATEMENTS))) {
16+
fprintf(stderr, "Couldn't connect to engine!\n%s\n\n", mysql_error(mysql));
17+
perror("");
18+
exit(1);
19+
}
20+
21+
// simple
22+
char sql[100] = {};
23+
24+
sprintf(sql, "INSERT INTO blob_test(a, b) VALUE (1, 2)");
25+
26+
mysql_query(sock, sql);
27+
28+
mysql_query(sock, "select * from blob_test");
29+
30+
result = mysql_store_result(mysql);
31+
mysql_free_result(result);
32+
33+
//prepare
34+
35+
MYSQL_STMT *stmt;
36+
MYSQL_BIND bind[2];
37+
my_ulonglong affected_rows;
38+
int param_count;
39+
short small_data;
40+
int int_data;
41+
char str_data[1000];
42+
unsigned long str_length;
43+
my_bool is_null;
44+
45+
stmt = mysql_stmt_init(mysql);
46+
47+
sprintf(sql, "INSERT INTO blob_test(a, b) VALUE (?, ?)");
48+
mysql_stmt_prepare(stmt, sql, strlen(sql));
49+
50+
memset(bind, 0, sizeof(bind));
51+
52+
/* INTEGER PARAM */
53+
/* This is a number type, so there is no need to specify buffer_length */
54+
bind[0].buffer_type= MYSQL_TYPE_LONG;
55+
bind[0].buffer= (char *)&int_data;
56+
bind[0].is_null= 0;
57+
58+
/* STRING PARAM */
59+
my_bool a = 1;
60+
bind[1].buffer_type= MYSQL_TYPE_BLOB;
61+
bind[1].buffer= (char *)str_data;
62+
bind[1].is_null= &a;
63+
bind[1].length= &str_length; //实际大小, bind_
64+
65+
mysql_stmt_bind_param(stmt, bind);
66+
67+
/* Specify the data values for the first row -------------------------------------------------- */
68+
int_data= 10; /* integer */
69+
strncpy(str_data, "MySQL", 1000); /* string */
70+
str_length= strlen(str_data);
71+
72+
mysql_stmt_execute(stmt);
73+
74+
mysql_stmt_close(stmt);
75+
76+
return 0;
77+
}
78+

0 commit comments

Comments
 (0)