usr.sbin/rtadvd/if.c
static void
parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
{
	int iflentry_size, malloc_size;
	struct if_msghdr *ifm;
	struct ifa_msghdr *ifam;
	char *lim;

	/*
	 * Estimate least size of an iflist entry, to be obtained from kernel.
	 * Should add sizeof(sockaddr) ??
	 */
	iflentry_size = sizeof(struct if_msghdr);
	/* roughly estimate max list size of pointers to each if_msghdr */
	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
		syslog(LOG_ERR, "<%s> malloc failed", __func__);
		exit(1);
	}

	lim = buf + bufsize;
	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
		if (ifm->ifm_msglen == 0) {
			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
			       "(buf=%p lim=%p ifm=%p)", __func__,
			       buf, lim, ifm);
			return;
		}

		if (ifm->ifm_type == RTM_IFINFO) {
			(*ifmlist_p)[ifm->ifm_index] = ifm;
		} else {
			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
			       "expected %d, got %d\n msglen = %d\n"
			       "buf:%p, ifm:%p, lim:%p\n",
			       RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
			       buf, ifm, lim);
			exit (1);
		}
		for (ifam = (struct ifa_msghdr *)
			((char *)ifm + ifm->ifm_msglen);
		     ifam < (struct ifa_msghdr *)lim;
		     ifam = (struct ifa_msghdr *)
		     	((char *)ifam + ifam->ifam_msglen)) {
			/* just for safety */
			if (!ifam->ifam_msglen) {
				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
				       "(buf=%p lim=%p ifam=%p)", __func__,
				       buf, lim, ifam);
				return;
			}
			if (ifam->ifam_type != RTM_NEWADDR)
				break;
		}
		ifm = (struct if_msghdr *)ifam;
	}
}
/*
 * alloc buffer and parse if_msghdrs block passed as arg,
 * and init the buffer as list of pointers ot each of the if_msghdr.
 */
alloc buffer and parse if_msghdrs block passed as arg,
and init the buffer as list of pointers ot each of the if_msghdr.
alloc buffer and parse if_msghdrs block passed as arg and init the buffer as list of pointers ot each of the if_msghdr .
static void
parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
{
	int iflentry_size, malloc_size;
	struct if_msghdr *ifm;
	struct ifa_msghdr *ifam;
	char *lim;
	iflentry_size = sizeof(struct if_msghdr);
	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
		syslog(LOG_ERR, "<%s> malloc failed", __func__);
		exit(1);
	}
	lim = buf + bufsize;
	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
		if (ifm->ifm_msglen == 0) {
			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
			       "(buf=%p lim=%p ifm=%p)", __func__,
			       buf, lim, ifm);
			return;
		}
		if (ifm->ifm_type == RTM_IFINFO) {
			(*ifmlist_p)[ifm->ifm_index] = ifm;
		} else {
			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
			       "expected %d, got %d\n msglen = %d\n"
			       "buf:%p, ifm:%p, lim:%p\n",
			       RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
			       buf, ifm, lim);
			exit (1);
		}
		for (ifam = (struct ifa_msghdr *)
			((char *)ifm + ifm->ifm_msglen);
		     ifam < (struct ifa_msghdr *)lim;
		     ifam = (struct ifa_msghdr *)
		     	((char *)ifam + ifam->ifam_msglen)) {
			if (!ifam->ifam_msglen) {
				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
				       "(buf=%p lim=%p ifam=%p)", __func__,
				       buf, lim, ifam);
				return;
			}
			if (ifam->ifam_type != RTM_NEWADDR)
				break;
		}
		ifm = (struct if_msghdr *)ifam;
	}
}
static void parse_iflist ( struct if_msghdr * * * ifmlist_p , char * buf , size_t bufsize ) { int iflentry_size , malloc_size ; struct if_msghdr * ifm ; struct ifa_msghdr * ifam ; char * lim ; iflentry_size = sizeof ( struct if_msghdr ) ; malloc_size = ( bufsize / iflentry_size ) * sizeof ( size_t ) ; if ( ( * ifmlist_p = ( struct if_msghdr * * ) malloc ( malloc_size ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; exit ( 1 ) ; } lim = buf + bufsize ; for ( ifm = ( struct if_msghdr * ) buf ; ifm < ( struct if_msghdr * ) lim ; ) { if ( ifm -> ifm_msglen == 0 ) { syslog ( LOG_WARNING , " " " " , __func__ , buf , lim , ifm ) ; return ; } if ( ifm -> ifm_type == RTM_IFINFO ) { ( * ifmlist_p ) [ ifm -> ifm_index ] = ifm ; } else { syslog ( LOG_ERR , " \n " " \n \n " " \n " , RTM_IFINFO , ifm -> ifm_type , ifm -> ifm_msglen , buf , ifm , lim ) ; exit ( 1 ) ; } for ( ifam = ( struct ifa_msghdr * ) ( ( char * ) ifm + ifm -> ifm_msglen ) ; ifam < ( struct ifa_msghdr * ) lim ; ifam = ( struct ifa_msghdr * ) ( ( char * ) ifam + ifam -> ifam_msglen ) ) { if ( ! ifam -> ifam_msglen ) { syslog ( LOG_WARNING , " " " " , __func__ , buf , lim , ifam ) ; return ; } if ( ifam -> ifam_type != RTM_NEWADDR ) break ; } ifm = ( struct if_msghdr * ) ifam ; } }
alloc buffer and parse if_msghdrs block passed as arg,
 and init the buffer as list of pointers ot each of the if_msghdr.
alloc buffer and parse if_msghdrs block passed as arg and init the buffer as list of pointers ot each of the if_msghdr .
/*
	 * Estimate least size of an iflist entry, to be obtained from kernel.
	 * Should add sizeof(sockaddr) ??
	 */ /* roughly estimate max list size of pointers to each if_msghdr */ /* just for safety */