int main() { /* content-length of the file */ unsigned opt; const char *port = "ftp"; ftp_host_info_t *server; /* socket to ftp server */ FILE *control_stream; static FILE ctrl_stream; /* continue previous transfer (-c) */ char caFtpHead[FTP_HEAD_SIZE]; int nFileSize; int rd = 0; int nFdData; int nSave; char *pcRam; //FILE* ftp_get_head(const char *host, int port, const char *user, const char *password, char *server_path, // char caFtpHead[FTP_HEAD_SIZE], int *pnHeadSize, int *pnFdData, FILE *ctrl_stream, int *pnFileSize) control_stream = ftp_get_head("192.168.1.170", 21, "sun", "123456", "sip11.cfg", caFtpHead, &rd, &nFdData, &ctrl_stream, &nFileSize); caFtpHead[rd - 1] = '\0'; printf("-------rd=%d===nFileSize=%d===\n", rd, nFileSize); printf("-------------------------n %s n------------------------\n", caFtpHead); //nSave = ftp_recv_to_file(nFdData, control_stream, caFtpHead, rd, "test.cfg"); pcRam = (char *)calloc(sizeof(char), nFileSize); if (NULL == pcRam) { perror("calloc error\n"); exit(-1); } nSave = ftp_recv_to_ram(nFdData, control_stream, caFtpHead, rd, pcRam); printf("nSave =%d \n", nSave); pcRam[200] = '\0'; printf("-------------------------n %s n------------------------\n", pcRam + 130000); return 0; /* Set default values */ server = xmalloc(sizeof(*server)); server->user = "******"; server->password = "******"; /* We want to do exactly _one_ DNS lookup, since some sites (i.e. ftp.us.debian.org) use round-robin DNS * and we want to connect to only one IP... */ server->lsa = str2sockaddr("192.168.1.151", 21, DIE_ON_ERROR); printf("BUFSIZ=%d off_t = %d\n", BUFSIZ, (int)((off_t) - 1)); /* Connect/Setup/Configure the FTP session */ control_stream = ftp_login(server); ftp_receive(server, control_stream, "ram.bin", "ram_zimage.bin"); return 0; }
FILE *ftp_get_head(const char *host, int port, const char *user, const char *password, char *server_path, char caFtpHead[FTP_HEAD_SIZE], int *pnHeadSize, int *pnFdData, FILE *ctrl_stream, int *pnFileSize) { printf("ftp_get_head++++++++++++++++++++++\n"); ftp_host_info_t server; FILE *control_stream; char caPort[10]; server.user = user; server.password = password; server.lsa = str2sockaddr(host, port, DIE_ON_ERROR); printf("ftp_get_head===1111111111111=======\n"); control_stream = ftp_login(&server); printf("ftp_get_head====2222222222222======\n"); memcpy(ctrl_stream, control_stream, sizeof(FILE)); *pnHeadSize = ftp_receive_head(control_stream, pnFdData, pnFileSize, &server, server_path, caFtpHead); printf("ftp_get_head====3333333333======\n"); return control_stream; }
bool add_edge_h(connection_t *c) { edge_t *e; node_t *from, *to; char from_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE]; char to_address[MAX_STRING_SIZE]; char to_port[MAX_STRING_SIZE]; sockaddr_t address; uint32_t options; int weight; if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d", from_name, to_name, to_address, to_port, &options, &weight) != 6) { logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name, c->hostname); return false; } /* Check if names are valid */ if(!check_id(from_name) || !check_id(to_name)) { logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name, c->hostname, "invalid name"); return false; } if(seen_request(c->buffer)) return true; /* Lookup nodes */ from = lookup_node(from_name); to = lookup_node(to_name); if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node) { /* ignore indirect edge registrations for tunnelserver */ ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s)", "ADD_EDGE", c->name, c->hostname); return true; } if(!from) { from = new_node(); from->name = xstrdup(from_name); node_add(from); } if(!to) { to = new_node(); to->name = xstrdup(to_name); node_add(to); } /* Convert addresses */ address = str2sockaddr(to_address, to_port); /* Check if edge already exists */ e = lookup_edge(from, to); if(e) { if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) { if(from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry", "ADD_EDGE", c->name, c->hostname); send_add_edge(c, e); return true; } else { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not match existing entry", "ADD_EDGE", c->name, c->hostname); edge_del(e); graph(); } } else return true; } else if(from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist", "ADD_EDGE", c->name, c->hostname); contradicting_add_edge++; e = new_edge(); e->from = from; e->to = to; send_del_edge(c, e); free_edge(e); return true; } e = new_edge(); e->from = from; e->to = to; e->address = address; e->options = options; e->weight = weight; edge_add(e); /* Tell the rest about the new edge */ if(!tunnelserver) forward_request(c); /* Run MST before or after we tell the rest? */ graph(); return true; }
bool ack_h(connection_t *c, const char *request) { if(c->protocol_minor == 1) return upgrade_h(c, request); char hisport[MAX_STRING_SIZE]; char *hisaddress; int weight, mtu; uint32_t options; node_t *n; bool choice; if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) { logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname); return false; } /* Check if we already have a node_t for him */ n = lookup_node(c->name); if(!n) { n = new_node(); n->name = xstrdup(c->name); node_add(n); } else { if(n->connection) { /* Oh dear, we already have a connection to this node. */ logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname); if(n->connection->outgoing) { if(c->outgoing) logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!"); else c->outgoing = n->connection->outgoing; n->connection->outgoing = NULL; } terminate_connection(n->connection, false); /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */ graph(); } } n->connection = c; c->node = n; if(!(c->options & options & OPTION_PMTU_DISCOVERY)) { c->options &= ~OPTION_PMTU_DISCOVERY; options &= ~OPTION_PMTU_DISCOVERY; } c->options |= options; if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu) n->mtu = mtu; if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu) n->mtu = mtu; if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) { if(choice) c->options |= OPTION_CLAMP_MSS; else c->options &= ~OPTION_CLAMP_MSS; } /* Activate this connection */ c->allow_request = ALL; logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name, c->hostname); /* Send him everything we know */ send_everything(c); /* Create an edge_t for this connection */ c->edge = new_edge(); c->edge->from = myself; c->edge->to = n; sockaddr2str(&c->address, &hisaddress, NULL); c->edge->address = str2sockaddr(hisaddress, hisport); free(hisaddress); sockaddr_t local_sa; socklen_t local_salen = sizeof local_sa; if (getsockname(c->socket, &local_sa.sa, &local_salen) < 0) logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get local socket address for connection with %s", c->name); else { char *local_address; sockaddr2str(&local_sa, &local_address, NULL); c->edge->local_address = str2sockaddr(local_address, myport); free(local_address); } c->edge->weight = (weight + c->estimated_weight) / 2; c->edge->connection = c; c->edge->options = c->options; edge_add(c->edge); /* Notify everyone of the new edge */ if(tunnelserver) send_add_edge(c, c->edge); else send_add_edge(everyone, c->edge); /* Run MST and SSSP algorithms */ graph(); return true; }
bool ans_key_h(connection_t *c) { char from_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE]; char key[MAX_STRING_SIZE]; char address[MAX_STRING_SIZE] = ""; char port[MAX_STRING_SIZE] = ""; int cipher, digest, maclength, compression; node_t *from, *to; if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING, from_name, to_name, key, &cipher, &digest, &maclength, &compression, address, port) < 7) { logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name, c->hostname); return false; } if(!check_id(from_name) || !check_id(to_name)) { logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name"); return false; } from = lookup_node(from_name); if(!from) { logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list", "ANS_KEY", c->name, c->hostname, from_name); return true; } to = lookup_node(to_name); if(!to) { logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list", "ANS_KEY", c->name, c->hostname, to_name); return true; } /* Forward it if necessary */ if(to != myself) { if(tunnelserver) return true; if(!to->status.reachable) { logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable", "ANS_KEY", c->name, c->hostname, to_name); return true; } if(!*address && from->address.sa.sa_family != AF_UNSPEC) { char *address, *port; ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name); sockaddr2str(&from->address, &address, &port); send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port); free(address); free(port); return true; } return send_request(to->nexthop->connection, "%s", c->buffer); } /* Update our copy of the origin's packet key */ from->outkey = xrealloc(from->outkey, strlen(key) / 2); from->outkeylength = strlen(key) / 2; hex2bin(key, from->outkey, from->outkeylength); /* Check and lookup cipher and digest algorithms */ if(cipher) { from->outcipher = EVP_get_cipherbynid(cipher); if(!from->outcipher) { logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname); return true; } if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) { logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname); return true; } } else { from->outcipher = NULL; } from->outmaclength = maclength; if(digest) { from->outdigest = EVP_get_digestbynid(digest); if(!from->outdigest) { logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname); return true; } if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) { logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname); return true; } } else { from->outdigest = NULL; } if(compression < 0 || compression > 11) { logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname); return true; } from->outcompression = compression; if(from->outcipher) if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) { logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s", from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL)); return true; } from->status.validkey = true; from->sent_seqno = 0; if(*address && *port) { ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port); sockaddr_t sa = str2sockaddr(address, port); update_node_udp(from, &sa); } if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent) send_mtu_probe(from); return true; }
static inline len_and_sockaddr* xhost2sockaddr(const char *host, int port) { return str2sockaddr(host, port, 0); }
bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { edge_t *e; node_t *from, *to; char from_name[MAX_STRING_SIZE]; int from_devclass; char to_name[MAX_STRING_SIZE]; char to_address[MAX_STRING_SIZE]; char to_port[MAX_STRING_SIZE]; int to_devclass; sockaddr_t address; uint32_t options; int weight; if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d", from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight) != 8) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name, c->hostname); return false; } /* Check if names are valid */ if(!check_id(from_name) || !check_id(to_name)) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name, c->hostname, "invalid name"); return false; } // Check if devclasses are valid if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name, c->hostname, "from devclass invalid"); return false; } if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name, c->hostname, "to devclass invalid"); return false; } if(seen_request(mesh, request)) return true; /* Lookup nodes */ from = lookup_node(mesh, from_name); to = lookup_node(mesh, to_name); if(!from) { from = new_node(); from->status.blacklisted = mesh->default_blacklist; from->name = xstrdup(from_name); node_add(mesh, from); } from->devclass = from_devclass; node_write_devclass(mesh, from); if(!to) { to = new_node(); to->status.blacklisted = mesh->default_blacklist; to->name = xstrdup(to_name); node_add(mesh, to); } to->devclass = to_devclass; node_write_devclass(mesh, to); /* Convert addresses */ address = str2sockaddr(to_address, to_port); /* Check if edge already exists */ e = lookup_edge(from, to); if(e) { if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) { if(from == mesh->self) { logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry", "ADD_EDGE", c->name, c->hostname); send_add_edge(mesh, c, e); return true; } else { logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) which does not match existing entry", "ADD_EDGE", c->name, c->hostname); edge_del(mesh, e); graph(mesh); } } else return true; } else if(from == mesh->self) { logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself which does not exist", "ADD_EDGE", c->name, c->hostname); mesh->contradicting_add_edge++; e = new_edge(); e->from = from; e->to = to; send_del_edge(mesh, c, e); free_edge(e); return true; } e = new_edge(); e->from = from; e->to = to; e->address = address; e->options = options; e->weight = weight; edge_add(mesh, e); /* Tell the rest about the new edge */ forward_request(mesh, c, request); /* Run MST before or after we tell the rest? */ graph(mesh); return true; }