std::string ipv6_address::to_string() const { char ip[INET6_ADDRSTRLEN] = { 0, }; char port[10] = { 0, }; int ret = ::getnameinfo(get_sock_addr(), get_sock_addr_len(), ip, sizeof(ip), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV /*| NI_NUMERICSCOPE*/); if (ret == 0) { std::string addr = ip; addr += ":"; addr += port; return addr; } return std::string(); }
int socket::recv(char* data, const unsigned bytes) { int received = -1; if(socket_type_ == socket::tcp) { received = ::recv(socket_fd_, data, bytes, flags_recv_); } else if(socket_type_ == socket::udp) { ::sockaddr_storage client_addr; ::socklen_t client_addr_len = sizeof(client_addr); char client_addr_str[INET6_ADDRSTRLEN]; if((received = ::recvfrom(socket_fd_, data, bytes, flags_recv_, reinterpret_cast<sockaddr*>(&client_addr), &client_addr_len)) != SOCK_ERROR) { inet_ntop(client_addr.ss_family, get_sock_addr((sockaddr*) &client_addr), client_addr_str, sizeof(client_addr_str)); address_ = string(client_addr_str); } } if(received == SOCK_ERROR) { switch(errno) { case ECONNABORTED: // Connection was terminated (close socket) case ECONNRESET: // Connection has been closed by peer (close socket) case ETIMEDOUT: // The connection timed out close(); default: break; } } return received; }
int read_config(sys_data_t *sys, char *config_file, int print_version) { FILE *f = fopen(config_file, "r"); int line; unsigned int val; char buf[MAX_CONFIG_LINE]; const char *tok; char *tokptr; int err = 0; const char *errstr; if (!f) { fprintf(stderr, "Unable to open configuration file '%s'\n", config_file); return -1; } line = 0; while (fgets(buf, sizeof(buf), f) != NULL) { line++; tok = mystrtok(buf, " \t\n", &tokptr); if (!tok || (tok[0] == '#')) continue; if (strcmp(tok, "define") == 0) { const char *varname, *value; varname = mystrtok(NULL, " \t\n", &tokptr); if (!varname) { err = EINVAL; errstr = "No variable supplied for define"; goto next; } err = get_delim_str(&tokptr, &value, &errstr); if (err) goto next; err = add_variable(varname, value); if (err) { err = ENOMEM; errstr = "Out of memory"; goto next; } } else if (strcmp(tok, "loadlib") == 0) { const char *library = NULL, *initstr = NULL; struct dliblist *dlib, *dlibp; err = get_delim_str(&tokptr, &library, &errstr); if (!err) err = get_delim_str(&tokptr, &initstr, &errstr); if (!err) { dlib = malloc(sizeof(*dlib)); if (!dlib) { err = ENOMEM; errstr = "Out of memory"; } else { dlib->file = library; dlib->init = initstr; dlib->next = NULL; if (!dlibs) { dlibs = dlib; } else { dlibp = dlibs; while (dlibp->next) dlibp = dlibp->next; dlibp->next = dlib; } } } if (err) { if (library) free((char *) library); if (initstr) free((char *) initstr); } goto next; } if (print_version) goto next; if (strcmp(tok, "startlan") == 0) { err = get_uint(&tokptr, &val, &errstr); if (!err && (val >= IPMI_MAX_CHANNELS)) { err = -1; errstr = "Channel number out of range"; } if (!err) { err = lanserv_read_config(sys, f, &line, val); } } else if (strcmp(tok, "user") == 0) { err = get_user(&tokptr, sys, &errstr); } else if (strcmp(tok, "serial") == 0) { err = serserv_read_config(&tokptr, sys, &errstr); } else if (strcmp(tok, "sol") == 0) { err = sol_read_config(&tokptr, sys, &errstr); } else if (strcmp(tok, "chassis_control") == 0) { const char *prog; err = get_delim_str(&tokptr, &prog, &errstr); if (!err) ipmi_set_chassis_control_prog(sys->mc, prog); } else if (strcmp(tok, "name") == 0) { err = get_delim_str(&tokptr, &sys->name, &errstr); } else if (strcmp(tok, "startcmd") == 0) { err = get_delim_str(&tokptr, &sys->startcmd->startcmd, &errstr); } else if (strcmp(tok, "startnow") == 0) { err = get_bool(&tokptr, &sys->startcmd->startnow, &errstr); } else if (strcmp(tok, "poweroff_wait") == 0) { err = get_uint(&tokptr, &sys->startcmd->poweroff_wait_time, &errstr); } else if (strcmp(tok, "kill_wait") == 0) { err = get_uint(&tokptr, &sys->startcmd->kill_wait_time, &errstr); } else if (strcmp(tok, "set_working_mc") == 0) { unsigned char ipmb; err = get_uchar(&tokptr, &ipmb, &errstr); if (!err) { lmc_data_t *mc; err = ipmi_mc_alloc_unconfigured(sys, ipmb, &mc); if (err == ENOMEM) { errstr = "Out of memory"; err = -1; } else if (err) { errstr = "Invalid IPMB specified"; err = -1; } else { sys->mc = mc; sys->cusers = ipmi_mc_get_users(mc); sys->chan_set = ipmi_mc_get_channelset(mc); sys->cpef = ipmi_mc_get_pef(mc); sys->startcmd = ipmi_mc_get_startcmdinfo(mc); sys->sol = ipmi_mc_get_sol(mc); } } } else if (strcmp(tok, "console") == 0) { err = get_sock_addr(&tokptr, &sys->console_addr, &sys->console_addr_len, NULL, SOCK_STREAM, &errstr); } else { errstr = "Invalid configuration option"; err = -1; } next: if (err) { fprintf(stderr, "Error on line %d: %s\n", line, errstr); break; } } fclose(f); if (print_version) load_dynamic_libs(sys, print_version); return err; }