discussion
[Top] [All Lists]

Re: [Discussion] Retrieving a CID

To: Martin Röhricht <ml@xxxxxxxxxxx>
Subject: Re: [Discussion] Retrieving a CID
From: John Heffner <jheffner@xxxxxxx>
Date: Mon, 24 Oct 2005 10:21:20 -0400
On Oct 24, 2005, at 5:32 AM, Martin Röhricht wrote:

Hello again,

I'd like to get the corresponding CID for a connection of which I know the
local and the remote IP address as well as the remote port (the local port is
not known by default). So I thought of using the
web100_connection_find(agent, spec);
function to obtain the information. So I store the IP addresses in char arrays
const char LocalAddress[16] = "192.168.0.17";
const char RemoteAddress[16] = "192.168.0.18";
and try to convert them into a given spec like this:
struct web100_connection_spec *spec;
spec = (struct web100_connection_spec*) malloc(sizeof(struct
web100_connection_spec));
[...]
inet_aton(LocalAddress, &src_t);
spec->src_addr = ntohl(src_t.s_addr);
inet_aton(RemoteAddress, &dst_t);
spec->dst_addr = ntohl(dst_t.s_addr);
spec->src_port = LocalPort;
spec->dst_port = RemotePort;


Unfortunately web100_connection_find returns a NULL pointer ...

web100_connection_find() only returns a single connection, not a list of connections, so it required a full match on the spec. What you need to do is step through the list of all connections in a loop doing a match on just the parts of the spec you want. See below for an example. (You seem not to have a remote port, but I'm assuming you do.)


Incidentally, the equivalent python code is:

import Web100
agent = Web100.Web100Agent()
cl = agent.connection_match('192.168.0.17', None, '192.168.0.18', RemotePort)
cid = cl[0].cid


It works pretty well for this if you can use it. :-)

-John


Here is the small source code:
--------------------------------8<---------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <pthread.h>
#include "web100.h"
#include "web100-int.h"

const unsigned int  LocalPort = 0;
const char LocalAddress[16] = "192.168.0.17";
const unsigned int  RemotePort = 0;
const char RemoteAddress[16] = "192.168.0.18";

int main(int argc, char *argv[]) {
        web100_agent            *agent;
        web100_group            *group;
        web100_connection       *conn;
        struct web100_connection_spec   *spec, *spec2;
        int     cid = -1;
        struct  in_addr src_t, dst_t;

        spec = (struct web100_connection_spec*) malloc(sizeof(struct
web100_connection_spec));

if((agent = web100_attach(WEB100_AGENT_TYPE_LOCAL, NULL)) == NULL) {
web100_perror("web100_attach");
exit(EXIT_FAILURE);
}
group = web100_group_head(agent);
printf("group: %p, agent %p\n", group, agent);


        inet_aton(LocalAddress, &src_t);
        spec->src_addr = ntohl(src_t.s_addr);
        inet_aton(RemoteAddress, &dst_t);
        spec->dst_addr = ntohl(dst_t.s_addr);
        spec->src_port = LocalPort;
        spec->dst_port = RemotePort;

printf("spec: %p\n", spec);

conn = web100_connection_head(agent);
while (conn) {
spec2 = web100_get_connection_spec(conn);
if (spec->src_addr == spec2->src_addr && spec->dst_addr == spec2->dst_addr && spec->dst_port == spec2->dst_port)
break;
}
if (conn == NULL) {
printf("connection not found\n");
return 1;
}


printf("conn: %p\n", conn);

cid = web100_get_connection_cid(conn);

        return 0;
}
--------------------------------8<---------------------------------

Has anyone an idea on what I am missing?

Thanks,
Martin

_______________________________________________
Discussion mailing list
Discussion@xxxxxxxxxx
http://internal.web100.org/mailman/listinfo/discussion


_______________________________________________
Discussion mailing list
Discussion@xxxxxxxxxx
http://internal.web100.org/mailman/listinfo/discussion

<Prev in Thread] Current Thread [Next in Thread>