/[LeafOK_CVS]/pvpgn-1.7.4/src/common/network.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/common/network.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (vendor branch)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 sysadm 1.1 /*
2     * Copyright (C) 1998 Mark Baysinger (mbaysing@ucsd.edu)
3     * Copyright (C) 1998,1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
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.
14     *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19     #include "common/setup_before.h"
20     #ifdef HAVE_STRING_H
21     # include <string.h>
22     #else
23     # ifdef HAVE_STRINGS_H
24     # include <strings.h>
25     # endif
26     #endif
27     #ifdef HAVE_UNISTD_H
28     # include <unistd.h>
29     #endif
30     #include <errno.h>
31     #include "compat/strerror.h"
32     #ifdef HAVE_SYS_TYPES_H
33     # include <sys/types.h>
34     #endif
35     #ifdef HAVE_SYS_SOCKET_H
36     # include <sys/socket.h>
37     #endif
38     #include "compat/socket.h"
39     #include "compat/recv.h"
40     #include "compat/send.h"
41     #ifdef HAVE_NETINET_IN_H
42     # include <netinet/in.h>
43     #endif
44     #include "compat/netinet_in.h"
45     #include "compat/psock.h"
46     #include "common/packet.h"
47     #include "common/eventlog.h"
48     #include "common/field_sizes.h"
49     #include "common/network.h"
50     #include "common/setup_after.h"
51    
52    
53     extern int net_recv(int sock, void *buff, int len)
54     {
55     int res;
56    
57     res = psock_recv(sock, buff, len, 0);
58     if (res > 0) return res;
59     if (!res) return -1; /* connection closed */
60    
61     if (
62     #ifdef PSOCK_EINTR
63     psock_errno()==PSOCK_EINTR ||
64     #endif
65     #ifdef PSOCK_EAGAIN
66     psock_errno()==PSOCK_EAGAIN ||
67     #endif
68     #ifdef PSOCK_EWOULDBLOCK
69     psock_errno()==PSOCK_EWOULDBLOCK ||
70     #endif
71     #ifdef PSOCK_ENOMEM
72     psock_errno()==PSOCK_ENOMEM ||
73     #endif
74     0) /* try later */
75     return 0;
76    
77     if (
78     #ifdef PSOCK_ENOTCONN
79     psock_errno()==PSOCK_ENOTCONN ||
80     #endif
81     #ifdef PSOCK_ECONNRESET
82     psock_errno()==PSOCK_ECONNRESET ||
83     #endif
84     0) {
85     /* eventlog(eventlog_level_debug,__FUNCTION__,"[%d] remote host closed connection (psock_recv: %s)",sock,pstrerror(psock_errno())); */
86     return -1; /* common error: close connection, but no message */
87     }
88    
89     eventlog(eventlog_level_debug,__FUNCTION__,"[%d] receive error (closing connection) (psock_recv: %s)",sock,pstrerror(psock_errno()));
90     return -1;
91     }
92    
93     extern int net_recv_packet(int sock, t_packet * packet, unsigned int * currsize)
94     {
95     int addlen;
96     unsigned int header_size;
97     void * temp;
98    
99     if (!packet) {
100     eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL packet (closing connection)",sock);
101     return -1;
102     }
103    
104     if (!currsize) {
105     eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL currsize (closing connection)",sock);
106     return -1;
107     }
108    
109     if ((header_size = packet_get_header_size(packet))>=MAX_PACKET_SIZE) {
110     eventlog(eventlog_level_error,__FUNCTION__,"[%d] could not determine header size (closing connection)",sock);
111     return -1;
112     }
113    
114     if (!(temp = packet_get_raw_data_build(packet,*currsize))) {
115     eventlog(eventlog_level_error,__FUNCTION__,"[%d] could not obtain raw data pointer at offset %u (closing connection)",sock,*currsize);
116     return -1;
117     }
118    
119     if (*currsize<header_size)
120     addlen = net_recv(sock, temp, header_size-*currsize);
121     else {
122     unsigned int total_size=packet_get_size(packet);
123    
124     if (total_size<header_size) {
125     eventlog(eventlog_level_warn,__FUNCTION__,"[%d] corrupted packet received (total_size=%u currsize=%u) (closing connection)",sock,total_size,*currsize);
126     return -1;
127     }
128    
129     if (*currsize>=total_size) {
130     eventlog(eventlog_level_warn,__FUNCTION__,"[%d] more data requested for already complete packet (total_size=%u currsize=%u) (closing connection)",sock,total_size,*currsize);
131     return -1;
132     }
133    
134     addlen = net_recv(sock, temp, total_size-*currsize);
135     }
136    
137     if (addlen<=0) return addlen;
138    
139     *currsize += addlen;
140    
141     if (*currsize>=header_size && *currsize==packet_get_size(packet))
142     return 1;
143    
144     return 0;
145     }
146    
147     extern int net_send(int sock, const void *buff, int len)
148     {
149     int res;
150    
151     res = psock_send(sock, buff, len, 0);
152    
153     if (res > 0) return res;
154     if (!res) return -1;
155    
156     if (
157     #ifdef PSOCK_EINTR
158     psock_errno()==PSOCK_EINTR ||
159     #endif
160     #ifdef PSOCK_EAGAIN
161     psock_errno()==PSOCK_EAGAIN ||
162     #endif
163     #ifdef PSOCK_EWOULDBLOCK
164     psock_errno()==PSOCK_EWOULDBLOCK ||
165     #endif
166     #ifdef PSOCK_ENOBUFS
167     psock_errno()==PSOCK_ENOBUFS ||
168     #endif
169     #ifdef PSOCK_ENOMEM
170     psock_errno()==PSOCK_ENOMEM ||
171     #endif
172     0)
173     return 0; /* try again later */
174    
175     if (
176     #ifdef PSOCK_EPIPE
177     psock_errno()!=PSOCK_EPIPE &&
178     #endif
179     #ifdef PSOCK_ECONNRESET
180     psock_errno()!=PSOCK_ECONNRESET &&
181     #endif
182     1) eventlog(eventlog_level_debug,__FUNCTION__,"[%d] could not send data (closing connection) (psock_send: %s)",sock,pstrerror(psock_errno()));
183    
184     return -1;
185     }
186    
187     extern int net_send_packet(int sock, t_packet const * packet, unsigned int * currsize)
188     {
189     unsigned int size;
190     int addlen;
191    
192     if (!packet) {
193     eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL packet (closing connection)",sock);
194     return -1;
195     }
196    
197     if (!currsize) {
198     eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL currsize (closing connection)",sock);
199     return -1;
200     }
201    
202     if ((size = packet_get_size(packet))<1) {
203     eventlog(eventlog_level_error,__FUNCTION__,"[%d] packet to send is empty (skipping it)",sock);
204     *currsize = 0;
205     return 1;
206     }
207    
208     addlen = net_send(sock,packet_get_raw_data_const(packet,*currsize),size-*currsize);
209    
210     if (addlen <= 0) return addlen;
211    
212     *currsize += addlen;
213     /* sent all data in this packet? */
214     if (size==*currsize)
215     {
216     *currsize = 0;
217     return 1;
218     }
219    
220     return 0;
221     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1