irc_message* parse_message(char *line) { char *save_pointer; char *part = strtok_r(line, " ", &save_pointer); irc_message *msg = (irc_message *)calloc(1, sizeof(irc_message)); if(part[0] == ':') { msg->prefix = parse_prefix(part); part = strtok_r(NULL, " ", &save_pointer); } uintmax_t num = strtoumax(part, NULL, 10); if(num != 0) { msg->command_id = num; } else { msg->command = alloc_ptr(part); strcpy(msg->command, part); } char* remainder = strtok_r(NULL, "\r", &save_pointer); msg->params = alloc_ptr(remainder); strcpy(msg->params, remainder); return msg; }
irc_message* create_message(char *prefix, char *command, int command_id, char *params) { irc_message *msg = (irc_message *)calloc(1, sizeof(irc_message)); if(prefix != NULL) { msg->prefix = parse_prefix(prefix); } if(command != NULL) { msg->command = alloc_ptr(command); strcpy(msg->command, command); } if(command_id > 0) { msg->command_id = command_id; } if(params != NULL) { msg->params = alloc_ptr(params); strcpy(msg->params, params); } return msg; }
void Config::parse(const char *pos) { while (1) { char *eol = strchr(pos, '\n'); if (!eol) break; // parse if (!parse_blank(pos) && !parse_prefix(pos) && !parse_partition(pos) && !parse_node(pos)) fatal("unexpected line in config file:\n%s<END>", pos); pos = eol + 1; } }
static int parse_config(gnc_t gnc, void *closure) { int c; char *token; c = gnc(closure); if(c < 2) return -1; while(c >= 0) { c = skip_whitespace(c, gnc, closure); if(c == '\n' || c == '#') { c = skip_to_eol(c, gnc, closure); continue; } if(c < 0) break; c = getword(c, &token, gnc, closure); if(c < -1) return -1; if(strcmp(token, "mode") == 0) { char *mtoken; c = getword(c, &mtoken, gnc, closure); if(c < -1) return -1; if(strcmp(mtoken, "server") == 0) { #ifndef NO_SERVER client_config = 0; if(!server_config) server_config = calloc(1, sizeof(struct server_config)); if(!server_config) return -1; #else return -1; #endif } else if(strcmp(mtoken, "client") == 0) { if(server_config) return -1; client_config = 1; } else if(strcmp(mtoken, "forwarder") == 0) { if(server_config) return -1; client_config = 0; } else { return -1; } free(mtoken); c = skip_eol(c, gnc, closure); if(c < -1) return -1; } else if(strcmp(token, "lease-dir") == 0) { char *dir; if(!server_config) return -1; c = getstring(c, &dir, gnc, closure); if(c < -1) return -1; if(dir[0] != '/') return -1; server_config->lease_dir = dir; c = skip_eol(c, gnc, closure); if(c < -1) return -1; } else if(strcmp(token, "prefix") == 0) { char *ptoken; struct prefix_list *prefix; if(!server_config) return -1; c = getword(c, &ptoken, gnc, closure); if(c < -1) return -1; prefix = parse_prefix(ptoken, PREFIX); if(prefix == NULL || prefix->n != 1) return -1; if(prefix_list_v4(prefix)) { unsigned const char zeroes[4] = {0}; unsigned mask, first, last; if(memcmp(server_config->lease_first, zeroes, 4) != 0) return -1; mask = 0xFFFFFFFF << (128 - prefix->l[0].plen); first = (prefix->l[0].p[12] << 24 | prefix->l[0].p[13] << 16 | prefix->l[0].p[14] << 8 | prefix->l[0].p[15] ) & mask; last = first | (~ mask); first = htonl(first + 1); last = htonl(last - 1); memcpy(server_config->lease_first, &first, 4); memcpy(server_config->lease_last, &last, 4); free(prefix); } else { server_config->ipv6_prefix = cat_prefix_list(server_config->ipv6_prefix, prefix); } free(ptoken); c = skip_eol(c, gnc, closure); if(c < -1) return -1; } else if(strcmp(token, "name-server") == 0 || strcmp(token, "ntp-server") == 0) { char *ptoken; struct prefix_list *prefix; if(!server_config) return -1; c = getword(c, &ptoken, gnc, closure); if(c < -1) return -1; prefix = parse_prefix(ptoken, ADDRESS); if(prefix == NULL) return -1; if(strcmp(token, "name-server") == 0) server_config->name_server = cat_prefix_list(server_config->name_server, prefix); else server_config->ntp_server = cat_prefix_list(server_config->ntp_server, prefix); free(ptoken); } else { return -1; } free(token); } return 1; }
int parse_config(char *config_file, boolean_t file_required) { FILE *fp; char line[MAXLINELEN]; char pline[MAXLINELEN]; int argcount; char *argvec[MAXARGSPERLINE]; int defaultdone = 0; /* Set when first non-default command found */ if (debug & D_CONFIG) logmsg(LOG_DEBUG, "parse_config()\n"); set_protocol_defaults(); if (debug & D_DEFAULTS) print_defaults(); fp = open_conffile(config_file); if (fp == NULL) { if (errno == ENOENT && !file_required) return (0); logperror(config_file); return (-1); } while (readline(fp, line, sizeof (line)) != 0) { (void) strncpy(pline, line, sizeof (pline)); pline[sizeof (pline) - 1] = '\0'; /* NULL terminate */ argcount = parse_line(pline, argvec, sizeof (argvec) / sizeof (argvec[0])); if (debug & D_PARSE) { int i; logmsg(LOG_DEBUG, "scanned %d args\n", argcount); for (i = 0; i < argcount; i++) logmsg(LOG_DEBUG, "arg[%d]: %s\n", i, argvec[i]); } if (argcount == 0) { /* Empty line - or comment only line */ continue; } if (strcmp(argvec[0], "ifdefault") == 0) { char save[sizeof (ifdefaults)]; if (defaultdone) { conferr("ifdefault after non-default " "command\n"); continue; } /* * Save existing values in case what we read is * invalid and we need to restore previous settings. */ (void) memcpy(save, ifdefaults, sizeof (ifdefaults)); parse_default(CONFIG_IF, iflist, argvec+1, argcount-1, ifdefaults); check_if_var_consistency(ifdefaults, save, sizeof (save)); } else if (strcmp(argvec[0], "prefixdefault") == 0) { char save[sizeof (prefixdefaults)]; if (defaultdone) { conferr("prefixdefault after non-default " "command\n"); continue; } /* * Save existing values in case what we read is * invalid and we need to restore previous settings. */ (void) memcpy(save, prefixdefaults, sizeof (prefixdefaults)); parse_default(CONFIG_PREFIX, prefixlist, argvec+1, argcount-1, prefixdefaults); check_var_consistency(prefixdefaults, save, sizeof (save)); } else if (strcmp(argvec[0], "if") == 0) { defaultdone = 1; parse_if(iflist, argvec+1, argcount-1); } else if (strcmp(argvec[0], "prefix") == 0) { defaultdone = 1; parse_prefix(prefixlist, argvec+1, argcount-1); } else { conferr("Unknown command: %s\n", argvec[0]); } } (void) fclose(fp); if (debug & D_DEFAULTS) print_defaults(); return (0); }
void irc_server_init_isupport(IRC_SERVER_REC *server) { char *sptr; gpointer key, value; /* chanmodes/prefix will fully override defaults */ memset(server->modes, 0, sizeof(server->modes)); memset(server->prefix, 0, sizeof(server->prefix)); if ((sptr = g_hash_table_lookup(server->isupport, "CHANMODES"))) parse_chanmodes(server, sptr); /* This is after chanmode because some servers define modes in both */ if (g_hash_table_lookup_extended(server->isupport, "PREFIX", &key, &value)) { sptr = value; if (*sptr != '(') { /* server incompatible with isupport draft */ g_hash_table_remove(server->isupport, key); g_free(key); g_free(value); sptr = NULL; } } else { sptr = NULL; } if (sptr == NULL) { sptr = g_strdup("(ohv)@%+"); g_hash_table_insert(server->isupport, g_strdup("PREFIX"), sptr); } parse_prefix(server, sptr); if ((sptr = g_hash_table_lookup(server->isupport, "MODES"))) { server->max_modes_in_cmd = atoi(sptr); if (server->max_modes_in_cmd < 1) server->max_modes_in_cmd = DEFAULT_MAX_MODES; } if ((sptr = g_hash_table_lookup(server->isupport, "CASEMAPPING"))) { if (strstr(sptr, "rfc1459") != NULL) server->nick_comp_func = irc_nickcmp_rfc1459; else server->nick_comp_func = irc_nickcmp_ascii; } if ((sptr = g_hash_table_lookup(server->isupport, "TARGMAX"))) { char *p = sptr; server->max_kicks_in_cmd = 1; server->max_msgs_in_cmd = 1; /* Not doing WHOIS here until it is clear what it means. */ while (*p != '\0') { if (!g_ascii_strncasecmp(p, "KICK:", 5)) { server->max_kicks_in_cmd = atoi(p + 5); if (server->max_kicks_in_cmd <= 0) server->max_kicks_in_cmd = 30; } else if (!g_ascii_strncasecmp(p, "PRIVMSG:", 8)) { server->max_msgs_in_cmd = atoi(p + 8); if (server->max_msgs_in_cmd <= 0) server->max_msgs_in_cmd = 30; } p = strchr(p, ','); if (p == NULL) break; p++; } } else if ((sptr = g_hash_table_lookup(server->isupport, "MAXTARGETS"))) { server->max_msgs_in_cmd = atoi(sptr); if (server->max_msgs_in_cmd <= 0) server->max_msgs_in_cmd = 1; } }
void parse_msg(char *msg) { // see RFC 1459, 2.3 int msg_pos = 0, cur_pos; // msg_pos: position in 'msg'; cur_pos: position in current string to parse (such as nickname etc.) char tmp_str[512]; memset(nfos->sender, 0, sizeof(_Sender_t)); if(msg[0] == ':') // not a protocol command? { // get the prefix for(cur_pos = 0; msg[msg_pos] != ' '; msg_pos++, cur_pos++) tmp_str[cur_pos] = msg[msg_pos]; tmp_str[cur_pos] = '\0'; parse_prefix(tmp_str + 1); // not sending the colon (:) // because RFC 1459 define <SPACE> as one ' ' _or more_ while(msg[msg_pos] == ' ') msg_pos++; } //get the command if(isalpha(msg[msg_pos])) // if it as a command { for(cur_pos = 0; msg[msg_pos] != ' '; msg_pos++, cur_pos++) tmp_str[cur_pos] = msg[msg_pos]; tmp_str[cur_pos] = '\0'; strncpy(nfos->sender->command, tmp_str, SERVER_CMD_MAX + 1); }else // if we have a request number { strcpy(nfos->sender->command, "0"); //sprintf(nfos->sender->request_nr, "%c%c%c", msg[msg_pos], msg[++msg_pos], msg[++msg_pos]); // is that fine code??? nfos->sender->request_nr[0] = msg[msg_pos]; nfos->sender->request_nr[1] = msg[++msg_pos]; nfos->sender->request_nr[2] = msg[++msg_pos]; nfos->sender->request_nr[3] = '\0'; msg_pos++; } while(msg[msg_pos] == ' ') msg_pos++; if(msg[msg_pos] != ':') // we got a middle! { for(cur_pos = 0; msg[msg_pos] != ':' && msg[msg_pos] != '\0'; msg_pos++, cur_pos++) tmp_str[cur_pos] = msg[msg_pos]; if(msg[msg_pos] != '\0') { // there might be spaces before colon (:) for(; msg[msg_pos] == ' '; msg_pos--, cur_pos--); tmp_str[--cur_pos] = '\0'; }else tmp_str[cur_pos] = '\0'; strncpy(nfos->sender->middle, tmp_str, MIDDLE_MAX + 1); } while(msg[msg_pos] != ':' && msg[msg_pos] != '\0') msg_pos++; if(msg[msg_pos] == ':') { msg_pos++; //because noone needs the colon (:)... // now the rest is called "message", so all of these <params> are 'ignored' //strncpy(nfos->sender->message, &msg[msg_pos], MSG_MAX + 1); snprintf(nfos->sender->message, MSG_MAX + 1, "%s", &msg[msg_pos]); } return; }
static const struct token * match_token(const char *word, const struct token *table, struct parse_result *res) { unsigned int i, match; const struct token *t = NULL; match = 0; for (i = 0; table[i].type != ENDTOKEN; i++) { switch (table[i].type) { case NOTOKEN: if (word == NULL || strlen(word) == 0) { match++; t = &table[i]; } break; case KEYWORD: if (word != NULL && strncmp(word, table[i].keyword, strlen(word)) == 0) { match++; t = &table[i]; if (t->value) res->action = t->value; } break; case FLAG: if (word != NULL && strncmp(word, table[i].keyword, strlen(word)) == 0) { match++; t = &table[i]; res->flags |= t->value; } break; case FAMILY: if (word == NULL) break; if (!strcmp(word, "inet") || !strcasecmp(word, "IPv4")) { match++; t = &table[i]; res->family = AF_INET; } if (!strcmp(word, "inet6") || !strcasecmp(word, "IPv6")) { match++; t = &table[i]; res->family = AF_INET6; } break; case ASNUM: if (parse_asnum(word, &res->as)) { match++; t = &table[i]; } break; case ADDRESS: if (parse_addr(word, &res->family, &res->addr)) { match++; t = &table[i]; if (t->value) res->action = t->value; } break; case PREFIX: if (parse_prefix(word, &res->family, &res->addr, &res->prefixlen)) { match++; t = &table[i]; if (t->value) res->action = t->value; } break; case IFNAME: if (!match && word != NULL && strlen(word) > 0) { if (strlcpy(res->ifname, word, sizeof(res->ifname)) >= sizeof(res->ifname)) err(1, "interface name too long"); match++; t = &table[i]; if (t->value) res->action = t->value; } break; case ENDTOKEN: break; } } if (match != 1) { if (word == NULL) fprintf(stderr, "missing argument:\n"); else if (match > 1) fprintf(stderr, "ambiguous argument: %s\n", word); else if (match < 1) fprintf(stderr, "unknown argument: %s\n", word); return (NULL); } return (t); }
const struct token * match_token(int *argc, char **argv[], const struct token table[]) { u_int i, match; const struct token *t = NULL; struct filter_set *fs; const char *word = *argv[0]; match = 0; for (i = 0; table[i].type != ENDTOKEN; i++) { switch (table[i].type) { case NOTOKEN: if (word == NULL || strlen(word) == 0) { match++; t = &table[i]; } break; case KEYWORD: if (word != NULL && strncmp(word, table[i].keyword, strlen(word)) == 0) { match++; t = &table[i]; if (t->value) res.action = t->value; } break; case FLAG: if (word != NULL && strncmp(word, table[i].keyword, strlen(word)) == 0) { match++; t = &table[i]; res.flags |= t->value; } break; case FAMILY: if (word == NULL) break; if (!strcmp(word, "inet") || !strcasecmp(word, "IPv4")) { match++; t = &table[i]; res.aid = AID_INET; } if (!strcmp(word, "inet6") || !strcasecmp(word, "IPv6")) { match++; t = &table[i]; res.aid = AID_INET6; } if (!strcasecmp(word, "VPNv4")) { match++; t = &table[i]; res.aid = AID_VPN_IPv4; } break; case ADDRESS: if (parse_addr(word, &res.addr)) { match++; t = &table[i]; if (t->value) res.action = t->value; } break; case PEERADDRESS: if (parse_addr(word, &res.peeraddr)) { match++; t = &table[i]; if (t->value) res.action = t->value; } break; case PREFIX: if (parse_prefix(word, &res.addr, &res.prefixlen)) { match++; t = &table[i]; if (t->value) res.action = t->value; } break; case ASTYPE: if (word != NULL && strncmp(word, table[i].keyword, strlen(word)) == 0) { match++; t = &table[i]; res.as.type = t->value; } break; case ASNUM: if (parse_asnum(word, &res.as.as)) { match++; t = &table[i]; } break; case PEERDESC: if (!match && word != NULL && strlen(word) > 0) { if (strlcpy(res.peerdesc, word, sizeof(res.peerdesc)) >= sizeof(res.peerdesc)) errx(1, "neighbor description too " "long"); match++; t = &table[i]; } break; case RIBNAME: if (!match && word != NULL && strlen(word) > 0) { if (strlcpy(res.rib, word, sizeof(res.rib)) >= sizeof(res.rib)) errx(1, "rib name too long"); match++; t = &table[i]; } break; case COMMUNITY: if (word != NULL && strlen(word) > 0 && parse_community(word, &res)) { match++; t = &table[i]; } break; case LOCALPREF: case MED: case PREPNBR: case PREPSELF: case WEIGHT: case RTABLE: if (word != NULL && strlen(word) > 0 && parse_number(word, &res, table[i].type)) { match++; t = &table[i]; } break; case NEXTHOP: if (word != NULL && strlen(word) > 0 && parse_nexthop(word, &res)) { match++; t = &table[i]; } break; case PFTABLE: if (word != NULL && strlen(word) > 0) { if ((fs = calloc(1, sizeof(struct filter_set))) == NULL) err(1, NULL); if (strlcpy(fs->action.pftable, word, sizeof(fs->action.pftable)) >= sizeof(fs->action.pftable)) errx(1, "pftable name too long"); TAILQ_INSERT_TAIL(&res.set, fs, entry); match++; t = &table[i]; } break; case GETOPT: if (bgpctl_getopt(argc, argv, table[i].value)) { match++; t = &table[i]; } break; case FILENAME: if (word != NULL && strlen(word) > 0) { if ((res.mrtfd = open(word, O_RDONLY)) == -1) { /* * ignore error if path has no / and * does not exist. In hope to print * usage. */ if (errno == ENOENT && !strchr(word, '/')) break; err(1, "mrt open(%s)", word); } match++; t = &table[i]; } break; case ENDTOKEN: break; } } if (match != 1) { if (word == NULL) fprintf(stderr, "missing argument:\n"); else if (match > 1) fprintf(stderr, "ambiguous argument: %s\n", word); else if (match < 1) fprintf(stderr, "unknown argument: %s\n", word); return (NULL); } return (t); }