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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/* network.c - network for typespeed
thanks goto Steinar H. Gunderson <sgunderson@bigfoot.com>
for the basic functions which we got from him
*/
#include "typespeed.h"
int close_network() {
close(ttss);
ttss=0;
return 0;
}
int init_network(char *serv,int graph)
{
int one = 1;
int sock = 0;
int i;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
if (graph) endcursestuff();
perror("socket()");
exit(1);
}
if (opt.client) {
struct sockaddr_in s;
s.sin_family = AF_INET;
s.sin_addr.s_addr = inet_addr(serv);
s.sin_port = htons(opt.port);
if (s.sin_addr.s_addr == -1) {
struct hostent *he;
if (graph) {
mvprintw(10,30,"Looking up %s...",serv);
refresh();
} else {
printf("Looking up %s...\n", serv);
}
he = gethostbyname(serv);
if (he == NULL) {
if (graph) endcursestuff();
perror(serv);
exit(1);
}
bcopy(he->h_addr, (char *) &s.sin_addr, he->h_length);
}
if (graph) {
mvprintw(10,30,"Connecting to typespeed server at %s (port %d)...\n",
inet_ntoa(s.sin_addr),opt.port);
refresh();
} else {
printf("Connecting to typespeed server at %s (port %d)...\n",
inet_ntoa(s.sin_addr),opt.port);
}
if (connect(sock, (struct sockaddr *) &s, sizeof(s)) == -1) {
if (graph) endcursestuff();
perror(serv);
exit(1);
}
i=net_ident(sock,0,graph);
if(i!=0) return i;
} else {
int ss;
struct sockaddr_in addr;
int one = 1;
bzero((char *) &addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(opt.port);
while(1) {
ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ss == -1) {
if (graph) endcursestuff();
perror("socket()");
exit(1);
}
setsockopt(ss, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (bind(ss, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == -1) {
if (graph) endcursestuff();
perror("cannot bind to port");
exit(1);
}
if (listen(ss, 1) == -1) {
if (graph) endcursestuff();
perror("listen");
exit(1);
}
if (graph) {
mvprintw(10,30,"Waiting for connections...");
refresh();
} else {
printf("Waiting for connections...\n");
}
sock = accept(ss, INADDR_ANY, NULL);
if (sock == -1) {
if (graph) endcursestuff();
perror("accept()");
exit(1);
}
ttss=ss;
i=net_ident(sock,ss,graph);
if(i!=0) {close(ss); return i;}
if(i==0) {break;}
}
}
ioctl(sock, FIONBIO, &one);
return sock;
}
int net_ident(int typesock,int tss,int graph) {
char buf[60];
int i;
sprintf(buf,"TYPESPEED %s\n",TVERSION);
send(typesock, buf, strlen(buf), 0);
strcpy(buf," ");
if (graph) {
mvprintw(10,30,"Connect.");
mvprintw(10,30,"Waiting for other party to identify...");
refresh();
} else {
printf("Connect.\n");
printf("Waiting for other party to identify...\n");
}
i = recv(typesock, buf, 59, MSG_PEEK);
if (i == -1 && errno != EWOULDBLOCK) {
if (graph) endcursestuff();
perror("recv()");
exit(1);
}
if (i > 0) {
recv(typesock, buf, strlen(buf) + 1, 0);
if(strstr(buf,"TYPESPEED") > 0) {
if (graph) {
mvprintw(10,30,"Remote is using: %s",buf);
refresh();
} else {
printf("Remote is using: %s\n",buf);
}
} else {
if (graph) {
mvprintw(10,30,"Remote is not TYPESPEED");
refresh();
} else {
printf("Remote is not TYPESPEED\n");
}
close(tss);
return -69;
}
} else {
return -69;
}
return 0;
}
void net_waitgame(int typesock) {
char buf[60];
int i;
sprintf(buf,"TYPESPEED %s\n",TVERSION);
i=send(typesock, buf, strlen(buf), 0);
strcpy(buf," ");
while(1) {
i = recv(typesock, buf, 59, MSG_PEEK);
if (i == -1 && errno != EWOULDBLOCK) {
endcursestuff();
perror("recv()");
exit(1);
}
if (i > 0) {
recv(typesock, buf, strlen(buf) + 1, 0);
if(strstr(buf,"TYPESPEED") > 0) {
break;
}
}
}
}
void net_swapscore(int typesock, stats_struct *stat,stats_struct *stat2)
{
char buf[60];
int x,i;
char *temp,*temp2;
char *number;
sprintf(buf,"SCORE: %d %f %f %f\n",stat->score,stat->speed,stat->totalspeed,stat->ratio);
i=send(typesock, buf, strlen(buf), 0);
strcpy(buf," ");
while(1) {
i = recv(typesock, buf, 59, MSG_PEEK);
if (i == -1 && errno != EWOULDBLOCK) {
endcursestuff();
perror("recv()");
exit(1);
}
if (i > 0) {
recv(typesock, buf, strlen(buf) + 1, 0);
if(strstr(buf,"SCORE:") > 0) {
break;
}
}
}
number=malloc(80*sizeof(char));
temp=malloc(80*sizeof(char));
i=0;
for(x=7;x<=strlen(buf);x++) {
temp[i]=buf[x];
i++;
}
strncpy(number,temp,strcspn(temp," "));
stat2->score=strtol(number, NULL, 10);
free(number);
temp2=malloc(80*sizeof(char));
i=0;
for(x=strcspn(temp," ")+1;x<=strlen(temp);x++) {
temp2[i]=temp[x];
i++;
}
free(temp);
number=malloc(80*sizeof(char));
strncpy(number,temp2,strcspn(temp2," "));
stat2->speed=(float) atof(number);
free(number);
temp=malloc(80*sizeof(char));
i=0;
for(x=strcspn(temp2," ")+1;x<=strlen(temp2);x++) {
temp[i]=temp2[x];
i++;
}
free(temp2);
number=malloc(80*sizeof(char));
strncpy(number,temp,strcspn(temp," "));
stat2->totalspeed= (float) atof(number);
temp2=malloc(80*sizeof(char));
i=0;
for(x=strcspn(temp," ")+1;x<=strlen(temp);x++) {
temp2[i]=temp[x];
i++;
}
stat2->ratio= (float) atof(temp2);
free(temp);
free(temp2);
}
|