Setting ssid - quick answer please

Bruce M Simpson bms at spc.org
Mon Jun 30 08:19:42 PDT 2003


On Mon, Jun 30, 2003 at 12:28:51PM +0200, Robert Dezorzo wrote:
> 
>  Or alternatively make start_if.wi0 file and wrote in this file all
> information about AP

You might also find this hack of mine useful under 4.x.
It forces a 'wait' in a shell script (such as start_if.wi0) until the
card's firmware has bound to an AP, before dhclient runs.

Attached.

BMS
-------------- next part --------------
#
# waitforap makefile
#

PROG=	waitforap
SRCS=	waitforap.c

NOMAN=	defined

.include <bsd.prog.mk>
-------------- next part --------------
/*
 * bms 2003/05/19
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <err.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_media.h>
#include <net/if_ieee80211.h>

void
usage(void)
{
	fprintf(stderr, "waitforap: usage: waitforap <interface>\n");
}

int
waitforap(int s, char *name)
{
	struct ieee80211req	ireq;                                           
	struct ifmediareq	ifmr;
        u_int8_t                data[32];                                       
	int			associated = 0;
        int *media_list, i;                                                     

	(void) memset(&ireq, 0, sizeof(ireq));
	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
	ireq.i_data = &data;
	ireq.i_type = IEEE80211_IOC_SSID;
	ireq.i_val = -1;
	if (ioctl(s, SIOCG80211, &ireq) < 0) {
		/* If we can't get the SSID, the this isn't an 802.11 device. */
		return -1;
	}

        (void) memset(&ifmr, 0, sizeof(ifmr));
        (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
        if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
                /*
                 * Interface doesn't support SIOC{G,S}IFMEDIA.
                 */
                return -1;
        }

        if (ifmr.ifm_count == 0) {
                warnx("%s: no media types?", name);
                return -1;
        }

        media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
        if (media_list == NULL)
                err(1, "malloc");
        ifmr.ifm_ulist = media_list;

	do {
		sleep(1);

		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
			err(1, "SIOCGIFMEDIA");

		if (ifmr.ifm_status & IFM_AVALID) {
			switch (IFM_TYPE(ifmr.ifm_active)) {
			case IFM_IEEE80211:
				/* XXX: Not valid for adhoc */
				if (ifmr.ifm_status & IFM_ACTIVE)
					associated = 1;
				break;
			default:
				warnx("%s: not 802.11 media?", name);
				free(media_list);
				return -1;
			}
		}
	} while (!associated);

	free(media_list);
	return 0;
}

int
main(int argc, char *argv[])
{
	int s = -1, r = -1;

	if (argc != 2) {
		usage();	
		exit(EXIT_FAILURE);
	}

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s == -1) {
		err(1, "socket");
	}

	r = waitforap(s, argv[1]);

cleanup:
	if (s != -1)
		close(s);
	exit(r == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}


More information about the freebsd-mobile mailing list