Esempio n. 1
0
void ipc3_connect( char servername[], int serverport, int *msgsock ) {
    struct sockaddr_in  server;
    struct hostent      *hp;
    int                 w_sock;

    sockInit();

    w_sock = socket(AF_INET, SOCK_STREAM, 0);
    if( w_sock < 0 ) {
        printf("error :opening stream socket\n");
        exit(0);
    }

    server.sin_family = AF_INET;
    hp = gethostbyname(servername);
    if( hp == 0 ) {
        printf("unknown host\n");
        exit(0);
    }
    server.sin_addr = *((struct in_addr *)(hp->h_addr_list[0]));
    server.sin_port = htons(serverport);

    if( connect(w_sock, (struct sockaddr *)&server, sizeof(server) ) < 0 ) {
        printf("error :connecting stream socket\n");
        exit(0);
    }

    *msgsock = w_sock;
}
Esempio n. 2
0
void ipc3_sopen( int *port, int *sock ) {
    struct sockaddr_in  server;
    int w_sock, w_port;
    unsigned int length;

    sockInit();

    w_sock = socket(AF_INET, SOCK_STREAM, 0);
    if( w_sock < 0 ) {
        printf("error :opening stream socket\n");
        exit(0);
    }

    w_port = *port;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( w_port );
    if( bind(w_sock, (struct sockaddr *)&server, sizeof(server) ) < 0 ) {
        printf("error :binding stream socket\n");
        exit(0);
    }

    if( w_port == 0 ) {
        length = sizeof( server );
        if( getsockname(w_sock, (struct sockaddr *)&server, &length) < 0 ) {
            printf("error :geting socket name\n");
            exit(0);
        }
        *port = ntohs(server.sin_port);
    }
    *sock = w_sock;

    listen( w_sock, 5 );
}
Esempio n. 3
0
void ipc3_wait_connection( int sock, int *msgsock ) {
    int     w_msgsock;

    sockInit();

    w_msgsock = accept( sock, (struct sockaddr *)0, (unsigned int *)0);
    if( w_msgsock == -1 ) {
            printf("error :accepting msg socket\n");
            exit(0);
    }

    *msgsock = w_msgsock;
}
Esempio n. 4
0
int main(int argc, char **argv) {
  SOCKET s;
  struct sockaddr_in broadcast_addr;
  int optval;
  int optlen;
  s = INVALID_SOCKET;
  CLEAR_ADDR(&broadcast_addr);

  if (argc != 3) {
    printf("usage: %s message port\n", argv[0]);
    return SUCCESS;
  }
  sockInit();
  /* specify broadcast address and port, byte order conversion*/
  broadcast_addr.sin_family = AF_INET;
  broadcast_addr.sin_port = htons((unsigned short)atoi(argv[2]));
  broadcast_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); /* send to all */

  /* create socket in the internet namespace */
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s == INVALID_SOCKET) {
    printf("Can't create socket\n");
    sockEnd();
    return SUCCESS;
  }
  optval = 1;
  optlen = sizeof(int);
  if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&optval, optlen)) {
    printf("Cannot create a BROADCAST socket\n");
    sockEnd();
    return FAILURE;
  }
  /* send data */
  if (sendto(s, argv[1], strlen(argv[1]), 0, (struct sockaddr *)&broadcast_addr,
             sizeof(struct sockaddr_in)) == -1) {
    printf("Can't send message\n");
    sockEnd();
    return FAILURE;
  }
  /* close the channel */
  if (closesocket(s)) {
    printf("Error on closing socket\n");
    sockEnd();
    return FAILURE;
  }
  sockEnd();
  return SUCCESS;
}
Esempio n. 5
0
/**
 * Initializes the network controller
 */
