/** * Write the header to the start of the file. This sets the error fields. * This does not necessarily compute the hash; we want to leave that until * the last moment, since it can be costly. * * @param pico The Pico data structure. * @return True iff there is an error. */ static bool write_header(PICO * pico) { // Seek to the start of the file. if (fseek(pico->file, 0, SEEK_SET)) { pico->errno = CANNOT_SEEK; sprintf(pico->error_text, "Unable to seek to start of file."); return true; } // Allocate the header. uint8_t * header = MALLOC(uint8_t, KEY_POS + pico->key_length); if (header == NULL) { pico->errno = NO_MEMORY; sprintf(pico->error_text, "Cannot get memory to write header."); return true; } // Write the data into the header. *((magic_t *)(header + MAGIC_POS)) = HTON(magic_t, magic); *(major_t *)(header + MAJOR_POS) = HTON(major_t, pico->major); *(minor_t *)(header + MINOR_POS) = HTON(minor_t, pico->minor); *(offset_t *)(header + OFFSET_POS) = HTON(offset_t, pico->offset); memcpy(header + HASH_POS, pico->hash, HASH_LEN); *(keylen_t *)(header + KEYLEN_POS) = HTON(keylen_t, pico->key_length); memcpy(header + KEY_POS, pico->key, pico->key_length); // Write the header to the file. size_t size = fwrite(header, 1, KEY_POS + pico->key_length, pico->file); if (size != KEY_POS + pico->key_length) { pico->errno = CANNOT_WRITE; sprintf(pico->error_text, "Unable to write header to file."); return true; } // Success. pico->errno = OK; return false; }
uint64_t ofp_switch_features_get_datapath_id(void* ofp_msg) { return HTON(((struct ofp_switch_features*) ofp_msg)->datapath_id, 64); }
void ofp_header_set_length(void* ofp_msg, uint16_t length) { ((struct ofp_header*) ofp_msg)->length = HTON(length, 16); }
static void *link_to_peers(void *arg) { int client_fd, index, rv; st_netfd_t client; struct addrinfo hints, *ai, *p; const char *host = "0.0.0.0"; fprintf(stderr, "link to perrs\n"); for (int i=0; i<vp_count; i++) { if (i == my_index) continue; char port[16]; //snprintf(port, 16 - 1, "%d", RPC_PORT + i); index = RPC_PORT + i; sprintf(port, "%d", index); memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(host, port, &hints, &ai)) != 0) { fprintf(stderr, "[%d] failed to getaddrinfo to peer #%d\n", my_index, i); continue; } for (p = ai; p != NULL; p = p->ai_next) { if ((client_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { continue; } if ((client = st_netfd_open(client_fd)) == NULL) { close(client_fd); continue; } if ((rv = st_connect(client, p->ai_addr, p->ai_addrlen, ST_UTIME_NO_TIMEOUT)) != 0) { st_netfd_close(client); close(client_fd); continue; } else { fd_list[i] = client; } // 写入 1 字节的 rpc 握手头 if ((rv = st_write(client, (char *)&my_index, 1, ST_UTIME_NO_TIMEOUT)) == -1) { fprintf(stderr, "[%d] handshake failed.\n", my_index); } fd_list[i] = client; break; } if (p == NULL) { fprintf(stderr, "[%d] failed to connect to peer #%d.\n", my_index, i); } freeaddrinfo(ai); ai = NULL; p = NULL; // 模拟:发出第一个 rpc 包 char message[] = "hello rpc."; rpcpkg_len len = strlen(message); rpcpkg_len nlen = HTON(len); // network order of len char *package = (char*)calloc(sizeof(rpcpkg_len) + len, sizeof(char)); memcpy(package, &nlen, sizeof(len)); memcpy(package + sizeof(len), message, len); fprintf(stdout, "[%d] construction an package: << ", my_index); for (int j=0; j<len + sizeof(len); j++) { fprintf(stdout, "%02X ", *((uint8_t*)package + j)); } fprintf(stdout, " >>\n"); if ((rv = st_write(client, package, len + sizeof(rpcpkg_len), ST_UTIME_NO_TIMEOUT)) == -1) { fprintf(stderr, "[%d] failed to write package into client\n", my_index); } free(package); } return NULL; }