static bool parse_sockaddr_components(struct sockaddr_storage *ss, const char *host_s, const char *port_s, uint16_t default_port, const char *s) { struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *, ss); int port; if (port_s && port_s[0]) { if (!str_to_int(port_s, 10, &port) || port < 0 || port > 65535) { VLOG_ERR("%s: bad port number \"%s\"", s, port_s); } } else { port = default_port; } memset(ss, 0, sizeof *ss); if (strchr(host_s, ':')) { struct sockaddr_in6 *sin6 = ALIGNED_CAST(struct sockaddr_in6 *, ss); sin6->sin6_family = AF_INET6; sin6->sin6_port = htons(port); if (!ipv6_parse(host_s, &sin6->sin6_addr)) { VLOG_ERR("%s: bad IPv6 address \"%s\"", s, host_s); goto exit; } } else {
/* Initializes 'buffer' with 'n' bytes of high-quality random numbers. Returns * 0 if successful, otherwise a positive errno value or EOF on error. */ int get_entropy(void *buffer, size_t n) { #ifndef _WIN32 size_t bytes_read; int error; int fd; fd = open(urandom, O_RDONLY); if (fd < 0) { VLOG_ERR("%s: open failed (%s)", urandom, ovs_strerror(errno)); return errno ? errno : EINVAL; } error = read_fully(fd, buffer, n, &bytes_read); close(fd); if (error) { VLOG_ERR("%s: read error (%s)", urandom, ovs_retval_to_string(error)); } #else int error = 0; HCRYPTPROV crypt_prov = 0; CryptAcquireContext(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!CryptGenRandom(crypt_prov, n, buffer)) { VLOG_ERR("CryptGenRandom: read error (%s)", ovs_lasterror_to_string()); error = EINVAL; } CryptReleaseContext(crypt_prov, 0); #endif return error; }
static void get_manuf_and_prodname(char *cmd_path, char **manufacturer, char **product_name) { char dmid_cmd[256]; snprintf(dmid_cmd, sizeof(dmid_cmd), "%s -s %s", cmd_path, "system-manufacturer"); get_sys_cmd_out(dmid_cmd, manufacturer); if (*manufacturer == NULL) { VLOG_ERR("Unable to get system manufacturer."); return; } snprintf(dmid_cmd, sizeof(dmid_cmd), "%s -s %s", cmd_path, "system-product-name"); get_sys_cmd_out(dmid_cmd, product_name); if (*product_name == NULL) { VLOG_ERR("Unable to get system product name."); return; } return; } /* get_manuf_and_prodname() */
static int ptcp_open(const char *name, char *suffix, struct pvconn **pvconnp) { struct sockaddr_in sin; int retval; int fd; unsigned int yes = 1; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { VLOG_ERR("%s: socket: %s", name, strerror(errno)); return errno; } if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) { VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno)); return errno; } memset(&sin, 0, sizeof sin); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT); retval = bind(fd, (struct sockaddr *) &sin, sizeof sin); if (retval < 0) { int error = errno; VLOG_ERR("%s: bind: %s", name, strerror(error)); close(fd); return error; } return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp); }
/* Sets 'fd' to non-blocking mode. Returns 0 if successful, otherwise a * positive errno value. */ int set_nonblocking(int fd) { #ifndef _WIN32 int flags = fcntl(fd, F_GETFL, 0); if (flags != -1) { if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1) { return 0; } else { VLOG_ERR("fcntl(F_SETFL) failed: %s", ovs_strerror(errno)); return errno; } } else { VLOG_ERR("fcntl(F_GETFL) failed: %s", ovs_strerror(errno)); return errno; } #else unsigned long arg = 1; if (ioctlsocket(fd, FIONBIO, &arg)) { int error = sock_errno(); VLOG_ERR("set_nonblocking failed: %s", sock_strerror(error)); return error; } return 0; #endif }
static int tcp_open(const char *name, char *suffix, struct vconn **vconnp) { char *save_ptr; const char *host_name; const char *port_string; struct sockaddr_in sin; int retval; int fd; /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that * can cause segfaults here: * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614. * Using "::" instead of the obvious ":" works around it. */ host_name = strtok_r(suffix, "::", &save_ptr); port_string = strtok_r(NULL, "::", &save_ptr); if (!host_name) { ofp_error(0, "%s: bad peer name format", name); return EAFNOSUPPORT; } memset(&sin, 0, sizeof sin); sin.sin_family = AF_INET; if (lookup_ip(host_name, &sin.sin_addr)) { return ENOENT; } sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT); fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { VLOG_ERR("%s: socket: %s", name, strerror(errno)); return errno; } retval = set_nonblocking(fd); if (retval) { close(fd); return retval; } retval = connect(fd, (struct sockaddr *) &sin, sizeof sin); if (retval < 0) { if (errno == EINPROGRESS) { return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp); } else { int error = errno; VLOG_ERR("%s: connect: %s", name, strerror(error)); close(fd); return error; } } else { return new_tcp_vconn(name, fd, 0, &sin, vconnp); } }
static int plugins_open_plugin(const char *filename, void *data) { struct plugin_class *plcl; lt_dlhandle handle; if (!(handle = lt_dlopenadvise(filename, *(lt_dladvise *)data))) { VLOG_ERR("Failed loading %s: %s", filename, lt_dlerror()); return 0; } if (!(plcl = (struct plugin_class *)malloc(sizeof(struct plugin_class)))) { VLOG_ERR("Couldn't allocate plugin class"); goto err_plugin_class; } if (!(plcl->init = lt_dlsym(handle, "init")) || !(plcl->run = lt_dlsym(handle, "run")) || !(plcl->wait = lt_dlsym(handle, "wait")) || !(plcl->destroy = lt_dlsym(handle, "destroy"))) { VLOG_ERR("Couldn't initialize the interface for %s", filename); goto err_dlsym; } // The following APIs are optional, so don't fail if they are missing. plcl->netdev_register = lt_dlsym(handle, "netdev_register"); plcl->ofproto_register = lt_dlsym(handle, "ofproto_register"); plcl->bufmon_register = lt_dlsym(handle, "bufmon_register"); if (lt_dlcaller_set_data(interface_id, handle, plcl)) { VLOG_ERR("plugin %s initialized twice? must be a bug", filename); goto err_set_data; } plcl->init(); VLOG_INFO("Loaded plugin library %s", filename); return 0; err_set_data: err_dlsym: free(plcl); err_plugin_class: if (lt_dlclose(handle)) { VLOG_ERR("Couldn't dlclose %s", filename); } return 0; }
/* Configure management interface as dhcp mode. * On success, returns CMD_SUCCESS. On failure, returns CMD_OVSDB_FAILURE. */ static int mgmt_intf_set_dhcp() { const struct ovsrec_system *row = NULL; struct ovsdb_idl_txn* status_txn = NULL; enum ovsdb_idl_txn_status status; struct smap smap = SMAP_INITIALIZER(&smap); status_txn = cli_do_config_start(); if (NULL == status_txn) { VLOG_ERR(OVSDB_TXN_CREATE_ERROR); cli_do_config_abort(status_txn); return CMD_OVSDB_FAILURE; } row = ovsrec_system_first(idl); if (!row) { VLOG_ERR(OVSDB_ROW_FETCH_ERROR); cli_do_config_abort(status_txn); return CMD_OVSDB_FAILURE; } /* If current mode is static remove static configs from DB. */ smap_clone(&smap, &row->mgmt_intf); if (is_mode_static(row)) { mgmt_intf_clear_ipv4_config_db(row, &smap); mgmt_intf_clear_ipv6_config_db(row, &smap); } smap_replace(&smap, SYSTEM_MGMT_INTF_MAP_MODE, SYSTEM_MGMT_INTF_MAP_MODE_DHCP); ovsrec_system_set_mgmt_intf(row, &smap); smap_destroy(&smap); status = cli_do_config_finish(status_txn); if (TXN_SUCCESS == status || TXN_UNCHANGED == status) { return CMD_SUCCESS; } else { VLOG_ERR(OVSDB_TXN_COMMIT_ERROR); return CMD_OVSDB_FAILURE; } }
static void discovery_local_port_cb(const struct ofp_port *port, void *d_) { struct discovery *d = d_; if (port) { char name[OFP_MAX_PORT_NAME_LEN + 1]; struct netdev *netdev; int retval; /* Check that this was really a change. */ get_port_name(port, name, sizeof name); if (d->dhcp && !strcmp(netdev_get_name(dhclient_get_netdev(d->dhcp)), name)) { return; } /* Destroy current DHCP client. */ dhclient_destroy(d->dhcp); d->dhcp = NULL; /* Bring local network device up. */ retval = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev); if (retval) { VLOG_ERR(LOG_MODULE, "Could not open %s device, discovery disabled: %s", name, strerror(retval)); return; } retval = netdev_turn_flags_on(netdev, NETDEV_UP, true); if (retval) { VLOG_ERR(LOG_MODULE, "Could not bring %s device up, discovery disabled: %s", name, strerror(retval)); return; } netdev_close(netdev); /* Initialize DHCP client. */ retval = dhclient_create(name, modify_dhcp_request, validate_dhcp_offer, (void *) d->s, &d->dhcp); if (retval) { VLOG_ERR(LOG_MODULE, "Failed to initialize DHCP client, " "discovery disabled: %s", strerror(retval)); return; } dhclient_set_max_timeout(d->dhcp, 3); dhclient_init(d->dhcp, 0); } else { dhclient_destroy(d->dhcp); d->dhcp = NULL; } }
static int ecmp_config_set_status (bool status, const char * field) { const struct ovsrec_system *ovs_row = NULL; enum ovsdb_idl_txn_status txn_status; struct ovsdb_idl_txn *status_txn = cli_do_config_start (); bool rc = false; struct smap smap_ecmp_config; if (status_txn == NULL) { VLOG_ERR (OVSDB_TXN_CREATE_ERROR); cli_do_config_abort (status_txn); return CMD_OVSDB_FAILURE; } /* Need to set ecmp_config status */ ovs_row = ovsrec_system_first (idl); if (!ovs_row) { VLOG_ERR (OVSDB_ROW_FETCH_ERROR); cli_do_config_abort (status_txn); return CMD_OVSDB_FAILURE; } rc = smap_get_bool (&ovs_row->ecmp_config, field, SYSTEM_ECMP_CONFIG_ENABLE_DEFAULT); if (rc != status) { smap_clone (&smap_ecmp_config, &ovs_row->ecmp_config); smap_replace (&smap_ecmp_config, field, status ? "true" : "false"); VLOG_DBG ("%s Set the ecmp config to status = %s old state = %s", __func__, status ? "enabled" : "disabled", rc ? "enabled" : "disabled"); ovsrec_system_set_ecmp_config (ovs_row, &smap_ecmp_config); smap_destroy (&smap_ecmp_config); } txn_status = cli_do_config_finish (status_txn); if (txn_status == TXN_SUCCESS || txn_status == TXN_UNCHANGED) return CMD_SUCCESS; else { VLOG_ERR (OVSDB_TXN_COMMIT_ERROR); return CMD_OVSDB_FAILURE; } }
/* If a pidfile has been configured, creates it and stores the running * process's pid in it. Ensures that the pidfile will be deleted when the * process exits. */ static void make_pidfile(void) { if (pidfile) { /* Create pidfile via temporary file, so that observers never see an * empty pidfile or an unlocked pidfile. */ long int pid = getpid(); char *tmpfile; int fd; tmpfile = xasprintf("%s.tmp%ld", pidfile, pid); fatal_signal_add_file_to_unlink(tmpfile); fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (fd >= 0) { struct flock lck; lck.l_type = F_WRLCK; lck.l_whence = SEEK_SET; lck.l_start = 0; lck.l_len = 0; if (fcntl(fd, F_SETLK, &lck) != -1) { char *text = xasprintf("%ld\n", pid); if (write(fd, text, strlen(text)) == strlen(text)) { fatal_signal_add_file_to_unlink(pidfile); if (rename(tmpfile, pidfile) < 0) { VLOG_ERR("failed to rename \"%s\" to \"%s\": %s", tmpfile, pidfile, strerror(errno)); fatal_signal_remove_file_to_unlink(pidfile); close(fd); } else { /* Keep 'fd' open to retain the lock. */ } free(text); } else { VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno)); close(fd); } } else { VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno)); close(fd); } } else { VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno)); } fatal_signal_remove_file_to_unlink(tmpfile); free(tmpfile); } free(pidfile); pidfile = NULL; }
void plugins_init(const char *path) { char *plugins_path; lt_dladvise advise; if (path && !strcmp(path, "none")) { return; } if (!(plugins_path = path ? xstrdup(path) : xstrdup(ovs_pluginsdir()))) { VLOG_ERR("Failed to allocate plugins path"); return; } if (lt_dlinit() || lt_dlsetsearchpath(plugins_path) || lt_dladvise_init(&advise)) { VLOG_ERR("ltdl initializations: %s", lt_dlerror()); goto err_init; } if (!(interface_id = lt_dlinterface_register("ovs-plugin", NULL))) { VLOG_ERR("lt_dlinterface_register: %s", lt_dlerror()); goto err_interface_register; } if (lt_dladvise_global(&advise) || lt_dladvise_ext (&advise) || lt_dlforeachfile(lt_dlgetsearchpath(), &plugins_open_plugin, &advise)) { VLOG_ERR("ltdl setting advise: %s", lt_dlerror()); goto err_set_advise; } VLOG_INFO("Successfully initialized all plugins"); return; err_set_advise: lt_dlinterface_free(interface_id); err_interface_register: if (lt_dladvise_destroy(&advise)) { VLOG_ERR("destroying ltdl advise%s", lt_dlerror()); return; } err_init: free(plugins_path); }
static bool parse_sockaddr_components(struct sockaddr_storage *ss, char *host_s, const char *port_s, uint16_t default_port, const char *s) { struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *, ss); int port; if (port_s && port_s[0]) { if (!str_to_int(port_s, 10, &port) || port < 0 || port > 65535) { VLOG_ERR("%s: bad port number \"%s\"", s, port_s); goto exit; } } else { port = default_port; } memset(ss, 0, sizeof *ss); if (host_s && strchr(host_s, ':')) { struct sockaddr_in6 *sin6 = ALIGNED_CAST(struct sockaddr_in6 *, ss); char *addr = strsep(&host_s, "%"); sin6->sin6_family = AF_INET6; sin6->sin6_port = htons(port); if (!addr || !*addr || !ipv6_parse(addr, &sin6->sin6_addr)) { VLOG_ERR("%s: bad IPv6 address \"%s\"", s, addr ? addr : ""); goto exit; } #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID char *scope = strsep(&host_s, "%"); if (scope && *scope) { if (!scope[strspn(scope, "0123456789")]) { sin6->sin6_scope_id = atoi(scope); } else { sin6->sin6_scope_id = if_nametoindex(scope); if (!sin6->sin6_scope_id) { VLOG_ERR("%s: bad IPv6 scope \"%s\" (%s)", s, scope, ovs_strerror(errno)); goto exit; } } } #endif } else {
static int _sysd_process_daemons(struct shash *object) { const struct shash_node *dnode; SHASH_FOR_EACH (dnode, object) { daemons = realloc(daemons, sizeof(daemon_info_t*)*(num_daemons+1)); daemons[num_daemons] = calloc(sizeof(daemon_info_t), 1); if (daemons[num_daemons] == (daemon_info_t *) NULL) { VLOG_ERR("Error processing daemon information"); return (-1); } strncpy(daemons[num_daemons]->name, dnode->name, strlen(dnode->name)); /* If this row is sysd, then go ahead and set cur_hw = 1 since ...everything is being done in one transaction. */ if (strncmp(daemons[num_daemons]->name, NAME_IN_DAEMON_TABLE, strlen(NAME_IN_DAEMON_TABLE)) == 0 ) { daemons[num_daemons]->cur_hw = 1; } else { daemons[num_daemons]->cur_hw = 0; } _sysd_get_hw_handler(json_object(dnode->data)); num_daemons++; }
/*----------------------------------------------------------------------------- | Function: vtysh_ovsdb_config_logmsg | Responsibility : logs info/dbg/err/warn level message | Parameters: loglevel - logging level INFO/DBG/ERR/WARN | fmt - log message foramt | elipses - variable args | Return: void -----------------------------------------------------------------------------*/ void vtysh_ovsdb_config_logmsg(int loglevel, char *fmt, ...) { va_list args; va_start(args, fmt); switch (loglevel) { case VTYSH_OVSDB_CONFIG_ERR: VLOG_ERR(fmt, args); break; case VTYSH_OVSDB_CONFIG_WARN: VLOG_WARN(fmt, args); break; case VTYSH_OVSDB_CONFIG_INFO: VLOG_INFO(fmt, args); break; case VTYSH_OVSDB_CONFIG_DBG: VLOG_DBG(fmt, args); break; default : break; } va_end(args); }
static int new_tcp_stream(const char *name, int fd, int connect_status, const struct sockaddr_in *remote, struct stream **streamp) { struct sockaddr_in local; socklen_t local_len = sizeof local; int on = 1; int retval; /* Get the local IP and port information */ retval = getsockname(fd, (struct sockaddr *)&local, &local_len); if (retval) { memset(&local, 0, sizeof local); } retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on); if (retval) { VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno)); close(fd); return errno; } retval = new_fd_stream(name, fd, connect_status, streamp); if (!retval) { struct stream *stream = *streamp; stream_set_remote_ip(stream, remote->sin_addr.s_addr); stream_set_remote_port(stream, remote->sin_port); stream_set_local_ip(stream, local.sin_addr.s_addr); stream_set_local_port(stream, local.sin_port); } return retval; }
static int new_tcp_stream(const char *name, int fd, int connect_status, struct stream **streamp) { struct sockaddr_storage local; socklen_t local_len = sizeof local; int on = 1; int retval; /* Get the local IP and port information */ retval = getsockname(fd, (struct sockaddr *) &local, &local_len); if (retval) { memset(&local, 0, sizeof local); } retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on); if (retval) { int error = sock_errno(); VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, sock_strerror(error)); close(fd); return error; } return new_fd_stream(name, fd, connect_status, streamp); }
/* Prepare to start a process whose command-line arguments are given by the * null-terminated 'argv' array. Returns 0 if successful, otherwise a * positive errno value. */ static int process_prestart(char **argv) { char *binary; process_init(); /* Log the process to be started. */ if (VLOG_IS_DBG_ENABLED()) { char *args = process_escape_args(argv); VLOG_DBG("starting subprocess: %s", args); free(args); } /* execvp() will search PATH too, but the error in that case is more * obscure, since it is only reported post-fork. */ binary = process_search_path(argv[0]); if (!binary) { VLOG_ERR("%s not found in PATH", argv[0]); return ENOENT; } free(binary); return 0; }
/*----------------------------------------------------------------------------- | Function : show_sftp_server | Responsibility : To show the current SFTP server status. | Return : On success returns CMD_SUCCESS, | On failure returns CMD_OVSDB_FAILURE -----------------------------------------------------------------------------*/ static int show_sftp_server (void) { const struct ovsrec_system *row = NULL; char *status = NULL; row = ovsrec_system_first(idl); if (!row) { VLOG_ERR(OVSDB_TXN_CREATE_ERROR); return CMD_OVSDB_FAILURE; } vty_out(vty, "%sSFTP server configuration %s", VTY_NEWLINE, VTY_NEWLINE); vty_out(vty, "-------------------------%s", VTY_NEWLINE); status = (char *)smap_get(&row->other_config, SFTP_SERVER_CONFIG); if (status == NULL) { vty_out(vty, "SFTP server : Disabled %s", VTY_NEWLINE); return CMD_SUCCESS; } else { if (!strcmp(status, "true")) { vty_out(vty, "SFTP server : Enabled %s", VTY_NEWLINE); } else if (!strcmp(status, "false")) { vty_out(vty, "SFTP server : Disabled %s", VTY_NEWLINE); } } vty_out(vty, "%s", VTY_NEWLINE); return CMD_SUCCESS; }
/*----------------------------------------------------------------------------- | Function: vtysh_context_iterateoverclients | Responsibility : iterates over the client callback for given contextid | Parameters: | vtysh_contextid contextid : contextid value | vtysh_ovsdb_cbmsg *p_msg: client param | Return: int : returns e_vtysh_ok if client callback invoked successfully | else e_vtysh_error -----------------------------------------------------------------------------*/ vtysh_ret_val vtysh_context_iterateoverclients(vtysh_contextid contextid, vtysh_ovsdb_cbmsg *p_msg) { int maxclientid = 0, i = 0; vtysh_context_client *povs_client = NULL; maxclientid = vtysh_context_get_maxclientid(contextid); if (e_vtysh_error == maxclientid ) { return e_vtysh_error; } for (i = 0; i < maxclientid-1; i++) { povs_client = &(*vtysh_context_table[contextid].clientlist)[i]; if (NULL != povs_client->p_callback) { p_msg->clientid = i+1; if(e_vtysh_ok != (*(povs_client->p_callback))(p_msg)) { /* log debug msg */ VLOG_ERR("iteration error for context-id %d, client-id %d", contextid, p_msg->clientid); assert(0); break; } } } return e_vtysh_ok; }
/* Gets the pointer to a TCP Buffer */ struct tcp_buf * get_tcp_buf(uint32_t flow_id){ int i; struct tcp_buf * new_buf; if(tcp_buffers_count == 0){ VLOG_DBG("Creating new tcp_buf, existing %d id %" PRIu32, tcp_buffers_count, flow_id); new_buf = create_new_tcp_buf(flow_id); ovs_tcp_buffer[tcp_buffers_count-1] = new_buf; return new_buf; } else{ for(i=0;i<tcp_buffers_count;i++){ if(!ovs_tcp_buffer[i]){ VLOG_ERR("Error! Buffer not found at %d", i); } else if(ovs_tcp_buffer[i]->flow_id == flow_id){ return ovs_tcp_buffer[i]; } } // If existing tcp_buf is not found VLOG_DBG("Creating new tcp_buf, existing %d", tcp_buffers_count); new_buf = create_new_tcp_buf(flow_id); ovs_tcp_buffer[tcp_buffers_count-1] = new_buf; return new_buf; } }
/* Deletes a TCP buffer */ void delete_tcp_buf(struct tcp_buf *tcp_buffer){ int i,j; int del_index; VLOG_DBG("Deleting tcp_buf with id: % " PRIu32, tcp_buffer->flow_id); // Delete the buffer del_index = -1; for(i=0;i<tcp_buffers_count;i++){ if(!ovs_tcp_buffer[i]){ VLOG_ERR("Error! Buffer not found at %d", i); } else if(ovs_tcp_buffer[i] == tcp_buffer){ del_index = i; free(tcp_buffer); break; } } // Compact the array if(del_index!=-i-1){ tcp_buffers_count--; for(i=del_index;i<tcp_buffers_count;i++){ ovs_tcp_buffer[i] = ovs_tcp_buffer[i+1]; } } }
/*----------------------------------------------------------------------------- | Function : sftp_client_copy | Responsibility : To handle SFTP client copy operation. | Parameters : | sftpClient *sc: Pointer to hold all the information needed to perform | SFTP copy. | Return : On success returns CMD_SUCCESS, | On failure returns CMD_WARNING -----------------------------------------------------------------------------*/ static int sftp_client_copy (sftpClient *sc) { int ret = CMD_SUCCESS; int len = 0, argc = 2; int cmd_len = 0; char *command = NULL; char *arguments[argc]; if (!sc) { VLOG_ERR("Structure pointer passed " "is null %s %d.\n",__FILE__,__LINE__); return CMD_WARNING; } cmd_len = strlen(sc->userName) + strlen(sc->hostName) + strlen(sc->srcFile); command = (char*)malloc ((cmd_len+3)*sizeof(char)); if (!command) { VLOG_ERR("Malloc returned null %s %d.",__FILE__,__LINE__); return CMD_WARNING; } len += sprintf(command+len, "%s@", sc->userName); if (sc->isInteractive) { /* SFTP client for interative mode. */ len += sprintf(command+len, "%s", sc->hostName); argc = 1; } else { /* SFTP client for non-interactive mode. */ len += sprintf(command+len, "%s:", sc->hostName); len += sprintf(command+len, "%s", sc->srcFile); arguments[1] = sc->dstFile; } arguments[0] = command; execute_command("sftp", argc, (const char **)arguments); free (command); return ret; }
/*----------------------------------------------------------------------------- | Function : sftp_server_enable_disable | Responsibility : To enable/disable SFTP server. | Parameters : | bool enable: If true, enable SFTP server and if false | disable the SFTP server. | Return : On success returns CMD_SUCCESS, | On failure returns CMD_OVSDB_FAILURE -----------------------------------------------------------------------------*/ static int sftp_server_enable_disable (bool enable) { const struct ovsrec_system *row = NULL; enum ovsdb_idl_txn_status txn_status; struct ovsdb_idl_txn *status_txn = cli_do_config_start(); struct smap smap; if (status_txn == NULL) { VLOG_ERR(OVSDB_TXN_CREATE_ERROR); cli_do_config_abort(status_txn); return CMD_OVSDB_FAILURE; } row = ovsrec_system_first(idl); if (!row) { VLOG_ERR(OVSDB_ROW_FETCH_ERROR); cli_do_config_abort(status_txn); return CMD_OVSDB_FAILURE; } smap_clone(&smap, &row->other_config); if (enable) { smap_replace(&smap, SFTP_SERVER_CONFIG, "true"); } else { smap_replace(&smap, SFTP_SERVER_CONFIG, "false"); } ovsrec_system_set_other_config(row, &smap); txn_status = cli_do_config_finish(status_txn); smap_destroy(&smap); if (txn_status == TXN_SUCCESS || txn_status == TXN_UNCHANGED) { return CMD_SUCCESS; } else { VLOG_ERR(OVSDB_TXN_COMMIT_ERROR); return CMD_OVSDB_FAILURE; } }
/* Initializes 'buffer' with 'n' bytes of high-quality random numbers. Returns * 0 if successful, otherwise a positive errno value or EOF on error. */ int get_entropy(void *buffer, size_t n) { #ifndef _WIN32 size_t bytes_read; int error; int fd; fd = open(urandom, O_RDONLY); if (fd < 0) { VLOG_ERR("%s: open failed (%s)", urandom, ovs_strerror(errno)); return errno ? errno : EINVAL; } error = read_fully(fd, buffer, n, &bytes_read); close(fd); if (error) { VLOG_ERR("%s: read error (%s)", urandom, ovs_retval_to_string(error)); } #else int error = 0; HCRYPTPROV crypt_prov = 0; LPVOID msg_buf; CryptAcquireContext(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!CryptGenRandom(crypt_prov, n, buffer)) { error = EINVAL; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR)&msg_buf, 0, NULL ); VLOG_ERR("CryptGenRandom: read error (%s)", msg_buf); LocalFree(msg_buf); } CryptReleaseContext(crypt_prov, 0); #endif return error; }
/* * Function : udpf_send_pkt_through_socket * Responsiblity : To send a unicast packet to a known server address. * Parameters : pkt - IP packet * size - size of udp payload * in_pktinfo - pktInfo * to - it has destination address and port number * Returns: true - packet is sent successfully. * false - any failures. */ static bool udpfwd_send_pkt_through_socket(void *pkt, int32_t size, struct in_pktinfo *pktInfo, struct sockaddr_in* to) { struct msghdr msg; struct iovec iov[1]; struct cmsghdr *cmptr; struct in_pktinfo p = *pktInfo; struct ip *iph; struct udphdr *udph; union control_u ctrl; char result = false; /* Update IP and UDP header checksum fields */ iph = (struct ip *) pkt; udph = (struct udphdr *) ((char *)iph + (iph->ip_hl * 4)); /* Set destination ip and udp port number in the packet */ iph->ip_dst.s_addr = to->sin_addr.s_addr; udph->uh_dport = to->sin_port; iph->ip_sum = in_cksum((uint16_t *) pkt, iph->ip_len, 0); /* FIXME: Add udp checksum calculation function */ udph->check = 0; iov[0].iov_base = pkt; iov[0].iov_len = size; msg.msg_control = NULL; msg.msg_controllen = 0; msg.msg_flags = 0; msg.msg_name = to; msg.msg_namelen = sizeof(struct sockaddr_in); msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_control = &ctrl; msg.msg_controllen = sizeof(union control_u); cmptr = CMSG_FIRSTHDR(&msg); memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); cmptr->cmsg_level = IPPROTO_IP; cmptr->cmsg_type = IP_PKTINFO; assert(udpfwd_ctrl_cb_p->udpSockFd); if (sendmsg(udpfwd_ctrl_cb_p->udpSockFd, &msg, 0) < 0 ) { VLOG_ERR("errno = %d, sending packet failed", errno); } else { result = true; } return result; }
/*----------------------------------------------------------------------------- | Function : udpfwd_globalconfig | Responsibility : To enable/disable udp broadcast forwarding and dhcp-relay. | Parameters : | status : If true, enable UDP broadcast forwarding/dhcp-relay and | if false disable the UDP broadcast forwarding/dhcp-relay | Return : On success returns CMD_SUCCESS, | On failure returns CMD_OVSDB_FAILURE -----------------------------------------------------------------------------*/ int8_t udpfwd_globalconfig(const char *status) { const struct ovsrec_system *ovs_row = NULL; struct ovsdb_idl_txn *status_txn = cli_do_config_start(); struct smap smap_status_value; enum ovsdb_idl_txn_status txn_status; char *key = NULL; if (status_txn == NULL) { VLOG_ERR(OVSDB_TXN_CREATE_ERROR); cli_do_config_abort(status_txn); return CMD_OVSDB_FAILURE; } ovs_row = ovsrec_system_first(idl); if (!ovs_row) { VLOG_ERR(OVSDB_ROW_FETCH_ERROR); cli_do_config_abort(status_txn); return CMD_OVSDB_FAILURE; } key = SYSTEM_OTHER_CONFIG_MAP_UDP_BCAST_FWD_ENABLED; smap_clone(&smap_status_value, &ovs_row->other_config); /* Update the latest config status. */ smap_replace(&smap_status_value, key, status); ovsrec_system_set_other_config(ovs_row, &smap_status_value); smap_destroy(&smap_status_value); txn_status = cli_do_config_finish(status_txn); if (txn_status == TXN_SUCCESS || txn_status == TXN_UNCHANGED) { return CMD_SUCCESS; } else { VLOG_ERR(OVSDB_TXN_COMMIT_ERROR); return CMD_OVSDB_FAILURE; } }
/** * Executes the qos_cos_map_command for the given * code_point, local_priority, color, and description. */ static int qos_cos_map_command(int64_t code_point, int64_t local_priority, const char *color, const char *description) { if (description != NULL) { if (!qos_is_valid_string(description)) { vty_out(vty, QOS_INVALID_STRING_ERROR_MESSAGE, VTY_NEWLINE); return CMD_OVSDB_FAILURE; } } /* Retrieve the row. */ struct ovsrec_qos_cos_map_entry *cos_map_row = qos_cos_map_row_for_code_point(code_point); if (cos_map_row == NULL) { vty_out(vty, "COS Map code point %" PRId64 " does not exist.%s", code_point, VTY_NEWLINE); return CMD_OVSDB_FAILURE; } struct ovsdb_idl_txn *txn = cli_do_config_start(); if (txn == NULL) { vty_out(vty, "Unable to start transaction.%s", VTY_NEWLINE); VLOG_ERR(OVSDB_TXN_CREATE_ERROR); return CMD_OVSDB_FAILURE; } /* Update the row. */ ovsrec_qos_cos_map_entry_set_local_priority(cos_map_row, local_priority); ovsrec_qos_cos_map_entry_set_color(cos_map_row, (color == NULL ? QOS_COLOR_DEFAULT : color)); ovsrec_qos_cos_map_entry_set_description(cos_map_row, (description == NULL ? QOS_DESCRIPTION_DEFAULT : description)); enum ovsdb_idl_txn_status status = cli_do_config_finish(txn); if (status != TXN_SUCCESS && status != TXN_UNCHANGED) { vty_out(vty, "Unable to commit transaction.%s", VTY_NEWLINE); VLOG_ERR(OVSDB_TXN_COMMIT_ERROR); return CMD_OVSDB_FAILURE; } return CMD_SUCCESS; }
int sysd_create_link_to_hwdesc_files(void) { char *manufacturer = NULL; char *product_name = NULL; char cmd_path[50]; int rc = 0; memset(cmd_path, 0, sizeof(cmd_path)); #ifdef PLATFORM_SIMULATION /* For x86/simulation assign the manufacturer and product name */ manufacturer = strdup(GENERIC_X86_MANUFACTURER); product_name = strdup(GENERIC_X86_PRODUCT_NAME); #else /* OPS_TODO: Add other methods to find manuf/prodname. * Run dmidecode command (if it exists) to get system info. */ rc = dmidecode_exists(cmd_path); if (rc) { VLOG_ERR("Unable to locate \"dmidecode\" command"); return -1; } get_manuf_and_prodname(cmd_path, &manufacturer, &product_name); if ((manufacturer == NULL) || (product_name == NULL)) { return -1; } #endif VLOG_DBG("manufacturer=%s product_name=%s", manufacturer, product_name); rc = create_link_to_desc_files(manufacturer, product_name); if (rc) { VLOG_ERR("Failed to create link to HW descriptor files"); return -1; } free(manufacturer); free(product_name); return 0; } /* sysd_create_link_to_hwdesc_files */
static int create_link_to_desc_files(char *manufacturer, char *product_name) { char hw_desc_dir[1024]; int rc = 0; struct stat sbuf; extern char *g_hw_desc_dir; snprintf(hw_desc_dir, sizeof(hw_desc_dir), "%s/%s/%s", HWDESC_FILES_PATH, manufacturer, product_name); VLOG_INFO("Location to HW descrptor files: %s", hw_desc_dir); g_hw_desc_dir = strdup(hw_desc_dir); if (stat(hw_desc_dir, &sbuf) != 0) { VLOG_ERR("Unable to find hardware description files at %s", hw_desc_dir); return -1; } /* Remove old link if it exists */ remove(HWDESC_FILE_LINK); /* mkdir for the new link */ rc = mkdir(HWDESC_FILE_LINK_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (rc == -1 && errno != EEXIST) { VLOG_ERR("Failed to create %s, Error %s", HWDESC_FILE_LINK_PATH, ovs_strerror(errno)); return -1; } /* Create link to these files */ if (-1 == symlink(hw_desc_dir, HWDESC_FILE_LINK)) { VLOG_ERR("Unable to create soft link to %s -> %s. Error %s", HWDESC_FILE_LINK, hw_desc_dir, ovs_strerror(errno)); return -1; } return 0; } /* create_link_to_desc_files */