Inform protocol about interfaces

This commit is contained in:
Matthias Schiffer 2013-03-18 19:30:37 +01:00
parent 33d1cf04a7
commit 976cea11de
6 changed files with 35 additions and 17 deletions

View file

@ -56,6 +56,7 @@ static inline bool gmrf_addr_equal(const gmrf_addr_t *addr1, const gmrf_addr_t *
void gmrf_logf(gmrf_t *gmrf, int priority, const char *format, ...); void gmrf_logf(gmrf_t *gmrf, int priority, const char *format, ...);
gmrf_addr_t gmrf_iface_get_addr(gmrf_t *gmrf, gmrf_iface_t *iface); gmrf_addr_t gmrf_iface_get_addr(gmrf_t *gmrf, gmrf_iface_t *iface);
const char* gmrf_iface_get_name(gmrf_t *gmrf, gmrf_iface_t *iface);
size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface); size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface);
bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest); bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest);
@ -70,6 +71,7 @@ extern const char *gmrf_protocol_name;
extern const char *gmrf_protocol_version; extern const char *gmrf_protocol_version;
gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf); gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf);
void gmrf_protocol_add_iface(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface);
void gmrf_protocol_handle_packet(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface, const gmrf_addr_t *source, const void *data, size_t len); void gmrf_protocol_handle_packet(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface, const gmrf_addr_t *source, const void *data, size_t len);
#endif /* _GMRF_GMRF_H_ */ #endif /* _GMRF_GMRF_H_ */

View file

@ -35,6 +35,7 @@ typedef struct mmss_protocol {
const char* (*get_version)(void); const char* (*get_version)(void);
gmrf_context_t* (*init)(gmrf_t *gmrf); gmrf_context_t* (*init)(gmrf_t *gmrf);
void (*add_iface)(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface);
void (*handle_packet)(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface, const gmrf_addr_t *source, const void *data, size_t len); void (*handle_packet)(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface, const gmrf_addr_t *source, const void *data, size_t len);
} mmss_protocol_t; } mmss_protocol_t;

View file

@ -40,5 +40,6 @@ const mmss_protocol_t mmss_protocol_info = {
.get_version = get_version, .get_version = get_version,
.init = gmrf_protocol_init, .init = gmrf_protocol_init,
.add_iface = gmrf_protocol_add_iface,
.handle_packet = gmrf_protocol_handle_packet, .handle_packet = gmrf_protocol_handle_packet,
}; };

View file

@ -33,6 +33,10 @@ gmrf_addr_t gmrf_iface_get_addr(gmrf_t *gmrf, gmrf_iface_t *iface) {
return iface->address; return iface->address;
} }
const char* gmrf_iface_get_name(gmrf_t *gmrf, gmrf_iface_t *iface) {
return iface->name;
}
size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) { size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) {
return iface->net->mtu; return iface->net->mtu;
} }
@ -74,3 +78,21 @@ void mmss_dispatch(mmss_packet_t *packet) {
&packet->source->address, packet->data, packet->len); &packet->source->address, packet->data, packet->len);
free(packet); free(packet);
} }
void mmss_add_iface(gmrf_t *node, mmss_network_t *net, const char *name, const gmrf_addr_t *address) {
gmrf_iface_t *iface = calloc(1, sizeof(gmrf_iface_t));
iface->name = strdup(name);
iface->address = *address;
iface->node = node;
iface->net = net;
iface->node_next = node->interfaces;
node->interfaces = iface;
iface->network_next = net->interfaces;
net->interfaces = iface;
node->proto->add_iface(node, node->ctx, iface);
}

View file

@ -68,27 +68,16 @@ int main(int argc, char *argv[]) {
mmss_network_t net = { .mtu = 1500 }; mmss_network_t net = { .mtu = 1500 };
gmrf_t node1 = { .name = "node1", .mmss = &mmss, .proto = proto }, node2 = { .name = "node2", .mmss = &mmss, .proto = proto }; gmrf_t node1 = { .name = "node1", .mmss = &mmss, .proto = proto }, node2 = { .name = "node2", .mmss = &mmss, .proto = proto };
gmrf_iface_t iface1 = {}, iface2 = {};
iface1.net = &net;
iface1.node = &node1;
iface1.address = (gmrf_addr_t){{1}};
iface2.net = &net;
iface2.node = &node2;
iface2.address = (gmrf_addr_t){{2}};
node1.interfaces = &iface1;
node2.interfaces = &iface2;
iface2.network_next = &iface1;
net.interfaces = &iface2;
node2.next = &node1; node2.next = &node1;
gmrf_t *nodes = &node2; gmrf_t *nodes = &node2;
init_nodes(nodes); init_nodes(nodes);
gmrf_addr_t addr1 = {{1}}, addr2 = {{2}};
mmss_add_iface(&node1, &net, "mmss0", &addr1);
mmss_add_iface(&node2, &net, "mmss0", &addr2);
while (true) { while (true) {
int timeout = get_queue_timeout(&mmss); int timeout = get_queue_timeout(&mmss);

View file

@ -80,15 +80,18 @@ struct gmrf_iface {
gmrf_iface_t *node_next; gmrf_iface_t *node_next;
gmrf_iface_t *network_next; gmrf_iface_t *network_next;
char *name;
gmrf_addr_t address;
gmrf_t *node; gmrf_t *node;
mmss_network_t *net; mmss_network_t *net;
gmrf_addr_t address;
}; };
const mmss_protocol_t* mmss_load_protocol(const char *module); const mmss_protocol_t* mmss_load_protocol(const char *module);
void mmss_add_iface(gmrf_t *node, mmss_network_t *net, const char *name, const gmrf_addr_t *address);
void mmss_dispatch(mmss_packet_t *packet); void mmss_dispatch(mmss_packet_t *packet);
void mmss_run_scheduled(mmss_scheduled_t *scheduled); void mmss_run_scheduled(mmss_scheduled_t *scheduled);