Esempio n. 1
0
extern unsigned int d2charinfo_get_difficulty(t_d2charinfo_summary const * charinfo)
{
	unsigned int	difficulty;

	ASSERT(charinfo,0);
	if (d2charinfo_get_expansion(charinfo)) {
		difficulty=charstatus_get_difficulty_expansion(bn_int_get(charinfo->charstatus));
	} else {
		difficulty=charstatus_get_difficulty(bn_int_get(charinfo->charstatus));
	}
	if (difficulty>2) difficulty=2;
	return difficulty;
}
Esempio n. 2
0
extern int d2charinfo_check(t_d2charinfo_file * data)
{
	ASSERT(data,-1);
	if (bn_int_get(data->header.magicword) != D2CHARINFO_MAGICWORD) {
		eventlog(eventlog_level_error,__FUNCTION__,"info data check failed (header 0x%08X)",bn_int_get(data->header.magicword));
		return -1;
	}
	if (bn_int_get(data->header.version) != D2CHARINFO_VERSION) {
		eventlog(eventlog_level_error,__FUNCTION__,"info data check failed (version 0x%08X)",bn_int_get(data->header.version));
		return -1;
	}
	return 0;
}
Esempio n. 3
0
static int on_bnetd_accountloginreply(t_connection * c, t_packet * packet)
{
	unsigned int	seqno;
	t_sq		* sq;
	t_packet	* opacket, * rpacket;
	t_connection	* client;
	int		result, reply;
	char const	* account;
	t_elem		* elem;

	if (!packet || !c)
	    return -1;

	seqno=bn_int_get(packet->u.d2cs_bnetd.h.seqno);
	if (!(sq=sqlist_find_sq(seqno))) {
		eventlog(eventlog_level_error,__FUNCTION__,"seqno %d not found",seqno);
		return -1;
	}
	if (!(client=d2cs_connlist_find_connection_by_sessionnum(sq_get_clientid(sq)))) {
		eventlog(eventlog_level_error,__FUNCTION__,"client %d not found",sq_get_clientid(sq));
		sq_destroy(sq,&elem);
		return -1;
	}
	if (!(opacket=sq_get_packet(sq))) {
		eventlog(eventlog_level_error,__FUNCTION__,"previous packet missing (seqno: %d)",seqno);
		sq_destroy(sq,&elem);
		return -1;
	}
	result=bn_int_get(packet->u.bnetd_d2cs_accountloginreply.reply);
	if (result==BNETD_D2CS_CHARLOGINREPLY_SUCCEED) {
		reply=D2CS_CLIENT_LOGINREPLY_SUCCEED;
		account=packet_get_str_const(opacket,sizeof(t_client_d2cs_loginreq),MAX_CHARNAME_LEN);
		d2cs_conn_set_account(client,account);
		d2cs_conn_set_state(client,conn_state_authed);
		eventlog(eventlog_level_info,__FUNCTION__,"account %s authed",account);
	} else {
		eventlog(eventlog_level_warn,__FUNCTION__,"client %d login request was rejected by bnetd",sq_get_clientid(sq));
		reply=D2CS_CLIENT_LOGINREPLY_BADPASS;
	}
	if ((rpacket=packet_create(packet_class_d2cs))) {
		packet_set_size(rpacket,sizeof(t_d2cs_client_loginreply));
		packet_set_type(rpacket,D2CS_CLIENT_LOGINREPLY);
		bn_int_set(&rpacket->u.d2cs_client_loginreply.reply,reply);
		conn_push_outqueue(client,rpacket);
		packet_del_ref(rpacket);
	}
	sq_destroy(sq,&elem);
	return 0;
}
Esempio n. 4
0
static int _client_anongame_set_icon(t_connection * c, t_packet const * const packet)
{
    //BlacKDicK 04/20/2003
    unsigned int desired_icon;
    char user_icon[5];

    /*FIXME: In this case we do not get a 'count' but insted of it we get the icon
    that the client wants to set.'W3H2' for an example. For now it is ok, since they share
    the same position	on the packet*/
    desired_icon=bn_int_get(packet->u.client_findanongame.count);
    user_icon[4]=0;
    if (desired_icon==0){
	std::strcpy(user_icon,"NULL");
	eventlog(eventlog_level_info,__FUNCTION__,"[%d] Set icon packet to DEFAULT ICON [%4.4s]",conn_get_socket(c),user_icon);
    }else{
	std::memcpy(user_icon,&desired_icon,4);
	eventlog(eventlog_level_info,__FUNCTION__,"[%d] Set icon packet to ICON [%s]",conn_get_socket(c),user_icon);
    }

    account_set_user_icon(conn_get_account(c),conn_get_clienttag(c),user_icon);
    //FIXME: Still need a way to 'refresh the user/channel'
    //_handle_rejoin_command(conn_get_account(c),"");
    /* ??? channel_update_userflags() */
	conn_update_w3_playerinfo(c);

    channel_rejoin(c);
    return 0;
}
Esempio n. 5
0
int d2ladder_checksum_check(void)
{
	std::FILE		* fdladder;
	long		filesize;
	int		curlen,readlen,len;
	unsigned char	* buffer;
	int		checksum,oldchecksum;
	t_d2ladderfile_header	* header;

	if (!d2ladder_ladder_file) return -1;
	fdladder=std::fopen(d2ladder_ladder_file,"rb");
	if(!fdladder) {
		eventlog(eventlog_level_error,__FUNCTION__,"error open ladder file %s",d2ladder_ladder_file);
		return -1;
	}
	std::fseek(fdladder,0,SEEK_END);
	filesize=std::ftell(fdladder);
	std::rewind(fdladder);
	if(filesize==-1) {
		eventlog(eventlog_level_error,__FUNCTION__,"lseek() error in  ladder file %s",d2ladder_ladder_file);
		std::fclose(fdladder);
		return -1;
	}
	if(filesize<(signed)sizeof(t_d2ladderfile_header)) {
		eventlog(eventlog_level_error,__FUNCTION__,"ladder file size error :%s",d2ladder_ladder_file);
		std::fclose(fdladder);
		return -1;
	}
	buffer=(unsigned char*)xmalloc(filesize);
	header=(t_d2ladderfile_header *)buffer;
	curlen=0;
	while(curlen<filesize) {
		if(filesize-curlen > 2000)
		    len = 2000;
		else
		    len = filesize-curlen;
		readlen=std::fread(buffer+curlen,1,len,fdladder);
		if (readlen<=0) {
			xfree(buffer);
			std::fclose(fdladder);
			eventlog(eventlog_level_error,__FUNCTION__,"got bad save file or read error(read:%s)",std::strerror(errno));
			return -1;
		}
		curlen+=readlen;
	}
	std::fclose(fdladder);

	oldchecksum=bn_int_get(header->checksum);
	checksum=d2ladder_checksum(buffer,filesize,LADDERFILE_CHECKSUM_OFFSET);
	xfree(buffer);

	if(oldchecksum==checksum) {
		eventlog(eventlog_level_info,__FUNCTION__,"ladder file check pass (checksum=0x%X)",checksum);
		return 1;
	} else {
		eventlog(eventlog_level_debug,__FUNCTION__,"ladder file checksum mismatch 0x%X - 0x%X",oldchecksum, checksum);
		return 0;
	}
}
Esempio n. 6
0
static int on_d2cs_authreply(t_connection * c, t_packet const * packet)
{
	t_packet	* rpacket;
	unsigned int	version;
	unsigned int	try_version;
	unsigned int	reply;
	char const	* realmname;
	t_realm		* realm;

	if (packet_get_size(packet)<sizeof(t_d2cs_bnetd_authreply)) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad packet size");
		return -1;
	}
	if (!(realmname=packet_get_str_const(packet,sizeof(t_d2cs_bnetd_authreply),REALM_NAME_LEN))) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad realmname");
		return -1;
	}
        if (!(realm=realmlist_find_realm(realmname))) {
		realm=realmlist_find_realm_by_ip(conn_get_addr(c)); /* should not fail - checked in handle_init_packet() handle_init.c */
		eventlog(eventlog_level_warn,__FUNCTION__, "warn: realm name mismatch %s %s", realm_get_name(realm), realmname);
		if (!(prefs_allow_d2cs_setname())) { /* fail if allow_d2cs_setname = false */
			eventlog(eventlog_level_error,__FUNCTION__, "d2cs not allowed to set realm name");
			return -1;
		}
		if (realm_get_active(realm)) { /* fail if realm already active */
			eventlog(eventlog_level_error,__FUNCTION__, "cannot set realm name to %s (realm already active)",realmname);
			return -1;
		}
		realm_set_name(realm,realmname);
        }
	version=prefs_get_d2cs_version();
	try_version=bn_int_get(packet->u.d2cs_bnetd_authreply.version);
	if (version && version != try_version) {
		eventlog(eventlog_level_error,__FUNCTION__,"d2cs version mismatch 0x%X - 0x%X",
			try_version,version);
		reply=BNETD_D2CS_AUTHREPLY_BAD_VERSION;
	} else {
		reply=BNETD_D2CS_AUTHREPLY_SUCCEED;
	}

	if (reply==BNETD_D2CS_AUTHREPLY_SUCCEED) {
		eventlog(eventlog_level_info,__FUNCTION__,"d2cs %s authed",
			addr_num_to_ip_str(conn_get_addr(c)));
		conn_set_state(c,conn_state_loggedin);
		realm_active(realm,c);
	} else {
		eventlog(eventlog_level_error,__FUNCTION__,"failed to auth d2cs %s",
			addr_num_to_ip_str(conn_get_addr(c)));
	}
	if ((rpacket=packet_create(packet_class_d2cs_bnetd))) {
		packet_set_size(rpacket,sizeof(t_bnetd_d2cs_authreply));
		packet_set_type(rpacket,BNETD_D2CS_AUTHREPLY);
		bn_int_set(&rpacket->u.bnetd_d2cs_authreply.h.seqno,1);
		bn_int_set(&rpacket->u.bnetd_d2cs_authreply.reply,reply);
		conn_push_outqueue(c,rpacket);
		packet_del_ref(rpacket);
	}
	return 0;
}
Esempio n. 7
0
		static int dbs_packet_charlock(t_d2dbs_connection * conn)
		{
			char CharName[MAX_CHARNAME_LEN];
			char AccountName[MAX_USERNAME_LEN];
			char RealmName[MAX_REALMNAME_LEN];
			t_d2gs_d2dbs_char_lock * charlock;
			char * readpos;

			readpos = conn->ReadBuf;
			charlock = (t_d2gs_d2dbs_char_lock*)readpos;

			readpos += sizeof(*charlock);
			std::strncpy(AccountName, readpos, MAX_USERNAME_LEN);
			if (AccountName[MAX_USERNAME_LEN - 1] != 0)
			{
				eventlog(eventlog_level_error, __FUNCTION__, "max account name length exceeded");
				return -1;
			}
			readpos += std::strlen(AccountName) + 1;
			std::strncpy(CharName, readpos, MAX_CHARNAME_LEN);
			if (CharName[MAX_CHARNAME_LEN - 1] != 0)
			{
				eventlog(eventlog_level_error, __FUNCTION__, "max char name length exceeded");
				return -1;
			}
			readpos += std::strlen(CharName) + 1;
			std::strncpy(RealmName, readpos, MAX_REALMNAME_LEN);
			if (RealmName[MAX_REALMNAME_LEN - 1] != 0)
			{
				eventlog(eventlog_level_error, __FUNCTION__, "max realm name length exceeded");
				return -1;
			}
			readpos += std::strlen(RealmName) + 1;

			if (readpos != conn->ReadBuf + bn_short_get(charlock->h.size)) {
				eventlog(eventlog_level_error, __FUNCTION__, "request packet size error");
				return -1;
			}

			if (bn_int_get(charlock->lockstatus)) {
				if (cl_lock_char((unsigned char*)CharName, (unsigned char*)RealmName, conn->serverid) != 0) {
					eventlog(eventlog_level_error, __FUNCTION__, "failed to lock character %s(*%s)@%s for gs %s(%d)", CharName, AccountName, RealmName, conn->serverip, conn->serverid);
				}
				else {
					eventlog(eventlog_level_info, __FUNCTION__, "lock character %s(*%s)@%s for gs %s(%d)", CharName, AccountName, RealmName, conn->serverip, conn->serverid);
				}
			}
			else {
				if (cl_unlock_char((unsigned char*)CharName, (unsigned char*)RealmName, conn->serverid) != 0) {
					eventlog(eventlog_level_error, __FUNCTION__, "failed to unlock character %s(*%s)@%s for gs %s(%d)", CharName, AccountName, RealmName, conn->serverip, conn->serverid);
				}
				else {
					eventlog(eventlog_level_info, __FUNCTION__, "unlock character %s(*%s)@%s for gs %s(%d)", CharName, AccountName, RealmName, conn->serverip, conn->serverid);
				}
			}
			return 1;
		}
