Commit 10f80112 authored by Joanne Hugé's avatar Joanne Hugé

Clean up code

parent a972e493
This diff is collapsed.
......@@ -22,10 +22,11 @@
#define BUFFER_SIZE 1024
static char rx_buffer[BUFFER_SIZE];
static int sock_fd;
int init_udp_recv() {
int status;
int fd = -1;
int sock_fd = -1;
struct addrinfo hints, *servinfo, *servinfo_it;
memset(&hints, 0, sizeof hints);
......@@ -41,11 +42,11 @@ int init_udp_recv() {
for (servinfo_it = servinfo; servinfo_it;
servinfo_it = servinfo_it->ai_next) {
fd = socket(servinfo->ai_family, servinfo->ai_socktype,
sock_fd = socket(servinfo->ai_family, servinfo->ai_socktype,
servinfo->ai_protocol);
if (bind(fd, servinfo_it->ai_addr, servinfo_it->ai_addrlen) == -1) {
close(fd);
if (bind(sock_fd, servinfo_it->ai_addr, servinfo_it->ai_addrlen) == -1) {
close(sock_fd);
continue;
}
break;
......@@ -53,23 +54,23 @@ int init_udp_recv() {
freeaddrinfo(servinfo);
if(fd == -1)
if(sock_fd == -1)
error(EXIT_FAILURE, errno, "Couldn't create receive socket");
printf("waiting to receive...\n");
return fd;
return sock_fd;
}
void recv_udp_packet(int fd) {
void recv_udp_packet() {
#ifdef DEBUG
int bytes_received = 0;
bytes_received = recvfrom(fd, rx_buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
bytes_received = recvfrom(sock_fd, rx_buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
if (bytes_received == -1)
error(EXIT_FAILURE, errno, "Error while attempting to receive packets");
#else
recvfrom(fd, rx_buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
recvfrom(sock_fd, rx_buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
#endif
}
......@@ -2,6 +2,6 @@
#define RECV_PACKET
int init_udp_recv();
void recv_udp_packet(int fd);
void recv_udp_packet();
#endif
......@@ -48,7 +48,7 @@ static int so_priority = 3;
static struct sock_txtime sk_txtime;
static unsigned char *tx_buffer;
static size_t tx_buffer_len;
static int fd;
static int sock_fd;
static int64_t tai_offset;
......@@ -62,7 +62,7 @@ static int set_if(char *network_if) {
memset(&ifreq, 0, sizeof(ifreq));
strncpy(ifreq.ifr_name, network_if, sizeof(ifreq.ifr_name) - 1);
if (ioctl(fd, SIOCGIFINDEX, &ifreq))
if (ioctl(sock_fd, SIOCGIFINDEX, &ifreq))
error(EXIT_FAILURE, errno, "ioctl SIOCGIFINDEX failed\n");
return ifreq.ifr_ifindex;
......@@ -103,17 +103,17 @@ void init_udp_send(int use_etf, int use_timestamps, int packet_priority,
so_priority = packet_priority;
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0) error(EXIT_FAILURE, errno, "Socket creation failed\n");
sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock_fd < 0) error(EXIT_FAILURE, errno, "Socket creation failed\n");
index = set_if(network_if);
if (index < 0) error(EXIT_FAILURE, errno, "Couldn't set interface\n");
if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &so_priority,
if (setsockopt(sock_fd, SOL_SOCKET, SO_PRIORITY, &so_priority,
sizeof(so_priority)))
error(EXIT_FAILURE, errno, "Couldn't set socket priority\n");
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, network_if,
if (setsockopt(sock_fd, SOL_SOCKET, SO_BINDTODEVICE, network_if,
strlen(network_if)))
error(EXIT_FAILURE, errno, "setsockopt SO_BINDTODEVICE failed\n");
......@@ -121,12 +121,12 @@ void init_udp_send(int use_etf, int use_timestamps, int packet_priority,
sk_txtime.clockid = CLOCK_TAI;
sk_txtime.flags = 0;
if (setsockopt(fd, SOL_SOCKET, SO_TXTIME, &sk_txtime, sizeof(sk_txtime)))
if (setsockopt(sock_fd, SOL_SOCKET, SO_TXTIME, &sk_txtime, sizeof(sk_txtime)))
error(EXIT_FAILURE, errno, "setsockopt SO_TXTIME failed\n");
}
if (use_timestamps) {
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &so_timestamping_flags,
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, &so_timestamping_flags,
sizeof(so_timestamping_flags)))
error(EXIT_FAILURE, errno, "setsockopt SO_TIMESTAMPING failed\n");
}
......@@ -157,7 +157,7 @@ struct packet_timestamps send_udp_packet(int use_etf, int use_timestamps,
struct iovec iov;
int sendmsgerr;
int res;
struct pollfd poll_fd = {fd, POLLPRI, 0};
struct pollfd poll_fd = {sock_fd, POLLPRI, 0};
struct packet_timestamps packet_ts;
struct timespec ts;
......@@ -199,7 +199,7 @@ struct packet_timestamps send_udp_packet(int use_etf, int use_timestamps,
packet_ts.user_call_sendmsg = ts_to_uint(ts);
}
sendmsgerr = sendmsg(fd, &msg, 0);
sendmsgerr = sendmsg(sock_fd, &msg, 0);
if (sendmsgerr < 0)
error(EXIT_FAILURE, errno, "sendmsg failed, ret value: %d\n", sendmsgerr);
......@@ -255,7 +255,7 @@ static void process_timestamps(struct packet_timestamps *packet_ts, int64_t hist
msg.msg_control = &control;
msg.msg_controllen = sizeof(control);
if (recvmsg(fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT) == -1) {
if (recvmsg(sock_fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT) == -1) {
fprintf(stderr, "recvmsg failed\n");
return;
}
......@@ -292,7 +292,7 @@ static int process_socket_error_queue() {
.msg_control = msg_control,
.msg_controllen = sizeof(msg_control)};
if (recvmsg(fd, &msg, MSG_ERRQUEUE) == -1) {
if (recvmsg(sock_fd, &msg, MSG_ERRQUEUE) == -1) {
fprintf(stderr, "recvmsg failed");
return -1;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment