~ubuntu-branches/ubuntu/maverick/typespeed/maverick

« back to all changes in this revision

Viewing changes to network.c

  • Committer: Bazaar Package Importer
  • Author(s): Dafydd Harries
  • Date: 2007-12-07 05:14:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071207051413-zn96fxeem6wknkiz
Tags: 0.6.4-1
* New upstream release. Closes: #375136.
  - High priority due to fix for DoS attack. Closes: #454527
    (CVE-2007-6220).
  - Fixes segfault when $HOME is unset. Closes: #355887.
  - Adds Italian and French word lists, Dutch word file merged.
* New upstream maintainer.
  - Update homepage URL, debian/copyright, debian/watch.
  - This version is not network-compatible with versions prior to 0.5.2.
  - Stricter network code.
  - Improved memory management.
* High score file format has changed: install score conversion program to
  /usr/lib/typespeed and run it when package is configured.
* postinst:
  - Remove obsolete score file backup/create/restore code.
  - Add code to upgrade score files to the new text-based format.
* rules:
  - Update to new autotools build.
  - Put stamp files in debian/.
  - Support DEB_BUILD_OPTS=noopt.
* Remove unnecessary debian/install.
* Update man page installation.
* Change menu file to Games/Action section as per new menu policy.
* Bump to debhelper compat version 5.
* Bump standards version to 3.7.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* network.c - network  for typespeed
2
 
 
3
 
   thanks goto Steinar H. Gunderson <sgunderson@bigfoot.com>
4
 
   for the basic functions which we got from him
5
 
   
6
 
 */
7
 
 
8
 
#include "typespeed.h"
9
 
 
10
 
int close_network() {
11
 
 
12
 
        close(ttss);
13
 
        ttss=0;
14
 
 
15
 
return 0;
16
 
}
17
 
 
18
 
int init_network(char *serv,int graph)
19
 
