Sendto works on localhost only

Guys, I have been trying to fix this but I think I need your help. I am newbie and I cant seem to find the issue. Sendto returns -1. For one, I don’t know how to make Sendto spit out more details about the error or what’s causing it to return -1. Im running Linux btw.

Here is the code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#define MAX_PACKET_SIZE 4096
u_int32_t src_addr, dst_addr;
u_int16_t src_port, dst_port;
unsigned short csum (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 (unsigned short)(~sum);
}
void setup_ip_header(struct iphdr *iph)
{
iph->saddr = src_addr; // SOURCE IP ADDRESS
iph->daddr = dst_addr; // TARGET IP ADDRESS
iph->ihl = 5;
iph->version = 4;
iph->tos = 0;
iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
iph->id = htonl(54321); /*setup any arbitrary number for id */
iph->frag_off = 0;
iph->ttl = MAXTTL;
iph->protocol = 6; /* upper layer protocol, TCP*/
iph->check = 0;
}
void setup_tcp_header(struct tcphdr *tcph)
{
tcph->source = htons(src_port); // SOURCE PORT
tcph->dest = htons(dst_port); // TARGET PORT
tcph->seq = random();
tcph->ack_seq = 0;
tcph->res2 = 0;
tcph->doff = 5;
tcph->syn = 1;
tcph->window = htonl(65535);
tcph->check = 0;
tcph->urg_ptr = 0;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct sockaddr_in sin;
const char *src_addr_str = "192.168.1.50";
src_addr = inet_addr(src_addr_str);
src_port = 777;
const char *dst_addr_str = "192.168.1.51";
dst_addr = inet_addr(dst_addr_str);
dst_port = 888;
char datagram[MAX_PACKET_SIZE];
struct iphdr *iph = (struct iphdr *)datagram;
struct tcphdr *tcph = (struct tcphdr *)((u_int8_t *)iph + (5 * sizeof(u_int32_t)));
char new_ip[sizeof "255.255.255.255"];
int src_socket = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
if(src_socket < 0){
fprintf(stderr, "Could not open raw socket.n");
exit(-1);
}
/* a IP_HDRINCL call, to make sure that the kernel knows
* the header is included in the data, and doesn't insert
* its own header into the packet before our data
*/
int tmp = 1;
const int *val = &tmp;
if(setsockopt(src_socket, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!n");
exit(-1);
}
// SETUP DETAILS ABOUT THE TARGET MACHINE
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = dst_addr;
sin.sin_port = htons(dst_port);
// Clear the data
memset(datagram, 0, MAX_PACKET_SIZE);
// Set appropriate fields in headers
setup_ip_header(iph);
setup_tcp_header(tcph);
iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
for(;;){
int ret = sendto(src_socket, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
if(ret == -1)
{
fprintf(stderr, "sendto() error!!!.n");
}
else
{
fprintf(stdout, "Flooding %s at %u...n", dst_addr_str , dst_port);
}
sleep(1);
}
return 0;
}
</code>
<code>#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <arpa/inet.h> #define MAX_PACKET_SIZE 4096 u_int32_t src_addr, dst_addr; u_int16_t src_port, dst_port; unsigned short csum (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 (unsigned short)(~sum); } void setup_ip_header(struct iphdr *iph) { iph->saddr = src_addr; // SOURCE IP ADDRESS iph->daddr = dst_addr; // TARGET IP ADDRESS iph->ihl = 5; iph->version = 4; iph->tos = 0; iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr)); iph->id = htonl(54321); /*setup any arbitrary number for id */ iph->frag_off = 0; iph->ttl = MAXTTL; iph->protocol = 6; /* upper layer protocol, TCP*/ iph->check = 0; } void setup_tcp_header(struct tcphdr *tcph) { tcph->source = htons(src_port); // SOURCE PORT tcph->dest = htons(dst_port); // TARGET PORT tcph->seq = random(); tcph->ack_seq = 0; tcph->res2 = 0; tcph->doff = 5; tcph->syn = 1; tcph->window = htonl(65535); tcph->check = 0; tcph->urg_ptr = 0; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); struct sockaddr_in sin; const char *src_addr_str = "192.168.1.50"; src_addr = inet_addr(src_addr_str); src_port = 777; const char *dst_addr_str = "192.168.1.51"; dst_addr = inet_addr(dst_addr_str); dst_port = 888; char datagram[MAX_PACKET_SIZE]; struct iphdr *iph = (struct iphdr *)datagram; struct tcphdr *tcph = (struct tcphdr *)((u_int8_t *)iph + (5 * sizeof(u_int32_t))); char new_ip[sizeof "255.255.255.255"]; int src_socket = socket(PF_INET, SOCK_RAW, IPPROTO_TCP); if(src_socket < 0){ fprintf(stderr, "Could not open raw socket.n"); exit(-1); } /* a IP_HDRINCL call, to make sure that the kernel knows * the header is included in the data, and doesn't insert * its own header into the packet before our data */ int tmp = 1; const int *val = &tmp; if(setsockopt(src_socket, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){ fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!n"); exit(-1); } // SETUP DETAILS ABOUT THE TARGET MACHINE sin.sin_family = AF_INET; sin.sin_addr.s_addr = dst_addr; sin.sin_port = htons(dst_port); // Clear the data memset(datagram, 0, MAX_PACKET_SIZE); // Set appropriate fields in headers setup_ip_header(iph); setup_tcp_header(tcph); iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1); for(;;){ int ret = sendto(src_socket, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin)); if(ret == -1) { fprintf(stderr, "sendto() error!!!.n"); } else { fprintf(stdout, "Flooding %s at %u...n", dst_addr_str , dst_port); } sleep(1); } return 0; } </code>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#define MAX_PACKET_SIZE 4096

u_int32_t src_addr, dst_addr;
u_int16_t src_port, dst_port;


unsigned short csum (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 (unsigned short)(~sum);
}

void setup_ip_header(struct iphdr *iph)
{
    iph->saddr = src_addr;                          // SOURCE IP ADDRESS 
    iph->daddr = dst_addr;                          // TARGET IP ADDRESS

    iph->ihl = 5;
    iph->version = 4;
    iph->tos = 0;
    iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
    iph->id = htonl(54321); /*setup any arbitrary number for id */
    iph->frag_off = 0;
    iph->ttl = MAXTTL;
    iph->protocol = 6; /* upper layer protocol, TCP*/
    iph->check = 0;

}

void setup_tcp_header(struct tcphdr *tcph)
{
    tcph->source = htons(src_port);                 // SOURCE PORT
    tcph->dest = htons(dst_port);                   // TARGET PORT

    tcph->seq = random();
    tcph->ack_seq = 0;
    tcph->res2 = 0;
    tcph->doff = 5;
    tcph->syn = 1;
    tcph->window = htonl(65535);
    tcph->check = 0;
    tcph->urg_ptr = 0;
}



int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    struct sockaddr_in sin;                                         

    const char *src_addr_str = "192.168.1.50";
    src_addr = inet_addr(src_addr_str);
    src_port = 777;

    const char *dst_addr_str = "192.168.1.51";
    dst_addr = inet_addr(dst_addr_str);
    dst_port = 888;

    char datagram[MAX_PACKET_SIZE];
    struct iphdr *iph = (struct iphdr *)datagram;
    struct tcphdr *tcph = (struct tcphdr *)((u_int8_t *)iph + (5 * sizeof(u_int32_t)));
   
    char new_ip[sizeof "255.255.255.255"];


    int src_socket = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);       

    if(src_socket < 0){
        fprintf(stderr, "Could not open raw socket.n");
        exit(-1);
    }

    /* a IP_HDRINCL call, to make sure that the kernel knows
    *     the header is included in the data, and doesn't insert
    *     its own header into the packet before our data
    */
    int tmp = 1;
    const int *val = &tmp;
    if(setsockopt(src_socket, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
        fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!n");
        exit(-1);
    }


    // SETUP DETAILS ABOUT THE TARGET MACHINE
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = dst_addr;
    sin.sin_port = htons(dst_port);

    // Clear the data
    memset(datagram, 0, MAX_PACKET_SIZE);

    // Set appropriate fields in headers
    setup_ip_header(iph);
    setup_tcp_header(tcph);

    iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);


    for(;;){

        int ret = sendto(src_socket, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));

        if(ret == -1)
        {
            fprintf(stderr, "sendto() error!!!.n");
        }
        else
        {
            fprintf(stdout, "Flooding %s at %u...n", dst_addr_str , dst_port);
        }

       
        sleep(1);

    }


    return 0;
}

Thank you so much in advance.

Regards,

X

I also noticed that the sizeof(sin) returns only 16, not sure if this is normal. Is that normal ?

How can I make the compiler spit out more info about an error when it encounters one ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật