Browse Source

log: make log happy when receiving malformd packet

Nick Peng 3 years ago
parent
commit
1f07a7ea97
3 changed files with 36 additions and 6 deletions
  1. 12 6
      src/dns_server.c
  2. 22 0
      src/util.c
  3. 2 0
      src/util.h

+ 12 - 6
src/dns_server.c

@@ -62,6 +62,7 @@
 #define RECV_ERROR_OK 0
 #define RECV_ERROR_OK 0
 #define RECV_ERROR_FAIL -1
 #define RECV_ERROR_FAIL -1
 #define RECV_ERROR_CLOSE -2
 #define RECV_ERROR_CLOSE -2
+#define RECV_ERROR_INVALID_PACKET -3
 
 
 typedef enum {
 typedef enum {
 	DNS_CONN_TYPE_UDP_SERVER = 0,
 	DNS_CONN_TYPE_UDP_SERVER = 0,
@@ -4000,11 +4001,12 @@ static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *in
 	struct dns_request *request = NULL;
 	struct dns_request *request = NULL;
 
 
 	/* decode packet */
 	/* decode packet */
-	tlog(TLOG_DEBUG, "recv query packet from %s, len = %d",
-		 gethost_by_addr(name, sizeof(name), (struct sockaddr *)from), inpacket_len);
+	tlog(TLOG_DEBUG, "recv query packet from %s, len = %d, type = %d",
+		 gethost_by_addr(name, sizeof(name), (struct sockaddr *)from), inpacket_len, conn->type);
 	decode_len = dns_decode(packet, DNS_PACKSIZE, inpacket, inpacket_len);
 	decode_len = dns_decode(packet, DNS_PACKSIZE, inpacket, inpacket_len);
 	if (decode_len < 0) {
 	if (decode_len < 0) {
 		tlog(TLOG_DEBUG, "decode failed.\n");
 		tlog(TLOG_DEBUG, "decode failed.\n");
+		ret = RECV_ERROR_INVALID_PACKET;
 		goto errout;
 		goto errout;
 	}
 	}
 
 
@@ -4306,10 +4308,10 @@ static int _dns_server_tcp_process_one_request(struct dns_server_conn_tcp_client
 		request_data = (unsigned char *)(tcpclient->recvbuff.buf + proceed_len + sizeof(unsigned short));
 		request_data = (unsigned char *)(tcpclient->recvbuff.buf + proceed_len + sizeof(unsigned short));
 
 
 		/* process one record */
 		/* process one record */
-		if (_dns_server_recv(&tcpclient->head, request_data, request_len, &tcpclient->localaddr,
-							 tcpclient->localaddr_len, &tcpclient->addr, tcpclient->addr_len) != 0) {
-			tlog(TLOG_ERROR, "process tcp request failed.");
-			return RECV_ERROR_FAIL;
+		ret = _dns_server_recv(&tcpclient->head, request_data, request_len, &tcpclient->localaddr,
+							   tcpclient->localaddr_len, &tcpclient->addr, tcpclient->addr_len);
+		if (ret != 0) {
+			return ret;
 		}
 		}
 
 
 		proceed_len += sizeof(unsigned short) + request_len;
 		proceed_len += sizeof(unsigned short) + request_len;
@@ -4446,6 +4448,10 @@ static int _dns_server_process(struct dns_server_conn_head *conn, struct epoll_e
 	}
 	}
 	_dns_server_conn_release(conn);
 	_dns_server_conn_release(conn);
 
 
+	if (ret == RECV_ERROR_INVALID_PACKET) {
+		ret = 0;
+	}
+
 	return ret;
 	return ret;
 }
 }
 
 

+ 22 - 0
src/util.c

@@ -1098,3 +1098,25 @@ void print_stack(void)
 		tlog(TLOG_FATAL, "#%.2d: %p %s from %s+%p", idx + 1, addr, symbol, info.dli_fname, offset);
 		tlog(TLOG_FATAL, "#%.2d: %p %s from %s+%p", idx + 1, addr, symbol, info.dli_fname, offset);
 	}
 	}
 }
 }
+
+int write_file(const char *filename, void *data, int data_len)
+{
+	int fd = open(filename, O_WRONLY|O_CREAT, 0644);
+	if (fd < 0) {
+		return -1;
+	}
+
+	int len = write(fd, data, data_len);
+	if (len < 0) {
+		goto errout;
+	}
+
+	close(fd);
+	return 0;
+errout:
+	if (fd > 0) {
+		close(fd);
+	}
+
+	return -1;
+}

+ 2 - 0
src/util.h

@@ -112,6 +112,8 @@ uint64_t get_free_space(const char *path);
 
 
 void print_stack(void);
 void print_stack(void);
 
 
+int write_file(const char *filename, void *data, int data_len);
+
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }
 #endif /*__cplusplus */
 #endif /*__cplusplus */