]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/sock.c
Update of ORTE. Configured to compile for Linux out of box.
[orte.git] / orte / liborte / sock.c
1 /*
2  *  $Id: sock.c,v 0.0.0.1               2003/08/21 
3  *
4  *  DEBUG:  section 6                   Socket 
5  *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
6  *
7  *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
8  *  --------------------------------------------------------------------
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  */ 
21
22 #include "orte.h"
23
24 /*********************************************************************/
25 int
26 sock_start(void) {
27 #if defined(SOCK_BSD) || defined (SOCK_RTL)
28   return 0;
29 #elif defined (SOCK_WIN)
30   WORD wVersionRequested;
31   WSADATA wsaData;
32   wVersionRequested = MAKEWORD(2, 0);
33   return WSAStartup(wVersionRequested, &wsaData);
34 #endif
35 }
36
37 /*********************************************************************/
38 inline void
39 sock_finish(void) {
40 #if defined(SOCK_WIN)
41   WSACleanup();
42 #endif
43 }
44
45 /*********************************************************************/
46 int
47 sock_init_udp(sock_t *sock) {
48   sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
49   if (sock->fd < 0) return -1;
50   return 0;
51 }
52
53 /*********************************************************************/
54 inline void
55 sock_cleanup(sock_t *sock) {
56 #if defined(SOCK_BSD)
57   close(sock->fd);
58 #elif defined(SOCK_RTL)
59   close_socket_np(sock->fd);
60 #elif defined(SOCK_WIN)
61   closesocket(sock->fd);
62 #endif
63 }
64
65 /*********************************************************************/
66 int
67 sock_setsockopt(sock_t *sock,int optname,const char *optval, int optlen) {
68   if (setsockopt(sock->fd, IPPROTO_IP, optname,(void *)&optval, optlen)) {
69     sock_cleanup(sock);
70     return -1;
71   }
72   return 0;
73 }
74
75 /*********************************************************************/
76 int
77 sock_getsockopt(sock_t *sock,int optname,char *optval, int *optlen) {
78   if (getsockopt(sock->fd, IPPROTO_IP, optname,(void *)&optval, optlen)) {
79     sock_cleanup(sock);
80     return -1;
81   }
82   return 0;
83 }
84
85 /*********************************************************************/
86 int
87 sock_bind(sock_t *sock,u_int16_t port) {
88   struct sockaddr_in name;
89   int32_t size;
90
91   name.sin_family = AF_INET;
92   name.sin_port = htons(port);
93   name.sin_addr.s_addr = htonl(INADDR_ANY);
94   if (bind(sock->fd, (struct sockaddr *)&name, sizeof(name)) < 0) {
95     sock_cleanup(sock);
96     return -1;
97   }
98   size = sizeof(name);
99   if (getsockname(sock->fd,(struct sockaddr *)&name, &size ) < 0) {
100     sock_cleanup(sock);
101     return -1;
102   }
103   sock->port=ntohs(name.sin_port);
104   return 0;
105 }
106
107 /*********************************************************************/
108 inline int
109 sock_recvfrom(sock_t *sock, void *buf, int max_len,struct sockaddr_in *des,int des_len) {
110   return recvfrom(sock->fd, buf, max_len, 0,(struct sockaddr*)des,&des_len);
111 }
112
113 /*********************************************************************/
114 inline int
115 sock_sendto(sock_t *sock, void *buf, int len,struct sockaddr_in *des,int des_len) {
116   return sendto(sock->fd, buf, len, 0,(struct sockaddr*)des,des_len);
117 }
118
119 /*********************************************************************/
120 inline int
121 sock_ioctl(sock_t *sock, int cmd, int *arg) {
122   return ioctl(sock->fd, cmd, arg);
123 }
124
125 /*********************************************************************/
126 int
127 sock_get_local_interfaces(sock_t *sock,ORTEIFProp *IFProp,char *IFCount) {
128 #if defined(SOCK_BSD)
129   struct ifconf           ifc;
130   char                    buf[MAX_INTERFACES*sizeof(struct ifreq)];
131   char                    *ptr;
132
133   ifc.ifc_len = sizeof(buf);
134   ifc.ifc_buf = buf;
135   *IFCount=0;
136   if (ioctl(sock->fd, SIOCGIFCONF, &ifc) < 0) return -1;
137   for (ptr = buf; ptr < (buf + ifc.ifc_len);ptr += sizeof(struct ifreq)) {
138     struct ifreq*     ifr = (struct ifreq*) ptr;
139     struct sockaddr   addr;
140     memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
141     ioctl(sock->fd, SIOCGIFFLAGS, ifr);
142     if ((ifr->ifr_flags & IFF_UP) && !(ifr->ifr_flags & IFF_LOOPBACK)) {
143       (*IFCount)++;
144       IFProp->ifFlags=ifr->ifr_flags;
145       IFProp->ipAddress=ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr);
146       IFProp++;
147     }
148   }
149   return 0;
150 #elif defined(SOCK_RTL)
151   /* loopback iface is recognized if it has this address */
152   char ip_address [] = "127.0.0.1";
153   struct in_addr loopaddr;
154   int                     i;
155
156   *IFCount=0;
157   if (inet_aton(ip_address, &loopaddr) != 0) return -1;
158   
159   for (i = 0; i < NIC_TABLE_SIZE; i++) {
160     if (nic_table [i].nic_struct != NULL) {
161       if (nic_table[i].ipad.s_addr != loopaddr.s_addr) {
162         (*IFCount)++;
163         IFProp->ifFlags=0; //RT-Linux doesn't flags
164         IFProp->ipAddress=ntohl(nic_table[i].ipad.s_addr);
165         IFProp++;
166       }
167     }
168   }
169   return 0;
170 #elif defined(SOCK_RTAI)
171
172
173 //insert code !!!
174
175
176 #elif defined(SOCK_WIN)
177   INTERFACE_INFO      InterfaceList[MAX_INTERFACES];
178   struct sockaddr_in* pAddress;
179   int                 len,i;
180
181   *IFCount=0;
182   if (WSAIoctl(sock->fd,SIO_GET_INTERFACE_LIST,NULL,0,
183                InterfaceList, sizeof(InterfaceList),
184                &len, NULL, NULL)==SOCKET_ERROR) return -1;
185   len=len/sizeof(INTERFACE_INFO);
186   for(i=0;i<len;i++) {
187     long  nFlags;
188     pAddress = (struct sockaddr_in*)&(InterfaceList[i].iiAddress);
189     nFlags = InterfaceList[i].iiFlags;
190     if ((nFlags & IFF_UP) && !(nFlags & IFF_LOOPBACK)) {
191       IFProp->ifFlags=nFlags;
192       IFProp->ipAddress=ntohl(pAddress->sin_addr.s_addr);
193       IFProp++;
194       (*IFCount)++;
195     }
196   }
197   return 0;
198 #endif
199 }
200
201