static int parse_addr(char *str, struct in_addr *ip, in_port_t *port) { char *p; if((p = strchr(str, ':')) != NULL) { /* its either host:port or ip:port */ *(p++) = '\0'; if(get_host_addr(str, ip) < 0) return -1; *port = htons(atoi(p)); /* reconstruct the string */ *(--p) = ':'; } else if(get_host_addr(str, ip) < 0) { return -1; } return 0; }
int is_proxy_address(char* host, char* port) { __tri(is_proxy_address); struct in_addr addr; if (RC_OK == get_host_addr(host, &addr)) { const int port = get_port_from_str (port); static const size_t sz = sizeof(struct in_addr); __tre(return) (port == get_out_port() && !memcmp (&addr, get_out_addr(), sz)) || (port == get_in_port() && !memcmp (&addr, get_in_addr(), sz)); } __tre(return) 0; }
int client(int sock_type, const void *host, unsigned short port, int isHdrIncl, int timeout) { int sock = 0, ret = 0; char buff[MAX_DATA_LEN] = {0}; int len = 0; int index = 0, grp = 0; struct timeval tim = {timeout, 0}; if(((char*)host)[0] == 0 || get_host_addr(addr, sizeof(addr), host, port) != 0) { return -1; } if((sock = socket(*(unsigned short*)(addr+1), sock_type, 0)) < 3) { //if((sock = socket(AF_INET, sock_type, 0)) < 3) { return -1; } setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tim, sizeof(tim)); if(sock_type == SOCK_STREAM) { if(connect(sock, (struct sockaddr *)(addr+1), addr[0]) != 0) { ret = -1; goto done; } } for(index = 0; index < g_data_num; index++) { len = sizeof buff; if(sock_type == SOCK_DGRAM) { char dst[MAX_ADDR_LEN] = {0}; int dst_len = sizeof(dst); if((len = sendto(sock, g_data_pair[index].data1+1, g_data_pair[index].data1[0], 0, (struct sockaddr *)(addr+1), addr[0])) < 0) { //return -1; ret = -1; goto done; } len = recvfrom(sock, buff, sizeof buff, 0, (struct sockaddr *)dst, &dst_len); } else if(sock_type == SOCK_STREAM) { if(send(sock, g_data_pair[index].data1+1, g_data_pair[index].data1[0], 0) < 0) { ret = -1; goto done; } len = recv(sock, buff, sizeof buff, 0); } if(g_data_pair[index].data2[0] == 0) { continue; } if(len != g_data_pair[index].data2[0] || memcmp(&g_data_pair[index].data2[1], buff, len) != 0) { ret = index+1; goto done; } } done: #ifdef _WINDOWS closesocket(sock); #else close(sock); #endif return ret; }