int do_write(const char* path, const char* buf, size_t size, off_t offset, struct fuse_file_info* fi) { char buffer[BUFFERSIZE]; memset(buffer, 0, BUFFERSIZE); corefs_packet req_packet; memset(&req_packet, 0, sizeof(req_packet)); int ret; unsigned int packet_size; server_data *sd=NULL; /* Get user info from the upper layer */ if(my_ops.up_get_user_info) my_ops.up_get_user_info(&(req_packet.payload.request.user_ids), path, NULL); char r_path_[MAXPATH]; char s_addr[MAXADDR]; char *r_path = &r_path_[0]; remove_addr(path, &r_path, s_addr); if (r_path[0]=='\0') { //there was no path other than the first '/' //ie this is for the root directory return do_write_local(path, buf, size, offset, fi); } ret = check_setup(s_addr, &sd); if (ret == -EHOSTUNREACH) return -EHOSTUNREACH; if (ret < 0) return -EIO; /* Send the write command first */ /* Prepare request */ packet_size = build_fileop(&req_packet, COREFS_REQUEST_WRITE, offset, size, r_path); /* Encapsualte the request */ ret = encap_corefs_header(buffer, &req_packet); encap_corefs_request(buffer + ret, &req_packet); #ifdef DEBUG dprintf(stderr, "WRITE: %s : %llu : %u\n", path, offset, size); #endif #ifdef DEBUG_NETWORK fprintf(stderr,"write request\n"); print_packet(req_packet); #endif if (send_packet(sd->ctx, buffer, packet_size) <=0) { dprintf(stderr, "error sending packet.\n"); return -EIO; } /* Now send the data */ packet_size = build_request_data((corefs_packet*) buffer, buf, size); char out_buffer[packet_size]; ret = encap_corefs_header(out_buffer,(corefs_packet*)buffer); encap_corefs_request(out_buffer + ret, (corefs_packet*)buffer); if (send_packet(sd->ctx, out_buffer, packet_size) <=0) { dprintf(stderr, "error sending packet.\n"); return -EIO; } memset(buffer, 0, BUFFERSIZE); if ((ret=client_receive_specified(sd->ctx, buffer, COREFS_RESPONSE_STATUS)) < 0) { dprintf(stderr, "write returned error.\n"); return ret; } return size; }
int do_setxattr(const char *path, const char *name, const char *value, size_t size, int flags) { char buffer[BUFFERSIZE]; char request_buf[BUFFERSIZE]; memset(buffer, 0, BUFFERSIZE); memset(request_buf, 0, BUFFERSIZE); corefs_packet* packet=(corefs_packet*)buffer; int ret; unsigned int packet_size; server_data *sd=NULL; char r_path_[MAXPATH]; char s_addr[MAXADDR]; char *r_path = &r_path_[0]; remove_addr(path, &r_path, s_addr); if (r_path[0]=='\0') { //there was no path other than the first '/' //ie this is for the root directory return do_setxattr_local(path, name, value, size, flags); } ret = check_setup(s_addr, &sd); if (ret == -EHOSTUNREACH) return -EHOSTUNREACH; if (ret < 0) return -EIO; #ifdef DEBUG dprintf(stderr, "SETXATTR: path[%s] : name[%s]\n", path, name); #endif /* Get user info from the upper layer */ if(my_ops.up_get_user_info) my_ops.up_get_user_info(&(packet->payload.request.user_ids), path, NULL); /* First send the command */ packet_size = build_xattr(packet, COREFS_XATTR_SETXATTR, name, r_path, size, flags); ret = encap_corefs_header(request_buf, packet); /* encapsulate in * network * order */ encap_corefs_request(request_buf + ret, packet); #ifdef DEBUG_NETWORK fprintf(stderr, "printing setxattr request\n"); print_packet(*packet); #endif if (send_packet(sd->ctx, request_buf, packet_size) <=0) { dprintf(stderr, "setxattr: error sending packet.\n"); return -EIO; } /* Now send the attribute value */ packet_size = build_request_data(packet, value, size); ret = encap_corefs_header(request_buf, packet); encap_corefs_request(request_buf + ret, packet); if (send_packet(sd->ctx, request_buf, packet_size) <=0) { dprintf(stderr, "setxattr: error sending packet.\n"); return -EIO; } memset(buffer, 0, BUFFERSIZE); if ((ret = client_receive_specified(sd->ctx, buffer, COREFS_RESPONSE_STATUS)) < 0) { dprintf(stderr, "setxattr returned error.\n"); return ret; } return 0; }
void gfw_resolve(const char *hostname, char *out_ip) { int sockfd; struct sockaddr_in dest; socklen_t addr_len; char *dns_data = NULL; int data_size; int i, n; char recv_buf[BUFSIZE]; char *host_buf = NULL; /* 根据hostname找出ip */ //get_hostname(hostname, out_ip); //printf("out_ip = %s\n", out_ip); /* 检测IP是否在黑名单上,如果不是则返回拿到的真实IP */ //if (is_bad_ip(out_ip) == 0) //return; /* 如果IP是在黑名单上,则用DNS查找真实的IP */ if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { perror("socket() failed"); exit(errno); } /* 设置服务器端(对方)地址和端口信息 */ memset(&dest, 0, sizeof(dest)); dest.sin_family = AF_INET; dest.sin_port = htons(53); //dest.sin_addr.s_addr = inet_addr(DNS_SERVER); // 不推荐inet_addr() if (inet_pton(AF_INET, DNS_SERVER, &(dest.sin_addr)) != 1) { fprintf(stderr, "inet_pton() convert %s failed: %s\n", DNS_SERVER, strerror(errno)); exit(errno); } addr_len = sizeof(struct sockaddr_in); /* 构造DNS数据包 */ /* 将hostname复制到host_buf,因为调用函数里的strtok会修改hostname */ host_buf = (char *) calloc(1, strlen(hostname) + 1); memcpy(host_buf, hostname, strlen(hostname)); dns_data = build_request_data(host_buf, &data_size); printf("send dns packet:\n"); char *pbuf = dns_data; for (i = 0; i < data_size; ++i) printf("%02X ", (unsigned char) pbuf[i]); printf("\n"); /* 发送DNS数据包 */ n = sendto(sockfd, dns_data, data_size, 0, (struct sockaddr *) &dest, addr_len); if (n == -1) { perror("sendto() failed"); exit(errno); } /* 接收返回的DNS响应包 */ for (i = 0; i < MAX_WAIT_TIMES; ++i) { memset(recv_buf, 0, BUFSIZE); n = recvfrom(sockfd, recv_buf, BUFSIZE, 0, (struct sockaddr *) &dest, &addr_len); if (n == -1) { perror("recvfrom() failed"); exit(errno); } printf("recv %d bytes:\n", n); int j; for (j = 0; j < n; ++j) printf("%02X ", (unsigned char) recv_buf[j]); printf("\n"); /* 解析返回的响应包 */ decode_dns_response(recv_buf, hostname, out_ip); break; /* 再次检测IP是否在黑名单上,如果不在则跳出 */ //if (is_bad_ip(out_ip) == 0) //break; /* 如果IP仍在黑名单上,继续等待接收DNS返回的数据,总共尝试 * MAX_WAIT_TIMES 次 */ } /* 记得释放 */ free(dns_data); free(host_buf); /* 这里释放的host_buf会不会有问题,毕竟strtok修改过 */ }