Esempio n. 8
0
static int on_d2cs_authreply(t_connection * c, t_packet const * packet)
{
	t_packet	* rpacket;
	unsigned int	version;
	unsigned int	try_version;
	unsigned int	reply;
	char const	* realmname;
	t_realm		* realm;

	if (packet_get_size(packet)<sizeof(t_d2cs_bnetd_authreply)) {
		eventlog(eventlog_level_error,"on_d2cs_authreply","got bad packet size");
		return -1;
	}
	if (!(realmname=packet_get_str_const(packet,sizeof(t_d2cs_bnetd_authreply),REALM_NAME_LEN))) {
		eventlog(eventlog_level_error,"on_d2cs_authreply","got bad realmname");
		return -1;
	}
        if (!(realm=realmlist_find_realm_by_ip(conn_get_addr(c)))) {
                eventlog(eventlog_level_error,"handle_init_packet", "realm not found");
                return -1;
        }
	if (realm_get_name(realm) && strcasecmp(realmname,realm_get_name(realm))) {
                eventlog(eventlog_level_error,"handle_init_packet", "warn: realm name mismatch %s %s",
			realm_get_name(realm),realmname);
	}

	version=prefs_get_d2cs_version();
	try_version=bn_int_get(packet->u.d2cs_bnetd_authreply.version);
	if (version && version != try_version) {
		eventlog(eventlog_level_error,"on_d2cs_authreply","d2cs version mismatch 0x%X - 0x%X",
			try_version,version);
		reply=BNETD_D2CS_AUTHREPLY_BAD_VERSION;
	} else {
		reply=BNETD_D2CS_AUTHREPLY_SUCCEED;
	}

	if (reply==BNETD_D2CS_AUTHREPLY_SUCCEED) {
		eventlog(eventlog_level_error,"on_d2cs_authreply","d2cs %s authed",
			addr_num_to_ip_str(conn_get_addr(c)));
		conn_set_state(c,conn_state_loggedin);
		if (prefs_allow_d2cs_setname()) realm_set_name(realm,realmname);
		realm_active(realm,c);
	} else {
		eventlog(eventlog_level_error,"on_d2cs_authreply","failed to auth d2cs %s",
			addr_num_to_ip_str(conn_get_addr(c)));
	}
	if ((rpacket=packet_create(packet_class_d2cs_bnetd))) {
		packet_set_size(rpacket,sizeof(t_bnetd_d2cs_authreply));
		packet_set_type(rpacket,BNETD_D2CS_AUTHREPLY);
		bn_int_set(&rpacket->u.bnetd_d2cs_authreply.reply,reply);
		queue_push_packet(conn_get_out_queue(c),rpacket);
		packet_del_ref(rpacket);
	}
	return 0;
}
Esempio n. 9
0
static int on_client_loginreq(t_connection * c, t_packet * packet)
{
	t_packet	* bnpacket;
	char const	* account;
	t_sq		* sq;
	unsigned int	sessionnum;

	if (!(account=packet_get_str_const(packet,sizeof(t_client_d2cs_loginreq),MAX_CHARNAME_LEN))) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad account name");
		return -1;
	}
	if (d2char_check_acctname(account)<0) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad account name");
		return -1;
	}
	if (!bnetd_conn()) {
		eventlog(eventlog_level_warn,__FUNCTION__,"d2cs is offline with bnetd, login request will be rejected");
		return -1;
	}
	sessionnum=bn_int_get(packet->u.client_d2cs_loginreq.sessionnum);
	conn_set_bnetd_sessionnum(c,sessionnum);
	eventlog(eventlog_level_info,__FUNCTION__,"got client (*%s) login request sessionnum=0x%X",account,sessionnum);
	if ((bnpacket=packet_create(packet_class_d2cs_bnetd))) {
		if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,0))) {
			packet_set_size(bnpacket,sizeof(t_d2cs_bnetd_accountloginreq));
			packet_set_type(bnpacket,D2CS_BNETD_ACCOUNTLOGINREQ);
			bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.h.seqno,sq_get_seqno(sq));
			bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.seqno,
				bn_int_get(packet->u.client_d2cs_loginreq.seqno));
			bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.sessionkey,
				bn_int_get(packet->u.client_d2cs_loginreq.sessionkey));
			bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.sessionnum,sessionnum);
			memcpy(bnpacket->u.d2cs_bnetd_accountloginreq.secret_hash,
				packet->u.client_d2cs_loginreq.secret_hash,
				sizeof(bnpacket->u.d2cs_bnetd_accountloginreq.secret_hash));
			packet_append_string(bnpacket,account);
			conn_push_outqueue(bnetd_conn(),bnpacket);
		}
		packet_del_ref(bnpacket);
	}
	return 0;
}
Esempio n. 10
0
	extern int little_endian_sha1_hash(t_hash * hashout, unsigned int size, void const * datain)
	{
		bn_int value;
		unsigned int i;
		sha1_hash(hashout, size, datain);
		for (i = 0; i < 5; i++)
		{
			bn_int_nset(&value, (*hashout)[i]);
			(*hashout)[i] = bn_int_get(value);
		}
		return 0;
	}
Esempio n. 11
0
	extern char const * little_endian_hash_get_str(t_hash const hash)
	{
		bn_int value;
		t_hash be_hash;
		unsigned int i;
		for (i = 0; i < 5; i++)
		{
			bn_int_nset(&value, hash[i]);
			be_hash[i] = bn_int_get(value);
		}
		return hash_get_str(be_hash);
	}
Esempio n. 12
0
		static int dbs_packet_updateladder(t_d2dbs_connection * conn)
		{
			char CharName[MAX_CHARNAME_LEN];
			char RealmName[MAX_REALMNAME_LEN];
			t_d2gs_d2dbs_update_ladder	* updateladder;
			char * readpos;
			t_d2ladder_info			charladderinfo;

			readpos = conn->ReadBuf;
			updateladder = (t_d2gs_d2dbs_update_ladder *)readpos;

			readpos += sizeof(*updateladder);
			std::strncpy(CharName, readpos, MAX_CHARNAME_LEN);
			if (CharName[MAX_CHARNAME_LEN - 1] != 0)
			{
				eventlog(eventlog_level_error, __FUNCTION__, "max char name length exceeded");
				return -1;
			}
			readpos += std::strlen(CharName) + 1;
			std::strncpy(RealmName, readpos, MAX_REALMNAME_LEN);
			if (RealmName[MAX_REALMNAME_LEN - 1] != 0)
			{
				eventlog(eventlog_level_error, __FUNCTION__, "max realm name length exceeded");
				return -1;
			}
			readpos += std::strlen(RealmName) + 1;
			if (readpos != conn->ReadBuf + bn_short_get(updateladder->h.size)) {
				eventlog(eventlog_level_error, __FUNCTION__, "request packet size error");
				return -1;
			}

			std::strcpy(charladderinfo.charname, CharName);
			charladderinfo.experience = bn_int_get(updateladder->charexplow);
			charladderinfo.level = bn_int_get(updateladder->charlevel);
			charladderinfo.status = bn_short_get(updateladder->charstatus);
			charladderinfo.chclass = bn_short_get(updateladder->charclass);
			eventlog(eventlog_level_info, __FUNCTION__, "update ladder for %s@%s for gs %s(%d)", CharName, RealmName, conn->serverip, conn->serverid);
			d2ladder_update(&charladderinfo);
			return 1;
		}
Esempio n. 13
0
static int on_bnetd_authreply(t_connection * c, t_packet * packet)
{
	unsigned int	reply;

	reply=bn_int_get(packet->u.bnetd_d2cs_authreply.reply);
	if (reply == BNETD_D2CS_AUTHREPLY_SUCCEED) {
		eventlog(eventlog_level_info,__FUNCTION__,"authed by bnetd");
		d2cs_conn_set_state(c,conn_state_authed);
	} else {
		eventlog(eventlog_level_error,__FUNCTION__,"failed to auth by bnetd (error=%d)",reply);
		d2cs_conn_set_state(c,conn_state_destroy);
	}
	return 0;
}
Esempio n. 14
0
		/* Choose icon by user from profile > portrait */
		static int _client_anongame_set_icon(t_connection * c, t_packet const * const packet)
		{
			//BlacKDicK 04/20/2003
			// Modified by aancw 16/12/2014
			unsigned int desired_icon;
			char user_icon[5];
			t_account * account;

			// disable with custom icons
			if (prefs_get_custom_icons() == 1)
			{
				return 0;
			}

			/*FIXME: In this case we do not get a 'count' but insted of it we get the icon
			that the client wants to set.'W3H2' for an example. For now it is ok, since they share
			the same position	on the packet*/
			desired_icon = bn_int_get(packet->u.client_findanongame.count);
			//user_icon[4]=0;
			if (desired_icon == 0){
				std::strcpy(user_icon, "1O3W"); // 103W is equal to Default Icon
				eventlog(eventlog_level_info, __FUNCTION__, "[%d] Set icon packet to DEFAULT ICON [%4.4s]", conn_get_socket(c), user_icon);
			}
			else{
				std::memcpy(user_icon, &desired_icon, 4);
				eventlog(eventlog_level_info, __FUNCTION__, "[%d] Set icon packet to ICON [%s]", conn_get_socket(c), user_icon);
			}

			account = conn_get_account(c);
			
			// ICON SWITCH HACK PROTECTION
			if (check_user_icon(account, user_icon) == 0)
			{
				std::strcpy(user_icon, "1O3W"); // set icon to default
				eventlog(eventlog_level_info, __FUNCTION__, "[%s] \"%s\" ICON SWITCH hack attempt, icon set to default ", conn_get_username(c), user_icon);
				//conn_set_state(c,conn_state_destroy); // dont kill user session
			}

			account_set_user_icon(conn_get_account(c), conn_get_clienttag(c), user_icon);
			//FIXME: Still need a way to 'refresh the user/channel'
			//_handle_rejoin_command(conn_get_account(c),"");
			/* ??? channel_update_userflags() */
			conn_update_w3_playerinfo(c);

			channel_rejoin(c);
			return 0;
		}
Esempio n. 15
0
static int on_bnetd_authreq(t_connection * c, t_packet * packet)
{
	t_packet	* rpacket;
	unsigned int	sessionnum;

	sessionnum=bn_int_get(packet->u.bnetd_d2cs_authreq.sessionnum);
	eventlog(eventlog_level_info,__FUNCTION__,"received bnetd sessionnum %d",sessionnum);
	if ((rpacket=packet_create(packet_class_d2cs_bnetd))) {
		packet_set_size(rpacket,sizeof(t_d2cs_bnetd_authreply));
		packet_set_type(rpacket,D2CS_BNETD_AUTHREPLY);
		bn_int_set(&rpacket->u.d2cs_bnetd_authreply.h.seqno,1);
		bn_int_set(&rpacket->u.d2cs_bnetd_authreply.version,D2CS_VERSION_NUMBER);
		packet_append_string(rpacket,prefs_get_realmname());
		conn_push_outqueue(c,rpacket);
		packet_del_ref(rpacket);
	}
	return 0;
}
Esempio n. 16
0
extern void bnhash_to_hash(bn_int const * bnhash, t_hash * hash)
{
    unsigned int i;
    
    if (!bnhash)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL bnhash");
        return;
    }
    if (!hash)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL hash");
        return;
    }
    
    for (i=0; i<5; i++)
        (*hash)[i] = bn_int_get(bnhash[i]);
}
Esempio n. 17
0
static int on_client_creategamereq(t_connection * c, t_packet * packet)
{
	char const	* gamename;
	char const	* gamepass;
	char const	* gamedesc;
	t_game		* game;
	t_d2gs		* gs;
	t_gq		* gq;
	unsigned int	tempflag,gameflag;
	unsigned int	leveldiff, maxchar, difficulty, expansion, hardcore, ladder;
	unsigned int	seqno, reply;
	unsigned int	pos;
	t_elem		* elem;

	pos=sizeof(t_client_d2cs_creategamereq);
	if (!(gamename=packet_get_str_const(packet,pos,MAX_GAMENAME_LEN))) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad game name");
		return -1;
	}
	pos+=std::strlen(gamename)+1;
	if (!(gamepass=packet_get_str_const(packet,pos,MAX_GAMEPASS_LEN))) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad game pass");
		return -1;
	}
	pos+=std::strlen(gamepass)+1;
	if (!(gamedesc=packet_get_str_const(packet,pos,MAX_GAMEDESC_LEN))) {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad game desc");
		return -1;
	}
	tempflag=bn_int_get(packet->u.client_d2cs_creategamereq.gameflag);
	leveldiff=bn_byte_get(packet->u.client_d2cs_creategamereq.leveldiff);
	maxchar=bn_byte_get(packet->u.client_d2cs_creategamereq.maxchar);
	difficulty=gameflag_get_difficulty(tempflag);
	if (difficulty > conn_get_charinfo_difficulty(c)) {
		eventlog(eventlog_level_error,__FUNCTION__,"game difficulty exceed character limit %d %d",difficulty,
			conn_get_charinfo_difficulty(c));
		return 0;
	}
	expansion=conn_get_charinfo_expansion(c);
	hardcore=conn_get_charinfo_hardcore(c);
	ladder=conn_get_charinfo_ladder(c);
	gameflag=gameflag_create(ladder,expansion,hardcore,difficulty);

	gs = NULL;
	game = NULL;
	gq=conn_get_gamequeue(c);
	if (d2cs_gamelist_find_game(gamename)) {
		eventlog(eventlog_level_info,__FUNCTION__,"game name %s is already exist in gamelist",gamename);
		reply=D2CS_CLIENT_CREATEGAMEREPLY_NAME_EXIST;
	} else if (!gq && gqlist_find_game(gamename)) {
		eventlog(eventlog_level_info,__FUNCTION__,"game name %s is already exist in game queue",gamename);
		reply=D2CS_CLIENT_CREATEGAMEREPLY_NAME_EXIST;
	} else if (!(gs=d2gslist_choose_server())) {
		if (gq) {
			eventlog(eventlog_level_error,__FUNCTION__,"client %d is already in game queue",d2cs_conn_get_sessionnum(c));
			conn_set_gamequeue(c,NULL);
			gq_destroy(gq,&elem);
			return 0;
		} else if ((gq=gq_create(d2cs_conn_get_sessionnum(c), packet, gamename))) {
			conn_set_gamequeue(c,gq);
			d2cs_send_client_creategamewait(c,gqlist_get_length());
			return 0;
		}
		reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
	} else if (hardcore && conn_get_charinfo_dead(c)) {
		reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
	} else if (!(game=d2cs_game_create(gamename,gamepass,gamedesc,gameflag))) {
		reply=D2CS_CLIENT_CREATEGAMEREPLY_NAME_EXIST;
	} else {
		reply=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED;
		game_set_d2gs(game,gs);
		d2gs_add_gamenum(gs, 1);
		game_set_gameflag_ladder(game,ladder);
		game_set_gameflag_expansion(game,expansion);
		game_set_created(game,0);
		game_set_leveldiff(game,leveldiff);
		game_set_charlevel(game,conn_get_charinfo_level(c));
		game_set_maxchar(game,maxchar);
		game_set_gameflag_difficulty(game,difficulty);
		game_set_gameflag_hardcore(game,hardcore);
	}

	seqno=bn_short_get(packet->u.client_d2cs_creategamereq.seqno);
	if (reply!=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED) {
		t_packet	* rpacket;

		if ((rpacket=packet_create(packet_class_d2cs))) {
			packet_set_size(rpacket,sizeof(t_d2cs_client_creategamereply));
			packet_set_type(rpacket,D2CS_CLIENT_CREATEGAMEREPLY);
			bn_short_set(&rpacket->u.d2cs_client_creategamereply.seqno,seqno);
			bn_short_set(&rpacket->u.d2cs_client_creategamereply.u1,0);
			bn_short_set(&rpacket->u.d2cs_client_creategamereply.gameid,0);
			bn_int_set(&rpacket->u.d2cs_client_creategamereply.reply,reply);
			conn_push_outqueue(c,rpacket);
			packet_del_ref(rpacket);
		}
	} else {
		t_packet	* gspacket;
		t_sq		* sq;
		struct in_addr	addr;

		if ((gspacket=packet_create(packet_class_d2gs))) {
			if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,d2cs_game_get_id(game)))) {
				packet_set_size(gspacket,sizeof(t_d2cs_d2gs_creategamereq));
				packet_set_type(gspacket,D2CS_D2GS_CREATEGAMEREQ);
				bn_int_set(&gspacket->u.d2cs_d2gs_creategamereq.h.seqno,sq_get_seqno(sq));
				bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.difficulty,difficulty);
				bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.hardcore,hardcore);
				bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.expansion,expansion);
				bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.ladder,ladder);
				packet_append_string(gspacket,gamename);
				packet_append_string(gspacket,gamepass);
				packet_append_string(gspacket,gamedesc);
				packet_append_string(gspacket,d2cs_conn_get_account(c));
				packet_append_string(gspacket,d2cs_conn_get_charname(c));
				addr.s_addr = htonl(d2cs_conn_get_addr(c));
				packet_append_string(gspacket,inet_ntoa(addr));
				conn_push_outqueue(d2gs_get_connection(gs),gspacket);
			}
			packet_del_ref(gspacket);
			eventlog(eventlog_level_info,__FUNCTION__,"request create game %s on gs %d",gamename,d2gs_get_id(gs));
		}
	}
	return 0;
}
Esempio n. 18
0
		/* Open portrait in Warcraft 3 user profile */
		static int _client_anongame_get_icon(t_connection * c, t_packet const * const packet)
		{
			t_packet * rpacket;
			//BlacKDicK 04/20/2003 Need some huge re-work on this.
			{
				struct
				{
					char	 icon_code[4];
					unsigned int portrait_code;
					char	 race;
					bn_short	 required_wins;
					char	 client_enabled;
				} tempicon;

				//FIXME: Add those to the prefs and also merge them on accoun_wrap;
				// FIXED BY DJP 07/16/2003 FOR 110 CHANGE ( TOURNEY & RACE WINS ) + Table_witdh
				short icon_req_race_wins;
				short icon_req_tourney_wins;
				int race[] = { W3_RACE_RANDOM, W3_RACE_HUMANS, W3_RACE_ORCS, W3_RACE_UNDEAD, W3_RACE_NIGHTELVES, W3_RACE_DEMONS };
				char race_char[6] = { 'R', 'H', 'O', 'U', 'N', 'D' };
				char icon_pos[5] = { '2', '3', '4', '5', '6', };
				char table_width = 6;
				char table_height = 5;
				int i, j;
				char rico;
				unsigned int rlvl, rwins;
				t_clienttag clienttag;
				t_account * acc;

				char user_icon[5];
				char const * uicon;

				clienttag = conn_get_clienttag(c);
				acc = conn_get_account(c);
				/* WAR3 uses a different table size, might change if blizzard add tournament support to RoC */
				if (clienttag == CLIENTTAG_WARCRAFT3_UINT) {
					table_width = 5;
					table_height = 4;
				}

				eventlog(eventlog_level_info, __FUNCTION__, "[%d] got FINDANONGAME Get Icons packet", conn_get_socket(c));

				if ((rpacket = packet_create(packet_class_bnet)) == NULL) {
					eventlog(eventlog_level_error, __FUNCTION__, "could not create new packet");
					return -1;
				}

				packet_set_size(rpacket, sizeof(t_server_findanongame_iconreply));
				packet_set_type(rpacket, SERVER_FINDANONGAME_ICONREPLY);
				bn_int_set(&rpacket->u.server_findanongame_iconreply.count, bn_int_get(packet->u.client_findanongame_inforeq.count));
				bn_byte_set(&rpacket->u.server_findanongame_iconreply.option, CLIENT_FINDANONGAME_GET_ICON);
				
				
				if (prefs_get_custom_icons() == 1)
				{
					// get current custom icon
					t_icon_info * icon;
					if (icon = customicons_get_icon_by_account(acc, clienttag))
						std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, icon->icon_code, 4);
				}
				else if ((uicon = account_get_user_icon(acc, clienttag)))
				{
					std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, uicon, 4);
				}
				else
				{
					account_get_raceicon(acc, &rico, &rlvl, &rwins, clienttag);
					std::sprintf(user_icon, "%1d%c3W", rlvl, rico);
					std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, user_icon, 4);
				}

				bn_byte_set(&rpacket->u.server_findanongame_iconreply.table_width, table_width);
				bn_byte_set(&rpacket->u.server_findanongame_iconreply.table_size, table_width*table_height);
				for (j = 0; j < table_height; j++){
					icon_req_race_wins = anongame_infos_get_ICON_REQ(j + 1, clienttag);
					for (i = 0; i < table_width; i++){
						tempicon.race = i;
						tempicon.icon_code[0] = icon_pos[j];
						tempicon.icon_code[1] = race_char[i];
						tempicon.icon_code[2] = '3';
						tempicon.icon_code[3] = 'W';
						tempicon.portrait_code = (account_icon_to_profile_icon(tempicon.icon_code, acc, clienttag));
						if (i <= 4){
							//Building the icon for the races
							bn_short_set(&tempicon.required_wins, icon_req_race_wins);
							if (account_get_racewins(acc, race[i], clienttag) >= icon_req_race_wins) {
								if (prefs_get_custom_icons() == 1)
									tempicon.client_enabled = 0;
								else
									tempicon.client_enabled = 1;
							}
							else{
								tempicon.client_enabled = 0;
							}
						}
						else{
							//Building the icon for the tourney
							icon_req_tourney_wins = anongame_infos_get_ICON_REQ_TOURNEY(j + 1);
							bn_short_set(&tempicon.required_wins, icon_req_tourney_wins);
							if (account_get_racewins(acc, race[i], clienttag) >= icon_req_tourney_wins) {
								if (prefs_get_custom_icons() == 1)
									tempicon.client_enabled = 0;
								else
									tempicon.client_enabled = 1;
							}
							else{
								tempicon.client_enabled = 0;
							}
						}
						packet_append_data(rpacket, &tempicon, sizeof(tempicon));
					}
				}
				//Go,go,go
				conn_push_outqueue(c, rpacket);
				packet_del_ref(rpacket);
			}
			return 0;
		}
