"Fossies" - the Fresh Open Source Software Archive

Member "bayonne-1.2.16/admin/sitemon/interface.cpp" (1 Jan 2003, 2096 Bytes) of package /linux/misc/old/bayonne-1.2.16.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "interface.cpp" see the Fossies "Dox" file reference documentation.

    1 // Copyright (C) 2000 Open Source Telecom Corporation.
    2 //  
    3 // This program is free software; you can redistribute it and/or modify
    4 // it under the terms of the GNU General Public License as published by
    5 // the Free Software Foundation; either version 2 of the License, or
    6 // (at your option) any later version.
    7 // 
    8 // This program is distributed in the hope that it will be useful,
    9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11 // GNU General Public License for more details.
   12 // 
   13 // You should have received a copy of the GNU General Public License
   14 // along with this program; if not, write to the Free Software 
   15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   16 
   17 #include "sitemon.h"
   18 
   19 #ifdef  __WIN32__
   20 WSAEVENT hEvent = WSA_INVALID_EVENT;
   21 WSADATA wsd;
   22 #endif
   23 
   24 void setInterface(void)
   25 {
   26     const char *ap = myapp->reg().readStringEntry("BAYONNE", "interface", "*");
   27     int pp = myapp->reg().readIntEntry("BAYONNE", "monitor", 7070);
   28 
   29     struct sockaddr addr;
   30     struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
   31     unsigned long *ul = (unsigned long *)&sin->sin_addr;
   32 
   33 #ifdef  __WIN32__
   34     if(hEvent == WSA_INVALID_EVENT)
   35     {
   36         WORD version = MAKEWORD (2, 2);
   37         WSAStartup(version, &wsd);
   38         hEvent = WSACreateEvent();
   39     }
   40 #endif
   41 
   42     int myso = socket(AF_INET, SOCK_DGRAM, 0);
   43 
   44     if(so > -1)
   45     {
   46 #ifdef  __WIN32__
   47         myapp->removeInput(hEvent, INPUT_READ);
   48 #else
   49         myapp->removeInput(so, INPUT_READ);
   50 #endif
   51         shutdown(so, 2);
   52 #ifdef  __WIN32__
   53         closesocket(so);
   54 #else
   55         close(so);
   56 #endif
   57         so = -1;
   58     }
   59 
   60     if(myso < 0)
   61         return;
   62 
   63     memset(&addr, 0, sizeof(addr));
   64     *ul = htonl(INADDR_ANY);
   65     if(strcmp(ap, "*"))
   66         *ul = (unsigned long)inet_addr(ap);
   67 
   68     sin->sin_port = htons((unsigned short)pp);
   69     sin->sin_family = AF_INET;
   70     
   71     if(bind(myso, &addr, sizeof(struct sockaddr_in)))
   72         return;
   73 
   74     so = myso;
   75 #ifdef  __WIN32__
   76 
   77     WSAEventSelect(so, hEvent, FD_READ);
   78     myapp->addInput(hEvent, INPUT_READ, mytable, MyTable::ID_PACKET);
   79 #else
   80     myapp->addInput(so, INPUT_READ, mytable, MyTable::ID_PACKET);
   81 #endif
   82 }
   83