static switch_status_t config_logger(void) { char *cf = "console.conf"; switch_xml_t cfg, xml, settings, param; if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf); return SWITCH_STATUS_TERM; } if (log_hash) { switch_core_hash_destroy(&log_hash); } switch_core_hash_init(&log_hash, module_pool); if ((settings = switch_xml_child(cfg, "mappings"))) { for (param = switch_xml_child(settings, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); add_mapping(var, val, 1); } for (param = switch_xml_child(settings, "map"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); add_mapping(var, val, 0); } } if ((settings = switch_xml_child(cfg, "settings"))) { for (param = switch_xml_child(settings, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); if (!strcasecmp(var, "colorize") && switch_true(val)) { #ifdef WIN32 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if (switch_core_get_console() == stdout && hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) { wOldColorAttrs = csbiInfo.wAttributes; COLORIZE = 1; } #else COLORIZE = 1; #endif } else if (!strcasecmp(var, "loglevel") && !zstr(val)) { hard_log_level = switch_log_str2level(val); } else if (!strcasecmp(var, "uuid") && switch_true(val)) { log_uuid = SWITCH_TRUE; } } } switch_xml_free(xml); return SWITCH_STATUS_SUCCESS; }
static void read_mailmap_line(struct string_list *map, char *buffer, char **repo_abbrev) { char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL; if (buffer[0] == '#') { static const char abbrev[] = "# repo-abbrev:"; int abblen = sizeof(abbrev) - 1; int len = strlen(buffer); if (!repo_abbrev) return; if (len && buffer[len - 1] == '\n') buffer[--len] = 0; if (!strncmp(buffer, abbrev, abblen)) { char *cp; free(*repo_abbrev); for (cp = buffer + abblen; isspace(*cp); cp++) ; /* nothing */ *repo_abbrev = xstrdup(cp); } return; } if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL) parse_name_and_email(name2, &name2, &email2, 1); if (email1) add_mapping(map, name1, email1, name2, email2); }
static int add_mapping_line( kum_file* kum, const char* line ) { kum_mapping m = { 0 }; ucs4_t uchar; uint8_t char_bytes[ KUCONV_MAX_CHAR_LEN ]; int32_t count; if( !parse_mapping_line( &m, &uchar, char_bytes, line ) ) { return 0; } count = kum_count_chars( &kum->states, char_bytes, m.blen ); if( count < 1 ) { print_mapping( stderr, &m, char_bytes ); return 0; } if( count > 1 ) { fprintf( stderr, "kum error: too long multibyte sequence (>1).\n" ); return 0; } add_mapping( kum->table, &m, char_bytes ); return 1; }
static switch_status_t load_profile(switch_xml_t xml) { switch_xml_t param, settings; char *name = (char *) switch_xml_attr_soft(xml, "name"); logfile_profile_t *new_profile; new_profile = switch_core_alloc(module_pool, sizeof(*new_profile)); memset(new_profile, 0, sizeof(*new_profile)); switch_core_hash_init(&(new_profile->log_hash)); new_profile->name = switch_core_strdup(module_pool, switch_str_nil(name)); new_profile->suffix = 1; new_profile->log_uuid = SWITCH_TRUE; if ((settings = switch_xml_child(xml, "settings"))) { for (param = switch_xml_child(settings, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); if (!strcmp(var, "logfile")) { new_profile->logfile = strdup(val); } else if (!strcmp(var, "rollover")) { new_profile->roll_size = switch_atoui(val); } else if (!strcmp(var, "maximum-rotate")) { new_profile->max_rot = switch_atoui(val); if (new_profile->max_rot == 0) { new_profile->max_rot = MAX_ROT; } } else if (!strcmp(var, "uuid")) { new_profile->log_uuid = switch_true(val); } } } if ((settings = switch_xml_child(xml, "mappings"))) { for (param = switch_xml_child(settings, "map"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); add_mapping(new_profile, var, val); } } if (zstr(new_profile->logfile)) { char logfile[512]; switch_snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch.log"); new_profile->logfile = strdup(logfile); } if (mod_logfile_openlogfile(new_profile, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) { return SWITCH_STATUS_GENERR; } switch_core_hash_insert_destructor(profile_hash, new_profile->name, (void *) new_profile, cleanup_profile); return SWITCH_STATUS_SUCCESS; }
// create the mapping table static void create_mappings(void) { free_mappings(); for ( int i=0; i < numports; i++ ) { const char *name = ports[i].name; ea_t nameea = get_name_ea(BADADDR, name); if ( nameea != BADADDR && nameea > dataseg) add_mapping(ports[i].address, nameea-dataseg); } }
void remove_mapping_by_rom( uint8_t rom[] ) { #if 1 add_mapping( rom, 0xFF ); #else uint8_t i,s; for(s = 0; s < N_TEMPERATURE_IN; s++) { for (i=0;i<OW_ROMCODE_SIZE;i++) if( gTempSensorIDs[s][i] != rom[i] ) goto next; gTempSensorLogicalNumber[s] = 0xFF; // unknown next: ; } #endif }
void server_init() #endif { for_each_rez([](const std::string &path, const rez_file_ent *ent) { add_mapping("GET", path, [ent](http_server::pipe_t &pipe, const http_request &request) { // note that if we get here we already know that the path maps to 'ent' // so just respond with that. But this can't quite be a simple, static // response because we want to respect the etag, etc... anon_log("client sent If-None-Match: " << request.headers.get_header("If-None-Match").str()); anon_log("client sent Accept-Encoding: " << request.headers.get_header("Accept-Encoding").str()); if (request.headers.get_header("If-None-Match").str() == ent->etag) { anon_log("etags match, returning 304"); pipe.respond(http_response("304 Not Modified")); } else { anon_log("etags do not match, returning 200 with " << (permits_gzip(request.headers.get_header("Accept-Encoding")) ? "gzip" : "identity") << " encoding"); http_response response; response.add_header("ETag", ent->etag); response.add_header("Content-Type", ent->content_type); if (permits_gzip(request.headers.get_header("Accept-Encoding"))) { response.add_header("Content-Encoding", "gzip"); response << std::string((const char *)ent->compressed, ent->sz_compressed); } else response << std::string((const char *)ent->uncompressed, ent->sz_uncompressed); pipe.respond(response); } }); }); }
int main(int argc, char *argv[]) { #ifdef TIOCGWINSZ struct winsize win; #endif int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper; char *p, *tcapbuf; const char *ttype; if (tcgetattr(STDERR_FILENO, &mode) < 0) err(1, "standard error"); oldmode = mode; Ospeed = cfgetospeed(&mode); if ((p = strrchr(*argv, '/'))) ++p; else p = *argv; usingupper = isupper(*p); if (!strcasecmp(p, "reset")) { isreset = 1; reset_mode(); } obsolete(argv); noinit = noset = quiet = Sflag = sflag = showterm = 0; while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) { switch (ch) { case '-': /* display term only */ noset = 1; break; case 'a': /* OBSOLETE: map identifier to type */ add_mapping("arpanet", optarg); break; case 'd': /* OBSOLETE: map identifier to type */ add_mapping("dialup", optarg); break; case 'e': /* erase character */ erasech = optarg[0] == '^' && optarg[1] != '\0' ? optarg[1] == '?' ? '\177' : CTRL(optarg[1]) : optarg[0]; break; case 'I': /* no initialization strings */ noinit = 1; break; case 'i': /* interrupt character */ intrchar = optarg[0] == '^' && optarg[1] != '\0' ? optarg[1] == '?' ? '\177' : CTRL(optarg[1]) : optarg[0]; break; case 'k': /* kill character */ killch = optarg[0] == '^' && optarg[1] != '\0' ? optarg[1] == '?' ? '\177' : CTRL(optarg[1]) : optarg[0]; break; case 'm': /* map identifier to type */ add_mapping(NULL, optarg); break; case 'n': /* OBSOLETE: set new tty driver */ break; case 'p': /* OBSOLETE: map identifier to type */ add_mapping("plugboard", optarg); break; case 'Q': /* don't output control key settings */ quiet = 1; break; case 'S': /* output TERM/TERMCAP strings */ Sflag = 1; break; case 'r': /* display term on stderr */ showterm = 1; break; case 's': /* output TERM/TERMCAP strings */ sflag = 1; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (argc > 1) usage(); ttype = get_termcap_entry(*argv, &tcapbuf); if (!noset) { Columns = tgetnum("co"); Lines = tgetnum("li"); #ifdef TIOCGWINSZ /* Set window size */ (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win); if (win.ws_row == 0 && win.ws_col == 0 && Lines > 0 && Columns > 0) { win.ws_row = Lines; win.ws_col = Columns; (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win); } #endif set_control_chars(); set_conversions(usingupper); if (!noinit) set_init(); /* Set the modes if they've changed. */ if (memcmp(&mode, &oldmode, sizeof(mode))) tcsetattr(STDERR_FILENO, TCSADRAIN, &mode); } if (noset) (void)printf("%s\n", ttype); else { if (showterm) (void)fprintf(stderr, "Terminal type is %s.\n", ttype); /* * If erase, kill and interrupt characters could have been * modified and not -Q, display the changes. */ if (!quiet) { report("Erase", VERASE, CERASE); report("Kill", VKILL, CKILL); report("Interrupt", VINTR, CINTR); } } if (Sflag) { (void)printf("%s ", ttype); if (strlen(tcapbuf) > 0) wrtermcap(tcapbuf); } if (sflag) { /* * Figure out what shell we're using. A hack, we look for an * environmental variable SHELL ending in "csh". */ if ((p = getenv("SHELL")) && !strcmp(p + strlen(p) - 3, "csh")) { printf("set noglob;\nsetenv TERM %s;\n", ttype); if (strlen(tcapbuf) > 0) { printf("setenv TERMCAP '"); wrtermcap(tcapbuf); printf("';\n"); } printf("unset noglob;\n"); } else { printf("TERM=%s;\n", ttype); if (strlen(tcapbuf) > 0) { printf("TERMCAP='"); wrtermcap(tcapbuf); printf("';\nexport TERMCAP;\n"); } printf("export TERM;\n"); } } exit(0); }
void c_add() { switch (sp->type) { #ifndef NO_BUFFER_TYPE case T_BUFFER: { if (!((sp-1)->type == T_BUFFER)) { error("Bad type argument to +. Had %s and %s.\n", type_name((sp - 1)->type), type_name(sp->type)); } else { buffer_t *b; b = allocate_buffer(sp->u.buf->size + (sp - 1)->u.buf->size); memcpy(b->item, (sp - 1)->u.buf->item, (sp - 1)->u.buf->size); memcpy(b->item + (sp - 1)->u.buf->size, sp->u.buf->item, sp->u.buf->size); free_buffer((sp--)->u.buf); free_buffer(sp->u.buf); sp->u.buf = b; } break; } /* end of x + T_BUFFER */ #endif case T_NUMBER: { switch ((--sp)->type) { case T_NUMBER: sp->u.number += (sp+1)->u.number; break; case T_REAL: sp->u.real += (sp+1)->u.number; break; case T_STRING: { char buff[20]; sprintf(buff, "%d", (sp+1)->u.number); EXTEND_SVALUE_STRING(sp, buff, "f_add: 2"); break; } default: error("Bad type argument to +. Had %s and %s.\n", type_name(sp->type), type_name((sp+1)->type)); } break; } /* end of x + NUMBER */ case T_REAL: { switch ((--sp)->type) { case T_NUMBER: sp->type = T_REAL; sp->u.real = sp->u.number + (sp+1)->u.real; break; case T_REAL: sp->u.real += (sp+1)->u.real; break; case T_STRING: { char buff[40]; sprintf(buff, "%f", (sp+1)->u.real); EXTEND_SVALUE_STRING(sp, buff, "f_add: 2"); break; } default: error("Bad type argument to +. Had %s and %s\n", type_name(sp->type), type_name((sp+1)->type)); } break; } /* end of x + T_REAL */ case T_ARRAY: { if (!((sp-1)->type == T_ARRAY)) { error("Bad type argument to +. Had %s and %s\n", type_name((sp - 1)->type), type_name(sp->type)); } else { /* add_array now free's the arrays */ (sp-1)->u.arr = add_array((sp - 1)->u.arr, sp->u.arr); sp--; break; } } /* end of x + T_ARRAY */ case T_MAPPING: { if ((sp-1)->type == T_MAPPING) { mapping_t *map; map = add_mapping((sp - 1)->u.map, sp->u.map); free_mapping((sp--)->u.map); free_mapping(sp->u.map); sp->u.map = map; break; } else error("Bad type argument to +. Had %s and %s\n", type_name((sp - 1)->type), type_name(sp->type)); } /* end of x + T_MAPPING */ case T_STRING: { switch ((sp-1)->type) { case T_NUMBER: { char buff[20]; sprintf(buff, "%d", (sp-1)->u.number); SVALUE_STRING_ADD_LEFT(buff, "f_add: 3"); break; } /* end of T_NUMBER + T_STRING */ case T_REAL: { char buff[40]; sprintf(buff, "%f", (sp - 1)->u.real); SVALUE_STRING_ADD_LEFT(buff, "f_add: 3"); break; } /* end of T_REAL + T_STRING */ case T_STRING: { SVALUE_STRING_JOIN(sp-1, sp, "f_add: 1"); sp--; break; } /* end of T_STRING + T_STRING */ default: error("Bad type argument to +. Had %s and %s\n", type_name((sp - 1)->type), type_name(sp->type)); } break; } /* end of x + T_STRING */ default: error("Bad type argument to +. Had %s and %s.\n", type_name((sp-1)->type), type_name(sp->type)); } }
int main(int argc, char **argv) { int ch, noinit, noset, quiet, Sflag, sflag, showterm; const char *p; const char *ttype; if (pledge("stdio rpath wpath tty", NULL) == -1) err("pledge: %s", strerror(errno)); obsolete(argv); noinit = noset = quiet = Sflag = sflag = showterm = 0; while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:np:qQSrsVw")) != -1) { switch (ch) { case 'c': /* set control-chars */ opt_c = TRUE; break; case 'a': /* OBSOLETE: map identifier to type */ add_mapping("arpanet", optarg); break; case 'd': /* OBSOLETE: map identifier to type */ add_mapping("dialup", optarg); break; case 'e': /* erase character */ terasechar = arg_to_char(); break; case 'I': /* no initialization strings */ noinit = 1; break; case 'i': /* interrupt character */ intrchar = arg_to_char(); break; case 'k': /* kill character */ tkillchar = arg_to_char(); break; case 'm': /* map identifier to type */ add_mapping(0, optarg); break; case 'n': /* OBSOLETE: set new tty driver */ break; case 'p': /* OBSOLETE: map identifier to type */ add_mapping("plugboard", optarg); break; case 'Q': /* don't output control key settings */ quiet = 1; break; case 'q': /* display term only */ noset = 1; break; case 'r': /* display term on stderr */ showterm = 1; break; case 'S': /* OBSOLETE: output TERM & TERMCAP */ Sflag = 1; break; case 's': /* output TERM set command */ sflag = 1; break; case 'V': /* print curses-version */ puts(curses_version()); ExitProgram(EXIT_SUCCESS); case 'w': /* set window-size */ opt_w = TRUE; break; case '?': default: usage(); } } _nc_progname = _nc_rootname(*argv); argc -= optind; argv += optind; if (argc > 1) usage(); if (!opt_c && !opt_w) opt_c = opt_w = TRUE; if (GET_TTY(STDERR_FILENO, &mode) < 0) failed("standard error"); can_restore = TRUE; original = oldmode = mode; #ifdef TERMIOS ospeed = (NCURSES_OSPEED) cfgetospeed(&mode); #else ospeed = (NCURSES_OSPEED) mode.sg_ospeed; #endif if (!strcmp(_nc_progname, PROG_RESET)) { isreset = TRUE; reset_mode(); } ttype = get_termcap_entry(*argv); if (!noset) { tcolumns = columns; tlines = lines; #if HAVE_SIZECHANGE if (opt_w) { struct winsize win; /* Set window size if not set already */ (void) ioctl(STDERR_FILENO, TIOCGWINSZ, &win); if (win.ws_row == 0 && win.ws_col == 0 && tlines > 0 && tcolumns > 0) { win.ws_row = tlines; win.ws_col = tcolumns; (void) ioctl(STDERR_FILENO, TIOCSWINSZ, &win); } } #endif if (opt_c) { set_control_chars(); set_conversions(); if (!noinit) set_init(); /* Set the modes if they've changed. */ if (memcmp(&mode, &oldmode, sizeof(mode))) { SET_TTY(STDERR_FILENO, &mode); } } } /* Get the terminal name from the entry. */ ttype = _nc_first_name(cur_term->type.term_names); if (noset) (void) printf("%s\n", ttype); else { if (showterm) (void) fprintf(stderr, "Terminal type is %s.\n", ttype); /* * If erase, kill and interrupt characters could have been * modified and not -Q, display the changes. */ #ifdef TERMIOS if (!quiet) { report("Erase", VERASE, CERASE); report("Kill", VKILL, CKILL); report("Interrupt", VINTR, CINTR); } #endif } if (Sflag) err("The -S option is not supported under terminfo."); if (sflag) { int len; char *var; char *leaf; /* * Figure out what shell we're using. A hack, we look for an * environmental variable SHELL ending in "csh". */ if ((var = getenv("SHELL")) != 0 && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3) && !strcmp(leaf + len - 3, "csh")) p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n"; else p = "TERM=%s;\n"; (void) printf(p, ttype); } ExitProgram(EXIT_SUCCESS); }
static void node_shader_init_mapping(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp)) { node->storage= add_mapping(); }
int main(int argc, char **argv) { int ch, noinit, noset, quiet, Sflag, sflag, showterm; const char *ttype; int terasechar = -1; /* new erase character */ int intrchar = -1; /* new interrupt character */ int tkillchar = -1; /* new kill character */ int my_fd; bool opt_c = FALSE; /* set control-chars */ bool opt_w = FALSE; /* set window-size */ TTY mode, oldmode; my_fd = STDERR_FILENO; obsolete(argv); noinit = noset = quiet = Sflag = sflag = showterm = 0; while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:p:qQrSsVw")) != -1) { switch (ch) { case 'c': /* set control-chars */ opt_c = TRUE; break; case 'a': /* OBSOLETE: map identifier to type */ add_mapping("arpanet", optarg); break; case 'd': /* OBSOLETE: map identifier to type */ add_mapping("dialup", optarg); break; case 'e': /* erase character */ terasechar = arg_to_char(); break; case 'I': /* no initialization strings */ noinit = 1; break; case 'i': /* interrupt character */ intrchar = arg_to_char(); break; case 'k': /* kill character */ tkillchar = arg_to_char(); break; case 'm': /* map identifier to type */ add_mapping(0, optarg); break; case 'p': /* OBSOLETE: map identifier to type */ add_mapping("plugboard", optarg); break; case 'Q': /* don't output control key settings */ quiet = 1; break; case 'q': /* display term only */ noset = 1; break; case 'r': /* display term on stderr */ showterm = 1; break; case 'S': /* OBSOLETE: output TERM & TERMCAP */ Sflag = 1; break; case 's': /* output TERM set command */ sflag = 1; break; case 'V': /* print curses-version */ puts(curses_version()); ExitProgram(EXIT_SUCCESS); case 'w': /* set window-size */ opt_w = TRUE; break; case '?': default: usage(); } } _nc_progname = _nc_rootname(*argv); argc -= optind; argv += optind; if (argc > 1) usage(); if (!opt_c && !opt_w) opt_c = opt_w = TRUE; my_fd = save_tty_settings(&mode, TRUE); oldmode = mode; #ifdef TERMIOS ospeed = (NCURSES_OSPEED) cfgetospeed(&mode); #else ospeed = (NCURSES_OSPEED) mode.sg_ospeed; #endif if (same_program(_nc_progname, PROG_RESET)) { reset_start(stderr, TRUE, FALSE); reset_tty_settings(my_fd, &mode); } else { reset_start(stderr, FALSE, TRUE); } ttype = get_termcap_entry(my_fd, *argv); if (!noset) { #if HAVE_SIZECHANGE if (opt_w) { set_window_size(my_fd, &lines, &columns); } #endif if (opt_c) { set_control_chars(&mode, terasechar, intrchar, tkillchar); set_conversions(&mode); if (!noinit) { if (send_init_strings(my_fd, &oldmode)) { (void) putc('\r', stderr); (void) fflush(stderr); (void) napms(1000); /* Settle the terminal. */ } } update_tty_settings(&oldmode, &mode); } } if (noset) { (void) printf("%s\n", ttype); } else { if (showterm) (void) fprintf(stderr, "Terminal type is %s.\n", ttype); /* * If erase, kill and interrupt characters could have been * modified and not -Q, display the changes. */ if (!quiet) { print_tty_chars(&oldmode, &mode); } } if (Sflag) err("The -S option is not supported under terminfo."); if (sflag) { print_shell_commands(ttype); } ExitProgram(EXIT_SUCCESS); }
/* Parses a config file into a config_t struct. @param config The config struct to parse the config too. @param file is the config file path to load. @return 1 if everthing went fine, 0 if there was a problem. */ int parse_config(config_t* config, char* file) { FILE* fid = fopen(file, "r"); if(fid == NULL) { log_error("Could not open file (%s): %s\n", file, strerror(errno)); return 0; } entry_t* mapping; char buffer[MAX_BUFFER]; unsigned int linenum = 0; char section[MAX_SECTION] = ""; char* key = NULL; char* value = NULL; while(fgets(buffer, MAX_BUFFER, fid)) { linenum++; trim(buffer); if(strlen(buffer) == 0 || buffer[0] == '#') { continue; } if(buffer[0] == '[' && buffer[strlen(buffer)-1] == ']') { // Parse new section strncpy(section, buffer + 1, sizeof(section)); section[strlen(section)-1] = '\0'; log_debug("config: new section: '%s'\n", section); if(!strlen(section)) { log_error("config: line %d: bad section name\n", linenum); return 0; } if(strcmp(section, "options")) { // start a new mapping record mapping = malloc(sizeof(entry_t)); if(mapping == NULL) { log_error("Could not malloc: %s\n", strerror(errno)); return 0; } mapping->key = parse_keyname(section); mapping->command = NULL; mapping->duration = 0; mapping->blocker = 0; add_mapping(config, mapping); } } else { // Parse parameters if(!strlen(section)) { log_error("config: line %d: all directives must belong to a section\n", linenum); return 0; } value = buffer; key = strsep(&value, "="); if(key == NULL) { log_error("config: line %d: syntax error\n", linenum); return 0; } trim(key); key = strtoupper(key); if(value == NULL) { // Non value parameter keys if(!strcmp(section, "options")) { if(!strcmp(key,"DEBUG")) { log_debug("config: %s: debug: true\n", section); config->debug = 1; } else { log_error("config: line %d: syntax error\n", linenum); return 0; } } else { if(!strcmp(key, "BLOCKER")) { mapping->blocker = 1; log_debug("config: %s: blocker: true\n", section); } else { log_error("config: line %d: syntax error\n", linenum); return 0; } } } else { trim(value); if(!strcmp(section, "options")) { // Parse option parameter if(!strcmp(key, "LOGFILE")) { strncpy(config->logfile, value, PATH_MAX-1); config->logfile[PATH_MAX-1] = '\0'; log_debug("config: log file: %s\n", config->logfile); } else if(!strcmp(key, "PIDFILE")) { strncpy(config->pidfile, value, PATH_MAX-1); config->pidfile[PATH_MAX-1] = '\0'; log_debug("config: pid file: %s\n", config->pidfile); } else if(!strcmp(key, "DEVICE")) { strncpy(config->device, value, PATH_MAX-1); config->device[PATH_MAX-1] = '\0'; log_debug("config: device: %s\n", config->device); } else { log_error("config: line %d: syntax error\n", linenum); return 0; } } else { // Parse mapping parameter if(!strcmp(key, "COMMAND")) { mapping->command = malloc((strlen(value) + 1) * sizeof(char)); if(mapping->command == NULL) { log_error("Could not malloc: %s\n", strerror(errno)); return 0; } strcpy(mapping->command, value); log_debug("config: %s: command: %s\n", section, mapping->command); } else if(!strcmp(key, "DURATION")) { mapping->duration = atoi(value); log_debug("config: %s: duration: %d\n", section, mapping->duration); } else { log_error("config: line %d: syntax error\n", linenum); return 0; } } // end of mapping value parsing } // enf of value parsing } // end of section/parameter parsing buffer[0] = '\0'; // ToDo: Do I need this? } // end of while fclose(fid); return 1; }
static void node_shader_init_mapping(bNode *node) { node->storage= add_mapping(); }
/* Actually load the configuration file. * You will almost certainly want to replace this with something better. */ static int load_config(void) { FILE *fd = NULL; char buffer[256]; char *extname = NULL; char *threadname = NULL; char *directive = NULL; char *check = NULL; int matches = 0; /* Belt and braces - if already loaded, don't re-load */ if( cfg_loaded ) return cfg_loaded; /* Find out where we load from */ char const *filename = get_config_filename(); if ( !filename ) return 0; LOG("Load Config from %s", filename); /* Try and open that file */ fd = fopen(filename, "r"); if( !fd ) { LOG_E("Opening file \"%s\" failed (error %d: %s)", filename, errno, strerror(errno)); return -errno; } /* Loop over the file */ while( !feof(fd) ) { check = fgets(buffer, 255, fd); /* Check nothing went wrong */ if( ferror(fd) || (!check && !feof(fd)) ) LOG_E("Reading file \"%s\" failed: %d %s", filename, errno, strerror(errno)); buffer[255] = '\0'; /* sscanf isn't particularly safe - a real application should use a better parser */ matches = sscanf(buffer, "%ms%ms%ms", &directive, &threadname, &extname); if( matches < 3 ) break; LOG("Read: %s %s %s", directive, threadname, extname); if( !strncasecmp("set", directive, 4) ) directive_set(threadname, atoi(extname)); else if( !strncasecmp("name", directive, 5) ) add_mapping(threadname, extname); else if( !strncasecmp("num", directive, 4) ) add_numeric(atoi(threadname), extname); else LOG_E("Invalid directive: %s", directive); free(directive); free(threadname); free(extname); } LOG("Load Complete"); fclose(fd); /* Debug print the loaded table */ dump_table(); return 1; }
enum armv2_status map_memory(struct armv2 *cpu, uint32_t device_num, uint32_t start, uint32_t end) { uint32_t page_pos = 0; uint32_t page_start = PAGEOF(start); uint32_t page_end = PAGEOF(end); struct hardware_mapping hw_mapping = {0}; if(NULL == cpu || end <= start) { return ARMV2STATUS_INVALID_ARGS; } if(device_num >= cpu->num_hardware_devices) { return ARMV2STATUS_NO_SUCH_DEVICE; } if(NULL == cpu->hardware_devices[device_num]) { return ARMV2STATUS_INVALID_CPUSTATE; } if(start&PAGE_MASK || end &PAGE_MASK || page_start == 0 || page_end == 0 || page_start >= NUM_PAGE_TABLES || page_end >= NUM_PAGE_TABLES //page_start == INTERRUPT_PAGE_NUM || //page_end == INTERRUPT_PAGE_NUM ) { ) { return ARMV2STATUS_INVALID_ARGS; } //First we need to know if all of the requested memory is available for mapping. That means //it must not have been mapped already, and it may not be the zero page for(page_pos = page_start; page_pos < page_end; page_pos++) { struct page_info *page; if(page_pos >= NUM_PAGE_TABLES) { // || page_pos == INTERRUPT_PAGE_NUM) { return ARMV2STATUS_MEMORY_ERROR; } page = cpu->page_tables[page_pos]; if(page == NULL) { //That's OK, that means this page is currently completely unmapped. We can make a page just for this continue; } if(page->read_callback || page->write_callback || page->read_byte_callback || page->write_byte_callback) { return ARMV2STATUS_ALREADY_MAPPED; } } hw_mapping.device = cpu->hardware_devices[device_num]; //If we get here then the entire range is free, so we can go ahead and fill it in for(page_pos = page_start; page_pos < page_end; page_pos++) { struct page_info *page = cpu->page_tables[page_pos]; if(NULL == page) { //we need a new page page = calloc(1, sizeof(struct page_info)); if(NULL == page) { //I don't think I'm leaving anything untidied up by returning here return ARMV2STATUS_MEMORY_ERROR; } page->flags = (PERM_READ|PERM_EXECUTE|PERM_WRITE); page->memory = NULL; if(hw_mapping.device) { page->mapped_device = hw_mapping.device->extra; } cpu->page_tables[page_pos] = page; } //Already checked everything's OK, and we're single threaded, so this should be ok I think... /* LOG("Setting page_pos %x to callbacks %p %p flags %x\n", */ /* page_pos, */ /* hw_mapping.device->read_callback, */ /* hw_mapping.device->write_callback, */ /* hw_mapping.device->read_byte_callback, */ /* hw_mapping.device->write_byte_callback); */ page->read_callback = hw_mapping.device->read_callback; page->write_callback = hw_mapping.device->write_callback; page->read_byte_callback = hw_mapping.device->read_byte_callback; page->write_byte_callback = hw_mapping.device->write_byte_callback; } hw_mapping.start = start; hw_mapping.end = end; hw_mapping.flags = 0; //maybe use these later add_mapping(&(cpu->hw_mappings),&hw_mapping); return ARMV2STATUS_OK; }
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { // static int count = 1; /* packet counter */ char errbuf[PCAP_ERRBUF_SIZE]; /* declare pointers to packet headers */ struct sniff_ethernet *ethernet; /* The ethernet header [1] */ struct sniff_ip *ip; /* The IP header */ struct sniff_tcp *tcp; /* The TCP header */ struct sniff_udp *udp; /* The UDP header */ struct sniff_icmp *icmp; /* The ICMP payload */ char *payload; /* Packet payload */ int size_ip; int size_tcp; int size_udp; int size_payload; int size_icmp; u_short mapped_id; u_short net_mapped_id; int has_mapped; struct udp_psd_hdr phdr_buf; u_short t_sum; int num; pcap_t *to_if; char *net_ifname; struct ether_addr dst_hwaddr, src_hwaddr; struct in_addr *real_dst; u_short *sport, *dport; int i; int vserver_flag; struct vserver_list_node *vserver_entry; /* define ethernet header */ ethernet = (struct sniff_ethernet*)(packet); /* define/compute ip header offset */ ip = (struct sniff_ip*)(packet + SIZE_ETHERNET); size_ip = IP_HL(ip)*4; if (size_ip < 20) { // printf(" * Invalid IP header length: %u bytes\n", size_ip); return; } /* define interface ip address and hardware address */ if (args == (void *)FROM_EXT) { to_if = int_if; net_ifname = int_ifname; memcpy((u_char *)&src_hwaddr, (u_char *)&int_hwaddr, ETHER_ADDR_LEN); write(1, "EXT\t", 4); } else if (args == (void *)FROM_INT) { to_if = ext_if; net_ifname = ext_ifname; memcpy((u_char *)&src_hwaddr, (u_char *)&ext_hwaddr, ETHER_ADDR_LEN); write(1, "INT\t", 4); } /* does this packet need to be NATed */ if ((args == (void *)FROM_INT) && (*((u_int *)&(ip->ip_dst)) == *((u_int *)&(int_ip)))) { write(1, "terminated\n", 11); return; } /* determine protocol */ switch(ip->ip_p) { case IPPROTO_TCP: write(1, "tcp\t", 4); tcp = (struct sniff_tcp *)(packet + SIZE_ETHERNET + size_ip); size_tcp = ntohs(ip->ip_len) - size_ip; sport = &(tcp->th_sport); dport = &(tcp->th_dport); break; case IPPROTO_UDP: write(1, "udp\t", 4); udp = (struct sniff_udp *)(packet + SIZE_ETHERNET + size_ip); size_udp = ntohs(udp->uh_len); sport = &(udp->uh_sport); dport = &(udp->uh_dport); break; case IPPROTO_ICMP: write(1, "icm\t", 4); icmp = (struct sniff_icmp *)(packet + SIZE_ETHERNET + size_ip); size_icmp = ntohs(ip->ip_len) - size_ip; sport = dport = &(icmp->ic_id); if ((icmp->ic_type != (u_char)0) && (icmp->ic_type != (u_char)8)) { printf("terminated\n"); return; } break; case IPPROTO_IP: return; default: return; } /* does this packet need to be NATed */ vserver_flag = 0; if (args == (void *)FROM_INT) { for (i = 0; i < vserver_size; i++) { if ((*sport == vserver_list[i].mport) && (*((u_int *)&(ip->ip_src)) == *((u_int *)&(vserver_list[i].mip)))) { vserver_flag = FROM_INT; vserver_entry = &(vserver_list[i]); goto mapping; } } } else if (args == (void *)FROM_EXT) { for (i = 0; i < vserver_size; i++) { if ((*dport == vserver_list[i].vport) && (*((u_int *)&(ip->ip_dst)) == *((u_int *)&ext_ip))) { vserver_flag = FROM_EXT; vserver_entry = &(vserver_list[i]); goto mapping; } } if ((ntohs(*dport) < MAPPED_ID_OFFSET)) { write(1, "Terminated\n", 11); return; } pthread_rwlock_rdlock(&(ip_map_table[ntohs(*dport) - MAPPED_ID_OFFSET].rwlock)); if (ip_map_table[ntohs(*dport) - MAPPED_ID_OFFSET].ip_prot_type == 0) { pthread_rwlock_unlock(&(ip_map_table[ntohs(*dport) - MAPPED_ID_OFFSET].rwlock)); write(1, "terminated\n", 11); return; } pthread_rwlock_unlock(&(ip_map_table[ntohs(*dport) - MAPPED_ID_OFFSET].rwlock)); } /* mapping work */ mapping:if (args == (void *)FROM_INT) { if (vserver_flag == FROM_INT) { printf("Isz:%d,sport:%d,sip:%s\t", vserver_size, ntohs(*sport), inet_ntoa(ip->ip_src)); memcpy(sport, &(vserver_entry->vport), sizeof(u_short)); } else { has_mapped = add_mapping(ip->ip_p, *sport, ip->ip_src, &mapped_id); net_mapped_id = htons(mapped_id + MAPPED_ID_OFFSET); printf("mapped_id: %d, net_mapped_id %d\n", mapped_id, net_mapped_id); memcpy(sport, &net_mapped_id, sizeof(u_short)); } } else if (args == (void *)FROM_EXT) { if (vserver_flag == FROM_EXT) { printf("Esz:%d,sport:%d,sip:%s\t", vserver_size, ntohs(*dport), inet_ntoa(ip->ip_dst)); memcpy(dport, &(vserver_entry->mport), sizeof(u_short)); } else { mapped_id = ntohs(*dport) - MAPPED_ID_OFFSET; pthread_rwlock_rdlock(&(ip_map_table[mapped_id].rwlock)); net_mapped_id = ip_map_table[mapped_id].port_num; pthread_rwlock_unlock(&(ip_map_table[mapped_id].rwlock)); memcpy((dport), &net_mapped_id, sizeof(u_short)); printf("mapped_id: %d, net_mapped_id %d\n", mapped_id, net_mapped_id); } } /* modify src/dst IP address and re-compute IP checksum */ if (args == (void *)FROM_EXT) { if (vserver_flag == FROM_EXT) { printf("Emodip:%s\n", inet_ntoa(vserver_entry->mip)); ip->ip_dst = vserver_entry->mip; } else { /* * Why using read lock ? * This section only touch timestamp, internal interface sniffer thread * wouldn't read or modify it, so no synchronization problem here. * The cleaning thread use "trylock" so it would skip if the mutex is locked. */ pthread_rwlock_rdlock(&(ip_map_table[mapped_id].rwlock)); ip->ip_dst = ip_map_table[mapped_id].ip_addr; ip_map_table[mapped_id].timestamp = time(NULL); if ((ip->ip_p == IPPROTO_TCP) && ((tcp->th_flags & TH_FIN) == TH_FIN)) { ip_map_table[mapped_id].timestamp = ~0; } pthread_rwlock_unlock(&(ip_map_table[mapped_id].rwlock)); } } else if (args == (void *)FROM_INT) { memcpy((struct in_addr *)&(ip->ip_src), &ext_ip, sizeof(struct in_addr)); } ip->ip_sum = 0; t_sum = ip_fast_csum(ip, IP_HL(ip)); ip->ip_sum = t_sum; /* compute tcp/udp or icmp check sum */ switch(ip->ip_p) { case IPPROTO_TCP: *((u_int *)&(phdr_buf.ph_src)) = *((u_int *)&(ip->ip_src)); *((u_int *)&(phdr_buf.ph_dst)) = *((u_int *)&(ip->ip_dst)); phdr_buf.ph_zero = 0; phdr_buf.ph_p = ip->ip_p; phdr_buf.ph_len = htons(size_tcp); tcp->th_sum = 0; // printf("len: %d\t", size_udp); t_sum = udp_fast_csum(&phdr_buf, tcp, size_tcp); tcp->th_sum = t_sum; break; case IPPROTO_UDP: *((u_int *)&(phdr_buf.ph_src)) = *((u_int *)&(ip->ip_src)); *((u_int *)&(phdr_buf.ph_dst)) = *((u_int *)&(ip->ip_dst)); phdr_buf.ph_zero = 0; phdr_buf.ph_p = ip->ip_p; phdr_buf.ph_len = udp->uh_len; udp->uh_sum = 0; // printf("len: %d\t", size_udp); t_sum = udp_fast_csum(&phdr_buf, udp, size_udp); udp->uh_sum = t_sum; break; case IPPROTO_ICMP: icmp->ic_sum = 0; t_sum = ip_fast_csum(icmp, size_icmp / 4 + ((size_icmp % 4) ? 1 : 0)); icmp->ic_sum = t_sum; break; case IPPROTO_IP: return; default: return; } /* modify src/dst ethernet hardware address */ if (args == (void *)FROM_EXT) { real_dst = &(ip->ip_dst); } else if (args == (void *)FROM_INT) { /* determine whether the destination host in local network */ if((*((u_int *)&(ip->ip_dst)) & *((u_int *)&(ext_mask))) == (*((u_int *)&(ext_ip)) & *((u_int *)&(ext_mask)))) { real_dst = &(ip->ip_dst); } else { real_dst = &def_gw; } } if (arp_find(real_dst, &dst_hwaddr, net_ifname) == 0) { fprintf(stderr, "Couldn't find MAC address for host %s\n", inet_ntoa(ip->ip_dst)); return; } memcpy((u_char *)(&(ethernet->ether_dhost)), (u_char *)&dst_hwaddr, ETHER_ADDR_LEN); memcpy((u_char *)(&(ethernet->ether_shost)), (u_char *)&src_hwaddr, ETHER_ADDR_LEN); if ((num = pcap_inject(to_if, packet, header->len)) == 0) { fprintf(stderr, "Couldn't send packet"); return; } return; /* define/compute tcp payload (segment) offset */ payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp); /* compute tcp payload (segment) size */ size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp); return; }