/** * nm_setting_bond_validate_option: * @name: the name of the option to validate * @value: the value of the option to validate * * Checks whether @name is a valid bond option and @value is a valid value for * the @name. If @value is %NULL, the function only validates the option name. * * Returns: %TRUE, if the @value is valid for the given name. * If the @name is not a valid option, %FALSE will be returned. **/ gboolean nm_setting_bond_validate_option (const char *name, const char *value) { guint i; if (!name || !name[0]) return FALSE; for (i = 0; i < G_N_ELEMENTS (defaults); i++) { if (g_strcmp0 (defaults[i].opt, name) == 0) { if (value == NULL) return TRUE; switch (defaults[i].opt_type) { case NM_BOND_OPTION_TYPE_INT: return validate_int (name, value, &defaults[i]); case NM_BOND_OPTION_TYPE_STRING: return validate_list (name, value, &defaults[i]); case NM_BOND_OPTION_TYPE_BOTH: return ( validate_int (name, value, &defaults[i]) || validate_list (name, value, &defaults[i])); case NM_BOND_OPTION_TYPE_IP: return validate_ip (name, value); case NM_BOND_OPTION_TYPE_MAC: return nm_utils_hwaddr_valid (value, ETH_ALEN); case NM_BOND_OPTION_TYPE_IFNAME: return validate_ifname (name, value); } return FALSE; } } return FALSE; }
char * get_network_ip(void) { char * ip = get_kcmdline_string(NETWORK_IP_KEY); if (ip != NULL) if(!validate_ip(ip)) return(ip); return(get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY)); }
int main(int argc, char* argv[]) { if ((ip = getenv("TCPREMOTEIP")) == 0 || (ip = validate_ip(ip)) == 0) die1(111, "Must be run from tcp-env or tcpserver."); if ((dir = getenv("RELAY_CTRL_DIR")) == 0) warn1("$RELAY_CTRL_DIR is not set."); else if (is_authenticated()) make_file(ip, argc > 1); if (argc > 1) { execvp(argv[1], argv+1); return 111; } return 0; }
/* * @param config : the key to be set * @param setting : The value to be set */ set_config_result_t set_network_setting(const char *config, const char *setting) { GKeyFile *settingsfile; gboolean test = FALSE; gchar *keyfile; if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY)) if(validate_ip(setting) != 0) return SET_CONFIG_ERROR; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY)) { set_config_result_t ret = SET_CONFIG_ERROR; if (test) { if(config_value_changed(settingsfile, NETWORK_ENTRY, config, setting)) { g_key_file_free(settingsfile); return SET_CONFIG_UNCHANGED; } } else { log_debug("No conffile. Creating.\n"); create_conf_file(); } g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting); keyfile = g_key_file_to_data (settingsfile, NULL, NULL); /* free the settingsfile before writing things out to be sure the contents will be correctly written to file afterwards. Just a precaution. */ g_key_file_free(settingsfile); if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL)) ret = SET_CONFIG_UPDATED; free(keyfile); return ret; } else { g_key_file_free(settingsfile); return SET_CONFIG_ERROR; } }
int handle_arp_packet(struct sr_instance * sr, uint8_t * packet, unsigned int len ){ int res; struct sr_arp_hdr *arp_hdr = (struct sr_arp_hdr *)(packet + sizeof(sr_ethernet_hdr_t)); int arp_op = ntohs(arp_hdr->ar_op); /* check to see if the target IP belongs to one of our routers */ struct sr_if* assoc_iface = validate_ip(sr->if_list, arp_hdr->ar_tip); if (!assoc_iface){ /* if its not one of ours, ignore it */ return -1; } if (arp_op == arp_op_request){ /* this is an incoming request */ res = send_arp_response(sr, assoc_iface, packet, len); if (res != 0){ fprintf(stderr, "bad send_arp_response\n"); return -1; } } else if (arp_op == arp_op_reply) { /* this is an incoming reply */ res = handle_arp_reply(sr, packet, len); if (res != 0){ return -1; } } else { /* bad arp_op type */ return -1; } return 0; }
void parse_args(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "invalid number of arguments: %d\n", argc); fprintf(stderr, "usage: %s [hostname] [port number]\n", argv[0]); fprintf(stderr, "\nABORTING PROGRAM\n"); exit(EXIT_FAILURE); } hostname = argv[1]; printf("parsed hostname: %s\n", hostname); if (!validate_ip(hostname)) { fprintf(stderr, "hostname validate: FAILURE\n"); fprintf(stderr, "\nABORTING PROGRAM\n"); exit(EXIT_FAILURE); } printf("hostname validate: SUCCESS\n"); port = argv[2]; printf("parsed port number: %s\n", port); if (atoi(port) < 1) { fprintf(stderr, "port number validate: FAILURE\n"); fprintf(stderr, "\nABORTING PROGRAM\n"); exit(EXIT_FAILURE); } printf("port number validate: SUCCESS\n"); }
int handle_ip_packet(struct sr_instance * sr, uint8_t * packet, unsigned int len ) { sr_ip_hdr_t *iphdr = (sr_ip_hdr_t *)(packet + sizeof(sr_ethernet_hdr_t)); /* validate checksum. */ uint16_t checksum; checksum = cksum(iphdr, sizeof(*iphdr)); if (checksum != 0xffff) { return -1; } uint8_t * newpacket_for_ip = (uint8_t *) malloc(len); memcpy(newpacket_for_ip, packet, len); sr_ip_hdr_t *new_iphdr = (sr_ip_hdr_t *)(newpacket_for_ip + sizeof(sr_ethernet_hdr_t)); /* Decrement the TTL by 1, and recompute the packet checksum over the modified header. */ /* decrement ttl */ new_iphdr->ip_ttl--; if (new_iphdr->ip_ttl <= 0) { /* check ttl, less than zero */ send_icmp_message(sr,packet, 11, 0); return -1; } /* update checksum. */ new_iphdr->ip_sum = 0; checksum = cksum(new_iphdr, sizeof(*new_iphdr)); new_iphdr->ip_sum = checksum; checksum = cksum(new_iphdr, sizeof(*new_iphdr)); struct sr_if* assoc_iface = validate_ip(sr->if_list, iphdr->ip_dst); if (assoc_iface) { /*it's destined to one of our IPs */ /* ICMP */ uint8_t ip_proto = ip_protocol(packet + sizeof(sr_ethernet_hdr_t)); if (ip_proto == ip_protocol_icmp) { int minlength = sizeof(sr_icmp_hdr_t) + sizeof(sr_ethernet_hdr_t) + sizeof(sr_ip_hdr_t); if (len < minlength){ return -1; } struct sr_icmp_hdr * icmp_hdr = (struct sr_icmp_hdr *) (packet + sizeof(sr_ethernet_hdr_t) + sizeof(sr_ip_hdr_t)); if(icmp_hdr->icmp_type == 8){ /* is an echo request */ int res; res = make_echo_request(&newpacket_for_ip, len); if (res == -1){ return -1; } } /* end ICMP */ } else { /* got a udp payload to a rounter interface */ int res; res = send_icmp_message(sr, packet, 3, 3); if (res == -1){ return -1; } } } /* Find out which entry in the routing table has the longest prefix match with the destination IP address. */ struct sr_rt* best_rt = find_best_rt(sr->routing_table, ntohl(new_iphdr->ip_dst)); if (!best_rt) { /* didn't find an interface, send an ICMP message type 3 code 0, also if there are any errors above */ int res = send_icmp_message(sr, packet, 3, 0); if (res == -1){ return -1; } return 0; } /* found an interface */ struct sr_if * best_iface = sr_get_interface(sr, best_rt->interface); if (!best_iface){ return -1; } struct sr_arpentry * forward_arp_entry = sr_arpcache_lookup(&(sr->cache), best_rt->gw.s_addr); struct sr_ethernet_hdr * new_ether_hdr = (struct sr_ethernet_hdr * ) newpacket_for_ip; /* ethernet -- update the source address */ memcpy(new_ether_hdr->ether_shost, best_iface->addr, ETHER_ADDR_LEN); if (forward_arp_entry) { /* we have a MAC address */ /* update packet */ /* ethernet -- set the dest address */ memcpy(new_ether_hdr->ether_dhost, forward_arp_entry->mac, ETHER_ADDR_LEN); /* send packet using correct interface */ int res = 0; res = sr_send_packet(sr, newpacket_for_ip, len, best_rt->interface); if (res != 0) { return -1; } free(forward_arp_entry); } else { /* we dont have a MAC address, add to arp queue */ struct sr_arpreq * arpreq; arpreq = sr_arpcache_queuereq(&(sr->cache), best_rt->gw.s_addr, newpacket_for_ip, len, best_rt->interface ); if (!arpreq){ return -1; } uint32_t ip, dest; ip = ntohl(best_iface->ip); dest = ntohl(best_rt->dest.s_addr); sr_handle_arp_req(sr, arpreq); } return 0; }
int parse_shell() { char shell_input[MAX_LENGTH]; char message[MAX_LENGTH]; static bool is_client_connected = false; int ret = 0; static int server_sock; /* * Get the input */ fgets(shell_input, MAX_LENGTH, stdin); if(strlen(shell_input) <= 1) return 0; shell_input[strlen(shell_input)-1] = '\0'; strcpy(message, shell_input); /* * Tokenize the strings */ char *arg; int argc = 0; char argv[4][256]; char *temp; char *command = strtok_r(shell_input, " ", &temp); //TODO reorder all commands!! // ----------------Commands common for server/client ------------ if(!strcmp("AUTHOR", command)) { print_success(1, command); LOG("I, g8, have read and understood the course academic integrity policy.\n"); } else if(!strcmp("EXIT", command)) { return 1; } // -------------------Common commands --------------------------- else if(!is_server) { char arg_copy[MAX_LENGTH]; if(temp!=NULL) { strcpy(arg_copy, temp); for(arg = strtok_r(NULL, " ", &temp); arg && argc<4; arg = strtok_r(NULL, " ", &temp)) { strcpy(argv[argc], arg); argc++; } } if(!strcmp("LOGIN", command)) { if(!is_server && !(is_client_connected || argc!=2) && validate_ip(argv[0]) && validate_port(argv[1])) { int newfd = client_connect(argv[0], argv[1]); if(newfd<=1) { print_success(0, command); } else { //TODO client connects to unknown port? is_client_connected = true; add_fd(newfd); client_identify(newfd); server_sock = newfd; print_success(1, command); } } else print_success(0, command); } else if(is_client_connected) { if(!strcmp("PORT", command)) { print_success(1, command); LOG("PORT:%d\n", get_listening_port()); } else if(!strcmp("IP", command)) { print_success(1, command); get_ip(); } else if(!strcmp("SEND", command)) { if(!is_client_connected) print_success(0,command); else print_success(client_send_msg(server_sock, arg_copy), command); } else if(!strcmp("SENDFILE", command)) { if(argc == 2 && validate_ip(argv[0])) { print_success(1, command); client_send_file(argv[0], argv[1]); } else print_success(0, command); } else if(!strcmp("LOGOUT", command)) { if(is_client_connected) { //close(server_sock); //clear_fd(server_sock); client_send(server_sock, command); is_client_connected = false; //server_sock = -1; print_success(1, command); } else print_success(0, command); } else if(!strcmp("LIST", command)) { print_success(1, command); print_client_list(); } else if(!strcmp("REFRESH", command)) { client_send(server_sock, command); print_success(1, command); } else if(!strcmp("BROADCAST", command)) { if(argc) { client_send(server_sock, message); print_success(1, command); } else print_success(0, command); } else if(!strcmp("BLOCK", command)) { if(argc == 1 && verify_ip(argv[0]) && !is_client_blocked(argv[0])) { //TODO CHECK IP in list!! done! print_success(1, command); add_to_block_list(argv[0]); client_send(server_sock, message); } else { print_success(0, command); } } else if(!strcmp("UNBLOCK", command)) { if(argc == 1 && verify_ip(argv[0]) && is_client_blocked(argv[0])) { print_success(1, command); remove_from_block_list(argv[0]); client_send(server_sock, message); } else print_success(0, command); } else print_success(0, command); } else print_success(0, command); } else { if(!strcmp("STATISTICS", command)) { print_success(1, command); print_stats(); } else if(!strcmp("BLOCKED", command)) { if(temp == NULL) print_success(0, command); else { char *ip = strtok_r(NULL, " ", &temp); if(validate_ip(ip) && find_client_by_ip(ip) !=NULL) { print_success(1, command); print_blocked_clients(ip); } else print_success(0, command); } } else if(!strcmp("LIST", command)) { print_success(1, command); print_connected_client_list(); } else if(!strcmp("PORT", command)) { print_success(1, command); LOG("PORT:%d\n", get_listening_port()); } else if(!strcmp("IP", command)) { print_success(1, command); get_ip(); } else print_success(0, command); } LOG("[%s:END]\n", command); return 0; }
/* * Gets breakpoint flags on the specified opcode. */ bpflags VMMethod::get_breakpoint_flags(STATE, size_t ip) { if(validate_ip(state, ip)) { return opcodes[ip] & ~cBreakpointMask; } return 0; }
/* * Sets breakpoint flags on the specified opcode. */ void VMMethod::set_breakpoint_flags(STATE, size_t ip, bpflags flags) { if(validate_ip(state, ip)) { opcodes[ip] &= cBreakpointMask; // Clear the high byte opcodes[ip] |= flags & ~cBreakpointMask; } }