Skip to content

Commit

Permalink
qcommon: NET_AdrToString fixes, uncrustify
Browse files Browse the repository at this point in the history
- Fixed IPv6 NET_AdrToString not returning port
-- Fixed NET_ADDRSTRMAXLEN for IPv6, 
- Fixed NET_AdrToString NA_BAD & 'NA_UKN' returning previous address
-- instead we are returning 'invalid' or 'unknown' mow

Final note: If you ever need the address without ports use
NET_AdrToStringNoPort function
  • Loading branch information
IR4T4 committed Oct 16, 2016
1 parent 05d4f08 commit 4e6e857
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/client/cl_main.c
Expand Up @@ -3552,7 +3552,6 @@ void CL_GlobalServers_f(void)
to.port = BigShort(PORT_MASTER);
}

// FIXME: NET_AdrToString doesn't deal with port for IPv6
Com_Printf("Requesting servers from the master %s (%s)...\n", masteraddress, NET_AdrToString(to));

// reset the list, waiting for response
Expand Down
42 changes: 25 additions & 17 deletions src/qcommon/net_ip.c
Expand Up @@ -52,22 +52,22 @@ typedef unsigned short sa_family_t;
# endif

#ifdef EAGAIN
# undef EAGAIN
# undef EAGAIN
#endif
#define EAGAIN WSAEWOULDBLOCK

#ifdef EADDRNOTAVAIL
# undef EADDRNOTAVAIL
# undef EADDRNOTAVAIL
#endif
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL

#ifdef EAFNOSUPPORT
# undef EAFNOSUPPORT
# undef EAFNOSUPPORT
#endif
#define EAFNOSUPPORT WSAEAFNOSUPPORT

#ifdef ECONNRESET
# undef ECONNRESET
# undef ECONNRESET
#endif
#define ECONNRESET WSAECONNRESET

Expand Down Expand Up @@ -571,7 +571,7 @@ qboolean NET_CompareBaseAdr(netadr_t a, netadr_t b)
return NET_CompareBaseAdrMask(a, b, -1);
}

const char *NET_AdrToString(netadr_t a)
const char *NET_AdrToStringNoPort(netadr_t a)
{
static char s[NET_ADDRSTRMAXLEN];

Expand All @@ -584,16 +584,11 @@ const char *NET_AdrToString(netadr_t a)
Com_sprintf(s, sizeof(s), "bot");
break;
case NA_IP:
// Port has to be returned along with ip address because of compatibility
Com_sprintf(s, sizeof(s), "%i.%i.%i.%i:%hu",
a.ip[0], a.ip[1], a.ip[2], a.ip[3], BigShort(a.port));
Com_sprintf(s, sizeof(s), "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3]);
break;
#ifdef FEATURE_IPV6
case NA_IP6:
{
// FIXME: add port for compatibility
// (joining a server through the server browser)
// Needs to be [ip]:port since : is a valid entry in ipv6
struct sockaddr_storage sadr;

memset(&sadr, 0, sizeof(sadr));
Expand All @@ -603,17 +598,25 @@ const char *NET_AdrToString(netadr_t a)
}
break;
#endif
case NA_BAD: // Invalid, unknown or non-applicable address type
//Com_Printf("NET_AdrToString: Address type: 0.0.0.0 or ::\n");
Com_sprintf(s, sizeof(s), "invalid");
break;
default:
Com_Printf("NET_AdrToString: Unknown address type: %i\n", a.type);
Com_Printf("NET_AdrToStringNoPort: Unknown address type: %i\n", a.type);
Com_sprintf(s, sizeof(s), "unknown");
break;
}

return s;
}

