Пример #1
0
ObjectChange *
aadlbox_add_port_callback (DiaObject *obj, Point *clicked, gpointer data)
{
  Aadlbox *aadlbox = (Aadlbox *) obj;
  Aadl_type type = *((Aadl_type *) data);
  Aadlport *port;

  port = new_port(type, "");
  aadlbox_add_port(aadlbox, clicked, port);
  aadlbox_update_data(aadlbox);

  return aadlbox_create_change(aadlbox, TYPE_ADD_POINT, clicked, port);
}
Пример #2
0
DiaObject *aadlbox_copy(DiaObject *obj)
{
  int i;
  Handle *handle1,*handle2;
  Aadlport *port;
  ConnectionPoint *connection;
  Aadlbox *aadlbox = (Aadlbox *) obj;
  void *user_data = ((Aadlbox *) obj)->specific;

  
  DiaObject *newobj = obj->type->ops->create(&obj->position,
					     user_data,
  					     &handle1,&handle2);
  object_copy_props(newobj,obj,FALSE);

  /* copy ports */
  for (i=0; i<aadlbox->num_ports; i++) {
    Point p;
    point_copy(&p, &aadlbox->ports[i]->handle->pos);
    port = new_port(aadlbox->ports[i]->type, aadlbox->ports[i]->declaration);
    
    aadlbox_add_port((Aadlbox *)newobj, &p, port);
  }

  /* copy connection points */
  for (i=0; i<aadlbox->num_connections; i++) {
    Point p;
    point_copy(&p, &aadlbox->connections[i]->pos);
    connection= g_new0(ConnectionPoint, 1);
    
    aadlbox_add_connection((Aadlbox *)newobj, &p, connection);
  }

  return newobj;

}
Пример #3
0
int
main(int argc, char *argv[])
{
    struct test_case *tc;
    FILE *input_file;
    int i;

    vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
    vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);

    if (argc != 2) {
        ovs_fatal(0, "usage: test-stp INPUT.STP\n");
    }
    file_name = argv[1];

    input_file = fopen(file_name, "r");
    if (!input_file) {
        ovs_fatal(errno, "error opening \"%s\"", file_name);
    }

    tc = new_test_case();
    for (i = 0; i < 26; i++) {
        char name[2];
        name[0] = 'a' + i;
        name[1] = '\0';
        new_lan(tc, name);
    }

    for (line_number = 1; fgets(line, sizeof line, input_file);
         line_number++)
    {
        char *newline, *hash;

        newline = strchr(line, '\n');
        if (newline) {
            *newline = '\0';
        }
        hash = strchr(line, '#');
        if (hash) {
            *hash = '\0';
        }

        pos = line;
        if (!get_token()) {
            continue;
        }
        if (match("bridge")) {
            struct bridge *bridge;
            int bridge_no, port_no;

            bridge_no = must_get_int();
            if (bridge_no < tc->n_bridges) {
                bridge = tc->bridges[bridge_no];
            } else if (bridge_no == tc->n_bridges) {
                bridge = new_bridge(tc, must_get_int());
            } else {
                err("bridges must be numbered consecutively from 0");
            }
            if (match("^")) {
                stp_set_bridge_priority(bridge->stp, must_get_int());
            }

            if (match("=")) {
                for (port_no = 0; port_no < STP_MAX_PORTS; port_no++) {
                    struct stp_port *p = stp_get_port(bridge->stp, port_no);
                    if (!token || match("X")) {
                        stp_port_disable(p);
                    } else if (match("_")) {
                        /* Nothing to do. */
                    } else {
                        struct lan *lan;
                        int path_cost;

                        if (!strcmp(token, "0")) {
                            lan = NULL;
                        } else if (strlen(token) == 1
                                && islower((unsigned char)*token)) {
                            lan = tc->lans[*token - 'a'];
                        } else {
                            err("%s is not a valid LAN name "
                                "(0 or a lowercase letter)", token);
                        }
                        get_token();

                        path_cost = match(":") ? must_get_int() : 10;
                        if (port_no < bridge->n_ports) {
                            stp_port_set_path_cost(p, path_cost);
                            stp_port_enable(p);
                            reconnect_port(bridge, port_no, lan);
                        } else if (port_no == bridge->n_ports) {
                            new_port(bridge, lan, path_cost);
                        } else {
                            err("ports must be numbered consecutively");
                        }
                        if (match("^")) {
                            stp_port_set_priority(p, must_get_int());
                        }
                    }
                }
            }
        } else if (match("run")) {
            simulate(tc, must_get_int());
        } else if (match("dump")) {
            dump(tc);
        } else if (match("tree")) {
            tree(tc);
        } else if (match("check")) {
            struct bridge *b;
            struct stp *stp;
            int bridge_no, port_no;

            bridge_no = must_get_int();
            if (bridge_no >= tc->n_bridges) {
                err("no bridge numbered %d", bridge_no);
            }
            b = tc->bridges[bridge_no];
            stp = b->stp;

            must_match("=");

            if (match("rootid")) {
                uint64_t rootid;
                must_match(":");
                rootid = must_get_int();
                if (match("^")) {
                    rootid |= (uint64_t) must_get_int() << 48;
                } else {
                    rootid |= UINT64_C(0x8000) << 48;
                }
                if (stp_get_designated_root(stp) != rootid) {
                    warn("%s: root %"PRIx64", not %"PRIx64,
                         stp_get_name(stp), stp_get_designated_root(stp),
                         rootid);
                }
            }

            if (match("root")) {
                if (stp_get_root_path_cost(stp)) {
                    warn("%s: root path cost of root is %u but should be 0",
                         stp_get_name(stp), stp_get_root_path_cost(stp));
                }
                if (!stp_is_root_bridge(stp)) {
                    warn("%s: root is %"PRIx64", not %"PRIx64,
                         stp_get_name(stp),
                         stp_get_designated_root(stp), stp_get_bridge_id(stp));
                }
                for (port_no = 0; port_no < b->n_ports; port_no++) {
                    struct stp_port *p = stp_get_port(stp, port_no);
                    enum stp_state state = stp_port_get_state(p);
                    if (!(state & (STP_DISABLED | STP_FORWARDING))) {
                        warn("%s: root port %d in state %s",
                             stp_get_name(b->stp), port_no,
                             stp_state_name(state));
                    }
                }
            } else {
                for (port_no = 0; port_no < STP_MAX_PORTS; port_no++) {
                    struct stp_port *p = stp_get_port(stp, port_no);
                    enum stp_state state;
                    if (token == NULL || match("D")) {
                        state = STP_DISABLED;
                    } else if (match("B")) {
                        state = STP_BLOCKING;
                    } else if (match("Li")) {
                        state = STP_LISTENING;
                    } else if (match("Le")) {
                        state = STP_LEARNING;
                    } else if (match("F")) {
                        state = STP_FORWARDING;
                    } else if (match("_")) {
                        continue;
                    } else {
                        err("unknown port state %s", token);
                    }
                    if (stp_port_get_state(p) != state) {
                        warn("%s port %d: state is %s but should be %s",
                             stp_get_name(stp), port_no,
                             stp_state_name(stp_port_get_state(p)),
                             stp_state_name(state));
                    }
                    if (state == STP_FORWARDING) {
                        struct stp_port *root_port = stp_get_root_port(stp);
                        if (match(":")) {
                            int root_path_cost = must_get_int();
                            if (p != root_port) {
                                warn("%s: port %d is not the root port",
                                     stp_get_name(stp), port_no);
                                if (!root_port) {
                                    warn("%s: (there is no root port)",
                                         stp_get_name(stp));
                                } else {
                                    warn("%s: (port %d is the root port)",
                                         stp_get_name(stp),
                                         stp_port_no(root_port));
                                }
                            } else if (root_path_cost
                                       != stp_get_root_path_cost(stp)) {
                                warn("%s: root path cost is %u, should be %d",
                                     stp_get_name(stp),
                                     stp_get_root_path_cost(stp),
                                     root_path_cost);
                            }
                        } else if (p == root_port) {
                            warn("%s: port %d is the root port but "
                                 "not expected to be",
                                 stp_get_name(stp), port_no);
                        }
                    }
                }
            }
            if (n_warnings) {
                exit(EXIT_FAILURE);
            }
        }
        if (get_token()) {
            err("trailing garbage on line");
        }
    }
    free(token);

    for (i = 0; i < tc->n_lans; i++) {
        struct lan *lan = tc->lans[i];
        free(CONST_CAST(char *, lan->name));
        free(lan);
    }
    for (i = 0; i < tc->n_bridges; i++) {
        struct bridge *bridge = tc->bridges[i];
        stp_destroy(bridge->stp);
        free(bridge);
    }
    free(tc);

    return 0;
}
Пример #4
0
static void
test_rstp_main(int argc, char *argv[])
{
    struct test_case *tc;
    FILE *input_file;
    int i;

    vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
    vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);

    if (argc != 2) {
        ovs_fatal(0, "usage: test-rstp INPUT.RSTP\n");
    }
    file_name = argv[1];

    input_file = fopen(file_name, "r");
    if (!input_file) {
        ovs_fatal(errno, "error opening \"%s\"", file_name);
    }

    tc = new_test_case();
    for (i = 0; i < 26; i++) {
        char name[2];

        name[0] = 'a' + i;
        name[1] = '\0';
        new_lan(tc, name);
    }

    for (line_number = 1; fgets(line, sizeof line, input_file);
         line_number++)
    {
        char *newline, *hash;

        newline = strchr(line, '\n');
        if (newline) {
            *newline = '\0';
        }
        hash = strchr(line, '#');
        if (hash) {
            *hash = '\0';
        }

        pos = line;
        if (!get_token()) {
            continue;
        }
        if (match("bridge")) {
            struct bridge *bridge;
            int bridge_no, port_no;

            bridge_no = must_get_int();
            if (bridge_no < tc->n_bridges) {
                bridge = tc->bridges[bridge_no];
            } else if (bridge_no == tc->n_bridges) {
                bridge = new_bridge(tc, must_get_int());
            } else {
                err("bridges must be numbered consecutively from 0");
            }
            if (match("^")) {
                rstp_set_bridge_priority(bridge->rstp, must_get_int());
            }
            if (match("=")) {
                for (port_no = 1; port_no < MAX_PORTS; port_no++) {
                    struct rstp_port *p = rstp_get_port(bridge->rstp, port_no);

                    if (!token || match("X")) {
                        /* Disable port. */
                        reinitialize_port(p);
                        rstp_port_set_state(p, RSTP_DISABLED);
                        rstp_port_set_mac_operational(p, false);
                    } else if (match("_")) {
                        /* Nothing to do. */
                    } else {
                        struct lan *lan;
                        uint32_t path_cost;

                        if (!strcmp(token, "0")) {
                            lan = NULL;
                        } else if (strlen(token) == 1
                                && islower((unsigned char)*token)) {
                            lan = tc->lans[*token - 'a'];
                        } else {
                            err("%s is not a valid LAN name "
                                "(0 or a lowercase letter)", token);
                        }
                        get_token();

                        path_cost = match(":") ? must_get_int() :
                                                 RSTP_DEFAULT_PORT_PATH_COST;
                        if (port_no < bridge->n_ports) {
                            /* Enable port. */
                            reinitialize_port(p);
                            rstp_port_set_path_cost(p, path_cost);
                            rstp_port_set_state(p, RSTP_DISCARDING);
                            rstp_port_set_mac_operational(p, true);
                            reconnect_port(bridge, port_no, lan);
                        } else if (port_no == bridge->n_ports) {
                            new_port(bridge, lan, path_cost);
                            bridge->n_active_ports++;
                        } else {
                            err("ports must be numbered consecutively");
                        }
                        if (match("^")) {
                            rstp_port_set_priority(p, must_get_int());
                        }
                    }
                }
            }
        } else if (match("run")) {
            simulate(tc, must_get_int());
        } else if (match("dump")) {
            dump(tc);
        } else if (match("tree")) {
            tree(tc);
        } else if (match("check")) {
            struct bridge *b;
            struct rstp *rstp;
            int bridge_no, port_no;
            uint32_t cost_value;

            bridge_no = must_get_int();
            if (bridge_no >= tc->n_bridges) {
                err("no bridge numbered %d", bridge_no);
            }
            b = tc->bridges[bridge_no];
            rstp = b->rstp;

            must_match("=");

            if (match("rootid")) {
                uint64_t rootid;

                must_match(":");
                rootid = must_get_int();
                if (match("^")) {
                    rootid |= (uint64_t) must_get_int() << 48;
                } else {
                    rootid |= UINT64_C(0x8000) << 48;
                }
                if (rstp_get_designated_root(rstp) != rootid) {
                    warn("%s: root "RSTP_ID_FMT", not %"PRIx64,
                         rstp_get_name(rstp),
                         RSTP_ID_ARGS(rstp_get_designated_root(rstp)),
                         rootid);
                }
            }
            cost_value = rstp_get_root_path_cost(rstp);
            if (match("root")) {
                if (cost_value != 0) {
                    warn("%s: root path cost of root is %d instead of 0 \n",
                         rstp_get_name(rstp), cost_value);
                }
                if (!rstp_is_root_bridge(rstp)) {
                    warn("%s: root is "RSTP_ID_FMT", not "RSTP_ID_FMT"",
                         rstp_get_name(rstp),
                         RSTP_ID_ARGS(rstp_get_designated_root(rstp)),
                         RSTP_ID_ARGS(rstp_get_bridge_id(rstp)));
                }
                for (port_no = 1; port_no < b->n_active_ports; port_no++) {
                    struct rstp_port *p = rstp_get_port(rstp, port_no);
                    enum rstp_state state = rstp_port_get_state(p);

                    if (state != RSTP_DISABLED && state != RSTP_FORWARDING) {
                        warn("%s: root port %d in state %s",
                             rstp_get_name(b->rstp), port_no,
                             rstp_state_name(state));
                    }
                }
            } else {
                for (port_no = 1; port_no < b->n_active_ports; port_no++) {
                    struct rstp_port *p = rstp_get_port(rstp, port_no);
                    enum rstp_state state;

                    if (token == NULL || match("D")) {
                        state = RSTP_DISABLED;
                    } else if (match("Di")) {
                        state = RSTP_DISCARDING;
                    } else if (match("Le")) {
                        state = RSTP_LEARNING;
                    } else if (match("F")) {
                        state = RSTP_FORWARDING;
                    } else if (match("_")) {
                        continue;
                    } else {
                        err("unknown port state %s", token);
                    }
                    if (rstp_port_get_state(p) != state) {
                        warn("%s port %d: state is %s but should be %s",
                             rstp_get_name(rstp), port_no,
                             rstp_state_name(rstp_port_get_state(p)),
                             rstp_state_name(state));
                    }
                    if (state == RSTP_FORWARDING) {
                        struct rstp_port *root_port = rstp_get_root_port(rstp);

                        if (match(":")) {
                            int root_path_cost = must_get_int();

                            if (p != root_port) {
                                warn("%s: port %d is not the root port",
                                     rstp_get_name(rstp), port_no);
                                if (!root_port) {
                                    warn("%s: (there is no root port)",
                                         rstp_get_name(rstp));
                                } else {
                                    warn("%s: (port %d is the root port)",
                                         rstp_get_name(rstp),
                                         rstp_port_get_number(root_port));
                                }
                            } else if (cost_value != root_path_cost) {
                                warn("%s: root path cost is %d, should be %d",
                                     rstp_get_name(rstp),
                                     cost_value,
                                     root_path_cost);
                            }
                        } else if (p == root_port) {
                            warn("%s: port %d is the root port but "
                                 "not expected to be",
                                 rstp_get_name(rstp), port_no);
                        }
                    }
                }
            }
            if (n_warnings) {
                printf("failing because of %d warnings\n", n_warnings);
                exit(EXIT_FAILURE);
            }
        }
        if (get_token()) {
            printf("failing because of errors\n");
            err("trailing garbage on line");
        }
    }
    free(token);
    fclose(input_file);

    for (i = 0; i < tc->n_lans; i++) {
        struct lan *lan = tc->lans[i];

        free(CONST_CAST(char *, lan->name));
        free(lan);
    }
    for (i = 0; i < tc->n_bridges; i++) {
        struct bridge *bridge = tc->bridges[i];
        int j;

        for (j = 1; j < MAX_PORTS; j++) {
            rstp_port_unref(rstp_get_port(bridge->rstp, j));
        }
        rstp_unref(bridge->rstp);
        free(bridge);
    }
    free(tc);
}
Пример #5
0
int open_server_port(QSP_ARG_DECL  const char *name,int  port_no)
{
	Port *mpp;
#ifdef HAVE_SOCKET
	socklen_t length;
#endif // HAVE_SOCKET
	int on=1;

	if ( (port_no < 2001) || (port_no > 6999) ) {
		WARN("Illegal port number");
		advise("Use 2001-6999");
		advise("Check /etc/services for other conflicts");
		return(-1);
	}

	mpp=new_port(QSP_ARG  name);
	if( mpp==NO_PORT ){
		if( verbose ){
			sprintf(ERROR_STRING,"open_server_port %s %d failed to create port struct",
				name,port_no);
			WARN(ERROR_STRING);
		}
		return(-1);
	}

#ifdef HAVE_SOCKET
	mpp->mp_o_sock=socket(AF_INET,SOCK_STREAM,0);
//fprintf(stderr,"socket returned %d (0x%x)\n",
//mpp->mp_o_sock,mpp->mp_o_sock);
#ifdef FOOBAR
	// pc-nfs???
	mpp->mp_o_sock=socket(PF_INET,SOCK_STREAM,0);
#endif /* FOOBAR */
#else // ! HAVE_SOCKET
	WARN("open_server_port:  Sorry, no socket implementation available!?");
	mpp->mp_o_sock=(-1);
#endif // HAVE_SOCKET

#ifdef BUILD_FOR_WINDOWS
	if( mpp->mp_o_sock == INVALID_SOCKET ){
		int e;
		e=WSAGetLastError();
		fprintf(stderr,"Invalid socket, error code = %d\n",e);
		switch(e){
			case WSANOTINITIALISED:
				WARN("Missing call to WSAStartup!?");
				break;
			default:
				WARN("Unclassified error.");
				break;
		}
	}
#endif // BUILD_FOR_WINDOWS

	// sleeptime used to be signed, but not now.
	//mpp->mp_sleeptime=(-1);
	mpp->mp_sleeptime=0;
	mpp->mp_sock=(-1);
		
	if( mpp->mp_o_sock<0 ){
		tell_sys_error("open_server_port (socket)");
		WARN("error opening stream socket");
		delport(QSP_ARG  mpp);
		return(-1);
	}

#ifdef HAVE_SOCKET
fprintf(stderr,"Setting socket options...\n");
	setsockopt(mpp->mp_o_sock,SOL_SOCKET,SO_REUSEADDR,
		(char *)&on,sizeof(on));

	mpp->mp_addrp =
		(struct sockaddr_in *) getbuf( sizeof(*(mpp->mp_addrp) ) );
	if( mpp->mp_addrp == NULL ) mem_err("open_server_port");

	mpp->mp_flags = 0;
	mpp->mp_flags |= PORT_SERVER;	// makes it keep trying!
	mpp->mp_pp = NO_PORT;
	mpp->mp_text_var_name=NULL;
	mpp->mp_output_filename=NULL;
	mpp->mp_auth_string=NULL;
	mpp->mp_portnum = port_no;

	mpp->mp_addrp->sin_family = AF_INET;
	mpp->mp_addrp->sin_addr.s_addr = INADDR_ANY;
	length=sizeof(*(mpp->mp_addrp));
	mpp->mp_addrp->sin_port = htons( port_no );
fprintf(stderr,"Binding socket...\n");
	if( bind(mpp->mp_o_sock,(struct sockaddr *)(mpp->mp_addrp), length) ){
		tell_sys_error("bind");
		WARN("open_server_port:  couldn't bind to port");
		goto cleanup;
	}

fprintf(stderr,"Getting socket name...\n");
	if( getsockname(mpp->mp_o_sock,
		(struct sockaddr *)mpp->mp_addrp,&length) ){
		WARN("open_server_port:  error getting socket name");
		goto cleanup;
	}
	/* We used to make sure that the port number
	 * was the one we requested, but we no longer
	 * insist upon that...
	 */

	if( port_listen(QSP_ARG  mpp) < 0 )
		goto cleanup;

fprintf(stderr,"Back from port_listen...\n");
#endif // HAVE_SOCKET

	return(port_no);

cleanup:
	givbuf((char *)mpp->mp_addrp);
	mpp->mp_addrp = NULL;
	delport(QSP_ARG  mpp);
	return(-1);
} // open_server_port