int main(int argc, char *argv[]) { if (argc > 1) { return mainUtil(argc, argv); }; printf("Kernel IPv4 routing table:\n"); printf("Network Gateway Interface\n"); int fd = _glidix_routetab(AF_INET); _glidix_in_route route; while (1) { ssize_t count = read(fd, &route, sizeof(_glidix_in_route)); if (count < sizeof(_glidix_in_route)) { break; }; char network[20]; char gate[17]; ip_to_string(&route.dest, &route.mask, network); ip_to_string(&route.gateway, NULL, gate); printf("%s%s%s\n", network, gate, route.ifname); }; close(fd); printf("\nKernel IPv6 routing table:\n"); printf("Network Next hop Interface\n"); fd = _glidix_routetab(AF_INET6); _glidix_in6_route route6; while (1) { ssize_t count = read(fd, &route6, sizeof(_glidix_in6_route)); if (count < sizeof(_glidix_in6_route)) { break; }; char network[35]; char gateway[35]; ip6_to_string(&route6.dest, &route6.mask, network); ip6_to_string(&route6.gateway, NULL, gateway); printf("%s%s%s\n", network, gateway, route6.ifname); }; close(fd); return 0; };
void print_addr_info (struct addr_info * ai) { printf ("(%d) ", ai->nbits); print_buffer ((char *) (ai->destination), (ai->nbits + 7) / 8, NULL, ADDRESS_SIZE, 0); printf (", v %d, port %d, addr ", ai->ip.ip_version, ntohs (ai->ip.port)); unsigned char * ap = (unsigned char *) &(ai->ip.ip); char ip6_buf [50]; ip6_to_string (ap, ip6_buf); if (ai->ip.ip_version == 4) printf ("%d.%d.%d.%d\n", ap [12], ap [13], ap [14], ap [15]); else printf ("%s\n", ip6_buf); }
/* includes a newline at the end of the address info */ int addr_info_to_string (struct addr_info * ai, char * buf, int bsize) { int offset = 0; offset += snprintf (buf, bsize, "(%d) ", ai->nbits); offset += buffer_to_string ((char *) (ai->destination), (ai->nbits + 7) / 8, NULL, ADDRESS_SIZE, 0, buf + offset, bsize - offset); offset += snprintf (buf + offset, bsize - offset, ", v %d, port %d, addr ", ai->ip.ip_version, ntohs (ai->ip.port)); unsigned char * ap = (unsigned char *) &(ai->ip.ip); if (ai->ip.ip_version == 4) offset += snprintf (buf + offset, bsize - offset, "%d.%d.%d.%d", ap [12], ap [13], ap [14], ap [15]); else if ((bsize - offset) >= 42) offset += ip6_to_string (ap, buf + offset); offset += snprintf (buf + offset, bsize - offset, "\n"); return offset; }
int join_mcast(struct interface *ifs, int sock) { /* See linux/in6.h */ struct ipaddr_str buf; struct ipv6_mreq mcastreq; mcastreq.ipv6mr_multiaddr = ifs->int6_multaddr.sin6_addr; mcastreq.ipv6mr_interface = ifs->if_index; #if !defined __FreeBSD__ && !defined __FreeBSD_kernel__ && !defined __APPLE__ && !defined __NetBSD__ OLSR_PRINTF(3, "Interface %s joining multicast %s...", ifs->int_name, ip6_to_string(&buf, &ifs->int6_multaddr.sin6_addr)); /* Send multicast */ if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mcastreq, sizeof(struct ipv6_mreq)) < 0) { perror("Join multicast"); return -1; } #else #warning implement IPV6_ADD_MEMBERSHIP #endif /* Old libc fix */ #ifdef IPV6_JOIN_GROUP /* Join receiver group */ if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mcastreq, sizeof(struct ipv6_mreq)) < 0) #else /* Join receiver group */ if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mcastreq, sizeof(struct ipv6_mreq)) < 0) #endif { perror("Join multicast send"); return -1; } if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *)&mcastreq.ipv6mr_interface, sizeof(mcastreq.ipv6mr_interface)) < 0) { perror("Set multicast if"); return -1; } OLSR_PRINTF(3, "OK\n"); return 0; }
/** *Sends a packet on a given interface. * *@param ifp the interface to send on. * *@return negative on error */ int net_output(struct interface_olsr *ifp) { struct sockaddr_in *sin = NULL; struct sockaddr_in6 *sin6 = NULL; struct sockaddr_in dst; struct sockaddr_in6 dst6; struct ptf *tmp_ptf_list; union olsr_packet *outmsg; int retval; if (!ifp->netbuf.pending) return 0; ifp->netbuf.pending += OLSR_HEADERSIZE; retval = ifp->netbuf.pending; outmsg = (union olsr_packet *)ifp->netbuf.buff; /* Add the Packet seqno */ outmsg->v4.olsr_seqno = htons(ifp->olsr_seqnum++); /* Set the packetlength */ outmsg->v4.olsr_packlen = htons(ifp->netbuf.pending); if (olsr_cnf->ip_version == AF_INET) { /* IP version 4 */ sin = (struct sockaddr_in *)&ifp->int_broadaddr; /* Copy sin */ dst = *sin; sin = &dst; if (sin->sin_port == 0) sin->sin_port = htons(olsr_cnf->olsrport); } else { /* IP version 6 */ sin6 = (struct sockaddr_in6 *)&ifp->int6_multaddr; /* Copy sin */ dst6 = *sin6; sin6 = &dst6; } /* *Call possible packet transform functions registered by plugins */ for (tmp_ptf_list = ptf_list; tmp_ptf_list != NULL; tmp_ptf_list = tmp_ptf_list->next) { tmp_ptf_list->function(ifp->netbuf.buff, &ifp->netbuf.pending); } if (olsr_cnf->ip_version == AF_INET) { /* IP version 4 */ if (olsr_sendto(ifp->send_socket, ifp->netbuf.buff, ifp->netbuf.pending, MSG_DONTROUTE, (struct sockaddr *)sin, sizeof(*sin)) < 0) { perror("sendto(v4)"); #ifndef _WIN32 olsr_syslog(OLSR_LOG_ERR, "OLSR: sendto IPv4 '%s' on interface %s", strerror(errno), ifp->int_name); #endif /* _WIN32 */ retval = -1; } } else { /* IP version 6 */ if (olsr_sendto(ifp->send_socket, ifp->netbuf.buff, ifp->netbuf.pending, MSG_DONTROUTE, (struct sockaddr *)sin6, sizeof(*sin6)) < 0) { struct ipaddr_str buf; perror("sendto(v6)"); #ifndef _WIN32 olsr_syslog(OLSR_LOG_ERR, "OLSR: sendto IPv6 '%s' on interface %s", strerror(errno), ifp->int_name); #endif /* _WIN32 */ fprintf(stderr, "Socket: %d interface: %d\n", ifp->olsr_socket, ifp->if_index); fprintf(stderr, "To: %s (size: %u)\n", ip6_to_string(&buf, &sin6->sin6_addr), (unsigned int)sizeof(*sin6)); fprintf(stderr, "Outputsize: %d\n", ifp->netbuf.pending); retval = -1; } } ifp->netbuf.pending = 0; /* * if we've just transmitted a TC message, let Dijkstra use the current * link qualities for the links to our neighbours */ lq_tc_pending = false; return retval; }