discordjs/discord.js · error · Error
Malformed IP address
Error message
Malformed IP address
What it means
parseLocalPacket parses the UDP IP-discovery response that Discord sends so the client can learn its local IP/port for voice. It extracts the IP from bytes 8..first-null of the 74-byte packet and validates it with node:net's isIPv4; if the extracted bytes are not a valid IPv4 address it throws this Error. This means the discovery response was malformed or the packet framing changed.
Source
Thrown at packages/voice/src/networking/VoiceUDPSocket.ts:26
* for Discord.
*/
export interface SocketConfig {
ip: string;
port: number;
}
/**
* Parses the response from Discord to aid with local IP discovery.
*
* @param message - The received message
*/
export function parseLocalPacket(message: Buffer): SocketConfig {
const packet = Buffer.from(message);
const ip = packet.subarray(8, packet.indexOf(0, 8)).toString('utf8');
if (!isIPv4(ip)) {
throw new Error('Malformed IP address');
}
const port = packet.readUInt16BE(packet.length - 2);
return { ip, port };
}
/**
* The interval in milliseconds at which keep alive datagrams are sent.
*/
const KEEP_ALIVE_INTERVAL = 5e3;
/**
* The maximum value of the keep alive counter.
*/
const MAX_COUNTER_VALUE = 2 ** 32 - 1;
export interface VoiceUDPSocket extends EventEmitter {View on GitHub (pinned to a81ed8a306)
Solutions
- Retry the voice connection (destroy and rejoin the voice channel) — transient UDP corruption is the most common cause.
- Check VPN, proxy, and firewall setups that intercept or rewrite UDP packets and try a direct connection.
- Verify the UDP socket is actually talking to the gateway-provided IP (see VoiceUDPSocket) and not an interception layer.
- Update @discordjs/voice in case of a protocol/framing change; report if it persists.
Example fix
// before
await connection.connect(); // may throw 'Malformed IP address' behind a VPN
// after
try {
await connection.connect();
} catch (e) {
connection.destroy(); // then rejoin to retry the UDP discovery handshake
} Defensive patterns
Strategy: retry
Validate before calling
// Validate the discovery response yourself if you handle raw UDP:
const net = require('node:net');
if (!net.isIPv4(ip)) { /* rejoin / retry instead of proceeding */ } Type guard
function isValidLocalPacket(cfg: { ip: string; port: number }): boolean {
return net.isIPv4(cfg.ip) && cfg.port > 0 && cfg.port <= 65535;
} Try / catch
try {
await connection.connect();
} catch (e) {
if (e.message === 'Malformed IP address') {
connection.destroy();
await joinVoiceChannel(channelConfig); // retry discovery once or twice
} else throw e;
} Prevention
- Avoid VPNs/proxies that intercept UDP for the bot process; allowlist Discord UDP endpoints in firewalls.
- Keep the voice connection lifecycle managed (destroy on failure) so discovery can be retried cleanly.
- Keep @discordjs/voice updated for protocol framing changes.
- Run with a stable network — check packet loss if this error recurs.
When it happens
Trigger: Receiving a UDP discovery (SELECT_PROTOCOL READY) response whose bytes 8..N do not form a valid IPv4 string — e.g. a truncated packet, a response from a non-Discord/intercepting server, or networking middleware rewriting the payload.
Common situations: Running voice traffic through a VPN/proxy/corporate firewall that mangles UDP; custom Discord API mocks returning a wrongly shaped discovery payload; rare Discord-side protocol changes; packet corruption on flaky networks.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- No compatible encryption modes. Available include: ${options
- Failed to parse packet
- HTTPError(status, res.statusText, method, url, requestData)
- response.statusText
- Cannot destroy VoiceConnection - it has already been destroy
AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30).
Data as JSON: /api/errors/d04b1ee23731501c.
Report an issue: GitHub.