C   31
tcp6send c
Guest on 4th February 2023 01:20:53 PM


  1.  
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #include <sys/time.h>
  9. #include <fcntl.h>
  10. #include <sys/times.h>
  11. #include <stdlib.h>
  12. #include <memory.h>
  13.  
  14.  
  15. /* IPv6 AF_INET6 sockets:
  16.  
  17. struct sockaddr_in6 {
  18.     u_int16_t       sin6_family;   // address family, AF_INET6
  19.     u_int16_t       sin6_port;     // port number, Network Byte Order
  20.     u_int32_t       sin6_flowinfo; // IPv6 flow information
  21.     struct in6_addr sin6_addr;     // IPv6 address
  22.     u_int32_t       sin6_scope_id; // Scope ID
  23. };
  24.  
  25. struct in6_addr {
  26.     unsigned char   s6_addr[16];   // load with inet_pton()
  27. };
  28.  
  29. */
  30.  
  31. struct sockaddr_in6 saddr =
  32. {
  33.    sin6_family:  AF_INET6,
  34. };
  35.  
  36. struct sockaddr_in6 raddr =
  37. {
  38.    sin6_family:  AF_INET6,
  39. };
  40.  
  41. struct in6_addr myaddr =
  42. {
  43. //  fe80:0000:0000:0000:0000:0214:4fff:fe80:db46
  44.    {0xfe ,0x80 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  45.     0x02 ,0x14 ,0x4f ,0xff ,0xfe ,0x80, 0xdb, 0x46},
  46. };
  47.  
  48. char buf[256];
  49.  
  50. char buf2[16];
  51.  
  52. int main(
  53. int argc,
  54. char **argv)
  55. {
  56.    int s;
  57.    int m;
  58.    int rc;
  59.    int addrlen = sizeof(raddr);
  60.  
  61.    saddr.sin6_port = htons(3334);
  62. // saddr.sin6_flowinfo = htonl(1);
  63. // saddr.sin6_scope_id = htonl(2);
  64.    bcopy(&myaddr, &saddr.sin6_addr, 16);
  65.  
  66.  
  67. // inet_pton(AF_INET6, "fe80::214:4fff:fe80:db46" , &saddr.sin6_addr);
  68. // inet_pton(AF_INET6, "fe80::214:4fff:fe80:db47" , &saddr.sin6_addr);
  69.    inet_pton(AF_INET6, "fec0::20c:29ff:fecf:5d69" , &saddr.sin6_addr);
  70. // inet_pton(AF_INET6, "::1" , &saddr.sin6_addr);
  71.  
  72.    s = socket(PF_INET6, SOCK_STREAM, 0);
  73.    if (s < 0)
  74.    {
  75.       perror("socket");
  76.       exit(1);
  77.    }
  78.  
  79. #if 0
  80.    rc = setsockopt(s, SOL_SOCKET,  SO_BINDTODEVICE,    "eth0", 8);
  81.    if (rc < 0)
  82.    {
  83.       perror("sockopt");
  84.       exit(1);
  85.    }
  86.  
  87.    rc = getsockopt(s, SOL_SOCKET,  SO_BINDTODEVICE,    buf2, &addrlen);
  88.    if (rc < 0)
  89.    {
  90.       perror("sockopt");
  91.       exit(1);
  92.    }
  93. #endif
  94.  
  95.    printf("Back from sockopt \n");
  96.  
  97.    saddr.sin6_scope_id = htonl(1);
  98.    rc = connect(s, (struct sockaddr *)&saddr,  sizeof(saddr));
  99.  
  100.    if (rc < 0)
  101.    {
  102.       perror("connect");
  103.       exit(1);
  104.  
  105.    }
  106.  
  107.    rc = read(0, buf, sizeof(buf));
  108.    while (rc > 0)
  109.    {
  110.       write(s, buf, rc);
  111.       rc = read(0, buf, sizeof(buf));
  112.    }
  113.  
  114. }

Raw Paste

Login or Register to edit or fork this paste. It's free.