Example #1
0
int getifaddrs_test(void)
{
    struct ifaddrs *ifs = NULL;
    struct ifaddrs *ifs_head = NULL;
    int ret;

    ret = getifaddrs(&ifs);
    ifs_head = ifs;
    if (ret != 0) {
        fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno));
        return 1;
    }

    while (ifs) {
        printf("%-10s ", ifs->ifa_name);
        if (ifs->ifa_addr != NULL) {
            char addrstring[INET6_ADDRSTRLEN];
            const char *result;

            result = format_sockaddr(ifs->ifa_addr,
                                     addrstring,
                                     sizeof(addrstring));
            if (result != NULL) {
                printf("IP=%s ", addrstring);
            }

            if (ifs->ifa_netmask != NULL) {
                result = format_sockaddr(ifs->ifa_netmask,
                                         addrstring,
                                         sizeof(addrstring));
                if (result != NULL) {
                    printf("NETMASK=%s", addrstring);
                }
            } else {
                printf("AF=%d ", ifs->ifa_addr->sa_family);
            }
        } else {
            printf("<no address>");
        }

        printf("\n");
        ifs = ifs->ifa_next;
    }

    freeifaddrs(ifs_head);

    return 0;
}
Example #2
0
/*
 * Open a UDP socket for receiving messages.
 *
 * This is only left here for you to look at
 */
int udp_open(int local_port)
{
    int fd; 
    struct   sockaddr_in sin;
    char buffer[32];
  
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0) 
		die0("Error creating UDP socket");
  
    /* Bind the local socket to listen at the local_port. */
    printf ("Binding locally to port %d\n", local_port);
    memset((char *)&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(local_port);
    if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) 
		die0("Bind failed");
    
    printf("UDP socket at %s configured\n",
	   format_sockaddr((struct sockaddr *)&sin, buffer));
    
	return (fd);
}