summaryrefslogtreecommitdiff
path: root/src/network.c
blob: 58fe6639b3f79ee755d924412d6139b9fe3b6790 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "junk/network.h"
#include <arpa/inet.h>
#include <errno.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#include <netdb.h>
#include <netinet/in.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#define TAG "network"

int tcp_ipv4_send(char *ip, char *port, char *data) {

  int sock;
  struct addrinfo hints, *p, *res;

  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;

  if (getaddrinfo(ip, port, &hints, &res) != 0) {
    fprintf(stderr, "%s: getaddrinfo (%d): %s\n", TAG, errno,
            gai_strerror(errno));
    return errno;
  }

  if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) ==
      -1) {
    fprintf(stderr, "%s: socket (%d): %s\n", TAG, errno, strerror(errno));
    return errno;
  }

  if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
    fprintf(stderr, "%s: connect (%d): %s\n", TAG, errno, strerror(errno));
    return errno;
  }

  int len = strlen(data);
  int bytes_sent = 0;
  int total_bytes_sent = 0;

  while (1) {
    if ((bytes_sent = send(sock, data, len, 0)) == -1) {
      fprintf(stderr, "%s: send (%d): %s\n", TAG, errno, strerror(errno));
      return errno;
    }
    total_bytes_sent += bytes_sent;
    if (bytes_sent == len) {
      break;
    }
  }
  close(sock);
  freeaddrinfo(res);
  return 0;
}

/* eth_bind
 * Bind to a L2 Ethernet address.
 *
 */
int eth_bind(char address[]) {

  int sock;
  struct sockaddr_ll addrinfo;

  if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
    fprintf(stderr, "%s: socket (%d): %s\n", TAG, errno, strerror(errno));
    return errno;
  }
  fprintf(stderr, "%s: socket fd: (%d)\n", TAG, sock);

  memset(&addrinfo, 0, sizeof addrinfo);
  addrinfo.sll_family = AF_PACKET;
  addrinfo.sll_protocol = htons(ETH_P_ARP);
  addrinfo.sll_ifindex = 0;
  addrinfo.sll_pkttype = PACKET_BROADCAST;
  sscanf((char *)address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
         &addrinfo.sll_addr[0], &addrinfo.sll_addr[1], &addrinfo.sll_addr[2],
         &addrinfo.sll_addr[3], &addrinfo.sll_addr[4], &addrinfo.sll_addr[5]);
  addrinfo.sll_halen = 6;

  fprintf(stderr, "%s: target mac (%02x:%02x:%02x:%02x:%02x:%02x)\n", TAG,
          addrinfo.sll_addr[0], addrinfo.sll_addr[1], addrinfo.sll_addr[2],
          addrinfo.sll_addr[3], addrinfo.sll_addr[4], addrinfo.sll_addr[5]);

  int status = 0;
  if ((status = bind(sock, (struct sockaddr *)&addrinfo, sizeof(addrinfo))) !=
      0) {
    fprintf(stderr, "%s: bind (%d): %s\n", TAG, errno, strerror(errno));
    return status;
  }

  return sock;
}

/* eth_recv
 * Recv on a L2 Ethernet address.
 *
 */
int eth_recv(int sockfd, arp_packet *packet) {
  int buffer_len = 200;
  char buffer[buffer_len];
  memset(buffer, 0, buffer_len);
  int bytes_read = 0;
  int total_bytes_read = 0;

  while (1) {
    bytes_read = recv(sockfd, &buffer[total_bytes_read],
                      buffer_len - total_bytes_read, 0);
    if (bytes_read == -1) {
      fprintf(stderr, "%s: recv (%d): %s\n", TAG, errno, strerror(errno));
      return -1;
    } else if (bytes_read > 0) {
      fprintf(stderr, "%s: recv: read %d bytes, total %d\n", TAG, bytes_read,
              total_bytes_read);
      total_bytes_read += bytes_read;
    } else {

      ethhdr *recv_struct = (ethhdr *)buffer;
      fprintf(stderr, "%s: source mac (%02x:%02x:%02x:%02x:%02x:%02x)\n", TAG,
              recv_struct->h_source[0], recv_struct->h_source[1],
              recv_struct->h_source[2], recv_struct->h_source[3],
              recv_struct->h_source[4], recv_struct->h_source[5]);
      fprintf(stderr, "%s: dest mac (%02x:%02x:%02x:%02x:%02x:%02x)\n", TAG,
              recv_struct->h_dest[0], recv_struct->h_dest[1],
              recv_struct->h_dest[2], recv_struct->h_dest[3],
              recv_struct->h_dest[4], recv_struct->h_dest[5]);

      memcpy((char *)packet, (char *)(buffer + sizeof(ethhdr)),
             sizeof(arp_packet));
      return 0;
    }
  }
}