Exemplo n.º 1
0
int
sys_rtlist(struct lwp *l, const struct sys_rtlist_args *uap, register_t *retval)
{
	int error;
	size_t len;

	(void)l;

	if ( SCARG(uap, buf) != NULL ) {
		error = ChkSpaceRW(SCARG(uap, buf), SCARG(uap, bufsz));
		if ( error < E_OK ) {
			return EFAULT;
		}
	}

	len = SCARG(uap, bufsz);
	error = sysctl_rtable(SCARG(uap,af), SCARG(uap,cmd), SCARG(uap,flags),
			      SCARG(uap,buf), &len );

	if (error == 0) {
		*retval = len;
	}

	return error;
}
Exemplo n.º 2
0
int flushroutes (void)
    {
    int s;
    size_t needed;
    int mib[3], rlen, seqno;
    char *buf, *next, *lim;
    register struct rt_msghdr *rtm;

    if ( (s = socket (PF_ROUTE, SOCK_RAW, 0)) < 0) 
        {
#ifdef DHCPC_DEBUG
        printf ("Warning: socket() error in flushroutes()");
#endif
        return (-1);
        }

    shutdown(s, 0);
    mib[0] = AF_INET;
    mib[1] = NET_RT_DUMP;
    mib[2] = 0;             /* no flags */

    /* Retrieve the size of the routing table and allocate needed memory. */

    if (sysctl_rtable (mib, 3, NULL, &needed, NULL, 0) < 0) 
        {
#ifdef DHCPC_DEBUG
        printf ("Warning: sysctl() error in flushroutes()");
#endif
        close (s);
        return (-1);
        }
    if ( (buf = calloc(1, needed)) == NULL) 
        {
#ifdef DHCPC_DEBUG
        printf ("Warning: calloc() error in flushroutes()");
#endif
        close (s);
        return(-1);
        }

    /* Copy the current routing table to the allocated buffer. */

    if (sysctl_rtable(mib, 3, buf, &needed, NULL, 0) < 0) 
        {
#ifdef DHCPC_DEBUG
        printf("Warning: sysctl() error in flushroutes()");
#endif
        free(buf);
        close(s);
        return(-1);
        }

    lim = buf + needed;
    seqno = 0;

    /* Delete each route contained in the routing tables. */

    for (next = buf; next < lim; next += rtm->rtm_msglen) 
         {
         rtm = (struct rt_msghdr *) next;

         if ((rtm->rtm_flags & (RTF_GATEWAY | RTF_LLINFO)) == 0)
             continue;
         rtm->rtm_type = RTM_DELETE;
         rtm->rtm_seq = seqno;
         rlen = write(s, next, rtm->rtm_msglen);
         if (rlen < (int)rtm->rtm_msglen) 
             break;
         seqno++;
         }

    free(buf);
    close(s);
    return(0);
    }