From owner-freebsd-net@FreeBSD.ORG Sun Sep 12 04:57:00 2010 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E15AE106566C for ; Sun, 12 Sep 2010 04:56:59 +0000 (UTC) (envelope-from sadishkr@gmail.com) Received: from mail-iw0-f182.google.com (mail-iw0-f182.google.com [209.85.214.182]) by mx1.freebsd.org (Postfix) with ESMTP id AAE918FC0A for ; Sun, 12 Sep 2010 04:56:59 +0000 (UTC) Received: by iwn34 with SMTP id 34so4396307iwn.13 for ; Sat, 11 Sep 2010 21:56:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:content-type; bh=MKZPow+PhFGfwfA66qDwql5Ul/2Bx6V7wv7Vs55hrBs=; b=K9NYNX9ohcF8AJEQK7wCzA1VDTBGxXQyZLPMzMtiYDtnUlVSQ2Esui5HALJmOPKL4k 63L5qgTbR6M8FJSj+op9AxCMFSKyF776eyX33lxm8vnQBDNTWl/+2uzo4uFUwBALurRA j++sRlagKL6+fa2Avb078pi3/8iWG/6oLuY+w= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=TBUl6y9ntCn3zk5iGe4DXuTo2oYVjWlxNcuOEz9KTpQHf0V3nGBe17+96AsY/k1kh5 MApi7XuPa0IclUmlAGEvrRYm183KNb6p9EPodFO4FViBwgY5pt/veqet3tNzXy9YZ5HH nNILT/e/73jLGquVR05Zv+MLb4VnkbcwXUN30= MIME-Version: 1.0 Received: by 10.231.11.197 with SMTP id u5mr3604194ibu.41.1284267418637; Sat, 11 Sep 2010 21:56:58 -0700 (PDT) Received: by 10.231.193.222 with HTTP; Sat, 11 Sep 2010 21:56:58 -0700 (PDT) Date: Sun, 12 Sep 2010 14:56:58 +1000 Message-ID: From: Sadish Kulasekere To: freebsd-net@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: Creating socket(9) functions in FreeBSD 8 kernel X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Sep 2010 04:57:00 -0000 Hi, I've been trying to write a program to send a message to a specific host with a specific MAC address. I used the socket(9) kernel interface socket functions, but the kernel keeps crashing when I try to load it. I have posted the code below. Can someone please tell me what is wrong with this code? Cheers, Sadish. #include //#include //#include #include //#include #include #include #include #include #include //#include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include #include #include #include #include #include static int wake(void); //typedef u8; static int wake(void) { unsigned int i,j; unsigned char k[6] = {0x1F,0x00,0xDD,0x1D,0x3B,0x2E}; /*need to fix the correct MAC*/ unsigned char wolBuff[136]; unsigned char* ptr; //int optval = 1; int packet; //int sendint; struct socket *sok; struct sockaddr_in sap; //struct sockaddr *sap; //struct ucred *crd; //struct thread *trd; struct uio auio; struct iovec aiov; struct sockopt sokoptions; aiov.iov_base = (caddr_t) &wolBuff;//&wolBuff; aiov.iov_len = sizeof(wolBuff);//strlen(wolBuff); auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = 0; auio.uio_resid = sizeof(wolBuff);//strlen(wolBuff); auio.uio_segflg = UIO_SYSSPACE; auio.uio_rw = UIO_WRITE; auio.uio_td = 0; //sokoptions.sopt_dir = SOPT_SET; //sokoptions.sopt_level = SOL_SOCKET; //sokoptions.sopt_name = packet; //sokoptions.sopt_val = 0; //sokoptions.sopt_valsize = 0; //sokoptions.sopt_td = NULL; ptr = wolBuff; for(i=0; i<6; i++) { *ptr++ = 0xFF; } for(j=0; j<16; j++) { for(i=0; i<6; i++) { *ptr++ = k[i]; } } //uprintf("print buffer %u",*ptr); //packet = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); packet = socreate(AF_LINK, &sok, SOCK_DGRAM,0, 0, 0); if(sok==NULL) { uprintf("sok null\n"); goto ret; } sokoptions.sopt_dir = SOPT_SET; sokoptions.sopt_level = SOL_SOCKET; sokoptions.sopt_name = packet; sokoptions.sopt_val = 0; sokoptions.sopt_valsize = 0; sokoptions.sopt_td = NULL; //sosetopt((struct socket *)packet, &sokoptions); /* Set up broadcast address */ sap.sin_family = AF_INET; sap.sin_addr.s_addr = INADDR_BROADCAST;//htonl(0xffffffff) sap.sin_port = htons(6000); uprintf("aaaaaaaaaaaaa \n"); //just for testing packet = sosend(sok, (struct sockaddr *)&sap, &auio, 0, 0, 0, 0); if(packet <0) { uprintf("bbbbbbbbbbbbb \n"); goto ret; } //soclose(sok); ret: return 0; } static int deinit(void) { uprintf("module unloaded \n"); return 0; } /* The function to load/unload kernel module*/ static int event_handler(struct module *module, int event, void *arg) { int error =0; /* Error, 0 for normal return status*/ switch (event) { case MOD_LOAD: error = wake(); break; case MOD_UNLOAD: error = deinit(); break; default: error = EOPNOTSUPP; /*Error operation not supported*/ break; } return(error); } /*argument for DECLARE_MODULE macro*/ static moduledata_t test_conf ={ "WOLgenerator", /*module name */ event_handler, /* event handler function*/ "NULL" /* extra data*/ }; /*int dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) { int err = 0; if (count > 0) return EBUSY; count = 1; return (err); } int dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td) { int err = 0; count = 0; return (err); } int dev_read(struct cdev *dev, struct uio *uio, int ioflag) { int rv = 0; sprintf(flwbuf, "%016d, %9u \n", bytes,sport); rv = uiomove(flwbuf, MIN(uio->uio_resid, strlen(flwbuf)), uio); return rv; }*/ DECLARE_MODULE(WOLgenerator,test_conf,SI_SUB_KLD,SI_ORDER_ANY); /* the name of module, the moduledata_t structure,???,order of loading*/