Esempio n. 19
0
static int d2ladder_readladder(void)
{
	FILE				* fp;
	t_d2ladderfile_ladderindex	* ladderheader;
	t_d2ladderfile_ladderinfo	* ladderinfo;
	char				* ladderfile;
	t_d2ladderfile_header		header;
	unsigned int			i, n, temp, count, type, number;

	if (!(ladderfile=malloc(strlen(prefs_get_ladder_dir())+1+strlen(LADDER_FILE_PREFIX)+1+
			strlen(CLIENTTAG_DIABLO2DV)+1))) {
		log_error("error allocate memory for ladderfile");
		return -1;
	}
	sprintf(ladderfile,"%s/%s.%s",prefs_get_ladder_dir(),LADDER_FILE_PREFIX,CLIENTTAG_DIABLO2DV);
	if (!(fp=fopen(ladderfile,"rb"))) {
		log_error("error opening ladder file \"%s\" for reading (fopen: %s)",ladderfile,strerror(errno));
		free(ladderfile);
		return -1;
	}
	free(ladderfile);
	if (fread(&header,1,sizeof(header),fp)!=sizeof(header)) {
		log_error("error reading ladder file");
		fclose(fp);
		return -1;
	}
	max_ladder_type= bn_int_get(header.maxtype);
	if (d2ladderlist_create(max_ladder_type)<0) {
		log_error("error create ladder list");
		fclose(fp);
		return -1;
	}
	temp= max_ladder_type * sizeof(*ladderheader);
	if (!(ladderheader=malloc(temp))) {
		log_error("error allocate ladderheader");
		fclose(fp);
		return -1;
	}
	if (fread(ladderheader,1,temp,fp)!=temp) {
		log_error("error read ladder file");
		free(ladderheader);
		fclose(fp);
		return -1;
	}
	for (i=0, count=0; i< max_ladder_type ; i++) {
		type=bn_int_get(ladderheader[i].type);
		number=bn_int_get(ladderheader[i].number);
		if (d2ladder_create(type,number)<0) {
			log_error("error create ladder %d",type);
			continue;
		}
		fseek(fp,bn_int_get(ladderheader[i].offset),SEEK_SET);
		temp=number * sizeof(*ladderinfo);
		if (!(ladderinfo=malloc(temp))) {
			log_error("error allocate ladder info");
			continue;
		}
		if (fread(ladderinfo,1,temp,fp)!=temp) {
			log_error("error read ladder file");
			free(ladderinfo);
			continue;
		}
		for (n=0; n< number; n++) {
			d2ladder_append_ladder(type,ladderinfo+n);
		}
		free(ladderinfo);
		if (number) count++;
	}
	free(ladderheader);
	fclose(fp);
	log_info("ladder file loaded successfully (%d types %d maxtype)",count,max_ladder_type);
	return 0;
}
Esempio n. 20
0
extern int d2ladder_saveladder(void)
{
	t_d2ladderfile_ladderindex	lhead[D2LADDER_MAXTYPE];
	t_d2ladderfile_header		fileheader;
	std::FILE				* fdladder;
	int				start;
	unsigned int			i,j, number;
	t_d2ladder			* d2ladder;
	t_d2ladderfile_ladderinfo	* ldata;
	char                            * XMLfilename;
        std::FILE                            * XMLfile;

/*
	if(!d2ladder_change_count) {
		eventlog(eventlog_level_debug,__FUNCTION__,"ladder data unchanged, skip saving");
		return 0;
	}
*/
	start=sizeof(fileheader)+sizeof(lhead);

	for(i=0;i<D2LADDER_MAXTYPE;i++) {
		d2ladder=d2ladderlist_find_type(i);
		bn_int_set(&lhead[i].type,d2ladder->type);
		bn_int_set(&lhead[i].offset,start);
		bn_int_set(&lhead[i].number,d2ladder->len);
		start+=d2ladder->len*sizeof(*ldata);
	}

	if (!d2ladder_ladder_file) return -1;
	if (!d2ladder_backup_file) return -1;

	if(d2ladder_checksum_check()==1) {
		eventlog(eventlog_level_info,__FUNCTION__,"backup ladder file");
		if (p_rename(d2ladder_ladder_file,d2ladder_backup_file)==-1) {
			eventlog(eventlog_level_warn,__FUNCTION__,"error std::rename %s to %s", d2ladder_ladder_file, d2ladder_backup_file);
		}
	}

	fdladder=std::fopen(d2ladder_ladder_file,"wb");
	if(!fdladder) {
		eventlog(eventlog_level_error,__FUNCTION__,"error open ladder file %s",d2ladder_ladder_file);
		return -1;
	}

	// aaron: add extra output for XML ladder here --->
	if (d2dbs_prefs_get_XML_output_ladder())
	{
	  XMLfilename = (char*)xmalloc(std::strlen(d2dbs_prefs_get_ladder_dir())+1+std::strlen(XMLname)+1);
	  std::sprintf(XMLfilename,"%s/%s",d2dbs_prefs_get_ladder_dir(),XMLname);
	  if (!(XMLfile = std::fopen(XMLfilename,"w")))
	  {
	      eventlog(eventlog_level_error,__FUNCTION__,"could not open XML ladder file for output");
	  }
	  else
	  {
             d2ladder_print_XML(XMLfile);
	     std::fclose(XMLfile);
	     xfree(XMLfilename);
	  }
	}

	// <---

	bn_int_set(&fileheader.maxtype,d2ladder_maxtype);
	bn_int_set(&fileheader.checksum,0);
	std::fwrite(&fileheader,1,sizeof(fileheader),fdladder);
	std::fwrite(lhead,1,sizeof(lhead),fdladder);
	for(i=0;i<d2ladder_maxtype;i++) {
		number=bn_int_get(lhead[i].number);
		if(number<=0) continue;
		d2ladder=d2ladderlist_find_type(i);
		ldata=(t_d2ladderfile_ladderinfo*)xmalloc(number * sizeof(*ldata));
		std::memset(ldata,0,number * sizeof(*ldata));
		for (j=0; j< number; j++) {
			bn_int_set(&ldata[j].experience,d2ladder->info[j].experience);
			bn_short_set(&ldata[j].status, d2ladder->info[j].status);
			bn_byte_set(&ldata[j].level, d2ladder->info[j].level);
			bn_byte_set(&ldata[j].chclass, d2ladder->info[j].chclass);
			std::strncpy(ldata[j].charname,d2ladder->info[j].charname,sizeof(ldata[j].charname));
		}
		std::fwrite(ldata,1,number*sizeof(*ldata),fdladder);
		xfree(ldata);
	}
	std::fclose(fdladder);
	d2ladder_checksum_set();
	eventlog(eventlog_level_info,__FUNCTION__,"ladder file saved (%d changes)",d2ladder_change_count);
	d2ladder_change_count=0;
	return 0;
}
Esempio n. 21
0
static int on_d2cs_charloginreq(t_connection * c, t_packet const * packet)
{
	t_connection * client;
	char const * charname;
	char const * portrait;
	char const * clienttag;
	char		* temp;
	unsigned int	sessionnum;
	char const * tname;
	char const * realmname;
	unsigned int	pos, reply;
	t_packet	* rpacket;
	
	if (packet_get_size(packet)<sizeof(t_d2cs_bnetd_charloginreq)) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","got bad packet size");
		return -1;
	}
	sessionnum=bn_int_get(packet->u.d2cs_bnetd_charloginreq.sessionnum);
	pos=sizeof(t_d2cs_bnetd_charloginreq);
	if (!(charname=packet_get_str_const(packet,pos,CHAR_NAME_LEN))) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","got bad character name");
		return -1;
	}
	pos+=strlen(charname)+1;
	if (!(portrait=packet_get_str_const(packet,pos,CHAR_PORTRAIT_LEN))) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","got bad character portrait");
		return -1;
	}
	if (!(client=connlist_find_connection_by_sessionnum(sessionnum))) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","user %d not found",sessionnum);
		reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
	} else if (!(clienttag=conn_get_clienttag(client))) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","got NULL clienttag");
		reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
	} else if (!(realmname=conn_get_realmname(client))) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","got NULL realm name");
		reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
	} else if (!(temp=malloc(strlen(clienttag)+strlen(realmname)+1+strlen(charname)+1+\
			strlen(portrait)+1))) {
		eventlog(eventlog_level_error,"on_d2cs_charloginreq","error allocate temp");
		reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
	} else {
		reply = BNETD_D2CS_CHARLOGINREPLY_SUCCEED;
		sprintf (temp,"PX2D%s,%s,%s",realmname,charname,portrait);
		bn_int_tag_set((bn_int *)temp,clienttag);
		conn_set_charname(client,charname);
		conn_set_realminfo(client,temp);
		free(temp);
		eventlog(eventlog_level_debug,"on_d2cs_charloginreq",\
			"loaded portrait for character %s",charname);
	}
	if ((rpacket=packet_create(packet_class_d2cs_bnetd))) {
		packet_set_size(rpacket,sizeof(t_bnetd_d2cs_charloginreply));
		packet_set_type(rpacket,BNETD_D2CS_CHARLOGINREPLY);
		bn_int_set(&rpacket->u.bnetd_d2cs_charloginreply.h.seqno,\
			bn_int_get(packet->u.d2cs_bnetd_charloginreq.h.seqno));
		bn_int_set(&rpacket->u.bnetd_d2cs_charloginreply.reply,reply);
		queue_push_packet(conn_get_out_queue(c),rpacket);
		packet_del_ref(rpacket);
	}
	return 0;
}
Esempio n. 22
0
static int on_d2cs_accountloginreq(t_connection * c, t_packet const * packet)
{
	unsigned int	sessionkey;
	unsigned int	sessionnum;
	unsigned int	salt;
	char const *	account;
	char const *	tname;
	t_connection	* client;
	int		reply;
	t_packet	* rpacket;
	struct
	{
		bn_int   salt;
		bn_int   sessionkey;
		bn_int   sessionnum;
		bn_int   secret;
		bn_int	 passhash[5];
	} temp;
	t_hash       secret_hash;
	char const * pass_str;
	t_hash	     passhash;
	t_hash	     try_hash;

	if (packet_get_size(packet)<sizeof(t_d2cs_bnetd_accountloginreq)) {
		eventlog(eventlog_level_error,"on_d2cs_accountloginreq","got bad packet size");
		return -1;
	}
	if (!(account=packet_get_str_const(packet,sizeof(t_d2cs_bnetd_accountloginreq),USER_NAME_MAX))) {
		eventlog(eventlog_level_error,"on_d2cs_accountloginreq","got bad account name");
		return -1;
	}
	sessionkey=bn_int_get(packet->u.d2cs_bnetd_accountloginreq.sessionkey);
	sessionnum=bn_int_get(packet->u.d2cs_bnetd_accountloginreq.sessionnum);
	salt=bn_int_get(packet->u.d2cs_bnetd_accountloginreq.seqno);
	if (!(client=connlist_find_connection_by_sessionnum(sessionnum))) {
		eventlog(eventlog_level_error,"on_d2cs_accountloginreq","sessionnum %d not found",sessionnum);
		reply=BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
	} else if (sessionkey!=conn_get_sessionkey(client)) {
		eventlog(eventlog_level_error,"on_d2cs_accountloginreq","sessionkey %d not match",sessionkey);
		reply=BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
	} else if (!(tname=conn_get_username(client))) {
		eventlog(eventlog_level_error,"on_d2cs_accountloginreq","got NULL username");
		reply=BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
	} else if (strcasecmp(account,tname)) {
		eventlog(eventlog_level_error,"on_d2cs_accountloginreq","username %s not match",account);
		conn_unget_username(client,tname);
		reply=BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
	} else {
		conn_unget_username(client,tname);
		bn_int_set(&temp.salt,salt);
		bn_int_set(&temp.sessionkey,sessionkey);
		bn_int_set(&temp.sessionnum,sessionnum);
		bn_int_set(&temp.secret,conn_get_secret(client));
		pass_str=account_get_pass(conn_get_account(client));
		if (hash_set_str(&passhash,pass_str)<0) {
			reply=BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
		} else {
			hash_to_bnhash((t_hash const *)&passhash,temp.passhash);
			bnet_hash(&secret_hash,sizeof(temp),&temp);
			bnhash_to_hash(packet->u.d2cs_bnetd_accountloginreq.secret_hash,&try_hash);
			if (hash_eq(try_hash,secret_hash)==1) {
				eventlog(eventlog_level_debug,"on_d2cs_accountloginreq","user %s loggedin on d2cs",\
					account);
				reply=BNETD_D2CS_ACCOUNTLOGINREPLY_SUCCEED;
			} else {
				eventlog(eventlog_level_error,"on_d2cs_accountloginreq","user %s hash not match",\
					account);
				reply=BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
			}
		}
		account_unget_pass(pass_str);
	}
	if ((rpacket=packet_create(packet_class_d2cs_bnetd))) {
		packet_set_size(rpacket,sizeof(t_bnetd_d2cs_accountloginreply));
		packet_set_type(rpacket,BNETD_D2CS_ACCOUNTLOGINREPLY);
		bn_int_set(&rpacket->u.bnetd_d2cs_accountloginreply.h.seqno,\
			bn_int_get(packet->u.d2cs_bnetd_accountloginreq.h.seqno));
		bn_int_set(&rpacket->u.bnetd_d2cs_accountloginreply.reply,reply);
		queue_push_packet(conn_get_out_queue(c),rpacket);
		packet_del_ref(rpacket);
	}
	return 0;
}
Esempio n. 23
0
static int on_bnetd_charloginreply(t_connection * c, t_packet * packet)
{
	unsigned int	seqno;
	t_sq		* sq;
	t_connection	* client;
	t_packet	* opacket, * rpacket;
	int		result, reply, type;
	char const	* charname;
	t_elem		* elem;

	if (!packet || !c)
	    return -1;

	seqno=bn_int_get(packet->u.d2cs_bnetd.h.seqno);
	if (!(sq=sqlist_find_sq(seqno))) {
		eventlog(eventlog_level_error,__FUNCTION__,"seqno %d not found",seqno);
		return -1;
	}
	if (!(client=d2cs_connlist_find_connection_by_sessionnum(sq_get_clientid(sq)))) {
		eventlog(eventlog_level_error,__FUNCTION__,"client %d not found",sq_get_clientid(sq));
		sq_destroy(sq,&elem);
		return -1;
	}
	if (!(opacket=sq_get_packet(sq))) {
		eventlog(eventlog_level_error,__FUNCTION__,"previous packet missing (seqno: %d)",seqno);
		sq_destroy(sq,&elem);
		return -1;
	}
	type=packet_get_type(opacket);
	result=bn_int_get(packet->u.bnetd_d2cs_charloginreply.reply);
	if (type==CLIENT_D2CS_CREATECHARREQ) {
		charname=packet_get_str_const(opacket,sizeof(t_client_d2cs_createcharreq),MAX_CHARNAME_LEN);
		if (result==BNETD_D2CS_CHARLOGINREPLY_SUCCEED) {
			if (conn_check_multilogin(client,charname)<0) {
				eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
				reply = D2CS_CLIENT_CHARLOGINREPLY_FAILED;
			} else {
				reply= D2CS_CLIENT_CREATECHARREPLY_SUCCEED;
				eventlog(eventlog_level_info,__FUNCTION__,"character %s authed",charname);
				d2cs_conn_set_charname(client,charname);
				d2cs_conn_set_state(client,conn_state_char_authed);
			}
		} else {
			reply = D2CS_CLIENT_CREATECHARREPLY_FAILED;
			eventlog(eventlog_level_error,__FUNCTION__,"failed to auth character %s",charname);
		}
		if ((rpacket=packet_create(packet_class_d2cs))) {
			packet_set_size(rpacket,sizeof(t_d2cs_client_createcharreply));
			packet_set_type(rpacket,D2CS_CLIENT_CREATECHARREPLY);
			bn_int_set(&rpacket->u.d2cs_client_createcharreply.reply,reply);
			conn_push_outqueue(client,rpacket);
			packet_del_ref(rpacket);
		}
	} else if (type==CLIENT_D2CS_CHARLOGINREQ) {
		charname=packet_get_str_const(opacket,sizeof(t_client_d2cs_charloginreq),MAX_CHARNAME_LEN);
		if (result==BNETD_D2CS_CHARLOGINREPLY_SUCCEED) {
			if (conn_check_multilogin(client,charname)<0) {
				eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
				reply = D2CS_CLIENT_CHARLOGINREPLY_FAILED;
			} else {
				reply = D2CS_CLIENT_CHARLOGINREPLY_SUCCEED;
				eventlog(eventlog_level_info,__FUNCTION__,"character %s authed",charname);
				d2cs_conn_set_charname(client,charname);
				d2cs_conn_set_state(client,conn_state_char_authed);
			}
		} else {
			reply = D2CS_CLIENT_CHARLOGINREPLY_FAILED;
			eventlog(eventlog_level_error,__FUNCTION__,"failed to auth character %s",charname);
		}
		if ((rpacket=packet_create(packet_class_d2cs))) {
			packet_set_size(rpacket,sizeof(t_d2cs_client_charloginreply));
			packet_set_type(rpacket,D2CS_CLIENT_CHARLOGINREPLY);
			bn_int_set(&rpacket->u.d2cs_client_charloginreply.reply,reply);
			conn_push_outqueue(client,rpacket);
			packet_del_ref(rpacket);
		}
	} else {
		eventlog(eventlog_level_error,__FUNCTION__,"got bad packet type %d",type);
		sq_destroy(sq,&elem);
		return -1;
	}
	sq_destroy(sq,&elem);
	return 0;
}
Esempio n. 24
0
extern unsigned int db_d2char_create(char const * db_table,char const * account,char const * charname,t_d2charinfo_file * charinfo,char * chardata, unsigned int chardata_size)
{
	MYSQL			mysql = {0};    /* MySQL headers aren't correct initialized */
	char 			* sql;
	unsigned int	charinfo_size = sizeof(*charinfo);
	unsigned int	esc_charinfo_size = charinfo_size*2+1;	/* Escapeing binary data needs in worst case 2*size + 1 real zero */
	char			esc_charinfo[esc_charinfo_size];
	unsigned int	esc_chardata_size = chardata_size*2+1;
	char			esc_chardata[esc_chardata_size];

	/* This shall allow us to reconnect, after a disconnected MySQL link */
	if (!db_connect(&mysql)) return 0;

	if (!strlen(charinfo->header.realmname)) {
		eventlog(eventlog_level_error, __FUNCTION__, "got no realm name");
		db_close(&mysql);
		return 0;
	}

	if (!(sql = malloc(strlen("INSERT  (charname,accname,realm,mtime,ctime,btime,recage,charinfo,chardata) VALUES ('','','',FROM_UNIXTIME()+0,FROM_UNIXTIME()+0,NOW()+0,0,'','')")+strlen(db_table)+strlen(charname)+strlen(account)+strlen(charinfo->header.realmname)+10+10+esc_charinfo_size+esc_chardata_size+1)))
	{
		eventlog(eventlog_level_error, __FUNCTION__, "unable to allocate memory for query");
		free(sql);
		db_close(&mysql);
		return 0;
	}

	/* Escape charinfo and chardata */
	if(!mysql_real_escape_string(&mysql, esc_charinfo, (char const *) charinfo, charinfo_size))
	{
		eventlog(eventlog_level_error, __FUNCTION__, "charinfo escape failed");
		free(sql);
		db_close(&mysql);
		return 0;
	}
	if(!mysql_real_escape_string(&mysql, esc_chardata, chardata, chardata_size))
	{
		eventlog(eventlog_level_error, __FUNCTION__, "chardata escape failed");
		free(sql);
		db_close(&mysql);
		return 0;
	}

	/* Build real sql command */
	sprintf(sql, "INSERT %s (charname,accname,realm,mtime,ctime,btime,recage,charinfo,chardata) VALUES (\'%s\',\'%s\',\'%s\',FROM_UNIXTIME(%lu)+0,FROM_UNIXTIME(%u)+0,NOW()+0,0,\'%s\',\'%s\')",db_table,charname,account,charinfo->header.realmname,bn_int_get(charinfo->header.last_time),bn_int_get(charinfo->header.create_time),esc_charinfo,esc_chardata);
	if(mysql_query(&mysql,sql))
	{
		eventlog(eventlog_level_error, __FUNCTION__,"creation of character \"%s(*%s)@%s\" failed (%d:%s)",
					charname, account, charinfo->header.realmname, mysql_errno(&mysql),mysql_error(&mysql));
		free(sql);
		db_close(&mysql);
		return 0;
	}
	free(sql);
	if(mysql_affected_rows(&mysql)<=0)
	{
		eventlog(eventlog_level_error, __FUNCTION__,"zero data records \"%s(*%s)@%s\" inserted (%d:%s)",charname,account,charinfo->header.realmname,mysql_errno(&mysql),mysql_error(&mysql));
		free(sql);
		db_close(&mysql);
		return 0;
	}
	eventlog(eventlog_level_trace, __FUNCTION__, "successfully inserted record \"%s(*%s)@%s\"", charinfo->header.charname, charinfo->header.account, charinfo->header.realmname);
	db_close(&mysql);
	return 1;
}
Esempio n. 25
0
extern int handle_file_packet(t_connection * c, t_packet const * const packet)
{
    if (!c)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL connection",conn_get_socket(c));
	return -1;
    }
    if (!packet)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL packet",conn_get_socket(c));
	return -1;
    }
