aupreti
aupreti

Reputation: 221

Invalid argument error while calling connect for IPv6 socket

I am getting an invalid argument error when I call

connect(m_socket, (struct sockaddr *)&m_socket_addrv6, sizeof(struct sockaddr));

m_socket_addrv6 is a sockaddr_in6 struct. From gdb, this is what m_socket_addrv6 looks like.

>{sin6_len = 0 '\0', sin6_family = 28 '\034', sin6_port = 20480,  sin6_flowinfo = 0, sin6_addr = {__u6_addr = {
  __u6_addr8 = "�\200\000\000\000\000\000\000\002PV���\000n", __u6_addr16 = {33022, 0, 0, 0, 20482, 65366, 48126, 28160},
  __u6_addr32 = {33022, 0, 4283846658, 1845541886}}}, sin6_scope_id = 0}. 

Similar code was working for IPv4. Is there something I am missing for v6?

Upvotes: 1

Views: 818

Answers (2)

Paul Rooney
Paul Rooney

Reputation: 21619

Here is the function signature of connect.

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

The addrlen argument needs to be the true size of the addr argument. You are specifying it as sizeof(struct sockaddr). This is too small for the actual struct you are passing, so connect will not use your structure correctly.

Instead pass the actual real size of the m_socket_addrv6 struct.

connect(m_socket, (struct sockaddr*)&m_socket_addrv6, sizeof(m_socket_addrv6));

Upvotes: 2

You need to pass the size of the address, which is sizeof(m_socket_addrv6), not sizeof(struct sockaddr).

Upvotes: 1

Related Questions