Agrego comando para levantar interfaces.

master
Félix Arreola Rodríguez 2018-08-09 23:59:02 -05:00
parent c03db09384
commit e0e878ffd3
2 changed files with 84 additions and 1 deletions

View File

@ -53,6 +53,12 @@ typedef struct {
struct rtgenmsg gen; struct rtgenmsg gen;
} nl_req_t; } nl_req_t;
typedef struct {
struct nlmsghdr hdr;
struct ifinfomsg i;
char buf[1024];
} nl_req_if;
Interface * interfaces_locate_by_index (Interface *list, int index) { Interface * interfaces_locate_by_index (Interface *list, int index) {
Interface *iface; Interface *iface;
@ -522,7 +528,7 @@ void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address)
// update nlmsghdr length // update nlmsghdr length
nl->nlmsg_len = NLMSG_ALIGN(nl->nlmsg_len) + rta->rta_len; nl->nlmsg_len = NLMSG_ALIGN(nl->nlmsg_len) + rta->rta_len;
// del interface address // add interface address
len = sizeof (buffer) - nl->nlmsg_len; len = sizeof (buffer) - nl->nlmsg_len;
rta = (struct rtattr*) RTA_NEXT (rta, len); rta = (struct rtattr*) RTA_NEXT (rta, len);
rta->rta_type = IFA_ADDRESS; rta->rta_type = IFA_ADDRESS;
@ -573,6 +579,82 @@ void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address)
} }
} }
void interfaces_bring_up (int sock, Interface *interface) {
struct msghdr rtnl_msg;
struct iovec io;
nl_req_if req;
struct sockaddr_nl kernel;
char reply[8192];
int len;
struct nlmsghdr *nl;
struct nlmsgerr *l_err;
struct sockaddr_nl local_nl;
socklen_t local_size;
/* Recuperar el puerto local del netlink */
local_size = sizeof (local_nl);
getsockname (sock, (struct sockaddr *) &local_nl, &local_size);
memset(&rtnl_msg, 0, sizeof(rtnl_msg));
memset(&kernel, 0, sizeof(kernel));
memset(&req, 0, sizeof(req));
kernel.nl_family = AF_NETLINK; /* fill-in kernel address (destination of our message) */
kernel.nl_groups = 0;
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.hdr.nlmsg_type = RTM_NEWLINK;
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
req.hdr.nlmsg_seq = global_nl_seq++;
req.hdr.nlmsg_pid = local_nl.nl_pid;
req.i.ifi_family = AF_PACKET;
req.i.ifi_index = interface->index;
req.i.ifi_change |= IFF_UP;
req.i.ifi_flags |= IFF_UP;
io.iov_base = &req;
io.iov_len = req.hdr.nlmsg_len;
rtnl_msg.msg_iov = &io;
rtnl_msg.msg_iovlen = 1;
rtnl_msg.msg_name = &kernel;
rtnl_msg.msg_namelen = sizeof(kernel);
len = sendmsg (sock, (struct msghdr *) &rtnl_msg, 0);
/* Esperar la respuesta */
memset (&io, 0, sizeof (io));
memset (&rtnl_msg, 0, sizeof (rtnl_msg));
io.iov_base = reply;
io.iov_len = sizeof (reply);
rtnl_msg.msg_iov = &io;
rtnl_msg.msg_iovlen = 1;
rtnl_msg.msg_name = &kernel;
rtnl_msg.msg_namelen = sizeof (kernel);
len = recvmsg (sock, &rtnl_msg, 0);
nl = (struct nlmsghdr *) reply;
for (; NLMSG_OK(nl, len); nl = NLMSG_NEXT(nl, len)) {
if (nl->nlmsg_type == NLMSG_DONE) {
printf ("Bring UP Msg type: DONE!\n");
break;
}
if (nl->nlmsg_type == NLMSG_ERROR) {
l_err = (struct nlmsgerr*) NLMSG_DATA (nl);
if (nl->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr))) {
printf ("Bring up Error tamaño truncado\n");
} else if (l_err->error != 0) {
// Error:
printf ("Bring up Error: %i\n", l_err->error);
}
break;
}
}
}
void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface) { void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface) {
IPv4 *address; IPv4 *address;

View File

@ -40,6 +40,7 @@ void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h);
void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface); void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface);
void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address); void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address);
void interfaces_bring_up (int sock, Interface *interface);
#endif #endif