Obsah

Programovani soketu v C

Sokety

Datové typy a struktury pro HW nebo protokoly

struct my_struct {
        uint8_t  field1;
        uint16_t field2;
} __attribute__((packed));
#include <netinet/in.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

Prace s adresami

include <netinet/in.h>

// All pointers to socket address structures are often cast to pointers
// to this type before use in various functions and system calls:

struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
};


// IPv4 AF_INET sockets:

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};


// IPv6 AF_INET6 sockets:

struct sockaddr_in6 {
    u_int16_t       sin6_family;   // address family, AF_INET6
    u_int16_t       sin6_port;     // port number, Network Byte Order
    u_int32_t       sin6_flowinfo; // IPv6 flow information
    struct in6_addr sin6_addr;     // IPv6 address
    u_int32_t       sin6_scope_id; // Scope ID
};

struct in6_addr {
    unsigned char   s6_addr[16];   // load with inet_pton()
};


// General socket address holding structure, big enough to hold either
// struct sockaddr_in or struct sockaddr_in6 data:

struct sockaddr_storage {
    sa_family_t  ss_family;     // address family

    // all this is padding, implementation specific, ignore it:
    char      __ss_pad1[_SS_PAD1SIZE];
    int64_t   __ss_align;
    char      __ss_pad2[_SS_PAD2SIZE];
};

Broadcast

Test příjmu broadcastu aplikací listen

 
nc -l -p -u 9999
mz eth0 -A 10.10.11.2 -B 255.255.255.255 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"
mz eth0 -A 10.10.11.2 -B 10.10.11.127 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"
mz eth0 -A 10.10.11.2 -B 10.10.11.128 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"
mz eth0 -A 10.10.11.2 -B 10.10.11.255 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"
mz eth0 -A 192.168.10.10 -B 10.10.11.127 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"  // z jiné sítě a neexistující
mz eth0 -A 10.10.11.8 -B 255.255.255.255 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"  // neexistujici ip
mz eth0 -A 10.10.11.3  -B 10.10.11.127 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"    // neexistujici ip
mz eth0 -A 10.10.11.1  -B 10.10.11.127 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"    // ip brany, ale moje mac
mz eth0 -A 192.168.100.1 -B 10.10.11.127 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"  // existující, ale jíná síť
mz eth0 -A 192.168.100.1 -B 255.255.255.255 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39" // existující, ale jíná síť
mz eth0 -A 192.168.20.20 -B 255.255.255.255 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39" // nebyla nastavená gw
mz eth0 -A 192.168.20.20 -B 10.10.11.127 -t udp "dp=9999,p=36:30:34:36:36:30:30:31:39"    // nevedel, ze je to bcast