{
20
 
        int one = 1;
21
 
        int sock = 0;
22
 
        int i;
23
 
 
24
 
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
25
 
 
26
 
        if (sock == -1) {
27
 
                if (graph) endcursestuff();
28
 
                perror("socket()");
29
 
                exit(1);
30
 
        }
31
 
        if (opt.client) {
32
 
                struct sockaddr_in s;
33
 
 
34
 
                s.sin_family = AF_INET;
35
 
                s.sin_addr.s_addr = inet_addr(serv);
36
 
                s.sin_port = htons(opt.port);
37
 
 
38
 
                if (s.sin_addr.s_addr == -1) {
39
 
                        struct hostent *he;
40
 
 
41
 
                        if (graph) {
42
 
                                mvprintw(10,30,"Looking up %s...",serv);
43
 
                                refresh();
44
 
                        } else {
45
 
                                printf("Looking up %s...\n", serv);
46
 
                        }
47
 
                        he = gethostbyname(serv);
48
 
 
49
 
                        if (he == NULL) {
50
 
                                if (graph) endcursestuff();
51
 
                                perror(serv);
52
 
                                exit(1);
53
 
                        }
54
 
                        bcopy(he->h_addr, (char *) &s.sin_addr, he->h_length);
55
 
                }
56
 
 
57
 
                if (graph) {
58
 
                        mvprintw(10,30,"Connecting to typespeed server at %s (port %d)...\n",
59
 
                               inet_ntoa(s.sin_addr),opt.port);
60
 
                        refresh();
61
 
                } else {
62
 
                        printf("Connecting to typespeed server at %s (port %d)...\n",
63
 
                               inet_ntoa(s.sin_addr),opt.port);
64
 
                }
65
 
 
66
 
                if (connect(sock, (struct sockaddr *) &s, sizeof(s)) == -1) {
67
 
                        if (graph) endcursestuff();
68
 
                        perror(serv);
69
 
                        exit(1);
70
 
                }
71
 
                i=net_ident(sock,0,graph);
72
 
                if(i!=0) return i;              
73
 
 
74
 
        } else {
75
 
                int ss;
76
 
                struct sockaddr_in addr;
77
 
                int one = 1;
78
 
 
79
 
                bzero((char *) &addr, sizeof(addr));
80
 
 
81
 
                addr.sin_family = AF_INET;
82
 
                addr.sin_addr.s_addr = INADDR_ANY;
83
 
                addr.sin_port = htons(opt.port);
84
 
 
85
 
                while(1) {
86
 
                    
87
 
                ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
88
 
                if (ss == -1) {
89
 
                        if (graph) endcursestuff();
90
 
                        perror("socket()");
91
 
                        exit(1);
92
 
                }
93
 
                setsockopt(ss, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
94
 
                if (bind(ss, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == -1) {
95
 
                        if (graph) endcursestuff();
96
 
                        perror("cannot bind to port");
97
 
                        exit(1);
98
 
                }
99
 
 
100
 
                if (listen(ss, 1) == -1) {
101
 
                        if (graph) endcursestuff();
102
 
                        perror("listen");
103
 
                        exit(1);
104
 
                }
105
 
 
106
 
                if (graph) {
107
 
                        mvprintw(10,30,"Waiting for connections...");
108
 
                        refresh();
109
 
                } else {
110
 
                        printf("Waiting for connections...\n");
111
 
                }
112
 
 
113
 
                sock = accept(ss, INADDR_ANY, NULL);
114
 
 
115
 
                if (sock == -1) {
116
 
                        if (graph) endcursestuff();
117
 
                        perror("accept()");
118
 
                        exit(1);
119
 
                }
120
 
 
121
 
                ttss=ss;
122
 
                i=net_ident(sock,ss,graph);
123
 
                if(i!=0) {close(ss); return i;}
124
 
                if(i==0) {break;}
125
 
                
126
 
                }
127
 
        }
128
 
        ioctl(sock, FIONBIO, &one);
129
 
 
130
 
        return sock;
131
 
}
132
 
 
133
 
 
134
 
int net_ident(int typesock,int tss,int graph) {
135
 
 
136
 
        char buf[60];
137
 
        int i;
138
 
 
139
 
        sprintf(buf,"TYPESPEED %s\n",TVERSION);
140
 
        send(typesock, buf, strlen(buf), 0);    
141
 
 
142
 
        strcpy(buf,"                                                           ");
143
 
 
144
 
        if (graph) {
145
 
                mvprintw(10,30,"Connect.");
146
 
                mvprintw(10,30,"Waiting for other party to identify...");
147
 
                refresh();
148
 
        } else {
149
 
                printf("Connect.\n");
150
 
                printf("Waiting for other party to identify...\n");
151
 
        }
152
 
 
153
 
        i = recv(typesock, buf, 59, MSG_PEEK);
154
 
        if (i == -1 && errno != EWOULDBLOCK) {
155
 
                if (graph) endcursestuff();
156
 
                perror("recv()");
157
 
                exit(1);
158
 
        }
159
 
        if (i > 0) {
160
 
                recv(typesock, buf, strlen(buf) + 1, 0);
161
 
                if(strstr(buf,"TYPESPEED") > 0) {
162
 
                        if (graph) {
163
 
                                mvprintw(10,30,"Remote is using: %s",buf);
164
 
                                refresh();
165
 
                        } else {
166
 
                                printf("Remote is using: %s\n",buf);
167
 
                        }
168
 
                } else {
169
 
                        if (graph) {
170
 
                                mvprintw(10,30,"Remote is not TYPESPEED");
171
 
                                refresh();
172
 
                        } else {
173
 
                                printf("Remote is not TYPESPEED\n");
174
 
                        }               
175
 
                close(tss);
176
 
                return -69;
177
 
                }
178
 
        } else {
179
 
            return -69;
180
 
        }
181
 
return 0;
182
 
}
183
 
 
184
 
 
185
 
void net_waitgame(int typesock) {
186
 
 
187
 
        char buf[60];
188
 
        int i;
189
 
 
190
 
        sprintf(buf,"TYPESPEED %s\n",TVERSION);
191
 
 
192
 
        i=send(typesock, buf, strlen(buf), 0);  
193
 
 
194
 
        strcpy(buf,"                                                           ");
195
 
 
196
 
        while(1) {
197
 
 
198
 
                i = recv(typesock, buf, 59, MSG_PEEK);
199
 
                if (i == -1 && errno != EWOULDBLOCK) {
200
 
                        endcursestuff();
201
 
                        perror("recv()");
202
 
                        exit(1);
203
 
                }
204
 
                if (i > 0) {
205
 
                        recv(typesock, buf, strlen(buf) + 1, 0);
206
 
                        if(strstr(buf,"TYPESPEED") > 0) {
207
 
                        break;
208
 
                        }
209
 
                }
210
 
        }
211
 
 
212
 
}
213
 
 
214
 
 
215
 
 
216
 
void net_swapscore(int typesock, stats_struct *stat,stats_struct *stat2)
217
 
{
218
 
 
219
 
        char buf[60];
220
 
        int x,i;
221
 
        char *temp,*temp2;
222
 
        char *number;
223
 
 
224
 
        sprintf(buf,"SCORE: %d %f %f %f\n",stat->score,stat->speed,stat->totalspeed,stat->ratio);
225
 
 
226
 
        i=send(typesock, buf, strlen(buf), 0);  
227
 
 
228
 
        strcpy(buf,"                                                           ");
229
 
 
230
 
        while(1) {
231
 
 
232
 
                i = recv(typesock, buf, 59, MSG_PEEK);
233
 
                if (i == -1 && errno != EWOULDBLOCK) {
234
 
                        endcursestuff();
235
 
                        perror("recv()");
236
 
                        exit(1);
237
 
                }
238
 
                if (i > 0) {
239
 
                        recv(typesock, buf, strlen(buf) + 1, 0);
240
 
                        if(strstr(buf,"SCORE:") > 0) {
241
 
                                break;
242
 
                        }
243
 
                }
244
 
        }
245
 
 
246
 
        number=malloc(80*sizeof(char));
247
 
        temp=malloc(80*sizeof(char));
248
 
 
249
 
        i=0;
250
 
        for(x=7;x<=strlen(buf);x++) {
251
 
          temp[i]=buf[x];
252
 
          i++;
253
 
        }
254
 
 
255
 
 
256
 
        strncpy(number,temp,strcspn(temp," "));
257
 
        stat2->score=strtol(number, NULL, 10);
258
 
        free(number);
259
 
        
260
 
        temp2=malloc(80*sizeof(char));
261
 
 
262
 
        i=0;
263
 
        for(x=strcspn(temp," ")+1;x<=strlen(temp);x++) {
264
 
          temp2[i]=temp[x];
265
 
          i++;
266
 
        }
267
 
        free(temp);
268
 
 
269
 
        number=malloc(80*sizeof(char));
270
 
        strncpy(number,temp2,strcspn(temp2," "));
271
 
        stat2->speed=(float) atof(number);
272
 
        free(number);
273
 
        temp=malloc(80*sizeof(char));
274
 
 
275
 
        i=0;
276
 
        for(x=strcspn(temp2," ")+1;x<=strlen(temp2);x++) {
277
 
          temp[i]=temp2[x];
278
 
          i++;
279
 
        }
280
 
        free(temp2);
281
 
 
282
 
        number=malloc(80*sizeof(char));
283
 
        strncpy(number,temp,strcspn(temp," "));
284
 
        stat2->totalspeed= (float) atof(number);
285
 
        temp2=malloc(80*sizeof(char));
286
 
 
287
 
        i=0;
288
 
        for(x=strcspn(temp," ")+1;x<=strlen(temp);x++) {
289
 
          temp2[i]=temp[x];
290
 
          i++;
291
 
        }
292
 
 
293
 
        stat2->ratio= (float) atof(temp2);
294
 
        free(temp);
295
 
        free(temp2);
296
 
 
297
 
}
298