int request_snmpEnableAuthTraps(char *error_label) { static int my_ip_address_initialized = False; static IPAddress my_ip_address; SNMP_pdu *request; SNMP_pdu *response; SNMP_variable *variable; int snmpEnableAuthTraps; struct timeval timeout; timeout.tv_sec = 5; timeout.tv_usec = 0; error_label[0] = '\0'; if(my_ip_address_initialized == False) { if(get_my_ip_address(&my_ip_address, error_label)) { return -1; } my_ip_address_initialized = True; } request = request_create("public", GET_REQ_MSG, error_label); if(request == NULL) { return -1; } if(snmp_pdu_append_null_variable(request, &snmpEnableAuthTraps_name, error_label) == NULL) { snmp_pdu_free(request); return -1; } response = request_send_to_port_time_out_blocking(&my_ip_address, \ SNMP_PORT, &timeout, request, error_label); if(response == NULL) { snmp_pdu_free(request); return -1; } snmp_pdu_free(request); if(response->error_status) { sprintf(error_label, "%s", error_status_string(response->error_status)); snmp_pdu_free(response); return -1; } variable = response->first_variable; if(variable->next_variable || SSAOidCmp(&(variable->name), &snmpEnableAuthTraps_name) || (variable->type != INTEGER) || (variable->val.integer == NULL) || (variable->val_len != sizeof(int32_t)) ) { sprintf(error_label, ERR_MSG_BAD_RESPONSE); snmp_pdu_free(response); return -1; } snmpEnableAuthTraps = *(variable->val.integer); snmp_pdu_free(response); if(trace_level > 0) { trace("snmpAuthTraps: %s\n\n", (snmpEnableAuthTraps == 1)? "enabled(1)": "disabled(2)"); } switch(snmpEnableAuthTraps) { case 1: /* enabled(1) */ return TRUE; case 2: /* disable(2) */ return FALSE; default: sprintf(error_label, ERR_MSG_BAD_VALUE); return -1; } }
/* * * Prepares the routing update packet that has to broadcasted to all neighbors * * @param argc * Number of command line arguments * * @param argv * Command line arguments * * @return * Integer indicating success/failure of function * */ int main(int argc, char** argv) { int c; int t_flag=0; int i_flag=0; char* topology_file; char* update_interval; /* parsing command line arguments */ static char usage[] = "usage: %s -t <topology file name> -i <update interval>\n"; while ((c = getopt (argc, argv, "t:i:")) != -1){ switch (c) { case 't': t_flag=1; topology_file=optarg; break; case 'i': i_flag=1; update_interval=optarg; break; case '?': fprintf(stderr, usage, argv[0]); exit(0); break; } } if(t_flag==0) { fprintf(stderr, "%s: missing -i option\n", argv[0]); fprintf(stderr, usage, argv[0]); exit(0); } if(i_flag==0) { fprintf(stderr, "%s: missing -t option\n", argv[0]); fprintf(stderr, usage, argv[0]); exit(0); } /* end parsing command line arguments */ get_my_ip_address(); char msg[1024]; //read int numBytes; // number of bytes read int i,j,selected; //iterators int select_return; //server info int server_id; //end server info //my details struct sockaddr_in my_ip_struct; int optval = 1; //for sockopt //end my details //FD fd_set read_fds, write_fds,read_fds_copy,write_fds_copy; int fdMax; struct timeval time_out; time_out.tv_sec=atoi(update_interval); parse_topology_file(topology_file); //create my socket if ((my_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ perror("socket"); return -1; } memset(&my_ip_struct, 0 , sizeof(my_ip_struct)); my_ip_struct.sin_family = AF_INET; my_ip_struct.sin_addr.s_addr = INADDR_ANY; my_ip_struct.sin_port = htons(my_port); setsockopt(my_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)); if (bind(my_socket, (struct sockaddr*)&my_ip_struct, sizeof(my_ip_struct)) < 0){ perror("bind"); return -1; } printf("Server IP-> %s Port-> %d \n",my_ip_raw,my_port); FD_ZERO (&read_fds); FD_SET (my_socket, &read_fds); FD_SET (0, &read_fds); fdMax=my_socket; while(1) { read_fds_copy = read_fds; select_return=select(fdMax+1, &read_fds_copy,NULL, NULL, &time_out); if (select_return== -1) { perror("select"); continue; } else if(select_return==0) { //timeout printf("Timeout! Sending updates to neighbors\n"); for (i = 0; i < num_of_servers; i++){ if(servers[i].is_neighbor==1) servers[i].num_of_skips++; } for (i = 0; i < num_of_servers; i++){ if(servers[i].is_neighbor){ if (servers[i].num_of_skips == 3) { servers[i].is_alive = 0; servers[i].cost=USHRT_MAX; servers[i].is_neighbor=0; servers[i].next_hop=-1; adj_matrix[my_id-1][servers[i].server_id-1]= USHRT_MAX; adj_matrix[servers[i].server_id-1][my_id-1]= USHRT_MAX; for(j=0;j<num_of_servers;j++){ if(servers[j].next_hop==servers[i].server_id){ servers[j].next_hop=-1; servers[j].cost=USHRT_MAX; } } //display_routes(); } } } send_update_pkt(); time_out.tv_sec=atoi(update_interval); //reset timer value continue; } for(selected = 0;selected<=fdMax;selected++) { if(FD_ISSET(selected,&read_fds_copy)) { if(selected==0){ memset(msg,0,sizeof(msg)); // clear msg array if ((numBytes = read(selected, msg, sizeof(msg))) <= 0) perror("Server read error"); else{ cmdNo=parse(msg); switch(cmdNo){ case 0: //update update_link_cost(atoi(parsedCommand[1]),atoi(parsedCommand[2]),parsedCommand[3]); printf("UPDATE: %s\n",response_message); break; case 1: //step send_update_pkt(); printf("%s SUCCESS\n",msg); break; case 2: //packets printf("Number of packets received %d\n",num_of_pkts_received); num_of_pkts_received=0; printf("%s SUCCESS\n",msg); break; case 3: //display display_routes(); printf("%s SUCCESS\n",msg); break; case 4: //disable disable(atoi(parsedCommand[1])); printf("DISABLE: %s\n",response_message); break; case 5: //crash close(my_socket); printf("%s SUCCESS\n",msg); return 1; break; } } } else if(selected==my_socket){ //receieved update packet from neighbors char recv_buf[1000]; struct sockaddr_in src_ip_struct; socklen_t src_ip_struct_len=sizeof(struct sockaddr); memset(&src_ip_struct, 0, sizeof(struct sockaddr_in)); if(recvfrom(my_socket, &recv_buf, sizeof(recv_buf),0, (struct sockaddr*)&src_ip_struct, &src_ip_struct_len)<0){ perror("recv"); } else { deserialize_pkt(recv_buf); } } } } } close(my_socket); printf("Closed socket bound to port %d \n",my_port); }
int32_t request_sysUpTime(char *error_label, char *community_name) { static int my_ip_address_initialized = False; static IPAddress my_ip_address; SNMP_pdu *request; SNMP_pdu *response; SNMP_variable *variable; static int32_t sysUpTime = 0; static clock_t last = 0; clock_t now; struct tms buffer; error_label[0] = '\0'; now = times(&buffer); if( (last == 0) || ((now - last) > 360000) ) /* 1 hour */ { if(my_ip_address_initialized == False) { if(get_my_ip_address(&my_ip_address, error_label)) { return 0; } my_ip_address_initialized = True; } if(community_name == NULL) request = request_create("public", GET_REQ_MSG, error_label); else request = request_create(community_name, GET_REQ_MSG, error_label); if(request == NULL) { return 0; } if(snmp_pdu_append_null_variable(request, &sysUptime_instance, error_label) == NULL) { snmp_pdu_free(request); return 0; } response = request_send_blocking(&my_ip_address, request, error_label); if(response == NULL) { snmp_pdu_free(request); return 0; } snmp_pdu_free(request); if(response->error_status) { sprintf(error_label, "%s", error_status_string(response->error_status)); snmp_pdu_free(response); return 0; } variable = response->first_variable; if(variable->next_variable || SSAOidCmp(&(variable->name), &sysUptime_instance) || (variable->type != TIMETICKS) || (variable->val.integer == NULL) || (variable->val_len != sizeof(int32_t)) ) { sprintf(error_label, ERR_MSG_BAD_RESPONSE); snmp_pdu_free(response); return 0; } sysUpTime = *(variable->val.integer); last = now; snmp_pdu_free(response); if(trace_level > 0) { trace("sysUpTime: %d\n\n", sysUpTime); } return sysUpTime; } /* LINTED */ return (sysUpTime + (int32_t)(now - last)); }
static void dispatcher_init() { struct sockaddr_in me; socklen_t len; /* init my_ip_address (we need it before parsing the config file) */ if(get_my_ip_address(&my_ip_address, error_label)) error_exit(ERR_MSG_MY_IP_ADDRESS, error_label); if(trace_level > 0) trace("Local IP Addresss : %s\n\n", inet_ntoa(my_ip_address)); /* init the config_dir pointer and then parse the configuration files */ if(config_dir == NULL) config_dir = default_config_dir; config_init(config_dir); /* read enterprise name-oid file */ if(name_oid_file == NULL) name_oid_file = default_name_oid_file; load_enterprise_oid(name_oid_file); /* set up the relay agent name */ if(sec_config_file == NULL) sec_config_file = default_sec_config_file; sec_config_init(sec_config_file); if(relay_agent_name == NULL) relay_agent_name = default_relay_agent_name; init_relay_agent(); /* read the resource file */ if(resource_file == NULL) resource_file = default_resource_file; if(pid_file == NULL) pid_file = default_pid_file; write_pid_file1(pid_file); res_config_init(config_dir); /* init clients_sd and agents_sd */ clients_sd = socket(AF_INET, SOCK_DGRAM, 0); if(clients_sd < 0) error_exit(ERR_MSG_SOCKET, errno_string()); memset(&me, 0, sizeof(me)); me.sin_family = AF_INET; me.sin_addr.s_addr = htonl(INADDR_ANY); me.sin_port = htons(port); if(trace_level > 0) trace("Waiting for incoming SNMP requests on UDP port %d\n\n", port); if (bind(clients_sd, (struct sockaddr *)&me, sizeof(me)) != 0) error_exit(ERR_MSG_BIND, port, errno_string()); agents_sd = socket(AF_INET, SOCK_DGRAM, 0); if (agents_sd < 0) error_exit(ERR_MSG_SOCKET, errno_string()); me.sin_family = AF_INET; me.sin_addr.s_addr = htonl(INADDR_ANY); me.sin_port = htons(0); if (bind(agents_sd, (struct sockaddr *)&me, sizeof(me)) != 0) error_exit(ERR_MSG_BIND, 0, errno_string()); trap_sd = socket(AF_INET, SOCK_DGRAM, 0); if (trap_sd < 0) error_exit(ERR_MSG_SOCKET, errno_string()); me.sin_family = AF_INET; me.sin_addr.s_addr = htonl(INADDR_ANY); me.sin_port = htons(0); if(bind(trap_sd, (struct sockaddr *)&me, sizeof(me)) != 0) error_exit(ERR_MSG_BIND, 0, errno_string()); len = (socklen_t)sizeof(me); if (getsockname(trap_sd, (struct sockaddr *)&me, &len) == -1) error_exit(ERR_MSG_BIND, 0, errno_string()); relay_agent_trap_port = ntohs(me.sin_port); write_pid_file(pid_file); }