File: snet.c

package info (click to toggle)
xscorch 0.2.1~pre2-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,848 kB
  • ctags: 3,514
  • sloc: ansic: 30,552; sh: 9,843; makefile: 472; perl: 16
file content (160 lines) | stat: -rw-r--r-- 4,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* $Header: /fridge/cvs/xscorch/snet/snet.c,v 1.12 2009-04-26 17:39:55 jacob Exp $ */
/*
   
   xscorch - snet.c           Copyright(c) 2000-2003 Justin David Smith
                              Copyright(c) 2001-2003 Jacob Luna Lundberg
   justins(at)chaos2.org      http://chaos2.org/
   jacob(at)chaos2.org        http://chaos2.org/~jacob
    
   Network main file
    

   This program is free software; you can redistribute it and/or modify 
   it under the terms of the GNU General Public License as published by 
   the Free Software Foundation, version 2 of the License ONLY. 

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

*/
#define  _BSD_SOURCE       /* Needed for gethostname */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <snetint.h>

#include <libj/jstr/libjstr.h>
#include <snet/tcpnet/tcpnet.h>
#include <sutil/srand.h>



#define SC_NET_ERROR_LENGTH       0x100
static char _net_error[SC_NET_ERROR_LENGTH] = { '\0' };



void sc_net_set_error(const char *function, const char *errormsg) {

   /* Write the new error message to the internal error variable */
   sbprintf(_net_error, sizeof(_net_error), "(%s)  %s", function, errormsg);
   fprintf(stderr, "network error: %s\n", _net_error);

}



void sc_net_set_info(const char *function, const char *errormsg) {

   /* Write the new error message to the internal error variable */
   printf("network: (%s)  %s\n", function, errormsg);

}



const char *sc_net_get_error(void) {

   /* Return a pointer to the internal error variable */
   return(_net_error);

}



int sc_net_get_hostname(char *buf, int size) {

   if(buf == NULL || size <= 0) return(0);
   if(gethostname(buf, size) == 0) {
      buf[size - 1] = '\0';
      return(1);
   } else {
      strcopyb(buf, "localhost", size);
      return(0);
   }

}



void sc_net_version_info(char *buf, int size) {

   if(buf == NULL || size <= 0) return;
   sbprintf(buf, size, "XScorch %s-net%d.%d.%d",
            VERSION, SC_NET_MAJOR_VERSION, SC_NET_MINOR_VERSION, SC_NET_PATCH_VERSION);

}



bool sc_net_check_size(const sc_packet *packet, sizea expectedsize, const char *description) {

   char buffer[SC_NET_BUFFER_SIZE];

   if(packet->data_size < expectedsize) {
      sbprintf(buffer, sizeof(buffer), "Size of data received is too small, %d < %d", packet->data_size, expectedsize);
      sc_net_set_error(description, buffer);
      return(false);
   }
   return(true);

}



bool sc_net_check_param(dword actual, dword expected, const char *description, const char *param) {

   char buffer[SC_NET_BUFFER_SIZE];

   if(actual != expected) {
      sbprintf(buffer, sizeof(buffer), "Data mismatch in expected %s data: %08x != %08x", param, actual, expected);
      sc_net_set_error(description, buffer);
      return(false);
   }
   return(true);

}



bool sc_net_packet_init(sc_packet *packet, dword type, dword size) {
/* sc_net_packet_init
   Prepare a packet to have data dumped into it. */

   if(packet == NULL) return(false);

   /* Packets get a small extra data size so the header can be sent easily. */
   packet->data = (byte *)malloc((size + SC_PACKET_HEADER_SIZE) * sizeof(byte));
   if(packet->data == NULL) return(false);
   packet->next_rnd  = game_rand_peek();
   packet->msg_type  = type;
   packet->data_size = size;
   return(true);

}



bool sc_net_packet_release(sc_packet *packet) {
/* sc_net_packet_release()
   Free the data from a packet; true on success. */

   if(packet == NULL) return(false);

   free(packet->data);
   packet->next_rnd  = -1;
   packet->msg_type  = 0;
   packet->data_size = 0;
   packet->data = NULL;
   return(true);

}