const char *NET_AdrToStringwPort(netadr_t a)
/**
* @brief Returns address & port
*/
const char *NET_AdrToString(netadr_t a)
{
static char s[NET_ADDRSTRMAXLEN];
static char s[NET_ADDRSTRMAXLEN_EXT];

switch (a.type)
{
Expand All @@ -624,15 +627,20 @@ const char *NET_AdrToStringwPort(netadr_t a)
Com_sprintf(s, sizeof(s), "bot");
break;
case NA_IP:
Com_sprintf(s, sizeof(s), "%s:%hu", NET_AdrToString(a), ntohs(a.port));
Com_sprintf(s, sizeof(s), "%i.%i.%i.%i:%hu", a.ip[0], a.ip[1], a.ip[2], a.ip[3], BigShort(a.port));
break;
#ifdef FEATURE_IPV6
case NA_IP6:
Com_sprintf(s, sizeof(s), "[%s]:%hu", NET_AdrToString(a), ntohs(a.port));
Com_sprintf(s, sizeof(s), "[%s]:%hu", NET_AdrToStringNoPort(a), ntohs(a.port));
break;
#endif
case NA_BAD: // Invalid, unknown or non-applicable address type
//Com_Printf("NET_AdrToString: Address type: 0.0.0.0 or ::\n");
Com_sprintf(s, sizeof(s), "invalid");
break;
default:
Com_Printf("NET_AdrToStringwPort: Unknown address type: %i\n", a.type);
Com_Printf("NET_AdrToString: Unknown address type: %i\n", a.type);
Com_sprintf(s, sizeof(s), "unknown");
break;
}

Expand Down
6 changes: 3 additions & 3 deletions src/qcommon/q_shared.h
Expand Up @@ -1375,8 +1375,8 @@ typedef struct entityState_s
// for players
int powerups; // bit flags. Used to store entState_t for non-player entities (so we know to draw them translucent clientsided)
int weapon; // determines weapon and flash model, etc
// OR fps to animate with (misc_gamemodel ents)
// which is the time in ms the model is updated (20 fps = default)
// OR fps to animate with (misc_gamemodel ents)
// which is the time in ms the model is updated (20 fps = default)
int legsAnim; // mask off ANIM_TOGGLEBIT
int torsoAnim; // mask off ANIM_TOGGLEBIT

Expand Down Expand Up @@ -1584,7 +1584,7 @@ typedef struct demoPlayInfo_s
#define NUMARGS(...) (sizeof((int[]) { 0, ## __VA_ARGS__ }) / sizeof(int) - 1)
#endif

typedef int(*cmpFunc_t)(const void *a, const void *b);
typedef int (*cmpFunc_t)(const void *a, const void *b);

void *Q_LinearSearch(const void *key, const void *ptr, size_t count, size_t size, cmpFunc_t cmp);

Expand Down
5 changes: 3 additions & 2 deletions src/qcommon/qcommon.h
Expand Up @@ -189,7 +189,8 @@ typedef enum
* @def NET_ADDRSTRMAXLEN
* @brief maximum length of an IPv6 address string including trailing '\0'
*/
#define NET_ADDRSTRMAXLEN 48
#define NET_ADDRSTRMAXLEN 48 // why 48? IPv4-mapped IPv6 maximum is 45 .. + trailing 0 is 46
#define NET_ADDRSTRMAXLEN_EXT 56 // NET_ADDRSTRMAXLEN + 8 (2xbrackets, colon, 5xport)
typedef struct
{
uint16_t type;
Expand All @@ -215,7 +216,7 @@ qboolean NET_CompareBaseAdr(netadr_t a, netadr_t b);
qboolean NET_IsLocalAddress(netadr_t adr);
qboolean NET_IsIPXAddress(const char *buf);
const char *NET_AdrToString(netadr_t a);
const char *NET_AdrToStringwPort(netadr_t a);
const char *NET_AdrToStringNoPort(netadr_t a);
int NET_StringToAdr(const char *s, netadr_t *a, netadrtype_t family);
qboolean NET_GetLoopPacket(netsrc_t sock, netadr_t *net_from, msg_t *net_message);
void NET_Sleep(int msec);
Expand Down

0 comments on commit 4e6e857

Please sign in to comment.