opengame.c 2.51 KB
/**************************************************************************
 *                                      *
 *         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);
}