/* REMOVED BY UNDYING SOULZZ 4/3/02 */
/*
    if (packet_get_class(packet)!=packet_class_file)
    {
        eventlog(eventlog_level_error,__FUNCTION__,"[%d] got bad packet (class %d)",conn_get_socket(c),(int)packet_get_class(packet));
        return -1;
    }
 */   
    switch (conn_get_state(c))
    {
    case conn_state_connected:
	switch (packet_get_type(packet))
	{
	case CLIENT_FILE_REQ:
	{
	    char const * rawname;
	    
	    if (!(rawname = packet_get_str_const(packet,sizeof(t_client_file_req),MAX_FILENAME_STR)))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"[%d] got bad FILE_REQ (missing or too long filename)",conn_get_socket(c));
		
		return -1;
	    }
	    
	    file_send(c,rawname,
		      bn_int_get(packet->u.client_file_req.adid),
		      bn_int_get(packet->u.client_file_req.extensiontag),
		      bn_int_get(packet->u.client_file_req.startoffset),
		      1);
	}
	break;

	case CLIENT_FILE_REQ2:
	{
	    t_packet * rpacket = NULL;
	    if((rpacket = packet_create(packet_class_raw))) {
		    packet_set_size(rpacket,sizeof(t_server_file_unknown1));
		    bn_int_set( &rpacket->u.server_file_unknown1.unknown, 0xdeadbeef );
		    conn_push_outqueue(c, rpacket );
		    packet_del_ref( rpacket );
	    }
	    conn_set_state(c, conn_state_pending_raw);
	    break;
	}

	default:
	    eventlog(eventlog_level_error,__FUNCTION__,"[%d] unknown file packet type 0x%04x, len %u",conn_get_socket(c),packet_get_type(packet),packet_get_size(packet));
	    
	    break;
	}
	break;

    case conn_state_pending_raw:
	switch (packet_get_type(packet))
	{
	    case CLIENT_FILE_REQ3:
	    {
		char rawname[MAX_FILENAME_STR];

		psock_recv( conn_get_socket(c), rawname, MAX_FILENAME_STR, 0 );
		file_send(c, rawname, 0, 0, 0, 1);
	    }
	    break;	

	    default:
		eventlog(eventlog_level_error, __FUNCTION__, "[%d] unknown file packet type 0x%04x, len %u",conn_get_socket(c),packet_get_type(packet),packet_get_size(packet));

	    break;
	}
	break;

    default:
	eventlog(eventlog_level_error,__FUNCTION__,"[%d] unknown file connection state %d",conn_get_socket(c),(int)conn_get_state(c));
    }

    return 0;
}
Esempio n. 26
0
extern char const * packet_get_type_str(t_packet const * packet, t_packet_dir dir)
{
    if (!packet)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL packet");
	return "unknown";
    }

    switch (dir)
    {
    case packet_dir_from_client:
	switch (packet->pclass)
	{
	case packet_class_init:
	    return "CLIENT_INITCONN";
	case packet_class_bnet:
	    if (packet_get_size(packet)<sizeof(t_bnet_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_short_get(packet->u.bnet.h.type))
	    {
	    case CLIENT_COMPINFO1:
				return "CLIENT_COMPINFO1";
			case CLIENT_COMPINFO2:
				return "CLIENT_COMPINFO2";
			case CLIENT_COUNTRYINFO1:
				return "CLIENT_COUNTRYINFO1";
			case CLIENT_COUNTRYINFO_109:
				return "CLIENT_COUNTRYINFO_109";
			case CLIENT_CREATEACCTREQ1:
				return "CLIENT_CREATEACCTREQ1";
			case CLIENT_UNKNOWN_2B:
				return "CLIENT_UNKNOWN_2B";
			case CLIENT_PROGIDENT:
				return "CLIENT_PROGIDENT";
			case CLIENT_AUTHREQ1:
				return "CLIENT_AUTHREQ1";
			case CLIENT_AUTHREQ_109:
				return "CLIENT_AUTHREQ_109";
			case CLIENT_REGSNOOPREPLY:
				return "CLIENT_REGSNOOPREPLY";
			case CLIENT_ICONREQ:
				return "CLIENT_ICONREQ";
			case CLIENT_LADDERSEARCHREQ:
				return "CLIENT_LADDERSEARCHREQ";
			case CLIENT_CDKEY:
				return "CLIENT_CDKEY";
			case CLIENT_CDKEY2:
				return "CLIENT_CDKEY2";
			case CLIENT_CDKEY3:
				return "CLIENT_CDKEY3";
			case CLIENT_REALMLISTREQ:
				return "CLIENT_REALMLISTREQ";
			case CLIENT_REALMLISTREQ_110:
				return "CLIENT_REALMLISTREQ_110";
			case CLIENT_PROFILEREQ:
				return "CLIENT_PROFILEREQ";
			case CLIENT_UNKNOWN_37:
				return "CLIENT_UNKNOWN_37";
			case CLIENT_UNKNOWN_39:
				return "CLIENT_UNKNOWN_39";
			case CLIENT_LOGINREQ2:
				return "CLIENT_LOGINREQ2";
			case CLIENT_MOTD_W3:
				return "CLIENT_MOTD_W3";
			case CLIENT_LOGINREQ_W3:
				return "CLIENT_LOGINREQ_W3";
			case CLIENT_LOGONPROOFREQ:
				return "CLIENT_LOGONPROOFREQ";
			case CLIENT_CREATEACCOUNT_W3:
				return "CLIENT_CREATEACCOUNT_W3";
			case CLIENT_PASSCHANGEREQ:
				return "CLIENT_PASSCHANGEREQ";
			case CLIENT_PASSCHANGEPROOFREQ:
				return "CLIENT_PASSCHANGEPROOFREQ";
			case CLIENT_CHANGEGAMEPORT:
				return "CLIENT_CHANGEGAMEPORT";
			case CLIENT_CREATEACCTREQ2:
				return "CLIENT_CREATEACCTREQ2";
			case CLIENT_UDPOK:
				return "CLIENT_UDPOK";
			case CLIENT_FILEINFOREQ:
				return "CLIENT_FILEINFOREQ";
			case CLIENT_STATSREQ:
				return "CLIENT_STATSREQ";
			case CLIENT_LOGINREQ1:
				return "CLIENT_LOGINREQ1";
			case CLIENT_CHANGEPASSREQ:
				return "CLIENT_CHANGEPASSREQ";
			case CLIENT_PLAYERINFOREQ:
				return "CLIENT_PLAYERINFOREQ";
			case CLIENT_PROGIDENT2:
				return "CLIENT_PROGIDENT2";
			case CLIENT_JOINCHANNEL:
				return "CLIENT_JOINCHANNEL";
			case CLIENT_MESSAGE:
				return "CLIENT_MESSAGE";
			case CLIENT_GAMELISTREQ:
				return "CLIENT_GAMELISTREQ";
			case CLIENT_STARTGAME1:
				return "CLIENT_STARTGAME1";
			case CLIENT_UNKNOWN_1B:
				return "CLIENT_UNKNOWN_1B";
			case CLIENT_STARTGAME3:
				return "CLIENT_STARTGAME3";
			case CLIENT_STARTGAME4:
				return "CLIENT_STARTGAME4";
			case CLIENT_CLOSEGAME:
				return "CLIENT_CLOSEGAME";
			case CLIENT_CLOSEGAME2:
				return "CLIENT_CLOSEGAME2";
			case CLIENT_LEAVECHANNEL:
				return "CLIENT_LEAVECHANNEL";
			case CLIENT_MAPAUTHREQ1:
				return "CLIENT_MAPAUTHREQ1";
			case CLIENT_MAPAUTHREQ2:
				return "CLIENT_MAPAUTHREQ2";
			case CLIENT_ADREQ:
				return "CLIENT_ADREQ";
			case CLIENT_ADACK:
				return "CLIENT_ADACK";
			case CLIENT_ADCLICK:
				return "CLIENT_ADCLICK";
			case CLIENT_ADCLICK2:
				return "CLIENT_ADCLICK2";
			case CLIENT_UNKNOWN_17:
				return "CLIENT_UNKNOWN_17";
			case CLIENT_UNKNOWN_24:
				return "CLIENT_UNKNOWN_24";
			case CLIENT_LADDERREQ:
				return "CLIENT_LADDERREQ";
			case CLIENT_ECHOREPLY:
				return "CLIENT_ECHOREPLY";
			case CLIENT_PINGREQ:
				return "CLIENT_PINGREQ";
			case CLIENT_GAME_REPORT:
				return "CLIENT_GAME_REPORT";
			case CLIENT_JOIN_GAME:
				return "CLIENT_JOIN_GAME";
			case CLIENT_STATSUPDATE:
				return "CLIENT_STATSUPDATE";
			case CLIENT_REALMJOINREQ_109:
				return "CLIENT_REALMJOINREQ_109";
			case CLIENT_CHANGECLIENT:
				return "CLIENT_CHANGECLIENT";
			case CLIENT_SETEMAILREPLY:
				return "CLIENT_SETEMAILREPLY";
			case CLIENT_GETPASSWORDREQ:
				return "CLIENT_GETPASSWORDREQ";
			case CLIENT_CHANGEEMAILREQ:
				return "CLIENT_CHANGEEMAILREQ";
			case CLIENT_CRASHDUMP:
				return "CLIENT_CRASHDUMP";
			case CLIENT_FINDANONGAME:
				return "CLIENT_FINDANONGAME";
			case CLIENT_ARRANGEDTEAM_FRIENDSCREEN:
				return "CLIENT_ARRANGEDTEAM_FRIENDSCREEN";
			case CLIENT_ARRANGEDTEAM_INVITE_FRIEND:
				return "CLIENT_ARRANGEDTEAM_INVITE_FRIEND";
			case CLIENT_ARRANGEDTEAM_ACCEPT_DECLINE_INVITE:
				return "CLIENT_ARRANGEDTEAM_ACCEPT_DECLINE_INVITE";
			case CLIENT_FRIENDSLISTREQ:
				return "CLIENT_FRIENDSLISTREQ";
			case CLIENT_FRIENDINFOREQ:
				return "CLIENT_FRIENDINFOREQ";
			case CLIENT_CLANINFOREQ:
				return "CLIENT_CLANINFOREQ";
			case CLIENT_CLAN_CREATEREQ:
				return "CLIENT_CLAN_CREATEREQ";
			case CLIENT_CLAN_CREATEINVITEREQ:
				return "CLIENT_CLAN_CREATEINVITEREQ";
			case CLIENT_CLAN_CREATEINVITEREPLY:
				return "CLIENT_CLAN_CREATEINVITEREPLY";
			case CLIENT_CLAN_DISBANDREQ:
				return "CLIENT_CLAN_DISBANDREQ";
			case CLIENT_CLAN_MEMBERNEWCHIEFREQ:
				return "CLIENT_CLAN_MEMBERNEWCHIEFREQ";
			case CLIENT_CLAN_INVITEREQ:
				return "CLIENT_CLAN_INVITEREQ";
			case CLIENT_CLANMEMBER_REMOVE_REQ:
				return "CLIENT_CLANMEMBER_REMOVE_REQ";
			case CLIENT_CLAN_INVITEREPLY:
				return "CLIENT_CLAN_INVITEREPLY";
			case CLIENT_CLANMEMBER_RANKUPDATE_REQ:
				return "CLIENT_CLANMEMBER_RANKUPDATE_REQ";
			case CLIENT_CLAN_MOTDCHG:
				return "CLIENT_CLAN_MOTDCHG";
			case CLIENT_CLAN_MOTDREQ:
				return "CLIENT_CLAN_MOTDREQ";
			case CLIENT_CLANMEMBERLIST_REQ:
				return "CLIENT_CLANMEMBERLIST_REQ";
			}
			return "unknown";

	case packet_class_file:
	    if (packet_get_size(packet)<sizeof(t_file_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_short_get(packet->u.file.h.type))
	    {
	    case CLIENT_FILE_REQ:
		return "CLIENT_FILE_REQ";
	    }
	    return "unknown";

	case packet_class_udp:
	    if (packet_get_size(packet)<sizeof(t_udp_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_int_get(packet->u.udp.h.type))
	    {
	    case SERVER_UDPTEST: /* we get these if we send stuff to ourself */
		return "SERVER_UDPTEST";
	    case CLIENT_UDPPING:
		return "CLIENT_UDPPING";
	    case CLIENT_SESSIONADDR1:
		return "CLIENT_SESSIONADDR1";
	    case CLIENT_SESSIONADDR2:
		return "CLIENT_SESSIONADDR2";
	    }
	    return "unknown";

	case packet_class_raw:
	    return "CLIENT_RAW";

       case packet_class_d2game:
	    if (packet_get_size(packet)<sizeof(t_d2game_header))
	    {
               eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
               return "unknown";
	    }
	    switch (bn_byte_get(packet->u.d2game.h.type))
	    {
	    default:
		return "CLIENT_D2GAME";
	    }
	    return "unknown";

        case packet_class_d2cs:
                return "D2CS";
        case packet_class_d2gs:
                return "D2GS";
        case packet_class_d2cs_bnetd:
                return "D2CS_BNETD";

	case packet_class_w3route:
	    if (packet_get_size(packet)<sizeof(t_w3route_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_short_get(packet->u.bnet.h.type))
	    {
	    case CLIENT_W3ROUTE_REQ:
	        return "CLIENT_W3ROUTE_REQ";
	    case CLIENT_W3ROUTE_LOADINGDONE:
	        return "CLIENT_W3ROUTE_LOADINGDONE";
	    case CLIENT_W3ROUTE_ABORT:
	        return "CLIENT_W3ROUTE_ABORT";
	    case CLIENT_W3ROUTE_CONNECTED:
	        return "CLIENT_W3ROUTE_CONNECTED";
	    case CLIENT_W3ROUTE_ECHOREPLY:
	        return "CLIENT_W3ROUTE_ECHOREPLY";
	    case CLIENT_W3ROUTE_GAMERESULT:
	        return "CLIENT_W3ROUTE_GAMERESULT";
	    case CLIENT_W3ROUTE_GAMERESULT_W3XP:
	        return "CLIENT_W3ROUTE_GAMERESULT_W3XP";
	    }
	    return "unknown";

	case packet_class_none:
	    return "unknown";
	}

	eventlog(eventlog_level_error,__FUNCTION__,"packet has invalid class %d",(int)packet->pclass);
	return "unknown";

    case packet_dir_from_server:
	switch (packet->pclass)
	{
	case packet_class_init:
	    return "unknown";
	case packet_class_bnet:
	    if (packet_get_size(packet)<sizeof(t_bnet_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_short_get(packet->u.bnet.h.type))
	    {
	    case SERVER_COMPREPLY:
				return "SERVER_COMPREPLY";
			case SERVER_SESSIONKEY1:
				return "SERVER_SESSIONKEY1";
			case SERVER_SESSIONKEY2:
				return "SERVER_SESSIONKEY2";
			case SERVER_CREATEACCTREPLY1:
				return "SERVER_CREATEACCTREPLY1";
			case SERVER_AUTHREQ1:
				return "SERVER_AUTHREQ1";
			case SERVER_AUTHREQ_109:
				return "SERVER_AUTHREQ_109";
			case SERVER_AUTHREPLY1:
				return "SERVER_AUTHREPLY1";
			case SERVER_AUTHREPLY_109:
				return "SERVER_AUTHREPLY_109";
			case SERVER_REGSNOOPREQ:
				return "SERVER_REGSNOOPREQ";
			case SERVER_ICONREPLY:
				return "SERVER_ICONREPLY";
			case SERVER_LADDERSEARCHREPLY:
				return "SERVER_LADDERSEARCHREPLY";
			case SERVER_CDKEYREPLY:
				return "SERVER_CDKEYREPLY";
			case SERVER_CDKEYREPLY2:
				return "SERVER_CDKEYREPLY2";
			case SERVER_CDKEYREPLY3:
				return "SERVER_CDKEYREPLY3";
			case SERVER_REALMLISTREPLY:
				return "SERVER_REALMLISTREPLY";
			case SERVER_REALMLISTREPLY_110:
				return "SERVER_REALMLISTREPLY_110";
			case SERVER_PROFILEREPLY:
				return "SERVER_PROFILEREPLY";
			case SERVER_UNKNOWN_37:
				return "SERVER_UNKNOWN_37";
			case SERVER_MOTD_W3:
				return "SERVER_MOTD_W3";
			case SERVER_LOGINREPLY_W3:
				return "SERVER_LOGINREPLY_W3";
			case SERVER_LOGONPROOFREPLY:
				return "SERVER_LOGONPROOFREPLY";
			case SERVER_CREATEACCOUNT_W3:
				return "SERVER_CREATEACCOUNT_W3";
			case SERVER_PASSCHANGEREPLY:
				return "SERVER_PASSCHANGEREPLY";
			case SERVER_PASSCHANGEPROOFREPLY:
				return "SERVER_PASSCHANGEPROOFREPLY";
			case SERVER_LOGINREPLY2:
				return "SERVER_LOGINREPLY2";
			case SERVER_CREATEACCTREPLY2:
				return "SERVER_CREATEACCTREPLY2";
			case SERVER_FILEINFOREPLY:
				return "SERVER_FILEINFOREPLY";
			case SERVER_STATSREPLY:
				return "SERVER_STATSREPLY";
			case SERVER_LOGINREPLY1:
				return "SERVER_LOGINREPLY1";
			case SERVER_CHANGEPASSACK:
				return "SERVER_CHANGEPASSACK";
			case SERVER_PLAYERINFOREPLY:
				return "SERVER_PLAYERINFOREPLY";
			case SERVER_CHANNELLIST:
				return "SERVER_CHANNELLIST";
			case SERVER_SERVERLIST:
				return "SERVER_SERVERLIST";
			case SERVER_MESSAGE:
				return "SERVER_MESSAGE";
			case SERVER_GAMELISTREPLY:
				return "SERVER_GAMELISTREPLY";
			case SERVER_STARTGAME1_ACK:
				return "SERVER_STARTGAME1_ACK";
			case SERVER_STARTGAME3_ACK:
				return "SERVER_STARTGAME3_ACK";
			case SERVER_STARTGAME4_ACK:
				return "SERVER_STARTGAME4_ACK";
			case SERVER_MAPAUTHREPLY1:
				return "SERVER_MAPAUTHREPLY1";
			case SERVER_MAPAUTHREPLY2:
				return "SERVER_MAPAUTHREPLY2";
			case SERVER_ADREPLY:
				return "SERVER_ADREPLY";
			case SERVER_ADCLICKREPLY2:
				return "SERVER_ADCLICKREPLY2";
			case SERVER_LADDERREPLY:
				return "SERVER_LADDERREPLY";
			case SERVER_ECHOREQ:
				return "SERVER_ECHOREQ";
			case SERVER_PINGREPLY:
				return "SERVER_PINGREPLY";
			case SERVER_REALMJOINREPLY_109:
				return "SERVER_REALMJOINREPLY_109";
			case SERVER_SETEMAILREQ:
				return "SERVER_SETEMAILREQ";
			case SERVER_FINDANONGAME:
				return "SERVER_FINDANONGAME";
			case SERVER_ARRANGEDTEAM_FRIENDSCREEN:
				return "SERVER_ARRANGEDTEAM_FRIENDSCREEN";
			case SERVER_ARRANGEDTEAM_INVITE_FRIEND_ACK:
				return "SERVER_ARRANGEDTEAM_INVITE_FRIEND_ACK";
			case SERVER_ARRANGEDTEAM_SEND_INVITE:
				return "SERVER_ARRANGEDTEAM_SEND_INVITE";
			case SERVER_ARRANGEDTEAM_MEMBER_DECLINE:
				return "SERVER_ARRANGEDTEAM_MEMBER_DECLINE";
			case SERVER_FRIENDSLISTREPLY:
				return "SERVER_FRIENDSLISTREPLY";
			case SERVER_FRIENDINFOREPLY:
				return "SERVER_FRIENDINFOREPLY";
			case SERVER_FRIENDADD_ACK:
				return "SERVER_FRIENDADD_ACK";
			case SERVER_FRIENDDEL_ACK:
				return "SERVER_FRIENDDEL_ACK";
			case SERVER_FRIENDMOVE_ACK:
				return "SERVER_FRIENDMOVE_ACK";
			case SERVER_CLANINFOREPLY:
				return "SERVER_CLANINFO_REPLY";
			case SERVER_CLAN_CREATEREPLY:
				return "SERVER_CLAN_CREATEREPLY";
			case SERVER_CLAN_CREATEINVITEREPLY:
				return "SERVER_CLAN_CREATEINVITEREPLY";
			case SERVER_CLAN_CREATEINVITEREQ:
				return "SERVER_CLAN_CREATEINVITEREQ";
			case SERVER_CLAN_DISBANDREPLY:
				return "SERVER_CLAN_DISBANDREPLY";
			case SERVER_CLAN_MEMBERNEWCHIEFREPLY:
				return "SERVER_CLAN_MEMBERNEWCHIEFREPLY";
			case SERVER_CLAN_CLANACK:
				return "SERVER_CLAN_CLANACK";
			case SERVER_CLANQUITNOTIFY:
				return "SERVER_CLANQUITNOTIFY";
			case SERVER_CLAN_INVITEREPLY:
				return "SERVER_CLAN_INVITEREPLY";
			case SERVER_CLANMEMBER_REMOVE_REPLY:
				return "SERVER_CLANMEMBER_REMOVE_REPLY";
			case SERVER_CLAN_INVITEREQ:
				return "SERVER_CLAN_INVITEREQ";
			case SERVER_CLANMEMBER_RANKUPDATE_REPLY:
				return "SERVER_CLANMEMBER_RANKUPDATE_REPLY";
			case SERVER_CLAN_MOTDREPLY:
				return "SERVER_CLAN_MOTDREPLY";
			case SERVER_CLANMEMBERLIST_REPLY:
				return "SERVER_CLANMEMBERLIST_REPLY";
			case SERVER_CLANMEMBER_REMOVED_NOTIFY:
				return "SERVER_CLANMEMBER_REMOVED_NOTIFY";
			case SERVER_CLANMEMBERUPDATE:
				return "SERVER_CLANMEMBERUPDATE";
			}
			return "unknown";

	case packet_class_file:
	    if (packet_get_size(packet)<sizeof(t_file_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_short_get(packet->u.file.h.type))
	    {
	    case SERVER_FILE_REPLY:
		return "SERVER_FILE_REPLY";
	    }
	    return "unknown";

	case packet_class_udp:
	    if (packet_get_size(packet)<sizeof(t_udp_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_int_get(packet->u.udp.h.type))
	    {
	    case SERVER_UDPTEST:
		return "SERVER_UDPTEST";
	    }
	    return "unknown";

	case packet_class_raw:
	    return "SERVER_RAW";

	case packet_class_d2game:
	    if (packet_get_size(packet)<sizeof(t_d2game_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_byte_get(packet->u.d2game.h.type))
	    {
	    default:
		return "SERVER_D2GAME";
	    }
	    return "unknown";

        case packet_class_d2cs:
                return "D2CS";
        case packet_class_d2gs:
                return "D2GS";
        case packet_class_d2cs_bnetd:
                return "D2CS_BNETD";

	case packet_class_w3route:
	    if (packet_get_size(packet)<sizeof(t_w3route_header))
	    {
		eventlog(eventlog_level_error,__FUNCTION__,"packet is shorter than header (len=%u)",packet_get_size(packet));
		return "unknown";
	    }
	    switch (bn_short_get(packet->u.bnet.h.type))
	    {
	    case SERVER_W3ROUTE_READY:
	        return "SERVER_W3ROUTE_READY";
	    case SERVER_W3ROUTE_LOADINGACK:
	        return "SERVER_W3ROUTE_LOADINGACK";
	    case SERVER_W3ROUTE_ECHOREQ:
	        return "SERVER_W3ROUTE_ECHOREQ";
	    case SERVER_W3ROUTE_ACK:
	        return "SERVER_W3ROUTE_ACK";
	    case SERVER_W3ROUTE_PLAYERINFO:
	        return "SERVER_W3ROUTE_PLAYERINFO";
	    case SERVER_W3ROUTE_LEVELINFO:
	        return "SERVER_W3ROUTE_LEVELINFO";
	    case SERVER_W3ROUTE_STARTGAME1:
	        return "SERVER_W3ROUTE_STARTGAME1";
	    case SERVER_W3ROUTE_STARTGAME2:
	        return "SERVER_W3ROUTE_STARTGAME2";
	    }
	    return "unknown";
 	case packet_class_wolgameres:
	    return "CLIENT_WOLGAMERES";
	case packet_class_none:
	    return "unknown";
	}

	eventlog(eventlog_level_error,__FUNCTION__,"packet has invalid class %d",(int)packet->pclass);
	return "unknown";
    }

    eventlog(eventlog_level_error,__FUNCTION__,"got unknown direction %d",(int)dir);
    return "unknown";
}
Esempio n. 27
0
extern int main(int argc, char * argv[])
{
    int                a;
    int                sd;
    struct sockaddr_in saddr;
    t_packet *         packet;
    t_packet *         rpacket;
    t_packet *         fpacket;
    char const *       clienttag=NULL;
    char const *       archtag=NULL;
    char const *       servname=NULL;
    unsigned short     servport=0;
    char const *       hexfile=NULL;
    char               text[MAX_MESSAGE_LEN];
    char const *       reqfile=NULL;
    struct hostent *   host;
    unsigned int       commpos;
    struct termios     in_attr_old;
    struct termios     in_attr_new;
    int                changed_in;
    unsigned int       currsize;
    unsigned int       filelen;
    unsigned int       startoffset;
    int                startoffsetoverride=0;
#define EXIST_ACTION_UNSPEC    -1
#define EXIST_ACTION_ASK        0
#define EXIST_ACTION_OVERWRITE  1
#define EXIST_ACTION_BACKUP     2
#define EXIST_ACTION_RESUME     3 
    int		       exist_action=EXIST_ACTION_UNSPEC;
    struct stat        exist_buf;
    char const *       filename;
    FILE *             fp;
    FILE *             hexstrm=NULL;
    int                fd_stdin;
    t_bnettime         bntime;
    time_t             tm;
    char               timestr[FILE_TIME_MAXLEN];
    unsigned int       screen_width,screen_height;
    int                munged;

    if (argc<1 || !argv || !argv[0])
    {
	fprintf(stderr,"bad arguments\n");
	return STATUS_FAILURE;
    }
    
    for (a=1; a<argc; a++)
	if (servname && isdigit((int)argv[a][0]) && a+1>=argc)
	{
            if (str_to_ushort(argv[a],&servport)<0)
            {
                fprintf(stderr,"%s: \"%s\" should be a positive integer\n",argv[0],argv[a]);
                usage(argv[0]);
            }
	}
	else if (!servname && argv[a][0]!='-' && a+2>=argc)
	    servname = argv[a];
        else if (strcmp(argv[a],"-b")==0 || strcmp(argv[a],"--client=SEXP")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_BROODWARS;
        }
        else if (strcmp(argv[a],"-d")==0 || strcmp(argv[a],"--client=DRTL")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_DIABLORTL;
        }
        else if (strcmp(argv[a],"--client=DSHR")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_DIABLOSHR;
        }
        else if (strcmp(argv[a],"-s")==0 || strcmp(argv[a],"--client=STAR")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_STARCRAFT;
        }
        else if (strcmp(argv[a],"--client=SSHR")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_SHAREWARE;
        }
	else if (strcmp(argv[a],"-w")==0 || strcmp(argv[a],"--client=W2BN")==0)
	{
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_WARCIIBNE;
	}
        else if (strcmp(argv[a],"--client=D2DV")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_DIABLO2DV;
        }
        else if (strcmp(argv[a],"--client=D2XP")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_DIABLO2XP;
        }
        else if (strcmp(argv[a],"--client=WAR3")==0)
        {
            if (clienttag)
            {
                fprintf(stderr,"%s: client type was already specified as \"%s\"\n",argv[0],clienttag);
                usage(argv[0]);
            }
            clienttag = CLIENTTAG_WARCRAFT3;
        }
        else if (strncmp(argv[a],"--client=",9)==0)
        {
            fprintf(stderr,"%s: unknown client tag \"%s\"\n",argv[0],&argv[a][9]);
            usage(argv[0]);
        }
	else if (strncmp(argv[a],"--hexdump=",10)==0)
	{
	    if (hexfile)
	    {
		fprintf(stderr,"%s: hexdump file was already specified as \"%s\"\n",argv[0],hexfile);
		usage(argv[0]);
	    }
	    hexfile = &argv[a][10];
	}
        else if (strcmp(argv[a],"--arch=IX86")==0)
        {
            if (archtag)
            {
                fprintf(stderr,"%s: client arch was already specified as \"%s\"\n",argv[0],archtag);
                usage(argv[0]);
            }
            archtag = ARCHTAG_WINX86;
        }
        else if (strcmp(argv[a],"--arch=PMAC")==0)
        {
            if (archtag)
            {
                fprintf(stderr,"%s: client arch was already specified as \"%s\"\n",argv[0],archtag);
                usage(argv[0]);
            }
            archtag = ARCHTAG_MACPPC;
        }
        else if (strcmp(argv[a],"--arch=XMAC")==0)
        {
            if (archtag)
            {
                fprintf(stderr,"%s: client arch was already specified as \"%s\"\n",argv[0],archtag);
                usage(argv[0]);
            }
            archtag = ARCHTAG_OSXPPC;
        }
        else if (strncmp(argv[a],"--arch=",7)==0)
        {
            fprintf(stderr,"%s: unknown arch tag \"%s\"\n",argv[0],&argv[a][9]);
            usage(argv[0]);
        }
	else if (strncmp(argv[a],"--startoffset=",14)==0)
	{
	    if (startoffsetoverride)
	    {
		fprintf(stderr,"%s: startoffset was already specified as %u\n",argv[0],startoffset);
		usage(argv[0]);
	    }
            if (str_to_uint(&argv[a][14],&startoffset)<0)
            {
                fprintf(stderr,"%s: startoffset \"%s\" should be a positive integer\n",argv[0],&argv[a][14]);
                usage(argv[0]);
            }
	    startoffsetoverride = 1;
	}
	else if (strncmp(argv[a],"--exists=",9)==0)
	{
	    if (exist_action!=EXIST_ACTION_UNSPEC)
	    {
		fprintf(stderr,"%s: exists was already specified\n",argv[0]);
		usage(argv[0]);
	    }
	    if (argv[a][9]=='o' || argv[a][9]=='O')
	    	exist_action = EXIST_ACTION_OVERWRITE;
	    else if (argv[a][9]=='a' || argv[a][9]=='A')
	    	exist_action = EXIST_ACTION_ASK;
	    else if (argv[a][9]=='b' || argv[a][9]=='B')
	    	exist_action = EXIST_ACTION_BACKUP;
	    else if (argv[a][9]=='r' || argv[a][9]=='R')
	    	exist_action = EXIST_ACTION_RESUME;
	    else {
		fprintf(stderr,"%s: exists must begin with a,A,o,O,b,B,r or R",argv[0]);
		usage(argv[0]);
	    }
	}
	else if (strncmp(argv[a],"--file=",7)==0)
	{
	    if (reqfile)
	    {
		fprintf(stderr,"%s: file was already specified as \"%s\"\n",argv[0],reqfile);
		usage(argv[0]);
	    }
	    reqfile = &argv[a][7];
	}
	else if (strcmp(argv[a],"-v")==0 || strcmp(argv[a],"--version")==0)
	{
            printf("version "BNETD_VERSION"\n");
            return 0;
	}
	else if (strcmp(argv[a],"-h")==0 || strcmp(argv[a],"--help")==0 || strcmp(argv[a],"--usage")==0)
            usage(argv[0]);
        else if (strcmp(argv[a],"--client")==0 || strcmp(argv[a],"--hexdump")==0)
        {
            fprintf(stderr,"%s: option \"%s\" requires an argument\n",argv[0],argv[a]);
            usage(argv[0]);
        }
	else
	{
	    fprintf(stderr,"%s: unknown option \"%s\"\n",argv[0],argv[a]);
	    usage(argv[0]);
	}
    
    if (servport==0)
        servport = BNETD_SERV_PORT;
    if (!clienttag)
        clienttag = CLIENTTAG_STARCRAFT;
    if (!archtag)
        archtag = ARCHTAG_WINX86;
    if (!servname)
	servname = BNETD_DEFAULT_HOST;
    if (exist_action==EXIST_ACTION_UNSPEC)
	exist_action = EXIST_ACTION_ASK;
    
    if (hexfile)
	if (!(hexstrm = fopen(hexfile,"w")))
	    fprintf(stderr,"%s: could not open file \"%s\" for writing the hexdump (fopen: %s)",argv[0],hexfile,strerror(errno));
	else
	    fprintf(hexstrm,"# dump generated by bnftp version "BNETD_VERSION"\n");
    
    if (psock_init()<0)
    {
        fprintf(stderr,"%s: could not inialialize socket functions\n",argv[0]);
        return STATUS_FAILURE;
    }
    
    if (!(host = gethostbyname(servname)))
    {
	fprintf(stderr,"%s: unknown host \"%s\"\n",argv[0],servname);
	return STATUS_FAILURE;
    }
    
    fd_stdin = fileno(stdin);
    if (tcgetattr(fd_stdin,&in_attr_old)>=0)
    {
        in_attr_new = in_attr_old;
        in_attr_new.c_lflag &= ~(ECHO | ICANON); /* turn off ECHO and ICANON */
	in_attr_new.c_cc[VMIN]  = 1; /* require reads to return at least one byte */
        in_attr_new.c_cc[VTIME] = 0; /* no timeout */
        tcsetattr(fd_stdin,TCSANOW,&in_attr_new);
        changed_in = 1;
    }
    else
    {
	fprintf(stderr,"%s: could not get terminal attributes for stdin\n",argv[0]);
	changed_in = 0;
    }
    
    if (client_get_termsize(fd_stdin,&screen_width,&screen_height)<0)
    {
        fprintf(stderr,"%s: could not determine screen size\n",argv[0]);
        if (changed_in)
            tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
        return STATUS_FAILURE;
    }
    
    if ((sd = psock_socket(PSOCK_PF_INET,PSOCK_SOCK_STREAM,PSOCK_IPPROTO_TCP))<0)
    {
	fprintf(stderr,"%s: could not create socket (psock_socket: %s)\n",argv[0],strerror(psock_errno()));
	if (changed_in)
	    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	return STATUS_FAILURE;
    }
    
    memset(&saddr,0,sizeof(saddr));
    saddr.sin_family = PSOCK_AF_INET;
    saddr.sin_port   = htons(servport);
    memcpy(&saddr.sin_addr.s_addr,host->h_addr_list[0],host->h_length);
    if (psock_connect(sd,(struct sockaddr *)&saddr,sizeof(saddr))<0)
    {
	fprintf(stderr,"%s: could not connect to server \"%s\" port %hu (psock_connect: %s)\n",argv[0],servname,servport,strerror(psock_errno()));
	if (changed_in)
	    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	return STATUS_FAILURE;
    }
    
    printf("Connected to %s:%hu.\n",inet_ntoa(saddr.sin_addr),servport);
    
#ifdef CLIENTDEBUG
    eventlog_set(stderr);
#endif
    
    if (!(packet = packet_create(packet_class_init)))
    {
	fprintf(stderr,"%s: could not create packet\n",argv[0]);
	if (changed_in)
	    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	return STATUS_FAILURE;
    }
    bn_byte_set(&packet->u.client_initconn.class,CLIENT_INITCONN_CLASS_FILE);
    if (hexstrm)
    {
	fprintf(hexstrm,"%d: send class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n",
		sd,
		packet_get_class_str(packet),(unsigned int)packet_get_class(packet),
		packet_get_type_str(packet,packet_dir_from_client),packet_get_type(packet),
		packet_get_size(packet));
	hexdump(hexstrm,packet_get_raw_data(packet,0),packet_get_size(packet));
    }
    client_blocksend_packet(sd,packet);
    packet_del_ref(packet);
    
    if (!(rpacket = packet_create(packet_class_file)))
    {
	fprintf(stderr,"%s: could not create packet\n",argv[0]);
	if (changed_in)
	    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	return STATUS_FAILURE;
    }
    
    if (!(fpacket = packet_create(packet_class_raw)))
    {
	fprintf(stderr,"%s: could not create packet\n",argv[0]);
	if (changed_in)
	    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	packet_del_ref(rpacket);
	return STATUS_FAILURE;
    }
    
    if (!reqfile) /* if not specified on the command line then prompt for it */
    {
    	munged = 1;
    	commpos = 0;
    	text[0] = '\0';
	
    	for (;;)
    	{
	    switch (client_get_comm("filename: ",text,sizeof(text),&commpos,1,munged,screen_width))
	    {
	    case -1: /* cancel or error */
	    	printf("\n");
	    	if (changed_in)
		    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	    	packet_del_ref(fpacket);
	    	packet_del_ref(rpacket);
	    	return STATUS_FAILURE;
		
	    case 0: /* timeout */
	    	munged = 0;
	    	continue;
		
	    case 1:
	    	munged = 0;
	    	if (text[0]=='\0')
		    continue;
	    	printf("\n");
	    }
	    break;
    	}
	reqfile = text;
    }
    
    if (stat(reqfile,&exist_buf)==0) /* check if the file exists */
    {
	char text2[MAX_MESSAGE_LEN];
	    
    	munged = 1;
	commpos = 0;
	text2[0] = '\0';
	
  	while (exist_action==EXIST_ACTION_ASK)
	{
	    switch (client_get_comm("File exists [O]verwrite, [B]ackup or [R]esume?: ",text2,sizeof(text2),&commpos,1,munged,screen_width))
	    {
	    case -1: /* cancel or error */
	    	printf("\n");
	    	if (changed_in)
		    tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
	    	packet_del_ref(fpacket);
	    	packet_del_ref(rpacket);
	    	return STATUS_FAILURE;
		
	    case 0: /* timeout */
	    	munged = 0;
	    	continue;
		
	    case 1:
	    	munged = 0;
	    	if (text2[0]=='\0')
		    continue;
	    	printf("\n");
		break;
	    }
	    
	    switch (text2[0])
	    {
	    case 'o':
	    case 'O':
		exist_action = EXIST_ACTION_OVERWRITE;
		break;
	    case 'b':
	    case 'B':
		exist_action = EXIST_ACTION_BACKUP;
		break;
	    case 'r':
	    case 'R':
		exist_action = EXIST_ACTION_RESUME;
		break;
	    default:
		printf("Please answer with o,O,b,B,r or R.\n");
		munged = 1;
		continue;
	    }
	    break;
	}
	
	switch (exist_action)
	{
	case EXIST_ACTION_OVERWRITE:
	    if (!startoffsetoverride)
	    	startoffset = 0;
	    break;
	case EXIST_ACTION_BACKUP:
	    {
		char *       bakfile;
		unsigned int bnr;
		int          renamed=0;
		
		if (!(bakfile = malloc(strlen(reqfile)+1+2+1))) /* assuming we go up to bnr 99 we need reqfile+'.'+'99'+'\0' */
		{
		    fprintf(stderr,"%s: unable to allocate memory for backup filename.\n",argv[0]);
		    if (changed_in)
			tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
		    packet_del_ref(fpacket);
		    packet_del_ref(rpacket);
		    return STATUS_FAILURE;
		}
		
		for (bnr=0; bnr<100; bnr++)
		{
		    sprintf(bakfile,"%s.%d",reqfile,bnr);
		    if (stat(bakfile,&exist_buf)==0)
			continue; /* backup exists */
		    /* backup does not exist */
		    if (rename(reqfile,bakfile)<0) /* just rename the existing file to the backup */
			fprintf(stderr,"%s: could not create backup file \"%s\" (rename: %s)\n",argv[0],bakfile,strerror(errno));
		    else
		    {
			renamed = 1;
			printf("Renaming \"%s\" to \"%s\".\n",reqfile,bakfile);
		    }
		    break;
		}
		free(bakfile);
		if (!renamed)
		{
		    fprintf(stderr,"%s: could not create backup for \"%s\".\n",argv[0],reqfile);
		    if (changed_in)
			tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
		    packet_del_ref(fpacket);
		    packet_del_ref(rpacket);
		    return STATUS_FAILURE;
		}
	    	if (!startoffsetoverride)
	    	    startoffset = 0;
	    }
	    break;
	case EXIST_ACTION_RESUME:
	    if (!startoffsetoverride)
	    	startoffset = exist_buf.st_size;
	    break;	    	
	}	
    }
    else
	if (!startoffsetoverride)
	    startoffset = 0;
    
    if (changed_in)
	tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
    
    if (!(packet = packet_create(packet_class_file)))
    {
	fprintf(stderr,"%s: could not create packet\n",argv[0]);
	packet_del_ref(fpacket);
	packet_del_ref(rpacket);
	return STATUS_FAILURE;
    }
    packet_set_size(packet,sizeof(t_client_file_req));
    packet_set_type(packet,CLIENT_FILE_REQ);
    bn_int_tag_set(&packet->u.client_file_req.archtag,archtag);
    bn_int_tag_set(&packet->u.client_file_req.clienttag,clienttag);
    bn_int_set(&packet->u.client_file_req.adid,0);
    bn_int_set(&packet->u.client_file_req.extensiontag,0);
    bn_int_set(&packet->u.client_file_req.startoffset,startoffset);
    bn_long_set_a_b(&packet->u.client_file_req.timestamp,0x00000000,0x00000000);
    packet_append_string(packet,reqfile);
    if (hexstrm)
    {
	fprintf(hexstrm,"%d: send class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n",
		sd,
		packet_get_class_str(packet),(unsigned int)packet_get_class(packet),
		packet_get_type_str(packet,packet_dir_from_client),packet_get_type(packet),
		packet_get_size(packet));
	hexdump(hexstrm,packet_get_raw_data(packet,0),packet_get_size(packet));
    }
    printf("\nRequesting info...");
    fflush(stdout);
    client_blocksend_packet(sd,packet);
    packet_del_ref(packet);
    
    do
    {
	if (client_blockrecv_packet(sd,rpacket)<0)
	{
	    fprintf(stderr,"%s: server closed connection\n",argv[0]);
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
	if (hexstrm)
	{
	    fprintf(hexstrm,"%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n",
		     sd,
		     packet_get_class_str(rpacket),(unsigned int)packet_get_class(rpacket),
		     packet_get_type_str(rpacket,packet_dir_from_server),packet_get_type(rpacket),
		     packet_get_size(rpacket));
	    hexdump(hexstrm,packet_get_raw_data(rpacket,0),packet_get_size(rpacket));
	}
    }
    while (packet_get_type(rpacket)!=SERVER_FILE_REPLY);
    
    filelen = bn_int_get(rpacket->u.server_file_reply.filelen);
    bn_long_to_bnettime(rpacket->u.server_file_reply.timestamp,&bntime);
    tm = bnettime_to_time(bntime);
    strftime(timestr,FILE_TIME_MAXLEN,FILE_TIME_FORMAT,localtime(&tm));
    filename = packet_get_str_const(rpacket,sizeof(t_server_file_reply),MAX_FILENAME_STR);
    
    if (exist_action==EXIST_ACTION_RESUME)
    {
	if (!(fp = fopen(reqfile,"ab")))
	{
	    fprintf(stderr,"%s: could not open file \"%s\" for appending (fopen: %s)\n",argv[0],reqfile,strerror(errno));
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
    }
    else
    {
	if (!(fp = fopen(reqfile,"wb")))
	{
	    fprintf(stderr,"%s: could not open file \"%s\" for writing (fopen: %s)\n",argv[0],reqfile,strerror(errno));
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
    }
    
    printf("\n name: \"");
    str_print_term(stdout,filename,0,0);
    printf("\"\n changed: %s\n length: %u bytes\n",timestr,filelen);
    fflush(stdout);
    
    if (startoffset>0) {
	filelen -= startoffset; /* for resuming files */
	printf("Resuming at position %u (%u bytes remaining).\n",startoffset,filelen);
    }
    
    printf("\nSaving to \"%s\"...",reqfile);

    for (currsize=0; currsize+MAX_PACKET_SIZE<=filelen; currsize+=MAX_PACKET_SIZE)
    {
	printf(".");
	fflush(stdout);
	
	if (client_blockrecv_raw_packet(sd,fpacket,MAX_PACKET_SIZE)<0)
	{
	    printf("error\n");
	    fprintf(stderr,"%s: server closed connection\n",argv[0]);
	    if (fclose(fp)<0)
		fprintf(stderr,"%s: could not close file \"%s\" after writing (fclose: %s)\n",argv[0],reqfile,strerror(errno));
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
	if (hexstrm)
	{
	    fprintf(hexstrm,"%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n",
		     sd,
		     packet_get_class_str(fpacket),(unsigned int)packet_get_class(fpacket),
		     packet_get_type_str(fpacket,packet_dir_from_server),packet_get_type(fpacket),
		     packet_get_size(fpacket));
	    hexdump(hexstrm,packet_get_raw_data(fpacket,0),packet_get_size(fpacket));
	}
	if (fwrite(packet_get_raw_data_const(fpacket,0),1,MAX_PACKET_SIZE,fp)<MAX_PACKET_SIZE)
	{
	    printf("error\n");
	    fprintf(stderr,"%s: could not write to file \"%s\" (fwrite: %s)\n",argv[0],reqfile,strerror(errno));
	    if (fclose(fp)<0)
		fprintf(stderr,"%s: could not close file \"%s\" after writing (fclose: %s)\n",argv[0],reqfile,strerror(errno));
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
    }
    filelen -= currsize;
    if (filelen)
    {
	printf(".");
	fflush(stdout);
	
	if (client_blockrecv_raw_packet(sd,fpacket,filelen)<0)
	{
	    printf("error\n");
	    fprintf(stderr,"%s: server closed connection\n",argv[0]);
	    if (fclose(fp)<0)
		fprintf(stderr,"%s: could not close file \"%s\" after writing (fclose: %s)\n",argv[0],reqfile,strerror(errno));
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
	if (hexstrm)
	{
	    fprintf(hexstrm,"%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n",
		     sd,
		     packet_get_class_str(fpacket),(unsigned int)packet_get_class(fpacket),
		     packet_get_type_str(fpacket,packet_dir_from_server),packet_get_type(fpacket),
		     packet_get_size(fpacket));
	    hexdump(hexstrm,packet_get_raw_data(fpacket,0),packet_get_size(fpacket));
	}
	if (fwrite(packet_get_raw_data_const(fpacket,0),1,filelen,fp)<filelen)
	{
	    printf("error\n");
	    fprintf(stderr,"%s: could not write to file \"%s\"\n",argv[0],reqfile);
	    if (fclose(fp)<0)
		fprintf(stderr,"%s: could not close file \"%s\" after writing (fclose: %s)\n",argv[0],reqfile,strerror(errno));
	    packet_del_ref(fpacket);
	    packet_del_ref(rpacket);
	    return STATUS_FAILURE;
	}
    }
    
    packet_del_ref(fpacket);
    packet_del_ref(rpacket);
    
    if (hexstrm)
    {
	fprintf(hexstrm,"# end of dump\n");
	if (fclose(hexstrm)<0)
	    fprintf(stderr,"%s: could not close hexdump file \"%s\" after writing (fclose: %s)",argv[0],hexfile,strerror(errno));
    }
    
    if (fclose(fp)<0)
    {
	fprintf(stderr,"%s: could not close file \"%s\" after writing (fclose: %s)\n",argv[0],reqfile,strerror(errno));
	return STATUS_FAILURE;
    }
    
    printf("done\n");
    return STATUS_FAILURE;
}
Esempio n. 28
0
		static int _client_anongame_infos(t_connection * c, t_packet const * const packet)
		{
			t_packet * rpacket;

			if (bn_int_get(packet->u.client_findanongame_inforeq.count) > 1) {
				/* reply with 0 entries found */
				int	temp = 0;

				if ((rpacket = packet_create(packet_class_bnet)) == NULL) {
					eventlog(eventlog_level_error, __FUNCTION__, "could not create new packet");
					return -1;
				}

				packet_set_size(rpacket, sizeof(t_server_findanongame_inforeply));
				packet_set_type(rpacket, SERVER_FINDANONGAME_INFOREPLY);
				bn_byte_set(&rpacket->u.server_findanongame_inforeply.option, CLIENT_FINDANONGAME_INFOS);
				bn_int_set(&rpacket->u.server_findanongame_inforeply.count, bn_int_get(packet->u.client_findanongame_inforeq.count));
				bn_byte_set(&rpacket->u.server_findanongame_inforeply.noitems, 0);
				packet_append_data(rpacket, &temp, 1);

				conn_push_outqueue(c, rpacket);
				packet_del_ref(rpacket);
			}
			else {
				int i;
				int client_tag;
				int server_tag_count = 0;
				int client_tag_unk;
				int server_tag_unk;
				bn_int temp;
				char noitems;
				char * tmpdata;
				int tmplen;
				t_clienttag clienttag = conn_get_clienttag(c);
				char last_packet = 0x00;
				char other_packet = 0x01;
				char langstr[5];
				t_gamelang gamelang = conn_get_gamelang(c);
				bn_int_tag_get((bn_int const *)&gamelang, langstr, 5);

				/* Send seperate packet for each item requested
				 * sending all at once overloaded w3xp
				 * [Omega] */
				for (i = 0; i < bn_byte_get(packet->u.client_findanongame_inforeq.noitems); i++){
					noitems = 0;

					if ((rpacket = packet_create(packet_class_bnet)) == NULL) {
						eventlog(eventlog_level_error, __FUNCTION__, "could not create new packet");
						return -1;
					}

					/* Starting the packet stuff */
					packet_set_size(rpacket, sizeof(t_server_findanongame_inforeply));
					packet_set_type(rpacket, SERVER_FINDANONGAME_INFOREPLY);
					bn_byte_set(&rpacket->u.server_findanongame_inforeply.option, CLIENT_FINDANONGAME_INFOS);
					bn_int_set(&rpacket->u.server_findanongame_inforeply.count, 1);

					std::memcpy(&temp, (packet_get_data_const(packet, 10 + (i * 8), 4)), sizeof(int));
					client_tag = bn_int_get(temp);
					std::memcpy(&temp, packet_get_data_const(packet, 14 + (i * 8), 4), sizeof(int));
					client_tag_unk = bn_int_get(temp);

					switch (client_tag){
					case CLIENT_FINDANONGAME_INFOTAG_URL:
						bn_int_set((bn_int*)&server_tag_unk, 0xBF1F1047);
						packet_append_data(rpacket, "LRU\0", 4);
						packet_append_data(rpacket, &server_tag_unk, 4);
						// FIXME: Maybe need do do some checks to avoid prefs empty strings.
						tmpdata = anongame_infos_data_get_url(clienttag, conn_get_versionid(c), &tmplen);
						packet_append_data(rpacket, tmpdata, tmplen);
						noitems++;
						server_tag_count++;
						eventlog(eventlog_level_debug, __FUNCTION__, "client_tag request tagid=(0x%01x) tag=(%s)  tag_unk=(0x%04x)", i, "CLIENT_FINDANONGAME_INFOTAG_URL", client_tag_unk);
						break;
					case CLIENT_FINDANONGAME_INFOTAG_MAP:
						bn_int_set((bn_int*)&server_tag_unk, 0x70E2E0D5);
						packet_append_data(rpacket, "PAM\0", 4);
						packet_append_data(rpacket, &server_tag_unk, 4);
						tmpdata = anongame_infos_data_get_map(clienttag, conn_get_versionid(c), &tmplen);
						packet_append_data(rpacket, tmpdata, tmplen);
						noitems++;
						server_tag_count++;
						eventlog(eventlog_level_debug, __FUNCTION__, "client_tag request tagid=(0x%01x) tag=(%s)  tag_unk=(0x%04x)", i, "CLIENT_FINDANONGAME_INFOTAG_MAP", client_tag_unk);
						break;
					case CLIENT_FINDANONGAME_INFOTAG_TYPE:
						bn_int_set((bn_int*)&server_tag_unk, 0x7C87DEEE);
						packet_append_data(rpacket, "EPYT", 4);
						packet_append_data(rpacket, &server_tag_unk, 4);
						tmpdata = anongame_infos_data_get_type(clienttag, conn_get_versionid(c), &tmplen);
						packet_append_data(rpacket, tmpdata, tmplen);
						noitems++;
						server_tag_count++;
						eventlog(eventlog_level_debug, __FUNCTION__, "client_tag request tagid=(0x%01x) tag=(%s) tag_unk=(0x%04x)", i, "CLIENT_FINDANONGAME_INFOTAG_TYPE", client_tag_unk);
						break;
					case CLIENT_FINDANONGAME_INFOTAG_DESC:
						bn_int_set((bn_int*)&server_tag_unk, 0xA4F0A22F);
						packet_append_data(rpacket, "CSED", 4);
						packet_append_data(rpacket, &server_tag_unk, 4);
						tmpdata = anongame_infos_data_get_desc(langstr, clienttag, conn_get_versionid(c), &tmplen);
						packet_append_data(rpacket, tmpdata, tmplen);
						eventlog(eventlog_level_debug, __FUNCTION__, "client_tag request tagid=(0x%01x) tag=(%s) tag_unk=(0x%04x)", i, "CLIENT_FINDANONGAME_INFOTAG_DESC", client_tag_unk);
						noitems++;
						server_tag_count++;
						break;
					case CLIENT_FINDANONGAME_INFOTAG_LADR:
						bn_int_set((bn_int*)&server_tag_unk, 0x3BADE25A);
						packet_append_data(rpacket, "RDAL", 4);
						packet_append_data(rpacket, &server_tag_unk, 4);
						tmpdata = anongame_infos_data_get_ladr(langstr, clienttag, conn_get_versionid(c), &tmplen);
						packet_append_data(rpacket, tmpdata, tmplen);
						noitems++;
						server_tag_count++;
						eventlog(eventlog_level_debug, __FUNCTION__, "client_tag request tagid=(0x%01x) tag=(%s) tag_unk=(0x%04x)", i, "CLIENT_FINDANONGAME_INFOTAG_LADR", client_tag_unk);
						break;
					default:
						eventlog(eventlog_level_debug, __FUNCTION__, "unrec client_tag request tagid=(0x%01x) tag=(0x%04x)", i, client_tag);

					}
					//Adding a last padding null-byte
					if (server_tag_count == bn_byte_get(packet->u.client_findanongame_inforeq.noitems))
						packet_append_data(rpacket, &last_packet, 1); /* only last packet in group gets 0x00 */
					else
						packet_append_data(rpacket, &other_packet, 1); /* the rest get 0x01 */

					//Go,go,go
					bn_byte_set(&rpacket->u.server_findanongame_inforeply.noitems, noitems);
					conn_push_outqueue(c, rpacket);
					packet_del_ref(rpacket);
				}
			}
			return 0;
		}
Esempio n. 29
0
		/* tournament notice disabled at this time, but responce is sent to cleint */
		static int _client_anongame_tournament(t_connection * c, t_packet const * const packet)
		{
			t_packet * rpacket;

			t_account * account = conn_get_account(c);
			t_clienttag clienttag = conn_get_clienttag(c);

			unsigned int start_prelim = tournament_get_start_preliminary();
			unsigned int end_signup = tournament_get_end_signup();
			unsigned int end_prelim = tournament_get_end_preliminary();
			unsigned int start_r1 = tournament_get_start_round_1();

			if ((rpacket = packet_create(packet_class_bnet)) == NULL) {
				eventlog(eventlog_level_error, __FUNCTION__, "could not create new packet");
				return -1;
			}

			packet_set_size(rpacket, sizeof(t_server_anongame_tournament_reply));
			packet_set_type(rpacket, SERVER_FINDANONGAME_TOURNAMENT_REPLY);
			bn_byte_set(&rpacket->u.server_anongame_tournament_reply.option, 7);
			bn_int_set(&rpacket->u.server_anongame_tournament_reply.count,
				bn_int_get(packet->u.client_anongame_tournament_request.count));

			if (!start_prelim || (end_signup <= now && tournament_user_signed_up(account) < 0) ||
				tournament_check_client(clienttag) < 0) { /* No Tournament Notice */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0);
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			else if (start_prelim >= now) { /* Tournament Notice */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 1);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0x0000); /* random */
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, _tournament_time_convert(start_prelim));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0x01);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, start_prelim - now);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x00);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			else if (end_signup >= now) { /* Tournament Signup Notice - Play Game Active */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0x0828); /* random */
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, _tournament_time_convert(end_signup));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0x01);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, end_signup - now);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, tournament_get_stat(account, 1));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, tournament_get_stat(account, 2));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, tournament_get_stat(account, 3));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x08);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			else if (end_prelim >= now) { /* Tournament Prelim Period - Play Game Active */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 3);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0x0828); /* random */
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, _tournament_time_convert(end_prelim));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0x01);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, end_prelim - now);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, tournament_get_stat(account, 1));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, tournament_get_stat(account, 2));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, tournament_get_stat(account, 3));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x08);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			else if (start_r1 >= now && (tournament_get_game_in_progress())) { /* Prelim Period Over - Shows user stats (not all prelim games finished) */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 4);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0x0000); /* random */
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, _tournament_time_convert(start_r1));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0x01);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, start_r1 - now);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0); /* 00 00 */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, tournament_get_stat(account, 1));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, tournament_get_stat(account, 2));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, tournament_get_stat(account, 3));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x08);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			else if (!(tournament_get_in_finals_status(account))) { /* Prelim Period Over - user did not make finals - Shows user stats */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 5);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0);
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, tournament_get_stat(account, 1));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, tournament_get_stat(account, 2));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, tournament_get_stat(account, 3));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x04);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			/* cycle through [type-6] & [type-7] packets
			 *
			 * use [type-6] to show client "eliminated" or "continue"
			 *     timestamp , countdown & round number (of next round) must be set if clinet continues
			 *
			 * use [type-7] to make cleint wait for 44FF packet option 1 to start game (A guess, not tested)
			 *
			 * not sure if there is overall winner packet sent at end of last final round
			 */
			// UNDONE: next two conditions never executed
			else if ((0)) { /* User in finals - Shows user stats and start of next round*/
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 6);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0x0000);
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, _tournament_time_convert(start_r1));
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0x01);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, start_r1 - now);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0x0000); /* 00 00 */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, 4); /* round number */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, 0); /* 0 = continue , 1= eliminated */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x04); /* number of rounds in finals */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}
			else if ((0)) { /* user waiting for match to be made */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.type, 7);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown4, 0);
				bn_int_set(&rpacket->u.server_anongame_tournament_reply.timestamp, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown5, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.countdown, 0);
				bn_short_set(&rpacket->u.server_anongame_tournament_reply.unknown2, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.wins, 1); /* round number */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.losses, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.ties, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.unknown3, 0x04); /* number of finals */
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.selection, 2);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.descnum, 0);
				bn_byte_set(&rpacket->u.server_anongame_tournament_reply.nulltag, 0);
			}

			conn_push_outqueue(c, rpacket);
			packet_del_ref(rpacket);
			return 0;
		}
