Ejemplo n.º 1
0
void test1() {
	RESIZABLE_TABLE *rt;
	char * address;
	int result;

	rt = rtable_create();

	rtable_print_str(rt);

	printf("Add George\n");
	result = rtable_add_str(rt, "George", "23 Oak St, West Lafayette, 47906");

	rtable_print_str(rt);

	printf("Add Peter\n");
	result = rtable_add_str(rt, "Peter", "27 Oak St, West Lafayette, 47906");
	assert(result==0);

	rtable_print_str(rt);

	address = rtable_lookup(rt, "Peter");
	printf("Peter's address is: %s\n", address);

	address = rtable_lookup(rt, "George");
	printf("George's address is: %s\n", address);
}
Ejemplo n.º 2
0
void test14() {
	RESIZABLE_TABLE *rt;
	long grade;
	int result;

	rt = rtable_create();

	rtable_print_int(rt);

	printf("Add George\n");
	result = rtable_add_int(rt, "George", 89);

	rtable_print_int(rt);

	printf("Add Peter\n");
	result = rtable_add_int(rt, "Peter", 97);
	assert(result==0);

	rtable_print_int(rt);

	printf("Add George again\n");
	result = rtable_add_int(rt, "George", 91);

	rtable_print_int(rt);

	grade =(long)  rtable_lookup(rt, "Peter");
	printf("Peter's grade is: %ld\n", grade);

	grade = (long) rtable_lookup(rt, "George");
	printf("George's grade is: %ld\n", grade);
}
Ejemplo n.º 3
0
/*
 * Insert a route from a string containing a destination: "192.168.1/24"
 */
void
route_insert(unsigned int rid, sa_family_t af, char *string)
{
	struct sockaddr_storage	 ss, ms;
	struct sockaddr		*ndst, *dst = (struct sockaddr *)&ss;
	struct sockaddr		*mask = (struct sockaddr *)&ms;
	struct rtentry		*rt, *nrt;
	char			 ip[INET6_ADDRSTRLEN];
	int			 plen, error;

	rt = calloc(1, sizeof(*rt));
	if (rt == NULL)
		errx(1, "out of memory");

	plen = inet_net_ptosa(af, string, dst, mask);
	if (plen == -1)
		err(1, "wrong line: %s", string);

	/* Normalize sockaddr a la rtrequest1(9) */
	ndst = malloc(dst->sa_len);
	if (ndst == NULL)
		errx(1, "out of memory");
	rt_maskedcopy(dst, ndst, mask);

	if ((error = rtable_insert(rid, ndst, mask, NULL, 0, rt)) != 0) {
		inet_net_satop(af, rt_key(rt), plen, ip, sizeof(ip));
		errx(1, "can't add route: %s, %s\n", ip, strerror(error));
	}
	nrt = rtable_lookup(rid, dst, mask, NULL, RTP_ANY);
	if (nrt != rt) {
		inet_net_satop(af, rt_key(rt), plen, ip, sizeof(ip));
		errx(1, "added route not found: %s\n", ip);
	}
}
Ejemplo n.º 4
0
void addWordsTable(FILE * f, RESIZABLE_TABLE * rt) {
    char c;
    char * charBuf = (char *)malloc(sizeof(char) * 2);
    char * buf = (char *)malloc(MAXLINE);
    void * val;
    long num;
    while ((c = fgetc(f)) != EOF) {
        //charBuf[0] = c;
        charBuf[1] = '\0';
        if ((c > 64) && (c < 91)) {
            charBuf[0] = c + 32;
            strcat(buf, charBuf);
        }
        else if ((c > 96) && (c < 123)) {
            charBuf[0] = c;
            strcat(buf, charBuf);
        }
        else {// ((c == 32) || (c == 10)) {
            if (strcmp(buf, "") == 0) {
                continue;
            }
            else {
                val = rtable_lookup(rt, buf);
                if (val == NULL) {
                    rtable_add_int(rt, buf, 1);
                } else {
                    num = (long)val;
                    num++;
                    rtable_add_int(rt, buf, num);
                }
            }
            memset(buf, 0, strlen(buf));
        }
    }
}
Ejemplo n.º 5
0
/*
 * Delete a route from a string containing a destination: "192.168.1/24"
 */
void
route_delete(unsigned int rid, sa_family_t af, char *string)
{
	struct sockaddr_storage	 ss, ms;
	struct sockaddr		*dst = (struct sockaddr *)&ss;
	struct sockaddr		*mask = (struct sockaddr *)&ms;
	struct rtentry		*rt, *nrt;
	char			 ip[INET6_ADDRSTRLEN];
	int			 plen, error;

	plen = inet_net_ptosa(af, string, dst, mask);
	if (plen == -1)
		err(1, "wrong line: %s", string);

	rt = rtable_lookup(0, dst, mask, NULL, RTP_ANY);
	if (rt == NULL) {
		inet_net_satop(af, dst, plen, ip, sizeof(ip));
		errx(1, "can't find route: %s\n", ip);
	}

	assert(memcmp(rt_key(rt), dst, dst->sa_len) == 0);
	assert(rt_plen(rt) == rtable_satoplen(af, mask));

	if ((error = rtable_delete(0, dst, mask, rt)) != 0) {
		inet_net_satop(af, dst, plen, ip, sizeof(ip));
		errx(1, "can't rm route: %s, %s\n", ip, strerror(error));
	}

	nrt = rtable_lookup(0, dst, mask, NULL, RTP_ANY);
	if (nrt != NULL) {
		char ip0[INET6_ADDRSTRLEN];
		inet_net_satop(af, rt_key(nrt), plen, ip, sizeof(ip));
		inet_net_satop(af, rt_key(rt), plen, ip0, sizeof(ip0));
		errx(1, "found: %s after deleting: %s", ip, ip0);
	}

	free(rt_key(rt));
	free(rt);
}
Ejemplo n.º 6
0
/*
 * Lookup a route from a string containing a destination: "192.168.1/24"
 */
void
route_lookup(unsigned int rid, sa_family_t af, char *string)
{
	struct sockaddr_storage	 ss, ms;
	struct sockaddr		*dst = (struct sockaddr *)&ss;
	struct sockaddr		*mask = (struct sockaddr *)&ms;
	struct rtentry		*rt;
	char			 ip[INET6_ADDRSTRLEN];
	int			 plen;

	plen = inet_net_ptosa(af, string, dst, mask);
	if (plen == -1)
		err(1, "wrong line: %s", string);

	rt = rtable_lookup(0, dst, mask, NULL, RTP_ANY);
	if (rt == NULL) {
		inet_net_satop(af, dst, plen, ip, sizeof(ip));
		errx(1, "%s not found\n", ip);
	}
	assert(memcmp(rt_key(rt), dst, dst->sa_len) == 0);
	assert(rt_plen(rt) == rtable_satoplen(af, mask));
}