{"id":8489,"date":"2024-05-22T13:03:01","date_gmt":"2024-05-22T13:03:01","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=8489"},"modified":"2024-05-22T13:03:02","modified_gmt":"2024-05-22T13:03:02","slug":"how-to-retrieve-tos-value-on-a-tcp-socket","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/how-to-retrieve-tos-value-on-a-tcp-socket\/","title":{"rendered":"How to Retrieve TOS Value on a TCP Socket"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 3,663<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p class=\"wp-block-paragraph\">To retrieve the Type of Service (TOS) value on a TCP socket in a C program, you can use the <code>getsockopt<\/code> function. The <code>getsockopt<\/code> function allows you to read socket options. For retrieving the TOS value, you&#8217;ll use the <code>IP_TOS<\/code> option with <code>getsockopt<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example of how to do this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-51acdd89f543b7d93754e2ee0a956c58\"><code><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;string.h>\n#include &lt;unistd.h>\n#include &lt;arpa\/inet.h>\n#include &lt;sys\/types.h>\n#include &lt;sys\/socket.h>\n#include &lt;netinet\/in.h>\n\nint main() {\n    int sockfd;\n    int tos;\n    socklen_t optlen;\n    struct sockaddr_in server_addr;\n\n    \/\/ Create a socket\n    sockfd = socket(AF_INET, SOCK_STREAM, 0);\n    if (sockfd &lt; 0) {\n        perror(\"socket\");\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ Initialize server address structure\n    memset(&amp;server_addr, 0, sizeof(server_addr));\n    server_addr.sin_family = AF_INET;\n    server_addr.sin_port = htons(8080);\n    server_addr.sin_addr.s_addr = inet_addr(\"127.0.0.1\");\n\n    \/\/ Connect to the server\n    if (connect(sockfd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &lt; 0) {\n        perror(\"connect\");\n        close(sockfd);\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ Retrieve the TOS value\n    optlen = sizeof(tos);\n    if (getsockopt(sockfd, IPPROTO_IP, IP_TOS, &amp;tos, &amp;optlen) &lt; 0) {\n        perror(\"getsockopt\");\n        close(sockfd);\n        exit(EXIT_FAILURE);\n    }\n\n    printf(\"TOS value: %d\\n\", tos);\n\n    \/\/ Close the socket\n    close(sockfd);\n    return 0;\n}<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating a Socket:<\/strong><\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>socket<\/code> function creates a TCP socket.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     2. <strong>Setting Up Server Address:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>server_addr<\/code> structure is initialized with the server&#8217;s IP address and port.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    3. <strong>Connecting to the Server:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>connect<\/code> function establishes a connection to the server.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    4. <strong>Retrieving the TOS Value:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getsockopt<\/code> is used to retrieve the TOS value.<\/li>\n\n\n\n<li>The arguments for <code>getsockopt<\/code> include the socket descriptor (<code>sockfd<\/code>), the protocol level (<code>IPPROTO_IP<\/code>), the option name (<code>IP_TOS<\/code>), a pointer to store the TOS value (<code>&amp;tos<\/code>), and the size of the option value (<code>&amp;optlen<\/code>).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    5. <strong>Printing the TOS Value:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The retrieved TOS value is printed to the console.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    6. <strong>Closing the Socket:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>close<\/code> function closes the socket to free resources.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important Notes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Error Handling:<\/strong> Always check for errors when calling socket functions and handle them appropriately.<\/li>\n\n\n\n<li><strong>Permissions:<\/strong> Depending on the system configuration, retrieving or setting the TOS value may require elevated permissions.<\/li>\n\n\n\n<li><strong>Compatibility:<\/strong> Ensure that your environment supports the required socket options and functions.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This example demonstrates how to retrieve the TOS value for a TCP socket in a C program, which can be useful for debugging or monitoring network traffic characteristics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3,663 Views To retrieve the Type of Service (TOS) value on a TCP socket in a C program, you can use the getsockopt function. The getsockopt function allows you to read socket options. For retrieving the TOS value, you&#8217;ll use the IP_TOS option with getsockopt. Here\u2019s an example of how to do this: Explanation 2. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[71],"tags":[],"class_list":["post-8489","post","type-post","status-publish","format-standard","hentry","category-how-tos"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=8489"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8489\/revisions"}],"predecessor-version":[{"id":8491,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8489\/revisions\/8491"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=8489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=8489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=8489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}