Esempio n. 30
0
extern unsigned int packet_get_type(t_packet const * packet)
{
    if (!packet)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL packet");
	return 0;
    }

    switch (packet->pclass)
    {
    case packet_class_init:
	return CLIENT_INITCONN; /* all init packets are of this type */

    case packet_class_bnet:
	if (packet_get_size(packet)<sizeof(t_bnet_header))
	{
	    eventlog(eventlog_level_error,__FUNCTION__,"bnet packet is shorter than header (len=%u)",packet_get_size(packet));
	    return 0;
	}
	return (unsigned int)bn_short_get(packet->u.bnet.h.type);

    case packet_class_file:
	if (packet_get_size(packet)<sizeof(t_file_header))
	{
	    eventlog(eventlog_level_error,__FUNCTION__,"file packet is shorter than header (len=%u)",packet_get_size(packet));
	    return 0;
	}
	return (unsigned int)bn_short_get(packet->u.file.h.type);

    case packet_class_udp:
	if (packet_get_size(packet)<sizeof(t_udp_header))
	{
	    eventlog(eventlog_level_error,__FUNCTION__,"udp packet is shorter than header (len=%u)",packet_get_size(packet));
	    return 0;
	}
	return bn_int_get(packet->u.udp.h.type);

    case packet_class_raw:
	return 0; /* raw packets don't have a type, but don't warn because the packet dump tries anyway */

    case packet_class_d2game:
	if (packet_get_size(packet)<sizeof(t_d2game_header))
	{
	    eventlog(eventlog_level_error,__FUNCTION__,"d2game packet is shorter than header (len=%u)",packet_get_size(packet));
	    return 0;
	}
	return bn_byte_get(packet->u.d2game.h.type);

    case packet_class_d2gs:
        if (packet_get_size(packet)<sizeof(t_d2cs_d2gs_header))
        {
            eventlog(eventlog_level_error,__FUNCTION__,"d2gs packet is shorter than header (len=%u)",packet_get_size(packet));
            return 0;
        }
        return bn_short_get(packet->u.d2cs_d2gs.h.type);
    case packet_class_d2cs_bnetd:
        if (packet_get_size(packet)<sizeof(t_d2cs_bnetd_header)) {
                eventlog(eventlog_level_error,__FUNCTION__,"d2cs_bnetd packet shorter than header (len=%u)",packet_get_size(packet));
                return 0;
        }
        return bn_short_get(packet->u.d2cs_d2gs.h.type);

    case packet_class_d2cs:
        if (packet_get_size(packet)<sizeof(t_d2cs_client_header))
        {
            eventlog(eventlog_level_error,__FUNCTION__,"d2cs packet is shorter than header (len=%u)",packet_get_size(packet));
            return 0;
        }
        return bn_byte_get(packet->u.d2cs_client.h.type);

    case packet_class_w3route:
	if (packet_get_size(packet)<sizeof(t_w3route_header))
	{
	    eventlog(eventlog_level_error,__FUNCTION__,"w3route packet is shorter than header (len=%u)",packet_get_size(packet));
	    return 0;
	}
	return bn_short_get(packet->u.w3route.h.type);
    case packet_class_wolgameres:
	    return 0; /* wolgameres packets don't have a type */

    default:
	eventlog(eventlog_level_error,__FUNCTION__,"packet has invalid class %d",(int)packet->pclass);
	return 0;
    }
}