Commit ce3dcf23 authored by Hubert Miś's avatar Hubert Miś Committed by Jukka Rissanen
Browse files

net: socket: getsockopt SO_PROTOCOL implementation



This patch adds implementation of socket option used to get
protocol used for given socket (e.g. IPPROTO_TCP). This option
is not defined in POSIX, but it is Linux extension.

Signed-off-by: default avatarHubert Miś <hubert.mis@nordicsemi.no>
parent a6e83eb7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -806,6 +806,8 @@ static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,

/** sockopt: Timestamp TX packets */
#define SO_TIMESTAMPING 37
/** sockopt: Protocol used with the socket */
#define SO_PROTOCOL 38

/* Socket options for IPPROTO_TCP level */
/** sockopt: Disable TCP buffering (ignored, for compatibility) */
+14 −0
Original line number Diff line number Diff line
@@ -1522,6 +1522,20 @@ int zsock_getsockopt_ctx(struct net_context *ctx, int level, int optname,

				return 0;
			}
			break;

		case SO_PROTOCOL: {
			int proto = (int)net_context_get_ip_proto(ctx);

			if (*optlen != sizeof(proto)) {
				errno = EINVAL;
				return -1;
			}

			*(int *)optval = proto;

			return 0;
		}
		}

		break;
+25 −1
Original line number Diff line number Diff line
@@ -1028,6 +1028,20 @@ static int tls_opt_sec_tag_list_set(struct tls_context *context,
	return 0;
}

static int sock_opt_protocol_get(struct tls_context *context,
				 void *optval, socklen_t *optlen)
{
	int protocol = (int)context->tls_version;

	if (*optlen != sizeof(protocol)) {
		return -EINVAL;
	}

	*(int *)optval = protocol;

	return 0;
}

static int tls_opt_sec_tag_list_get(struct tls_context *context,
				    void *optval, socklen_t *optlen)
{
@@ -2034,7 +2048,17 @@ int ztls_getsockopt_ctx(struct tls_context *ctx, int level, int optname,
		return -1;
	}

	if (level != SOL_TLS) {
	if ((level == SOL_SOCKET) && (optname == SO_PROTOCOL)) {
		/* Protocol type is overridden during socket creation. Its
		 * value is restored here to return current value.
		 */
		err = sock_opt_protocol_get(ctx, optval, optlen);
		if (err < 0) {
			errno = -err;
			return -1;
		}
		return err;
	} else if (level != SOL_TLS) {
		return zsock_getsockopt(ctx->sock, level, optname,
					optval, optlen);
	}