Send packets from a specific interface in cC pcap detecting inbound datagramsC - choose interface for UDP/multicast socketData is not received correctly from TCP socket using CDoes INADDR_ANY care about new interface?How to stop behaviour: C++ Socket sendto changes interfaceTCP client using a specific interface while connecting to a webserverLinux: Bind UDP listening socket to specific interface (or find out the interface a datagram came in from)?data link socket read outgoing packets?No traffic bind on interface using SO_BINDTODEVICEHow to make setsockopt IP_ADD_MEMBERSHIP honor the local interface address to receive multicast packets on only one specific interface?

Why are some files not movable on Windows 10?

Bash awk command with quotes

Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?

Bit one of the Intel 8080's Flags register

What are the typical trumpet parts in classical music?

Is it appropriate to CC a lot of people on an email?

Why is belonging not transitive?

Ambiguity in notation resolved by +

How does a simple logistic regression model achieve a 92% classification accuracy on MNIST?

Has Dumbledore ever scolded Harry?

Why does the speed of sound decrease at high altitudes although the air density decreases?

Amortized Loans seem to benefit the bank more than the customer

What was the ultimate objective of The Party in 1984?

Proof using derivative information to find limit

What does "boys rule, girls drool" mean?

How would you control supersoldiers in a late iron-age society?

Exam design: give maximum score per question or not?

How To Make Earth's Oceans as Brackish as Lyr's

Insight into cavity resonators

Why don't airports use arresting gears to recover energy from landing passenger planes?

What would happen if Protagoras v Euathlus were heard in court today?

Why is the car dealer insisting on a loan instead of cash?

If I want an interpretable model, are there methods other than Linear Regression?

Why does '/' contain '..'?



Send packets from a specific interface in c


C pcap detecting inbound datagramsC - choose interface for UDP/multicast socketData is not received correctly from TCP socket using CDoes INADDR_ANY care about new interface?How to stop behaviour: C++ Socket sendto changes interfaceTCP client using a specific interface while connecting to a webserverLinux: Bind UDP listening socket to specific interface (or find out the interface a datagram came in from)?data link socket read outgoing packets?No traffic bind on interface using SO_BINDTODEVICEHow to make setsockopt IP_ADD_MEMBERSHIP honor the local interface address to receive multicast packets on only one specific interface?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I've been doing some socket programing in c and I would like to let the user of my program specify the interface to send and receive packets from. The linux man page for socket(7) says that you can set the socket option SO_BINDTODEVICE to bind the socket to a particular device such as "eth0". It also occurred to me that when calling bind() you typically pass it a sockaddr_in struct with the sin_addr.s_addr property set to INADDR_ANY to tell the socket to bind to all interfaces as shown below



int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

struct sockaddr_in local;

local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = 0;

bind(sd, (struct sockaddr *)&local, sizeof local);


If the point of passing in the sockaddr to bind is to specify where to bind the socket locally on your machine, could you just pass in the IP address of a specific interface to bind your socket to it? I was hoping someone could clarify what the socket is doing here, and what the difference is between that approach and using setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &devicename, sizeof devicename); before you bind.



Note: In the example above I used UDP and removed error checking for simplicity, Ideally the answer to the above question should be able to work independently of the protocol being used.










