| 94 | } |
| 95 | |
| 96 | int CreateTcpSocket(const unsigned short port /* = 0 */, const char *ip /* = "*" */, bool is_reuse /* = false */) { |
| 97 | int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 98 | if (fd >= 0) { |
| 99 | if (port != 0) { |
| 100 | if (is_reuse) { |
| 101 | int reuse_addr = 1; |
| 102 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr)); |
| 103 | } |
| 104 | struct sockaddr_in addr; |
| 105 | if (SetAddr(ip, port, addr) != 0) { |
| 106 | return -1; |
| 107 | } |
| 108 | int ret = bind(fd, (struct sockaddr*) &addr, sizeof(addr)); |
| 109 | if (ret != 0) { |
| 110 | close(fd); |
| 111 | return -1; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return fd; |
| 116 | } |
| 117 | |
| 118 | bool IsAuthReqPkg(const char * buf, int len) { |
| 119 | if (len < 5) { |
no test coverage detected