Like Tree2Likes
  • 1 Post By rags_to_riches
  • 1 Post By Salem

SOCK_RAW programing linker error

This is a discussion on SOCK_RAW programing linker error within the C Programming forums, part of the General Programming Boards category; I am creating a program that injects tcp packets using SOCK_RAW. I am using Xcode and it confirms all my ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Question SOCK_RAW programing linker error

    I am creating a program that injects tcp packets using SOCK_RAW. I am using Xcode and it confirms all my references are correct, however, when I compile I get a mach-o clang linker(ld) error. I have included all the necessary libs at the top of my code and have never had to import frameworks from osx for c programing before.

    I am really lost here. Any help would be appreciated.

    checkSum.c:

    Code:
    #include <stdio.h>
    
    
    //Check Sum generator (generic)
    unsigned short ourCheckSum (unsigned short *buf, int nwords);
    
    
    unsigned short ourCheckSum (unsigned short *buf, int nwords)
    {
        unsigned long sum;
        for (sum = 0; nwords > 0; nwords--)
            sum += *buf++;
        sum = (sum >> 16) + (sum & 0xffff);
        sum += (sum >> 16);
        return ~sum;
    }
    RSTSender.c (sorry its longish)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <netinet/tcp.h>
    #include <netinet/ip.h>
    #include <arpa/inet.h>
    #include "checkSum.c"
    
    
    int RSTInjector(int connectionSource, int connectionPort, int host);
    
    
    
    
    
    
    
    
    int RSTInjector(int connectionSource, int connectionPort, int host)
    {
    
    
    //Var
    int socketMaster;
    char datagram[4096]; //Contains entire packet iphdr + tcphdr
    struct ip *iph = (struct ip *) datagram;
    struct tcphdr *tcph = (struct tcphdr *) datagram +sizeof(struct ip);
    struct sockaddr_in sin;
    
    
    
    
    //Creat Socket
    socketMaster = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
        if (socketMaster == -1){
            perror("Could Not Open Socket");
            return -1;
        }
        
    //
    sin.sin_family = AF_INET;
    sin.sin_port = htons(connectionPort);
    sin.sin_addr.s_addr = inet_addr("127.0.0.1"); // DEST or SRC
    
    
    //Clean datagram
    memset(datagram, 0, 4096);
    
    
    //Construct Headers
    //IP Headers
    iph->ip_hl = 5;
    iph->ip_v = 4;
    iph->ip_tos = 0;
    iph->ip_len = sizeof(struct ip) + sizeof(struct tcphdr);
    iph->ip_id = htonl(2539);
    iph->ip_off = 0;
    iph->ip_ttl = 255;
    iph->ip_p = 6;
    iph->ip_sum = 0; //csum will be calculated later
    iph->ip_src.s_addr = inet_addr("127.0.0.1"); //Connection Source
    iph->ip_dst.s_addr = sin.sin_addr.s_addr; //Uses previuse host or connection dest
    
    
    //TCP Headers
    tcph->th_sport = htons(connectionPort); //TCP Port
    tcph->th_dport = htons(connectionPort); //TCP Port
    tcph->th_seq = rand(); // We really dont care as this is to close a connection
    tcph->th_ack = 0; //Is 0 in first packet -- Lets hope it dosent have to be the real value
    tcph->th_x2 = 0;
    tcph->th_off = 0;
    tcph->th_flags = TH_RST; //RST the connection, killing it
    tcph->th_win = htonl(65535); //Not sure about this number
    tcph->th_sum = 0; // Kernal *should* fill this value in correctly
    tcph->th_urp = 0; //What does this do?
    
    
    //Checksum call
    //IP Header
    iph->ip_sum = ourCheckSum((unsigned short *) datagram, iph->ip_len >> 1);
    
    
    //Inform Kernal of header inclusion
        {
        int one = 1;
        const int *val = &one;
        if (setsockopt(socketMaster, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0){
            perror("Could Not Set HDRINCL");
        }
        }
    
    
    //Inject RST Packet
        if (sendto(socketMaster, datagram, iph->ip_len, 0, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
            perror("Could not inject RST packet");
            return  -1;
        }
        
        else 
        return 0;
    
    
    
    
                  
    }
    and main.c (dont really care about this part yet):

    Code:
    #include <stdio.h>
    #include "RSTSender.c"
    
    
    
    
    int main (int argc, const char * argv[])
    {
    
    
    RSTInjector("127.0.0.1", 20, "127.0.0.1");
    
    
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    a mach-o clang linker(ld) error
    It would be helpful to have the exact error, so copy and paste it into your next post.
    dmh2000 likes this.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    it is weird that you are passing strings as arg 1 and 3 of RSTInjector but the function takes 3 int's. if this were c++, i would guess it was looking for the overload of RSTInjector(char *,int,char *) but C wouldn't do that.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Error

    I knew I was forgetting something. Here is error in question:

    Apple Mach-O linker(ld) error:

    Ld /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Products/Debug/connectionSniper normal x86_64
    cd "/Users/November/Documents/Xcode Files/Networking/connectionSniper"
    setenv MACOSX_DEPLOYMENT_TARGET 10.6
    /Developer/usr/bin/clang -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -L/Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Products/Debug -F/Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Products/Debug -filelist /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Intermediates/connectionSniper.build/Debug/connectionSniper.build/Objects-normal/x86_64/connectionSniper.LinkFileList -mmacosx-version-min=10.6 -o /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Products/Debug/connectionSniper


    ld: duplicate symbol _ourCheckSum in /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Intermediates/connectionSniper.build/Debug/connectionSniper.build/Objects-normal/x86_64/RSTSender.o and /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Intermediates/connectionSniper.build/Debug/connectionSniper.build/Objects-normal/x86_64/main.o for architecture x86_64
    Command /Developer/usr/bin/clang failed with exit code 1

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Beginning programmers SHOULD NEVER include C or C++ files!!
    Code:
    #include "checkSum.c"
    Instead, learn how to make and use a header correctly and your error will likely go away.

    Tim S.
    Last edited by stahta01; 10-22-2012 at 06:43 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Quote Originally Posted by stahta01 View Post
    Beginning programmers SHOULD NEVER include C or C++ files!!
    Code:
    #include "checkSum.c"
    Instead, learn how to make and use a header correctly and your error will likely go away.

    Tim S.
    I really appreciate your feed back Tim, though some additional content might have been nice. Your right, I dont really know how to link c files. Do you have any examples of a header/main configuration or just a collection of linked c files?

    The reason I gave the ourCheckSum function its own files was kind of odd: when I included the function in the RSTSender.c clang wanted a semicolon after the function name, before the brackets.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Try this link for an example header and why to use a header guard.

    Include guard - Wikipedia, the free encyclopedia

    I converted your checkSum.c into a header and source file below.

    If the problem is what I suspect the linker error will change to a different error.

    Remember change all
    #include "checkSum.c"
    into

    #include "checkSum.h"


    Tim S.

    Code:
    /* checkSum.h */
    #ifndef CHECKSUM_H
    #define CHECKSUM_H
     
    unsigned short ourCheckSum (unsigned short *buf, int nwords);
     
    #endif /* CHECKSUM_H */
    Edit: I forgot to add the include to the .c file fixed now.

    Code:
    /* checkSum.c */
    
    #include "checkSum.h" 
    
    unsigned short ourCheckSum (unsigned short *buf, int nwords)
    {
        unsigned long sum;
        for (sum = 0; nwords > 0; nwords--)
            sum += *buf++;
        sum = (sum >> 16) + (sum & 0xffff);
        sum += (sum >> 16);
        return ~sum;
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Changes made and still nothing

    I have made the changes, however, I am returned the same error. I feel like I missing something really simple here.

    What did you suspects the error was?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    That you were linking the object file(s) created by compiling the checkSum file twice or maybe more times.

    The other thing is maybe you are linking to a library that provides another function with the same name as "ourCheckSum" which results in

    Code:
    ld: duplicate symbol _ourCheckSum
    I was going with the error now being a different function name.

    Note: Did you really read the error message?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Quote Originally Posted by stahta01 View Post
    That you were linking the object file(s) created by compiling the checkSum file twice or maybe more times.

    The other thing is maybe you are linking to a library that provides another function with the same name as "ourCheckSum" which results in

    Code:
    ld: duplicate symbol _ourCheckSum
    I was going with the error now being a different function name.

    Note: Did you really read the error message?

    Tim S.
    Yeah, I had the same thought as you about the double inclusion. Does c have a checksum generator I should be using?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Have you edited ALL the source files that had #include "checkSum.c", to change it to #include "checkSum.h", and recompiled them?

    Keywords in error message highlighted
    ld: duplicate symbol _ourCheckSum in /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Intermediates/connectionSniper.build/Debug/connectionSniper.build/Objects-normal/x86_64/RSTSender.o and /Users/November/Library/Developer/Xcode/DerivedData/connectionSniper-fozyvuepfhsdkxcikgywpnawiesx/Build/Intermediates/connectionSniper.build/Debug/connectionSniper.build/Objects-normal/x86_64/main.o for architecture x86_64

    The messages tell you which files to look in.
    stahta01 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Thank You

    I followed your advice and created a header for both my function file, including only the headers were necessary.

    Thank you so much for your time helping me debug this. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm getting a linker error?
    By Jesse20ghet in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2011, 06:23 PM
  2. Linker error help
    By bonks in forum C Programming
    Replies: 14
    Last Post: 03-17-2011, 08:18 AM
  3. linker error
    By rajdey1 in forum C Programming
    Replies: 2
    Last Post: 04-17-2008, 09:15 AM
  4. Programing error
    By pwrade in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2007, 09:00 AM
  5. Sock_raw
    By JagWire in forum Linux Programming
    Replies: 2
    Last Post: 07-30-2002, 03:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21