void tftpInit()
{
	// Open socket
	sockInit(TFTP_PORT);

#ifndef _TFTP_RANDOM_PORT
	if(eeprom_read_byte(EEPROM_SIG_3) == EEPROM_SIG_3_VALUE)
		tftpTransferPort = ((eeprom_read_byte(EEPROM_PORT + 1) << 8) + eeprom_read_byte(EEPROM_PORT));
	else
		tftpTransferPort = TFTP_STATIC_PORT;
#endif
#ifdef _VERBOSE
	traceln("Tftp: TFTP server init done");
#ifndef _TFTP_RANDOM_PORT
	traceln("\t   Port: ");
	tracenum(tftpTransferPort);
#endif
#endif
}
Esempio n. 6
0
int main(int argc, char * argv[]) {
	SOCKET sock, clientSock;
	struct sockaddr_in addr;
	struct sockaddr clientAddr;
	char buffer[BUFFER_SIZE];
	int clientAddrSize = 0, readBytes = 0;
	WoopsaServer server;
	WoopsaBufferSize responseLength;

	memset(buffer, 0, sizeof(buffer));
	WoopsaServerInit(&server, "/woopsa/", woopsaEntries, ServeHTML);

	printf("Woopsa C library v0.1 demo server.\n");

	if (sockInit() != 0) {
		printf("Error initializing sockets\n");
		EXIT_ERROR();
	}

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (!CHECK_SOCKET(sock)) {
		printf("Error creating socket\n");
		EXIT_ERROR();
	}

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = htons(WOOPSA_PORT);

	if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
		printf("Error binding socket\n");
		EXIT_ERROR();
	}

	listen(sock, 5);
	printf("Server listening on port %d\n", WOOPSA_PORT);

	while (1) {
		clientAddrSize = sizeof(struct sockaddr_in);
		clientSock = accept(sock, &clientAddr, &clientAddrSize);
		if (!CHECK_SOCKET(clientSock)) {
			printf("Received an invalid client socket.\n");
			EXIT_ERROR();
		}

		while (1) {
			readBytes = recv(clientSock, buffer + readBytes, sizeof(buffer), 0);

			if (readBytes == SOCKET_ERROR) {
				printf("Error %d", WSAGetLastError());
				break;
			}

			if (readBytes == 0) {
				printf("Finished\n");
				break;
			}

			if (WoopsaCheckRequestComplete(&server, buffer, sizeof(buffer)) != WOOPSA_REQUEST_COMLETE) {
				// If the request is not complete, it means more data needs 
				// to be -added- to the buffer
				continue;
			}

			if (WoopsaHandleRequest(&server, buffer, sizeof(buffer), buffer, sizeof(buffer), &responseLength) >= WOOPSA_SUCCESS) {
				send(clientSock, buffer, responseLength, 0);
			}
			readBytes = 0;
			memset(buffer, 0, sizeof(buffer));
		}
	}

	if (sockClose(sock) != 0) {
		printf("Error closing socket\n");
		EXIT_ERROR();
	}

	if (sockQuit() != 0) {
		printf("Error quitting sockets\n");
		EXIT_ERROR();
	}

	getchar();

	return 0;
}
Esempio n. 7
0
uint8_t processPacket()
{
#endif

	uint8_t buffer[TFTP_PACKET_MAX_SIZE];
	uint16_t readPointer;
	uint32_t writeAddr;
	// Transfer entire packet to RAM
	uint8_t* bufPtr = buffer;
	uint16_t count;

#ifdef _DEBUG_TFTP
	traceln("Tftp: ----");
	traceln("Tftp: Starting processing packet of size ");
	tracenum(packetSize);
	if(packetSize >= 0x800) traceln("Tftp: Overflow");
	//  step();
#endif

	// Read data from chip to buffer
	readPointer = netReadWord(REG_S3_RX_RD0);
#ifdef _DEBUGMORE_TFTP
	traceln("Tftp: readPointer at position ");
	tracenum(readPointer);
#endif
	if(readPointer == 0) readPointer += S3_RX_START;
	for(count = TFTP_PACKET_MAX_SIZE; count--;) {
#ifdef _DEBUGMORE_TFTP
		if((count == TFTP_PACKET_MAX_SIZE - 1) || (count == 0)) {
			traceln("Tftp: Reading from position ");
			tracenum(readPointer);
		}
#endif
		*bufPtr++ = netReadReg(readPointer++);
		if(readPointer == S3_RX_END) readPointer = S3_RX_START;
	}
	netWriteWord(REG_S3_RX_RD0, readPointer);     // Write back new pointer
	netWriteReg(REG_S3_CR, CR_RECV);
	while(netReadReg(REG_S3_CR));
#ifdef _DEBUGMORE_TFTP
	traceln("Tftp: Bytes left to read ");
	tracenum(netReadWord(REG_S3_RX_RSR0));
#endif

#ifdef _DEBUGMORE_TFTP
	// Dump packet
	bufPtr = buffer;
	traceln("");
	for(count = TFTP_PACKET_MAX_SIZE / 2; count--;) {
		uint16_t val = *bufPtr++;
		val |= (*bufPtr++) << 8;
		tracenum(val);
		if((count % 8) == 0 && count != 0) traceln("");
		else trace(" ");
	}
#endif

#ifdef _DEBUG_TFTP
	traceln("Tftp: Setting return address");
#endif

	// Set up return IP address and port
	uint8_t i;
	for(i = 0; i < 6; i++) netWriteReg(REG_S3_DIPR0 + i, buffer[i]);

	// Parse packet
	uint16_t tftpDataLen = (buffer[6] << 8) + buffer[7];
	uint16_t tftpOpcode = (buffer[8] << 8) + buffer[9];
	uint16_t tftpBlock = (buffer[10] << 8) + buffer[11];
#ifdef _DEBUG
	traceln("Tftp: This is block ");
	tracenum(tftpBlock);
	trace(" with opcode ");
	tracenum(tftpOpcode);
	trace(" and data length ");
	tracenum(tftpDataLen - (TFTP_OPCODE_SIZE + TFTP_BLOCKNO_SIZE));
#endif

	if((tftpOpcode == TFTP_OPCODE_DATA)
		&& ((tftpBlock > MAX_ADDR/0x200)
		|| (tftpBlock < highPacket)
		|| (tftpBlock > highPacket+1))) tftpOpcode = TFTP_OPCODE_UKN;
	if(tftpDataLen > (0x200 + TFTP_OPCODE_SIZE + TFTP_BLOCKNO_SIZE)) tftpOpcode = TFTP_OPCODE_UKN;

	uint8_t returnCode = ERROR_UNKNOWN;
	uint16_t packetLength;


	switch(tftpOpcode) {

		case TFTP_OPCODE_RRQ: // Read request
#ifdef _DEBUG_TFTP
			traceln("Tftp: Read request");
#endif
			break;

		case TFTP_OPCODE_WRQ: // Write request
			// Valid WRQ -> reset timer
			resetTick();
#ifdef _VERBOSE
			traceln("Tftp: Write request");
#endif
			// Flagging image as invalid since the flashing process has started
			eeprom_write_byte(EEPROM_IMG_STAT, EEPROM_IMG_BAD_VALUE);

#ifdef _TFTP_RANDOM_PORT
			sockInit((buffer[4] << 8) | ~buffer[5]); // Generate a 'random' TID (RFC1350)
#else
			sockInit(tftpTransferPort);
#endif
#ifdef _DEBUG_TFTP
			traceln("Tftp: Changed to port ");
#ifdef _TFTP_RANDOM_PORT
			tracenum((buffer[4] << 8) | (buffer[5] ^ 0x55));
#else
			tracenum(tftpTransferPort);
#endif
#endif

			lastPacket = highPacket = 0;
			returnCode = ACK; // Send back acknowledge for packet 0
			break;

		case TFTP_OPCODE_DATA:
			// Valid Data Packet -> reset timer
			resetTick();

			packetLength = tftpDataLen - (TFTP_OPCODE_SIZE + TFTP_BLOCKNO_SIZE);
			lastPacket = tftpBlock;
			writeAddr = (tftpBlock - 1) << 9; // Flash write address for this block
#ifdef _DEBUGMORE_TFTP
			traceln("Tftp: Data for block ");
			tracenum(lastPacket);
#endif

			if((writeAddr + packetLength) > MAX_ADDR) {
				// Flash is full - abort with an error before a bootloader overwrite occurs
				// Application is now corrupt, so do not hand over.
#ifdef _VERBOSE
				traceln("Tftp: Flash is full");
#endif
				returnCode = ERROR_FULL;
			} else {
#ifdef _DEBUG_TFTP
				traceln("Tftp: Writing data from address ");
				tracenum(writeAddr);
#endif

				uint8_t* pageBase = buffer + (UDP_HEADER_SIZE + TFTP_OPCODE_SIZE + TFTP_BLOCKNO_SIZE); // Start of block data
				uint16_t offset = 0; // Block offset


				// Set the return code before packetLength gets rounded up
				if(packetLength < TFTP_DATA_SIZE) returnCode = FINAL_ACK;
				else returnCode = ACK;

				// Round up packet length to a full flash sector size
				while(packetLength % SPM_PAGESIZE) packetLength++;
#ifdef _DEBUG_TFTP
				traceln("Tftp: Packet length adjusted to ");
				tracenum(packetLength);
#endif
				if(writeAddr == 0) {
					// First sector - validate
					if(!validImage(pageBase)) {
						returnCode = INVALID_IMAGE;
						/* FIXME: Validity checks. Small programms (under 512 bytes?) don't
						 * have the the JMP sections and that is why app.bin was failing.
						 * When flashing big binaries is fixed, uncomment the break below.*/
#ifndef _DEBUG_TFTP
						break;
#endif
					}
				}

				// Flash packets
				for(offset = 0; offset < packetLength;) {
					uint16_t writeValue = (pageBase[offset]) | (pageBase[offset + 1] << 8);
					boot_page_fill(writeAddr + offset, writeValue);
#ifdef _DEBUGMORE_TFTP
					if((offset == 0) || ((offset == (packetLength - 2)))) {
						traceln("Tftp: Writing ");
						tracenum(writeValue);
						trace(" at offset ");
						tracenum(writeAddr + offset);
					}
#endif
					offset += 2;
					if(offset % SPM_PAGESIZE == 0) {
						boot_page_erase(writeAddr + offset - SPM_PAGESIZE);
						boot_spm_busy_wait();
						boot_page_write(writeAddr + offset - SPM_PAGESIZE);
						boot_spm_busy_wait();
#if defined(RWWSRE)
						// Reenable read access to flash
						boot_rww_enable();
#endif
					}
				}

				if(returnCode == FINAL_ACK) {
					// Flash is complete
					// Hand over to application
#ifdef _VERBOSE
					traceln("Tftp: Flash is complete");
#endif
					// Flag the image as valid since we received the last packet
					eeprom_write_byte(EEPROM_IMG_STAT, EEPROM_IMG_OK_VALUE);
				}
			}
			break;

			// Acknowledgment
		case TFTP_OPCODE_ACK:
#ifdef _DEBUG_TFTP
			traceln("Tftp: Acknowledge");
#endif
			break;

			// Error signal
		case TFTP_OPCODE_ERROR:
#ifdef _DEBUG_TFTP
			traceln("Tftp: Error");
#endif
			/* FIXME: Resetting might be needed here too */
			break;

		default:
#ifdef _DEBUG_TFTP
			traceln("Tftp: Invalid opcode ");
			tracenum(tftpOpcode);
#endif

#ifdef _TFTP_RANDOM_PORT
			sockInit((buffer[4] << 8) | ~buffer[5]); // Generate a 'random' TID (RFC1350)
#else
			sockInit(tftpTransferPort);
#endif
			/* FIXME: This is where the tftp server should be resetted.
			 * It can be done by reinitializig the tftpd or
			 * by resetting the device. I should find out which is best...
			 * Right now it is being done by resetting the timer if we have a
			 * data packet. */
			// Invalid - return error
			returnCode = ERROR_INVALID;
			break;

	}
	return(returnCode);
}
Esempio n. 8
0
int main(int argc, const char *argv[])
// return values:
//  1 == error init dpmi / couldn't lock
//  2 == error initing log buffers
//  3 == error installing lammcall rmcode
//  4 == useipxlink requested, but couldn't install
//  5 == error while linking
//  6 == error loading gp2.exe
{
	char *fullname = NULL;
	DWORD *pULongCfg = NULL;
	char *pStrCfg = NULL;
	char cfgnamebuf[_MAX_PATH];
	char *cfgname = NULL;
	char *gp2exename = GP2_EXE_FILENAME;
	char *gp2logname = GP2LOG_DEBUG_FILENAME;
	DWORD dpmicode;
	ubyte log_flags = 0, i /* count var */;
	char tmpbuf[128];


#ifndef TEST
			//----- invoked by our own stub? --------
			// ACHTUNG: "GP2LINT" muss ungerade Anzahl Buchstaben haben (wegen updown & gamma)
	if (strcmp(getenv(updown("GP2LINT")), updown("GAMMA")) != 0) {
							//--- it's not defined, so leave quiet ---
#ifdef TEST
		fprintf(stderr, "GP2LINT not defined!\n");
#endif
		return 140;
	}
#endif

	printf(GP2LAP_BANNER_STR);
#ifdef SOCKCLNT
	printf("This version of GP2Lap was compiled as a socket client.\n");
#endif
#ifndef AUTH
	printf("This version of GP2Lap cannot be used for online leagues that require authentication.\n");
#endif
	//------- Init configuration
	if (GetConfigFileNameOpt(cfgnamebuf, _MAX_PATH, argc, (void*)argv)) {
		cfgname = cfgnamebuf;
		if (!strchr(cfgname, '.'))
			strcat(cfgname, ".cfg");
	} else
		cfgname = GP2LAP_CFG_FILENAME;

	switch (InitCfgValues(cfgname, &paths_to_check, &items, &fullname)) {
		case 0: printf("- Configuration read from: %s\n", fullname); break;
		case 1: fprintf(stderr, "*** can't open %s\n", fullname); break;
		case 2: fprintf(stderr, "*** can't locate %s\n", fullname); break;
	}

	pULongCfg = GetCfgULong("logDebug");
	if (pULongCfg)
		setbits(log_flags, BLF_DISABLED, !*pULongCfg);
	pULongCfg = GetCfgULong("logDebugFlush");
	if (pULongCfg)
		setbits(log_flags, BLF_FLUSHALWAYS, *pULongCfg);
	pStrCfg = GetCfgString("logDebugName");
	if (pStrCfg && strlen(pStrCfg) > 0)     // strlen doesn't seem to do nullptr chk
		gp2logname = pStrCfg;
	if (isoff(log_flags, BLF_DISABLED))
		printf("- Logging debug output to: %s\n", gp2logname);
	pULongCfg = GetCfgULong("hof25Enable");
	if (pULongCfg)
		opt_hof25 = *pULongCfg;
	if (opt_hof25)
		printf("- HOF2.5 mode enabled\n");
	pULongCfg = GetCfgULong("logPerf");
	if (pULongCfg)
		opt_log_perf = *pULongCfg;
	if (opt_log_perf)
		printf("- Extended perfing enabled\n");
	pULongCfg = GetCfgULong("logGLX");
	if (pULongCfg)
		opt_log_glx = *pULongCfg;
	if (opt_log_glx) {
		printf("- GLX log file enabled\n");
		// only enable opt_log_cc if opt_log_glx is TRUE
		pULongCfg = GetCfgULong("logCC");
		if (pULongCfg)
			opt_log_cc = *pULongCfg;
		if (opt_log_cc)
			printf("- Computer car logging enabled\n");
	}
	pULongCfg = GetCfgULong("Spa98");
	if (pULongCfg)
		opt_spa98 = *pULongCfg;
	if (opt_spa98)
		printf("- Spa '98 enabled\n");

	if (!LogStart(log_flags, gp2logname) && isoff(log_flags, BLF_DISABLED))
		fprintf(stderr, "*** error opening logfile '%s'\n", gp2logname);
	atexit(LogEnd);

#ifdef SOCKCLNT
	sockInit();
	atexit(sockExit);
#endif

	//--- init Frank's stuff ------
	init_new_gp2strings();  // init our strings
	FrankSoftInit();  // atexit'ed

	//---- init dpmi before all other stuff now ------
	dpmicode = dpmi_init(0 /* no verbose */); // atexit'ed
	if (dpmicode) {
		fprintf(stderr, "*** dpmi: error %04u\n", dpmicode);
		return 1;
	}

	initvesa();
 
	//---- lock my int9 handler ------
	if (!dpmi_lock_region((void near *)(MyInt9), 4096)) {  // should be enough
		 if (GetLogDpmiInfo())
			 LogLine("- dpmi: error: MI9 can't be locked!\n");
	} else {
		 if (GetLogDpmiInfo())
			 LogLine("- dpmis: MI9 locked\n");
	}

	//============================================
	//======== the complete logging stuff ========
	//============================================
	// Warning: don't change options after starting the log system!
	if (!Log_Create()) {
		fprintf(stderr, "*** error initing log buffers\n");
		return 2;
	}
	atexit(Log_Kill);

	if (!PrfLog_Create()) {
		fprintf(stderr, "*** error initing perf log buffers\n");
		return 2;
	}
	atexit(PrfLog_Kill);

	//=================================================
	//======== RM-Code fuer int21h vorbereiten =========
	//=================================================
	if ( !install_int21_hook() ) { // atexit'ed
		fprintf(stderr, "*** lowp: error e004");
		return 3;
	}

	//============================================
	//======== the complete network stuff ========
	//============================================
	if ( UseIpxLink ) {
		if ( !ipx_basic_init(0) ) // init the ipx  // atexit'ed
			return 4;
		if ( !start_ipx_link() )  // init the link
			return 5;
	}

	CloserInit();   // atexit'ed  // the very last

	//---- ok, send alive to logfile -----------
	_strdate(&tmpbuf);
	sprintf(strbuf,"\n"GP2LAP_NAME" started on %s ", tmpbuf);
	_strtime(&tmpbuf);
	strcat(strbuf, tmpbuf);
	strcat(strbuf, "\n");
	LogLine(strbuf);

	sprintf(strbuf, "- Code start at 0x%08x\n", &__begtext);
	LogLine(strbuf);

	//----- 08/99  for solving the int2F prob---------------
	InitFixInt2F();

	//----- Fremdapplikation starten -------
	putenv("DOS4G=QUIET");
	sprintf(strbuf, "Loading %s...\n", gp2exename);
	LogLine(strbuf);
	printf(strbuf);
	argv[0] = gp2exename;
	if (spawnv(P_WAIT, gp2exename, (void*)argv) < 0) {      // cast to void* to avoid warning about double indirection constness
		sprintf(strbuf, "*** error loading %s: %s\n", gp2exename, strerror(errno));
		LogLine(strbuf);
		fprintf(stderr, strbuf);
	}

#ifdef TEST
	sprintf(strbuf, "flagfield = 0x%08x\n", flagfield);
	LogLine(strbuf);
	if (GP2_Found && ((flagfield & 0xFFF) != 0x7))
		printf("\nflags == 0x%08X\n*** error flag field incorrect\n", flagfield);
#endif

	//--- saying bye now ------
	_strdate(&tmpbuf);
	sprintf(strbuf, GP2LAP_NAME" exiting on %s ", tmpbuf);
	_strtime(&tmpbuf);
	strcat(strbuf, tmpbuf);
	strcat(strbuf, "\n");
	LogLine(strbuf);

	return 0;
}
Esempio n. 9
0
long CALLBACK NETinit() {
	return sockInit();
}