opengame.c
2.51 KB
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
/**************************************************************************
* *
* Copyright (C) 1995, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
**************************************************************************/
#include <fcntl.h>
#include <ultrahost.h>
#ifndef WIN32
#ifdef BBPLAYER
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#else
#include <sys/u64gio.h>
#endif
#else
#include <stdio.h>
#include <winsock2.h>
#endif
/*
* Name: uhOpenGame
*
* Description:
* Open the given device file and set appropriate modes for
* host <> game data transfer.
*/
int uhOpenGame(const char *pathName)
{
int fd;
#ifndef WIN32
#ifdef BBPLAYER
struct sockaddr_un sa;
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
return fd;
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, pathName, sizeof sa.sun_path);
if ((connect(fd, (struct sockaddr*)&sa, sizeof sa)) < 0) {
perror("connect");
return -1;
}
#else
if ((fd = open(pathName, O_RDWR)) == -1)
return(-1);
#endif
#else /* WIN32 */
WSADATA wsd;
char localhost[64];
struct hostent *localhost_p;
int rv;
struct sockaddr_in sa;
if ((rv = WSAStartup(MAKEWORD(2,2), &wsd)) != 0) {
printf("WSAStartup() failed: %d\n", rv);
return -1;
}
gethostname(localhost, sizeof(localhost));
localhost_p = gethostbyname (localhost);
if ( localhost_p == NULL )
{
printf ("** Cannot get hostaddress of %s **\n",localhost);
return -1;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = atoi(pathName);
memcpy((char*)&(sa.sin_addr), localhost_p->h_addr, localhost_p->h_length);
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Cannot create socket\n");
return -1;
}
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa))) {
printf("Cannot connect with mux\n");
printf("Please make sure mux is start and port number is the same\n");
return -1;
}
#endif
return(fd);
}