share|improve this question






























    3















    I've been doing some socket programing in c and I would like to let the user of my program specify the interface to send and receive packets from. The linux man page for socket(7) says that you can set the socket option SO_BINDTODEVICE to bind the socket to a particular device such as "eth0". It also occurred to me that when calling bind() you typically pass it a sockaddr_in struct with the sin_addr.s_addr property set to INADDR_ANY to tell the socket to bind to all interfaces as shown below



    int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    struct sockaddr_in local;

    local.sin_family = AF_INET;
    local.sin_addr.s_addr = INADDR_ANY;
    local.sin_port = 0;

    bind(sd, (struct sockaddr *)&local, sizeof local);


    If the point of passing in the sockaddr to bind is to specify where to bind the socket locally on your machine, could you just pass in the IP address of a specific interface to bind your socket to it? I was hoping someone could clarify what the socket is doing here, and what the difference is between that approach and using setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &devicename, sizeof devicename); before you bind.



    Note: In the example above I used UDP and removed error checking for simplicity, Ideally the answer to the above question should be able to work independently of the protocol being used.










    share|improve this question


























      3












      3








      3


      1






      I've been doing some socket programing in c and I would like to let the user of my program specify the interface to send and receive packets from. The linux man page for socket(7) says that you can set the socket option SO_BINDTODEVICE to bind the socket to a particular device such as "eth0". It also occurred to me that when calling bind() you typically pass it a sockaddr_in struct with the sin_addr.s_addr property set to INADDR_ANY to tell the socket to bind to all interfaces as shown below



      int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

      struct sockaddr_in local;

      local.sin_family = AF_INET;
      local.sin_addr.s_addr = INADDR_ANY;
      local.sin_port = 0;

      bind(sd, (struct sockaddr *)&local, sizeof local);


      If the point of passing in the sockaddr to bind is to specify where to bind the socket locally on your machine, could you just pass in the IP address of a specific interface to bind your socket to it? I was hoping someone could clarify what the socket is doing here, and what the difference is between that approach and using setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &devicename, sizeof devicename); before you bind.



      Note: In the example above I used UDP and removed error checking for simplicity, Ideally the answer to the above question should be able to work independently of the protocol being used.










      share|improve this question














      I've been doing some socket programing in c and I would like to let the user of my program specify the interface to send and receive packets from. The linux man page for socket(7) says that you can set the socket option SO_BINDTODEVICE to bind the socket to a particular device such as "eth0". It also occurred to me that when calling bind() you typically pass it a sockaddr_in struct with the sin_addr.s_addr property set to INADDR_ANY to tell the socket to bind to all interfaces as shown below



      int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

      struct sockaddr_in local;

      local.sin_family = AF_INET;
      local.sin_addr.s_addr = INADDR_ANY;
      local.sin_port = 0;

      bind(sd, (struct sockaddr *)&local, sizeof local);


      If the point of passing in the sockaddr to bind is to specify where to bind the socket locally on your machine, could you just pass in the IP address of a specific interface to bind your socket to it? I was hoping someone could clarify what the socket is doing here, and what the difference is between that approach and using setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &devicename, sizeof devicename); before you bind.



      Note: In the example above I used UDP and removed error checking for simplicity, Ideally the answer to the above question should be able to work independently of the protocol being used.







      c sockets network-interface






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 12:29









      Alex VAlex V

      183 bronze badges




      183 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0

















          If the point of passing in the sockaddr to bind is to specify where to
          bind the socket locally on your machine, could you just pass in the IP
          address of a specific interface to bind your socket to it?




          Binding to an address (via bind()) and binding to an interface (via setsockopt()) serve different, but overlapping purposes. Usually bind() is what you want.



          In particular, your question seems to assume that there is a 1:1 mapping between addresses and interfaces, but that it is not a safe assumption. One interface can have multiple addresses, and, at least in principle, one address can be served by multiple interfaces.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397673%2fsend-packets-from-a-specific-interface-in-c%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0

















            If the point of passing in the sockaddr to bind is to specify where to
            bind the socket locally on your machine, could you just pass in the IP
            address of a specific interface to bind your socket to it?




            Binding to an address (via bind()) and binding to an interface (via setsockopt()) serve different, but overlapping purposes. Usually bind() is what you want.



            In particular, your question seems to assume that there is a 1:1 mapping between addresses and interfaces, but that it is not a safe assumption. One interface can have multiple addresses, and, at least in principle, one address can be served by multiple interfaces.






            share|improve this answer





























              0

















              If the point of passing in the sockaddr to bind is to specify where to
              bind the socket locally on your machine, could you just pass in the IP
              address of a specific interface to bind your socket to it?




              Binding to an address (via bind()) and binding to an interface (via setsockopt()) serve different, but overlapping purposes. Usually bind() is what you want.



              In particular, your question seems to assume that there is a 1:1 mapping between addresses and interfaces, but that it is not a safe assumption. One interface can have multiple addresses, and, at least in principle, one address can be served by multiple interfaces.






              share|improve this answer



























                0














                0










                0










                If the point of passing in the sockaddr to bind is to specify where to
                bind the socket locally on your machine, could you just pass in the IP
                address of a specific interface to bind your socket to it?




                Binding to an address (via bind()) and binding to an interface (via setsockopt()) serve different, but overlapping purposes. Usually bind() is what you want.



                In particular, your question seems to assume that there is a 1:1 mapping between addresses and interfaces, but that it is not a safe assumption. One interface can have multiple addresses, and, at least in principle, one address can be served by multiple interfaces.






                share|improve this answer














                If the point of passing in the sockaddr to bind is to specify where to
                bind the socket locally on your machine, could you just pass in the IP
                address of a specific interface to bind your socket to it?




                Binding to an address (via bind()) and binding to an interface (via setsockopt()) serve different, but overlapping purposes. Usually bind() is what you want.



                In particular, your question seems to assume that there is a 1:1 mapping between addresses and interfaces, but that it is not a safe assumption. One interface can have multiple addresses, and, at least in principle, one address can be served by multiple interfaces.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 12:49









                John BollingerJohn Bollinger

                96.4k8 gold badges48 silver badges91 bronze badges




                96.4k8 gold badges48 silver badges91 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397673%2fsend-packets-from-a-specific-interface-in-c%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript