Example #1
0
void goto_dialog::address_entered()
{
	bool status;
	QString input = offset_input->text().remove(QRegExp("[^0-9A-Fa-f]"));
	int address = input.toInt(&status, 16);
	if(input.isEmpty()){
		return;
	}
	if(relative->isChecked()){
		address = active_editor->get_buffer()->pc_to_snes(active_editor->get_relative_position(address));
	}
	if(validate_address(address)){
		emit triggered(address);
	}
}
Example #2
0
/* 	Increase refcount */
seg_t mm_realloc(seg_t base)
{
    register struct malloc_hole *m;

#ifdef CONFIG_SWAP

    base = validate_address(base);

#endif

    m = find_hole(&memmap, base);
    m->refcount++;

    return m->page_base;
}
Example #3
0
static gboolean
validate_configuration (GVariant *configuration,
                        GError **error)
{
  GVariantDict *dict = NULL;
  GVariant *value = NULL;

  dict = g_variant_dict_new (configuration);

  if (g_variant_dict_contains (dict, "address"))
    {
      value = g_variant_dict_lookup_value (dict, "address",
                                           G_VARIANT_TYPE_STRING);
      if (value == NULL)
        {
          g_variant_dict_unref (dict);
          *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                                _("'address' entry must be a string"));
          return FALSE;
        }
      if (!validate_address ("address", g_variant_get_string (value, NULL),
                             TRUE, error))
        {
          g_variant_dict_unref (dict);
          return FALSE;
        }
    }
  else
    {
      g_variant_dict_unref (dict);
      *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                            _("'address' entry is required"));
      return FALSE;
    }

  if (g_variant_dict_contains (dict, "router"))
    {
      value = g_variant_dict_lookup_value (dict, "router",
                                           G_VARIANT_TYPE_STRING);
      if (value == NULL)
        {
          g_variant_dict_unref (dict);
          *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                                _("'router' entry must be a string"));
          return FALSE;
        }
      if (!validate_address ("router", g_variant_get_string (value, NULL),
                             FALSE, error))
        {
          g_variant_dict_unref (dict);
          return FALSE;
        }
    }

  if (g_variant_dict_contains (dict, "nameservers"))
    {
      value = g_variant_dict_lookup_value (dict, "nameservers",
                                           G_VARIANT_TYPE_STRING_ARRAY);
      if (value == NULL)
        {
          g_variant_dict_unref (dict);
          *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                            _("'nameservers' entry must be a string array"));
          return FALSE;
        }
      gsize len;
      gs_strfreev gchar **nameservers = g_variant_dup_strv (value, &len);
      for (gsize i = 0; i < len; i++)
        {
          if (!validate_address ("nameservers", nameservers[i],
                                 FALSE, error))
            {
              g_variant_dict_unref (dict);
              return FALSE;
            }
        }
    }

  if (g_variant_dict_contains (dict, "domain"))
    {
      value = g_variant_dict_lookup_value (dict, "domain",
                                           G_VARIANT_TYPE_STRING);
      if (value == NULL)
        {
          g_variant_dict_unref (dict);
          *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                                _("'domain' entry must be a string"));
          return FALSE;
        }
      if (!validate_domainname ("domain", g_variant_get_string (value, NULL),
                                error))
        {
          g_variant_dict_unref (dict);
          return FALSE;
        }
    }

  if (g_variant_dict_contains (dict, "searches"))
    {
      value = g_variant_dict_lookup_value (dict, "searches",
                                           G_VARIANT_TYPE_STRING_ARRAY);
      if (value == NULL)
        {
          g_variant_dict_unref (dict);
          *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                               _("'searches' entry must be a string array"));

          return FALSE;
        }
      gsize len;
      gs_strfreev gchar **searches = g_variant_dup_strv (value, &len);
      for (gsize i = 0; i < len; i++)
        {
          if (!validate_domainname ("searches", searches[i], error))
            {
              g_variant_dict_unref (dict);
              return FALSE;
            }
        }
    }

  g_variant_dict_unref (dict);

  return TRUE;
}
Example #4
0
void sg_xmlcfg::load_netents(const bpt::iptree& pt)
{
	gpenv& env_mgr = gpenv::instance();

	try {
		BOOST_FOREACH(const bpt::iptree::value_type& v, pt.get_child("servicegalaxy.networks")) {
			if (v.first != "network")
				continue;

			const bpt::iptree& v_pt = v.second;
			sg_netent_t item;
			memset(&item, '\0', sizeof(sg_netent_t));

			string hostid = v_pt.get<string>("hostid");
			if (hostid.length() > MAX_IDENT)
				throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.hostid is too long.")).str(SGLOCALE));
			strcpy(item.lmid, hostid.c_str());

			string gwsaddr;
			env_mgr.expand_var(gwsaddr, get_value(v_pt, "gwsaddr", string("")));
			if (gwsaddr.length() > MAX_STRING)
				throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.gwsaddr is too long.")).str(SGLOCALE));
			validate_address("networks.network.gwsaddr", gwsaddr);
			strcpy(item.naddr, gwsaddr.c_str());

			string agtaddr;
			env_mgr.expand_var(agtaddr, get_value(v_pt, "agtaddr", string("")));
			if (agtaddr.length() > MAX_STRING)
				throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.agtaddr is too long.")).str(SGLOCALE));
			validate_address("networks.network.agtaddr", agtaddr);
			strcpy(item.nlsaddr, agtaddr.c_str());

			string netname = get_value(v_pt, "netname", string(""));
			if (netname.empty())
				netname = NM_DEFAULTNET;
			if (netname.length() > MAX_IDENT)
				throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.netname is too long.")).str(SGLOCALE));
			strcpy(item.netgroup, netname.c_str());

			string tcpkeepalive = get_value(v_pt, "tcpkeepalive", string("Y"));
			if (strcasecmp(tcpkeepalive.c_str(), "Y") == 0)
				item.tcpkeepalive = true;
			else if (strcasecmp(tcpkeepalive.c_str(), "N") == 0)
				item.tcpkeepalive = false;
			else
				throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.tcpkeepalive value invalid.")).str(SGLOCALE));

			string nodelay = get_value(v_pt, "nodelay", string("N"));
			if (strcasecmp(nodelay.c_str(), "Y") == 0)
				item.nodelay = true;
			else if (strcasecmp(nodelay.c_str(), "N") == 0)
				item.nodelay = false;
			else
				throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.nodelay value invalid.")).str(SGLOCALE));

			BOOST_FOREACH(const sg_netent_t& netent, netents) {
				if (hostid == netent.lmid && netname == netent.netgroup)
					throw sg_exception(__FILE__, __LINE__, SGEINVAL, 0, (_("ERROR: networks.network.netgroup must be unique on same node.")).str(SGLOCALE));
			}

			netents.push_back(item);
		}
	} catch (bpt::ptree_bad_data& ex) {
		throw sg_exception(__FILE__, __LINE__, SGESYSTEM, 0, (_("ERROR: Parse section networks failure, {1} ({2})") % ex.what() % ex.data<string>()).str(SGLOCALE));
	} catch (bpt::ptree_error& ex) {
		throw sg_exception(__FILE__, __LINE__, SGESYSTEM, 0, (_("ERROR: Parse section networks failure, {1}") % ex.what()).str(SGLOCALE));
	}
}
Example #5
0
int main(int argc, char *argv[]) {

    int read_pipe[2];
    int write_pipe[2];

    if (argc != 3) {
        printf("error: missing argument(s)\n");
        exit(1);
    }
    char *filename = argv[1];

    // make pipe to communicate
    if (pipe(read_pipe) < 0 || pipe(write_pipe) < 0) {
        printf("Pipe failed!\n");
        exit(1);
    }

    int pid = fork();
    if (pid == -1) {
        /* Here pid is -1, the fork failed */
        /* Some possible reasons are that you're */
        /* out of process slots or virtual memory */
        perror("The fork failed!\n");
        exit(2);
    }
    if (pid == 0) {
        /********** Memory **********/

        // close parent's pipes
        close(read_pipe[0]);
        close(write_pipe[1]);

        // initialize memory from file
        int memory[MEMORY_SIZE];
        int initialize_error = initialize(memory, filename);
        write(MEMORY_WRITE, &initialize_error, sizeof(int));
        if (initialize_error == 1) _exit(0);

        // read from parent
        while (1) {
            int address = -1;
            int command = -2;

            if (read(MEMORY_READ, &command, sizeof(int)) >= 0) {
                if (command == CRASH_COMMAND) {
                    // crash signal from the parent process
                    _exit(1);
                } else if (command == READ_COMMAND) {
                    // returns the value at address
                    read(MEMORY_READ, &address, sizeof(int));
                    write(MEMORY_WRITE, &memory[address], sizeof(int));
                } else if (command == WRITE_COMMAND) {
                    // writes a value to address
                    int data = -1;
                    read(MEMORY_READ, &address, sizeof(int));
                    read(MEMORY_READ, &data, sizeof(int));
                    memory[address] = data;
                }
            }
        } // END while
        _exit(0);
    }

    /********** CPU **********/

    // close child's pipes
    close(write_pipe[0]);
    close(read_pipe[1]);

    // check whether child process fails to initialize
    int initialize_error = -1;
    while (initialize_error == -1) {
        read(CPU_READ, &initialize_error, sizeof(int));
        if (initialize_error == 1) {
            printf("error: input file does not exist!\n");
            waitpid(-1, NULL, 0);
            exit(1);
        }
    }

    int TIMEOUT = -1;
    int timer = 0;
    int is_timer_interrupt = false;
    int interrupted = false;

    // initialize CPU's variables
    int program_counter = 0;
    int stack_pointer = SYSTEM_CODE_START;
    int instruction_register = 0;
    int accumulator = 0;
    int x = 0;
    int y = 0;
    int mode = USER_MODE;

    sscanf(argv[2], "%d", &TIMEOUT);
    if (TIMEOUT < 5) {
        printf("timeout too short!\n");
        goto ERROR_EXIT;
    }

    int address = -1;
    int flag = -2;
    while (1) {
        /* Fetch new instruction */
        write(CPU_WRITE, &READ_COMMAND, sizeof(int));
        write(CPU_WRITE, &program_counter, sizeof(int));
        program_counter++;
        if (!interrupted) timer ++;

        // read the returned data/instruction from memory
        // then execute
        if (read(CPU_READ, &instruction_register, sizeof(int)) >= 0) {
            switch (instruction_register) {
                case 1:
                    /* Loads the value into AC */
                    // fetch the value from the memory
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;

                    // reads the value to the AC
                    read(CPU_READ, &accumulator, sizeof(int));
                    if (!interrupted) timer++;
                    break;

                case 2:
                    /* Loads the value in address into AC */
                    address = -1;
                    // fetch the address from memory
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }
                    // read the value at address to the AC
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &address, sizeof(int));
                    read(CPU_READ, &accumulator, sizeof(int));
                    if (!interrupted) timer++;
                    break;

                case 3:
                    /* Loads the value from the address found in the address into the AC */
                    address = -1;
                    // fetch the address from the memory
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }
                    // read the address at address
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &address, sizeof(int));
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }
                    // read the value at adress to the AC
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &address, sizeof(int));
                    read(CPU_READ, &accumulator, sizeof(int));
                    if (!interrupted) timer++;
                    break;

                case 4: case 5:
                    /* Loads the value at address (address + X) or (address + Y) into the AC */
                    address = -1;
                    // read the address
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // read the value at (address + X) or (address + Y) into the AC
                    if (instruction_register == 4) {
                        address += x;
                    } else if (instruction_register == 5) {
                        address += y;
                    }

                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &address, sizeof(int));
                    read(CPU_READ, &accumulator, sizeof(int));
                    if (!interrupted) timer++;
                    break;

                case 6:
                    /* Loads from (stack_pointer + X) into the AC */
                    address = stack_pointer + x;
                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &address, sizeof(int));
                    read(CPU_READ, &accumulator, sizeof(int));
                    if (!interrupted) timer++;
                    break;

                case 7:
                    /* Stores the value in AC to the address */
                    address = -1;
                    // fetch the address from memory
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }

                    // writes the AC to the address
                    write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
                    write(CPU_WRITE, &address, sizeof(int));
                    write(CPU_WRITE, &accumulator, sizeof(int));
                    break;

                case 8:
                    // gets a random integer from 1 to 100 into the AC
                    srand(time(NULL));
                    accumulator = rand() % 99 + 1;
                    break;

                case 9: ;
                    /* Writes AC as an int or char into the screen */
                    // fetch port number from the memory
                    int port = -1;
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &port, sizeof(int));
                    if (!interrupted) timer++;

                    if (port == 1) {
                        // writes as an integer
                        printf("%d", accumulator);
                    } else if (port == 2) {
                        // writes as a character
                        write(1, &accumulator, sizeof(int));
                    } else {
                        printf("error: print to invalid port!\n");
                        goto ERROR_EXIT;
                    }
                    break;

                case 10:
                    // add the value from X to AC
                    accumulator += x;
                    break;

                case 11:
                    // add the value from Y to AC
                    accumulator += y;
                    break;

                case 12:
                    // subtract the value from X from AC
                    accumulator -= x;
                    break;

                case 13:
                    // subtract the value from Y from AC
                    accumulator -= y;
                    break;

                case 14:
                    // copy the value from AC to X
                    x = accumulator;
                    break;

                case 15:
                    // copy the value from X to AC
                    accumulator = x;
                    break;

                case 16:
                    // copy the value from AC to Y
                    y = accumulator;
                    break;

                case 17:
                    // copy the value from Y to AC
                    accumulator = y;
                    break;

                case 18:
                    // copy the value from AC to SP
                    stack_pointer = accumulator;
                    break;

                case 19:
                    // copy the value from SP to AC
                    accumulator = stack_pointer;
                    break;

                case 20:
                    /* Jumps to the address */
                    // fetch the address
                    address = -1;
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // jump
                    JUMP:
                    // check validity of the address
                    flag = validate_address(mode, address);
                    if (flag != 0) {
                        if (flag == 1) {
                            // address does not exist!
                            printf("error: address does not exist!\n");
                        } else if (flag == 2) {
                            // invalid write operation!
                            printf("error: permission denied! Cannot access system code in user mode\n");
                        }
                        goto ERROR_EXIT;
                    }
                    program_counter = address;  // jump
                    break;

                case 21: case 22:
                    /*  case 21: Jumps to the address only if the value in the AC is zero 
                        case 22: Jumps if the value in the AC is not zero */
                    // fetch the address from memory
                    address = -1;
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    if (instruction_register == 21) {
                        if (accumulator == 0) {
                            goto JUMP;
                        }
                    } else if (instruction_register == 22) {
                        if (accumulator != 0) {
                            goto JUMP;
                        }
                    }
                    break;

                case 23:
                    /* Push return address (i.e. program counter) onto stack, jump to the address */

                    // read address
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));
                    program_counter++;
                    read(CPU_READ, &address, sizeof(int));
                    if (!interrupted) timer++;

                    // push return address to user's stack
                    stack_pointer --;
                    write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
                    write(CPU_WRITE, &stack_pointer, sizeof(int));
                    write(CPU_WRITE, &program_counter, sizeof(int));

                    goto JUMP;
                    break;

                case 24:
                    /* Pop return address from the stack, jump to the address */
                    if (stack_pointer == SYSTEM_CODE_START || stack_pointer == MEMORY_SIZE) {
                        // nothing to pop
                        printf("error: invalid stack-popping operation!\n");
                        goto ERROR_EXIT;
                    }
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &stack_pointer, sizeof(int));
                    read(CPU_READ, &address, sizeof(int));
                    stack_pointer++;
                    if (!interrupted) timer++;

                    goto JUMP;
                    break;

                case 25:
                    // Increment the value in X
                    x ++;
                    break;

                case 26:
                    // Increment the value in Y
                    x --;
                    break;

                case 27:
                    // Push AC onto stack
                    stack_pointer --;
                    write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
                    write(CPU_WRITE, &stack_pointer, sizeof(int));
                    write(CPU_WRITE, &accumulator, sizeof(int));
                    break;

                case 28:
                    // Pop from stack into AC
                    if (stack_pointer == SYSTEM_CODE_START || stack_pointer == MEMORY_SIZE) {
                        // nothing to pop
                        printf("error: invalid stack-popping operation!\n");
                        goto ERROR_EXIT;
                    }
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &stack_pointer, sizeof(int));
                    read(CPU_READ, &accumulator, sizeof(int));
                    stack_pointer ++;
                    if (!interrupted) timer++;
                    break;

                case 29:
                    /* System call: interrupt */
                    // Set system mode, switch stack, push stack pointer and program counter
                    // Set new stack pointer and program counter
                    if (!interrupted) {
                        interrupted = true;
                        mode = SYSTEM_MODE;

                        // push stack pointer to system stack
                        address = MEMORY_SIZE - 1;
                        write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
                        write(CPU_WRITE, &address, sizeof(int));
                        write(CPU_WRITE, &stack_pointer, sizeof(int));
                        stack_pointer = MEMORY_SIZE - 1;    // switch stack

                        // push program counter
                        stack_pointer --;
                        write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
                        write(CPU_WRITE, &stack_pointer, sizeof(int));
                        write(CPU_WRITE, &program_counter, sizeof(int));
                        program_counter = INT_INTERRUPT_START;
                    }
                    break;

                case 30:
                    /* Restores registers, set user mode */
                    if (mode == USER_MODE) {
                        // invalid instruction
                        printf("error: call instruction 30 in user mode!\n");
                        goto ERROR_EXIT;
                    }

                    /* SYSTEM MODE */
                    // pop program counter
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &stack_pointer, sizeof(int));
                    read(CPU_READ, &program_counter, sizeof(int));
                    stack_pointer ++;

                    // pop user's stack pointer
                    write(CPU_WRITE, &READ_COMMAND, sizeof(int));
                    write(CPU_WRITE, &stack_pointer, sizeof(int));
                    read(CPU_READ, &stack_pointer, sizeof(int));

                    // get out of the interrupt handler
                    interrupted = false;
                    is_timer_interrupt = false;
                    mode = USER_MODE;
                    break;

                case 50:
                    // end of program
                    goto NORMAL_EXIT;

                default:
                    /* Invalid instruction */
                    printf("error: invalid instruction at %d\n", (--program_counter));
                    goto ERROR_EXIT;
            }
        }
        if (timer >= TIMEOUT)  {
            // cause interrupt
            timer = 0;
            interrupted = true;
            is_timer_interrupt = true;

            mode = SYSTEM_MODE;

            // push stack pointer to system stack
            address = MEMORY_SIZE - 1;
            write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
            write(CPU_WRITE, &address, sizeof(int));
            write(CPU_WRITE, &stack_pointer, sizeof(int));
            stack_pointer = MEMORY_SIZE - 1;    // switch stack

            // push program counter
            stack_pointer --;
            write(CPU_WRITE, &WRITE_COMMAND, sizeof(int));
            write(CPU_WRITE, &stack_pointer, sizeof(int));
            write(CPU_WRITE, &program_counter, sizeof(int));
            program_counter = TIMER_INTERRUPT_START;
        }
    } // END while

    NORMAL_EXIT:
    // wait for child process to end
    write(CPU_WRITE, &CRASH_COMMAND, sizeof(int));
    waitpid(-1, NULL, 0);
    return 0;

    ERROR_EXIT:
    write(CPU_WRITE, &CRASH_COMMAND, sizeof(int));
    waitpid(-1, NULL, 0);
    exit(1);
}
Example #6
0
int main(int argc, char **argv) {
   char c, *id = NULL, *spoof_addr = NULL;

   opterr = 0;

   while ((c = getopt(argc, argv, "hVi:m:")) != -1) {
      switch(c) {
         case 'h':
            help_message();
            return 0;
         case 'V':
            printf("%s\n", version);
            return 0;
         case 'i':
            if (validate_interface(optarg)) {
               id = calloc(1, strlen(optarg)+1);
               strcpy(id, optarg);
            }
            else {
               fprintf(stderr, "Invalid interface specified after -i.\n");
               return 1;
            }
            break;
         case 'm':
            if (validate_address(optarg)) {
               spoof_addr = calloc(1, strlen(optarg)+1);
               strcpy(spoof_addr, optarg);
            }
            else {
               fprintf(stderr, "Invalid address specified after -m.\n");
               return 1;
            }
            break;
         case '?':
            if (optopt == 'i' || optopt == 'm') {
               fprintf(stderr, "Option -%c requires an argument.\n", optopt);
               return 1;
            }
            else if (isprint(optopt)) {
               fprintf(stderr, "Unknown option -%c.\n", optopt);
               return 1;
            }
            else {
               fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
               return 1;
            }
         default:
            abort(); /* if we get here, bad things have happened */
      }
   }
   if (geteuid() != 0) {
      fprintf(stderr, "This tools needs to be run as root.\n");
      return 1;
   }
   if (!id) { 
      fprintf(stderr, "No interface supplied.\n");
      return 1;
   }
   if (!spoof_addr) { 
      spoof_addr = generate_mac();
      spoof_addr[ADDR_BYTES-1] = '\0';
   }
   spoof_interface_mac(id, spoof_addr);
   sleep(2);
   bounce_interface(id);
   sleep(1);
   return 0;
}
gboolean
mm_mbm_parse_e2ipcfg_response (const gchar *response,
                               MMBearerIpConfig **out_ip4_config,
                               MMBearerIpConfig **out_ip6_config,
                               GError **error)
{
    MMBearerIpConfig **ip_config = NULL;
    gboolean got_address = FALSE, got_gw = FALSE, got_dns = FALSE;
    GRegex *r;
    GMatchInfo *match_info = NULL;
    GError *match_error = NULL;
    gchar *dns[3] = { 0 };
    guint dns_idx = 0;
    int family = AF_INET;
    MMBearerIpMethod method = MM_BEARER_IP_METHOD_STATIC;

    g_return_val_if_fail (out_ip4_config, FALSE);
    g_return_val_if_fail (out_ip6_config, FALSE);

    if (!response || !g_str_has_prefix (response, E2IPCFG_TAG)) {
        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Missing " E2IPCFG_TAG " prefix");
        return FALSE;
    }

    response = mm_strip_tag (response, "*E2IPCFG: ");

    if (strchr (response, ':')) {
        family = AF_INET6;
        ip_config = out_ip6_config;
        method = MM_BEARER_IP_METHOD_DHCP;
    } else if (strchr (response, '.')) {
        family = AF_INET;
        ip_config = out_ip4_config;
        method = MM_BEARER_IP_METHOD_STATIC;
    } else {
        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
                     "Failed to detect " E2IPCFG_TAG " address family");
        return FALSE;
    }

    /* *E2IPCFG: (1,<IP>)(2,<gateway>)(3,<DNS>)(3,<DNS>)
     *
     * *E2IPCFG: (1,"46.157.32.246")(2,"46.157.32.243")(3,"193.213.112.4")(3,"130.67.15.198")
     * *E2IPCFG: (1,"fe80:0000:0000:0000:0000:0000:e537:1801")(3,"2001:4600:0004:0fff:0000:0000:0000:0054")(3,"2001:4600:0004:1fff:0000:0000:0000:0054")
     * *E2IPCFG: (1,"fe80:0000:0000:0000:0000:0027:b7fe:9401")(3,"fd00:976a:0000:0000:0000:0000:0000:0009")
     */
    r = g_regex_new ("\\((\\d),\"([0-9a-fA-F.:]+)\"\\)", 0, 0, NULL);
    g_assert (r != NULL);

    if (!g_regex_match_full (r, response, -1, 0, 0, &match_info, &match_error)) {
        if (match_error) {
            g_propagate_error (error, match_error);
            g_prefix_error (error, "Could not parse " E2IPCFG_TAG " results: ");
        } else {
            g_set_error_literal (error,
                                 MM_CORE_ERROR,
                                 MM_CORE_ERROR_FAILED,
                                 "Couldn't match " E2IPCFG_TAG " reply");
        }
        goto done;
    }

    *ip_config = mm_bearer_ip_config_new ();
    mm_bearer_ip_config_set_method (*ip_config, method);
    while (g_match_info_matches (match_info)) {
        char *id = g_match_info_fetch (match_info, 1);
        char *str = g_match_info_fetch (match_info, 2);

        switch (atoi (id)) {
        case 1:
            if (validate_address (family, str)) {
                mm_bearer_ip_config_set_address (*ip_config, str);
                mm_bearer_ip_config_set_prefix (*ip_config, (family == AF_INET6) ? 64 : 28);
                got_address = TRUE;
            }
            break;
        case 2:
            if ((family == AF_INET) && validate_address (family, str)) {
                mm_bearer_ip_config_set_gateway (*ip_config, str);
                got_gw = TRUE;
            }
            break;
        case 3:
            if (validate_address (family, str)) {
                dns[dns_idx++] = g_strdup (str);
                got_dns = TRUE;
            }
            break;
        default:
            break;
        }
        g_free (id);
        g_free (str);
        g_match_info_next (match_info, NULL);
    }

    if (got_dns) {
        mm_bearer_ip_config_set_dns (*ip_config, (const gchar **) dns);
        g_free (dns[0]);
        g_free (dns[1]);
    }

    if (!got_address || (family == AF_INET && !got_gw)) {
        g_object_unref (*ip_config);
        *ip_config = NULL;
        g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
                             "Got incomplete IP configuration from " E2IPCFG_TAG);
    }

done:
    if (match_info)
        g_match_info_free (match_info);
    g_regex_unref (r);
    return !!*ip_config;
}