int main(void) { int s, t, len, skip_cmd = 0; struct sockaddr_un remote; char msgbuf[4096], buf[UNIX_PATH_MAX], *env; if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } printf("Trying to connect...\n"); remote.sun_family = AF_UNIX; env = getenv("EVAS_CSERVE2_SOCKET"); if (!env) { snprintf(buf, sizeof(buf), "/tmp/.evas-cserve2-%x.socket", (int)getuid()); env = buf; } strncpy(remote.sun_path, env, UNIX_PATH_MAX - 1); len = strlen(remote.sun_path) + sizeof(remote.sun_family); if (connect(s, (struct sockaddr *)&remote, len) == -1) { perror("connect"); exit(1); } printf("Connected.\n"); while(!feof(stdin)) { char cmd[1024]; int i; int size; void *msg; if (skip_cmd) skip_cmd = 0; else printf("\n> "); if (!fgets(cmd, sizeof(cmd), stdin)) break; len = strlen(cmd) - 1; cmd[len] = '\0'; if (!len) { skip_cmd = 1; continue; } for (i = 0; _msg_types[i].name; i++) { if (!strcmp(cmd, _msg_types[i].name)) break; } // discards the end of the message if we can't parse it if (!_msg_types[i].name) { printf("Invalid command.\n"); continue; } if (!_msg_types[i].parse_func) { printf("Command %s still unhandled.\n", _msg_types[i].name); continue; } msg = _msg_types[i].parse_func(&size); if (send(s, &size, sizeof(size), MSG_NOSIGNAL) == -1) { perror("send size"); exit(1); } if (send(s, msg, size, MSG_NOSIGNAL) == -1) { perror("send"); exit(1); } free(msg); usleep(100000); if ((t=recv(s, &size, sizeof(size), MSG_DONTWAIT)) > 0) { len = recv(s, msgbuf, size, 0); printf("size of received message: %d\n", len); if (len != size) { printf("couldn't read entire message.\n"); continue; } debug_msg(&size, sizeof(size)); debug_msg(msgbuf, size); parse_answer(msgbuf); } else { if (t < 0 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) continue; else fprintf(stderr, "Server closed connection\n"); exit(1); } } close(s); return 0; }
static int query_domain(st_netfd_t nfd, const char *name, struct in_addr *addr, st_utime_t timeout) { querybuf_t qbuf; u_char *buf = qbuf.buf; HEADER *hp = &qbuf.hdr; int blen = sizeof(qbuf); int i, len, id; for (i = 0; i < _res.nscount; i++) { len = res_mkquery(QUERY, name, C_IN, T_A, NULL, 0, NULL, buf, blen); if (len <= 0) { h_errno = NO_RECOVERY; return -1; } id = hp->id; if (st_sendto(nfd, buf, len, (struct sockaddr *)&(_res.nsaddr_list[i]), sizeof(struct sockaddr), timeout) != len) { h_errno = NETDB_INTERNAL; /* EINTR means interrupt by other thread, NOT by a caught signal */ if (errno == EINTR) return -1; continue; } /* Wait for reply */ do { len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); if (len <= 0) break; } while (id != hp->id); if (len < HFIXEDSZ) { h_errno = NETDB_INTERNAL; if (len >= 0) errno = EMSGSIZE; else if (errno == EINTR) /* see the comment above */ return -1; continue; } hp->ancount = ntohs(hp->ancount); hp->qdcount = ntohs(hp->qdcount); if ((hp->rcode != NOERROR) || (hp->ancount == 0)) { switch (hp->rcode) { case NXDOMAIN: h_errno = HOST_NOT_FOUND; break; case SERVFAIL: h_errno = TRY_AGAIN; break; case NOERROR: h_errno = NO_DATA; break; case FORMERR: case NOTIMP: case REFUSED: default: h_errno = NO_RECOVERY; } continue; } if (parse_answer(&qbuf, len, addr) == 0) return 0; } return -1; }
static int query_domain(st_netfd_t nfd, const char *name, struct in_addr *addr, st_utime_t timeout) { // name="www.baidu.com"; #if 0 char *name=malloc(128); memset(name,0,128); strcpy(name,hostname); LOGD("hostname [%s] name[%s]",hostname,name); #endif querybuf_t qbuf; u_char *buf = qbuf.buf; HEADER *hp = &qbuf.hdr; int blen = sizeof(qbuf); int i, len, id; for (i = 0; i < _res.nscount; i++) { len = res_mkquery(QUERY, name, C_IN, T_A, NULL, 0, NULL, buf, blen); if (len <= 0) { h_errno = NO_RECOVERY; return -1; } id = hp->id; if (st_sendto(nfd, buf, len, (struct sockaddr *)&(_res.nsaddr_list[i]), sizeof(struct sockaddr), timeout) != len) { h_errno = NETDB_INTERNAL; /* EINTR means interrupt by other thread, NOT by a caught signal */ if (errno == EINTR) return -1; continue; } len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); LOGD(" st_recvfrom len[%d]",len); return 0; /* Wait for reply */ do { len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); if (len <= 0) /*一直接收不到应答消息啊*/ { LOGD(" st_recvfrom len[%d]",len); break; } } while (id != hp->id); if (len < HFIXEDSZ) { h_errno = NETDB_INTERNAL; if (len >= 0) errno = EMSGSIZE; else if (errno == EINTR) /* see the comment above */ return -1; continue; } hp->ancount = ntohs(hp->ancount); hp->qdcount = ntohs(hp->qdcount); if ((hp->rcode != NOERROR) || (hp->ancount == 0)) { switch (hp->rcode) { case NXDOMAIN: h_errno = HOST_NOT_FOUND; break; case SERVFAIL: h_errno = TRY_AGAIN; break; case NOERROR: /*走这里*/ h_errno = NO_DATA; break; case FORMERR: case NOTIMP: case REFUSED: default: /*走这里*/ h_errno = NO_RECOVERY; } continue; } if (parse_answer(&qbuf, len, addr) == 0) { LOGD("after parse_answer"); //free(name);//add by me for malloc return 0;//走这里 } } ///free(name);//add by me 20150923 return -1; }