Exemple #1
0
int main()
{
	entry **t = malloc(sizeof(entry *)*100010);
	char *s = malloc(sizeof(char)*128);
	freopen("big_file.txt","r",stdin);
	freopen("big_file_sorted.txt","w",stdout);
	unsigned int cnt = 0;
	unsigned a, b, c, d;
	while (scanf("%s %u.%u.%u.%u",s, &a, &b, &c, &d)!=EOF)
	{
		entry *temp =(entry *) malloc(sizeof(entry));
		temp->host = s;
		temp->ip = ip2int(a, b, c, d);
		t[cnt++] = temp;
		s = malloc(sizeof(char)*128);
	}
	qsort(t, cnt, sizeof(entry*),cmp);
	int i;
	printf("Comparari %lld %lld\n", comparari, comparari2);
	for (i = 0; i < cnt; ++ i)
		printf("%s %u ", t[i]->host, t[i]->ip), int2ip(t[i]->ip);
	return 0;
}
Exemple #2
0
int read_cb(struct wx_conn_s* wx_conn, struct wx_buf_s* buf, ssize_t n) {
    struct conn_s* conn = container_of(wx_conn, struct conn_s, wx_conn);

    if (n == 0 && buf->size!=0) {
        conn_close(conn);
        return 0;
    }

    wx_timer_stop(&conn->closetimer);

    char* rnrnpos = strstr(buf->data, "\r\n\r\n");
    if (rnrnpos) {
        char resbody[128]={0};

        uint16_t sid;
        if (1 == sscanf(buf->data, "GET /get/%hu HTTP/", &sid)) {
            uint32_t ip;
            uint16_t port;
            if (0 == l5_sid_route_ipport(sid, &ip, &port)) {
                char sip[16]={0};
                int2ip(ip, sip);
                sprintf(resbody, "%s:%d", sip, port);
            } else {
                sprintf(resbody, "0.0.0.0:0");
            }
        } else {
            sprintf(resbody, "bad request");
        }

        if (strstr(buf->data, "HTTP/1.1\r\n")) {
            conn->keepalivems = 15000;
        }

        conn->buf = NULL;

        struct wx_buf_chain_s* bc = (struct wx_buf_chain_s*)conn->data;
        bc->base = bc->data;
        bc->size = sizeof(conn->data) - sizeof(bc);
        bc->cleanup = cleanup_cb;
        bc->arg = conn;
        bc->next = NULL;


        int vertail = 1;
        char connection[]="keep-alive";
        if (conn->keepalivems == 0) {
            vertail = 0;
            sprintf(connection, "close");
        }
        bc->size = (size_t)sprintf(bc->base, "HTTP/1.%d 200 OK\r\nConnection: %s\r\nContent-Length: %d\r\n\r\n%s", vertail, connection, strlen(resbody), resbody);

        wx_conn_write_start(wx_conn, wx_conn->rwatcher.fd, bc);
    }

    if (buf->size == 0) {
        conn->buf = NULL;
        wx_err("header buffer overflow");
        conn_close(conn);
        return EXIT_FAILURE;
    }

    return 0;
}