2 * Netris -- A free networked version of T*tris
3 * Copyright (C) 1994,1995,1996 Mark H. Weaver <mhw@netris.org>
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.
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 * $Id: inet.c,v 1.18 1996/02/09 08:22:13 mhw Exp $
23 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
32 #define HEADER_SIZE sizeof(netint2[2])
34 static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event);
37 static EventGenRec netGen = { NULL, 0, FT_read, -1, NetGenFunc, EM_net };
39 static char netBuf[64];
40 static int netBufSize, netBufGoal = HEADER_SIZE;
41 static int isServer, lostConn, gotEndConn;
43 ExtFunc void InitNet(void)
48 ExtFunc int WaitForConnection(char *portStr)
50 struct sockaddr_in addr;
59 port = atoi(portStr); /* XXX Error checking */
62 memset(&addr, 0, sizeof(addr));
63 addr.sin_family = AF_INET;
64 addr.sin_addr.s_addr = htonl(INADDR_ANY);
65 addr.sin_port = htons(port);
66 sockListen = socket(AF_INET, SOCK_STREAM, 0);
70 setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR,
71 (void *)&val1, sizeof(val1));
72 if (bind(sockListen, (struct sockaddr *)&addr, sizeof(addr)) < 0)
74 if (listen(sockListen, 1) < 0)
76 addrLen = sizeof(addr);
77 sock = accept(sockListen, (struct sockaddr *)&addr, &addrLen);
83 setsockopt(sock, SOL_SOCKET, SO_LINGER,
84 (void *)&val2, sizeof(val2));
86 strcpy(opponentHost, "???");
87 if (addr.sin_family == AF_INET) {
88 host = gethostbyaddr((void *)&addr.sin_addr,
89 sizeof(struct in_addr), AF_INET);
91 strncpy(opponentHost, host->h_name, sizeof(opponentHost)-1);
92 opponentHost[sizeof(opponentHost)-1] = 0;
100 ExtFunc int InitiateConnection(char *hostStr, char *portStr)
102 struct sockaddr_in addr;
103 struct hostent *host;
108 port = atoi(portStr); /* XXX Error checking */
111 host = gethostbyname(hostStr);
113 die("gethostbyname");
114 assert(host->h_addrtype == AF_INET);
115 strncpy(opponentHost, host->h_name, sizeof(opponentHost)-1);
116 opponentHost[sizeof(opponentHost)-1] = 0;
118 memset(&addr, 0, sizeof(addr));
119 addr.sin_family = host->h_addrtype;
120 memcpy(&addr.sin_addr, host->h_addr, host->h_length);
121 addr.sin_port = htons(port);
122 mySock = socket(AF_INET, SOCK_STREAM, 0);
125 if (connect(mySock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
126 if (errno != ECONNREFUSED)
132 netGen.fd = sock = mySock;
133 AddEventGen(&netGen);
137 static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event)
143 result = MyRead(sock, netBuf + netBufSize, netBufGoal - netBufSize);
148 netBufSize += result;
149 if (netBufSize < netBufGoal)
151 memcpy(data, netBuf, sizeof(data));
152 type = ntoh2(data[0]);
153 size = ntoh2(data[1]);
155 if (netBufSize < netBufGoal)
158 netBufGoal = HEADER_SIZE;
159 event->u.net.type = type;
160 event->u.net.size = size - HEADER_SIZE;
161 event->u.net.data = netBuf + HEADER_SIZE;
162 if (type == NP_endConn) {
166 else if (type == NP_byeBye) {
173 ExtFunc void CheckNetConn(void)
177 ExtFunc void SendPacket(NetPacketType type, int size, void *data)
181 header[0] = hton2(type);
182 header[1] = hton2(size + HEADER_SIZE);
183 if (MyWrite(sock, header, HEADER_SIZE) != HEADER_SIZE)
185 if (size > 0 && data && MyWrite(sock, data, size) != size)
189 ExtFunc void CloseNet(void)
195 SendPacket(NP_endConn, 0, NULL);
198 WaitMyEvent(&event, EM_net);
202 WaitMyEvent(&event, EM_net);
203 SendPacket(NP_byeBye, 0, NULL);
210 RemoveEventGen(&netGen);