1 /* Loglan82 Compiler&Interpreter
2 Copyright (C) 1981-1993 Institute of Informatics, University of Warsaw
3 Copyright (C) 1993, 1994 LITA, Pau
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 contacts: Andrzej.Salwicki@univ-pau.fr
22 LITA Departement d'Informatique
24 Avenue de l'Universite
26 tel. ++33 59923154 fax. ++33 59841696
28 =======================================================================
44 #define BITS_PER_INT 32
45 #define FD_SET(f,fds) (fds)->fds_bits[(f)/BITS_PER_INT]|=(1<<((f)%BITS_PER_INT))
46 #define FD_ZERO(fds) { (fds)->fds_bits[0]=0; (fds)->fds_bits[1]=0; }
52 void bzero( buf, size ) char *buf; int size; {
56 void bcopy( from, to, size ) char *from,*to; int size; {
65 #define INADDR_NONE 0xffffffffUL
68 /* Zerowanie adresu O.S.*/
69 #define init_addr(addr) \
70 bzero((char *)&(addr),sizeof(addr)); \
71 (addr).sin_family=AF_INET; \
72 (addr).sin_port=htons(0); \
73 (addr).sin_addr.s_addr=htonl(INADDR_ANY);
77 int host_addr( host, buf ) char *host; struct sockaddr_in *buf; {
86 addr = strchr( host, ':' );
94 if( sscanf( addr, "%d", &port ) != 1 ) usage();
100 buf->sin_port = htons( port );
102 /* try dotted-decimal address */
103 if( (inaddr = inet_addr(host)) == INADDR_NONE ){
104 if( (hp = gethostbyname(host)) == NULL )
106 assert( hp->h_length == sizeof( inaddr ) );
107 bcopy( (char *)( hp->h_addr ), (char *)&( buf->sin_addr ), sizeof( inaddr ) );
114 /* zwraca otwarte gniazdko */
116 int sock_open( socket_type, protocol, service, port )
119 int socket_type,port;
122 struct sockaddr_in my_addr;
126 init_addr( my_addr );
128 if( (pp=getprotobyname(protocol)) == NULL )
131 if( service != NULL ){
132 if( (sp=getservbyname(service,protocol)) == NULL )
134 my_addr.sin_port = sp->s_port;
138 my_addr.sin_port = htons( port );
140 if( (fd = socket(AF_INET,socket_type,pp->p_proto)) < 0 )
143 if( bind( fd, (struct sockaddr *)&my_addr, sizeof(my_addr) ) < 0 )
151 int sock_poll( sock, ms ) int sock,ms; {
152 struct timeval timeout;
156 timeout.tv_sec = ms/1000;
157 timeout.tv_usec = (ms%1000)*1000;
158 if( sock>=0 ) /* sock==-1 means we only sleep */
159 FD_SET(sock,&rd_fds);
161 nfds = select(sock+1,&rd_fds,NULL,NULL,&timeout);
163 nfds = select(sock+1,&rd_fds,NULL,NULL,NULL);
165 return ( nfds == 1 );
170 static int send_wait_time = 0;
172 void set_send_delay( ms ) int ms; { /* set delay to miliseconds */
176 int sock_send( sock, buf, size, addr )
179 struct sockaddr_in *addr;
181 struct sockaddr *adr;
183 int namelen = sizeof( *addr );
184 if( send_wait_time > 0 )
185 sock_poll( -1, send_wait_time );
186 if( sendto( sock, (char *)buf, size, 0, addr, namelen ) <= 0 ){
189 "socket send to %s:%d\n",
190 inet_ntoa( addr->sin_addr ),
191 (int)ntohs( addr->sin_port )
193 perror("send error");
199 int sock_recv( sock, buf, size, addr )
202 struct sockaddr_in *addr;
205 int namelen = sizeof( struct sockaddr_in );
206 if( ( recv_size = recvfrom( sock, buf, size, 0, addr, &namelen ) ) < 0 ){
210 if( namelen != sizeof( struct sockaddr_in ) ){
211 printf("str sockaddr_in %d namelen %d\n",sizeof(struct sockaddr_in),namelen);
212 printf("addr %s\n",inet_ntoa(addr->sin_addr));
213 abend("strenge message");