Convert <netinet/in.h> protocols to names? - c

There is a long list of protocols defined in <netinet/in.h>:
/*
* Protocols (RFC 1700)
*/
#define IPPROTO_IP 0 /* dummy for IP */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_HOPOPTS 0 /* IP6 hop-by-hop options */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
#define IPPROTO_ICMP 1 /* control message protocol */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_IGMP 2 /* group mgmt protocol */
#define IPPROTO_GGP 3 /* gateway^2 (deprecated) */
#define IPPROTO_IPV4 4 /* IPv4 encapsulation */
#define IPPROTO_IPIP IPPROTO_IPV4 /* for compatibility */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
#define IPPROTO_TCP 6 /* tcp */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_ST 7 /* Stream protocol II */
/* ... */
Is there a default function/call to translate protocols defined in <netinet/in.h> to names? Ideal to have chars[] in return:
ipproto2str(IPPROTO_ICMP): "Control Message Protocol (ICMP)"

You're looking for getprotobynumber().
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
int main(void) {
struct protoent *p = getprotobynumber(IPPROTO_ICMP);
if (p) {
printf("IPPROTO_ICMP (%d) = %s\n", p->p_proto, p->p_name);
} else {
puts("Couldn't find IPPROTO_ICMP");
}
}

Related

xQueueReceive blocks the execution of other tasks

I am developing a FreeRTOS application on a Cortex-M4 (NXP i.MX8MM) that creates 4 tasks.
The first task is waiting to receive a character on a UART. As soon as a buffer is received, it is sent to task 3 with xQueueSend.
The second task is the transmission task via the UART. It waits permanently for the reception of a buffer (sent by task 4) with xQueueReceive.
Currently, I manage to create these 4 tasks, but only the first 2 are executed. I have the impression that the second task, and particularly the xQueueReceive function, is blocking (or not executing properly) the execution of the last 2 tasks. If I comment out the xQueueReceive function, all tasks run without any problem. All configASSERT in the xQueueReceive function pass without problem.
Where do you think this can come from?
My code:
main.c
/* System includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Kernel includes */
#include "FreeRTOS.h"
#include "task.h"
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "rsc_table.h"
/* Other includes */
#include "A_version.h"
#include "A_rpmsg.h"
#include "A_ip_task.h"
#include "A_queue.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Globals
******************************************************************************/
/* Queue RPMSG -> UART RS-485 */
static QueueHandle_t rpmsg_to_uart_queue = NULL;
/* Queue RPMSG <- UART RS-485 */
static QueueHandle_t uart_to_rpmsg_queue = NULL;
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Code
******************************************************************************/
/*!
* #brief Main function
*/
int main(void)
{
uint8_t status = 1;
/* Initialize standard SDK demo application pins */
/* Board specific RDC settings */
BOARD_RdcInit();
BOARD_InitBootPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
BOARD_InitMemory();
copyResourceTable();
#ifdef MCMGR_USED
/* Initialize MCMGR before calling its API */
(void)MCMGR_Init();
#endif /* MCMGR_USED */
PRINTF("Start Appli_metier: %s\r\n", NUM_ET_DATE_VERSION_CARTE_CM);
/* Chargement des paramètres de l'UART */
F_ip_485_load_params();
/* Initialisation de l'UART */
status = F_init_ip_485();
/* Initialisation de RPMsg */
if(status != 0)
{
PRINTF("ERROR INIT UART\r\n");
} else {
status = rpmsg_init();
}
rpmsg_to_uart_queue = xQueueCreate(10, sizeof(struct Message *));
if (rpmsg_to_uart_queue != NULL)
{
vQueueAddToRegistry(rpmsg_to_uart_queue, "RPMsg to UART queue");
}
uart_to_rpmsg_queue = xQueueCreate(10, sizeof(struct Message *));
if (uart_to_rpmsg_queue != NULL)
{
vQueueAddToRegistry(uart_to_rpmsg_queue, "UART to RPMsg queue");
}
/* Lancement des taches */
if(status != 0)
{
PRINTF("ERROR INIT RPMSG\r\n");
} else {
PRINTF("Avant F_ip_485_task\r\n");
/* Lancement de la tache UART */
F_ip_485_task(rpmsg_to_uart_queue, uart_to_rpmsg_queue);
PRINTF("Avant rpmsg_task\r\n");
/* Lancement de la tache RPMSG (cote A53) */
rpmsg_task(rpmsg_to_uart_queue, uart_to_rpmsg_queue);
}
vTaskStartScheduler();
PRINTF("Failed to start FreeRTOS on core0.\n");
for (;;)
;
}
tasks 1 and 2:
#include "A_ip_task.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "fsl_debug_console.h"
#include "A_ip_485.h"
#include "A_queue.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define IP_485_RX_TASK_STACK_SIZE (256)
#define IP_485_TX_TASK_STACK_SIZE (256)
#define MAX_BUF_TRAME_ETHERNET 1520
/* Task priorities. */
#define hello_task_PRIORITY (configMAX_PRIORITIES - 1)
#define TASK_DELAY_IP_485 200
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
typedef struct _uart_struct
{
QueueHandle_t my_queue_to_uart;
QueueHandle_t my_queue_to_rpmsg;
} uart_struct;
static uart_struct my_uart_struct;
static TaskHandle_t ip_485_rx_task_handle = NULL;
static TaskHandle_t ip_485_tx_task_handle = NULL;
STRUCT_parametre_uart S_device_UART[NBR_MAX_UART];
unsigned char TUCH_ip_485_buffer[MAX_BUF_TRAME_ETHERNET];
static void F_ip_485_rx_task(void *pvParameters)
{
uart_struct * param_struct = pvParameters;
Message st_msg;
uint16_t UI_lg;
/* Block for 5µs. */
const TickType_t xDelay = 5 / (portTICK_PERIOD_MS * 1000);
PRINTF("\r\nF_ip_485_rx_task running ...\r\n");
/* On attend la réception d'une trame sur la file */
for (;;)
{
if ((UI_lg = F_driverUART_read(UART_IP485, TUCH_ip_485_buffer, MAX_BUF_TRAME_ETHERNET)) != 0)
{
//TODO: F_traitement_RX_IP_485(TUCH_ip_485_buffer, UI_lg); /* Traitement dans MQX */
/* Remplacer par écriture dans la queue de RPMsg pour envoi vers Cortex-A53 */
st_msg.size = UI_lg;
xQueueSend(param_struct->my_queue_to_rpmsg, (void *)&st_msg, 10);
}
vTaskDelay(xDelay);
}
vTaskSuspend(NULL);
}
static void F_ip_485_tx_task(void *pvParameters)
{
uart_struct * param_struct = pvParameters;
Message rcv_msg;
/* Block for 5µs. */
const TickType_t xDelay = 5 / (portTICK_PERIOD_MS * 1000);
PRINTF("\r\nF_ip_485_tx_task running ...\r\n");
for (;;)
{
if (xQueueReceive(param_struct->my_queue_to_uart, (void *)&rcv_msg, 0) != pdTRUE)
{
PRINTF("Failed to receive queue.\r\n");
}
else
{
/* On envoie tous les octets */
F_driverUART_IP485_write(rcv_msg.body, rcv_msg.size);
}
vTaskDelay(xDelay);
}
vTaskSuspend(NULL);
}
void F_ip_485_task(QueueHandle_t r_to_u, QueueHandle_t u_to_r)
{
my_uart_struct.my_queue_to_uart = r_to_u;
my_uart_struct.my_queue_to_rpmsg = u_to_r;
if (xTaskCreate(F_ip_485_rx_task, "IP 485 RX", configMINIMAL_STACK_SIZE + 100, NULL, tskIDLE_PRIORITY + 1, &ip_485_rx_task_handle) !=
pdPASS)
{
PRINTF("Task creation failed!.\r\n");
while (1)
; }
if (xTaskCreate(F_ip_485_tx_task, "IP 485 TX", configMINIMAL_STACK_SIZE + 100, NULL, tskIDLE_PRIORITY + 1, &ip_485_tx_task_handle) !=
pdPASS)
{
PRINTF("Task creation failed!.\r\n");
while (1)
;
}
}
tasks 3 and 4:
#include "A_rpmsg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "rpmsg_lite.h"
#include "rpmsg_queue.h"
#include "rpmsg_ns.h"
#include "A_queue.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define RPMSG_LITE_SHMEM_BASE (VDEV0_VRING_BASE)
#define RPMSG_LITE_LINK_ID (RL_PLATFORM_IMX8MM_M4_USER_LINK_ID)
#define RPMSG_LITE_NS_ANNOUNCE_STRING "rpmsg-virtual-tty-channel-1"
#define RPMSG_RX_TASK_STACK_SIZE (128)
#define RPMSG_TX_TASK_STACK_SIZE (128)
#ifndef LOCAL_EPT_ADDR
#define LOCAL_EPT_ADDR (30)
#endif
/*******************************************************************************
* Prototypes
******************************************************************************/
typedef struct _rpmsg_struct
{
struct rpmsg_lite_endpoint *volatile my_ept;
volatile rpmsg_queue_handle my_queue;
struct rpmsg_lite_instance *volatile my_rpmsg;
QueueHandle_t my_queue_to_uart;
QueueHandle_t my_queue_to_rpmsg;
} rpmsg_struct;
static TaskHandle_t rpmsg_rx_task_handle = NULL;
static TaskHandle_t rpmsg_tx_task_handle = NULL;
static SemaphoreHandle_t mutex;
static rpmsg_struct my_rpmsg_struct;
static volatile uint32_t remote_addr;
static char app_rx_buf[1500]; /* Each RPMSG buffer can carry less than 512 payload */
static char app_tx_buf[1500]; /* Each RPMSG buffer can carry less than 512 payload */
static void rpmsg_rx_task(void *param)
{
rpmsg_struct * param_struct = param;
Message st_msg;
volatile uint32_t local_remote_addr;
void *rx_buf;
uint32_t len;
int32_t result;
void *tx_buf;
uint32_t size;
boolean init = false;
/* Block for 5µs. */
const TickType_t xDelay = 5 / (portTICK_PERIOD_MS * 1000);
for (;;)
{
/* Get RPMsg rx buffer with message */
result =
rpmsg_queue_recv_nocopy(param_struct->my_rpmsg, param_struct->my_queue, (uint32_t *)&remote_addr, (char **)&rx_buf, &len, RL_BLOCK);
if (result != 0)
{
assert(false);
} else {
PRINTF("RPMsg received !\r\n");
}
/* Copy string from RPMsg rx buffer */
assert(len < sizeof(app_rx_buf));
memcpy(st_msg.body, rx_buf, len);
st_msg.size = len;
xQueueSend(param_struct->my_queue_to_uart, (void *)&st_msg, 10);
//app_rx_buf[len] = 0; /* End string by '\0' */
if ((len == 2) && (app_rx_buf[0] == 0xd) && (app_rx_buf[1] == 0xa))
PRINTF("Get New Line From Master Side\r\n");
else
PRINTF("Get Message From Master Side : \"%s\" [len : %d]\r\n", app_rx_buf, len);
if(!init) {
local_remote_addr = remote_addr;
// Release the mutex so that the creating function can finish
xSemaphoreGive(mutex);
init = true;
}
/* Release held RPMsg rx buffer */
result = rpmsg_queue_nocopy_free(param_struct->my_rpmsg, rx_buf);
if (result != 0)
{
assert(false);
}
}
vTaskSuspend(NULL);
}
static void rpmsg_tx_task(void *param)
{
rpmsg_struct * param_struct = param;
Message rcv_msg;
volatile uint32_t local_remote_addr;
void *rx_buf;
uint32_t len;
int32_t result;
void *tx_buf;
uint32_t size;
/* Block for 5µs. */
const TickType_t xDelay = 5 / (portTICK_PERIOD_MS * 1000);
/* On attend de recevoir l'adresse de destination */
PRINTF("\r\nM4 waiting for destination address ...\r\n");
// Take the mutex
xSemaphoreTake(mutex, portMAX_DELAY);
local_remote_addr = remote_addr;
for (;;)
{
if (xQueueReceive(param_struct->my_queue_to_rpmsg, (void *)&rcv_msg, portMAX_DELAY) != pdTRUE)
{
PRINTF("Failed to receive queue.\r\n");
}
else
{
/* Get tx buffer from RPMsg */
tx_buf = rpmsg_lite_alloc_tx_buffer(param_struct->my_rpmsg, &size, RL_BLOCK);
assert(tx_buf);
/* Copy string to RPMsg tx buffer */
memcpy(tx_buf, rcv_msg.body, rcv_msg.size);
/* Echo back received message with nocopy send */
result = rpmsg_lite_send_nocopy(param_struct->my_rpmsg, param_struct->my_ept, local_remote_addr, tx_buf, rcv_msg.size);
if (result != 0)
{
assert(false);
} else {
PRINTF("RPMsg sent !\r\n");
}
}
}
vTaskSuspend(NULL);
}
uint8_t rpmsg_init(QueueHandle_t r_to_u, QueueHandle_t u_to_r)
{
uint8_t status;
#ifdef MCMGR_USED
uint32_t startupData;
/* Get the startup data */
(void)MCMGR_GetStartupData(kMCMGR_Core1, &startupData);
my_rpmsg = rpmsg_lite_remote_init((void *)startupData, RPMSG_LITE_LINK_ID, RL_NO_FLAGS);
/* Signal the other core we are ready */
(void)MCMGR_SignalReady(kMCMGR_Core1);
#else
my_rpmsg_struct.my_rpmsg = rpmsg_lite_remote_init((void *)RPMSG_LITE_SHMEM_BASE, RPMSG_LITE_LINK_ID, RL_NO_FLAGS);
#endif /* MCMGR_USED */
PRINTF("En attente du chargement du module RPMsg...\r\n");
while (0 == rpmsg_lite_is_link_up(my_rpmsg_struct.my_rpmsg))
;
my_rpmsg_struct.my_queue = rpmsg_queue_create(my_rpmsg_struct.my_rpmsg);
my_rpmsg_struct.my_ept = rpmsg_lite_create_ept(my_rpmsg_struct.my_rpmsg, LOCAL_EPT_ADDR, rpmsg_queue_rx_cb, my_rpmsg_struct.my_queue);
(void)rpmsg_ns_announce(my_rpmsg_struct.my_rpmsg, my_rpmsg_struct.my_ept, RPMSG_LITE_NS_ANNOUNCE_STRING, RL_NS_CREATE);
PRINTF("\r\nNameservice sent, ready for incoming messages...\r\n");
// Create mutex before starting tasks
mutex = xSemaphoreCreateMutex();
// Take the mutex
if(xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE)
{
status = 0;
} else {
status = -1;
}
return status;
}
void rpmsg_task(QueueHandle_t r_to_u, QueueHandle_t u_to_r)
{
my_rpmsg_struct.my_queue_to_uart = r_to_u;
my_rpmsg_struct.my_queue_to_rpmsg = u_to_r;
if (xTaskCreate(rpmsg_rx_task, "RPMSG RX", configMINIMAL_STACK_SIZE + 100, NULL, tskIDLE_PRIORITY + 1, &rpmsg_rx_task_handle) !=
pdPASS)
{
PRINTF("Task creation failed!.\r\n");
while (1)
;
}
if (xTaskCreate(rpmsg_tx_task, "RPMSG TX", configMINIMAL_STACK_SIZE + 100, NULL, tskIDLE_PRIORITY + 1, &rpmsg_tx_task_handle) !=
pdPASS)
{
PRINTF("Task creation failed!.\r\n");
while (1)
;
}
}

GCC won't compile program

Alright, so I'm going crazy over this one. I'm currently trying to go through Hacking: Art of Exploitation and I'm on the part about socket programming. So here is the code and the compiler output below it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "hacking.h"
#define PORT 7890 // the port users will be connecting to
int main(void) {
int sockfd, new_sockfd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in host_addr, client_addr; // my address information
socklen_t sin_size;
int recv_length=1, yes=1;
char buffer[1024];
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
fatal("in socket");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
fatal("setting socket option SO_REUSEADDR");
host_addr.sin_family = AF_INET; // host byte order
host_addr.sin_port = htons(PORT); // short, network byte order
host_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(host_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1)
fatal("binding to socket");
if (listen(sockfd, 5) == -1)
fatal("listening on socket");
while(1) { // Accept loop
sin_size = sizeof(struct sockaddr_in);
new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
if(new_sockfd == -1)
fatal("accepting connection");
printf("server: got connection from %s port %d\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
send(new_sockfd, "Hello World!\n", 13, 0);
recv_length = recv(new_sockfd, &buffer, 1024, 0);
while(recv_length > 0) {
printf("RECV: %d bytes\n", recv_length);
dump(buffer, recv_length);
recv_length = recv(new_sockfd, &buffer, 1024, 0);
}
close(new_sockfd);
}
return 0;
}
Compiler output:
root#root-laptop:~ $ gcc simple_server.c
In file included from /usr/include/sys/socket.h:35,
from simple_server.c:7:
/usr/include/bits/socket.h:122: error: syntax error before "define"
In file included from /usr/include/sys/socket.h:35,
from simple_server.c:7:
/usr/include/bits/socket.h:147: error: syntax error before "sa_family_t"
/usr/include/bits/socket.h:149: error: syntax error before '}' token
/usr/include/bits/socket.h:164: error: syntax error before "sa_family_t"
/usr/include/bits/socket.h:167: error: syntax error before '}' token
In file included from simple_server.c:9:
/usr/include/netinet/in.h:221: error: syntax error before "sa_family_t"
/usr/include/netinet/in.h:226: error: invalid application of `sizeof' to an incomplete type
/usr/include/netinet/in.h:229: error: size of array `sin_zero' is too large
/usr/include/netinet/in.h:230: error: syntax error before '}' token
/usr/include/netinet/in.h:235: error: syntax error before "sa_family_t"
/usr/include/netinet/in.h:240: error: syntax error before '}' token
/usr/include/netinet/in.h:283: error: field `gr_group' has incomplete type
/usr/include/netinet/in.h:292: error: field `gsr_group' has incomplete type
/usr/include/netinet/in.h:295: error: field `gsr_source' has incomplete type
/usr/include/netinet/in.h:327: error: field `gf_group' has incomplete type
/usr/include/netinet/in.h:335: error: field `gf_slist' has incomplete type
In file included from simple_server.c:9:
/usr/include/netinet/in.h:465: error: field `ip6m_addr' has incomplete type
simple_server.c: In function `main':
simple_server.c:17: error: storage size of `host_addr' isn't known
simple_server.c:17: error: storage size of `client_addr' isn't known
simple_server.c:34: error: invalid application of `sizeof' to an incomplete type
simple_server.c:41: error: invalid application of `sizeof' to an incomplete type
simple_server.c: At top level:
/usr/include/netinet/in.h:229: error: storage size of `sin_zero' isn't known
For some reason GCC isn't finding the predefined struct in the header files. I've tried everything to fix this, from removing header files to adding only certain ones.
Also I should note that I'm using the VM image that came with the book. It's an old version of Ubuntu and GCC.
Edit:
Here are the requested files.
socket.h
#ifndef __BITS_SOCKET_H
#define __BITS_SOCKET_H
#if !defined _SYS_SOCKET_H && !defined _NETINET_IN_H
# error "Never include <bits/socket.h> directly; use <sys/socket.h> instead."
#endif
#define __need_size_t
#define __need_NULL
#include <stddef.h>
#include <limits.h>
#include <sys/types.h>
/* Type for length arguments in socket calls. */
#ifndef __socklen_t_defined
typedef __socklen_t socklen_t;
# define __socklen_t_defined
#endif
/* Types of sockets. */
enum __socket_type
{
SOCK_STREAM = 1, /* Sequenced, reliable, connection-based
byte streams. */
#define SOCK_STREAM SOCK_STREAM
SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams
of fixed maximum length. */
#define SOCK_DGRAM SOCK_DGRAM
SOCK_RAW = 3, /* Raw protocol interface. */
#define SOCK_RAW SOCK_RAW
SOCK_RDM = 4, /* Reliably-delivered messages. */
#define SOCK_RDM SOCK_RDM
SOCK_SEQPACKET = 5, /* Sequenced, reliable, connection-based,
datagrams of fixed maximum length. */
#define SOCK_SEQPACKET SOCK_SEQPACKET
SOCK_PACKET = 10 /* Linux specific way of getting packets
at the dev level. For writing rarp and
other similar things on the user level. */
#define SOCK_PACKET SOCK_PACKET
};
/* Protocol families. */
#define PF_UNSPEC 0 /* Unspecified. */
#define PF_LOCAL 1 /* Local to host (pipes and file-domain). */
#define PF_UNIX PF_LOCAL /* Old BSD name for PF_LOCAL. */
#define PF_FILE PF_LOCAL /* Another non-standard name for PF_LOCAL. */
#define PF_INET 2 /* IP protocol family. */
#define PF_AX25 3 /* Amateur Radio AX.25. */
#define PF_IPX 4 /* Novell Internet Protocol. */
#define PF_APPLETALK 5 /* Appletalk DDP. */
#define PF_NETROM 6 /* Amateur radio NetROM. */
#define PF_BRIDGE 7 /* Multiprotocol bridge. */
#define PF_ATMPVC 8 /* ATM PVCs. */
#define PF_X25 9 /* Reserved for X.25 project. */
#define PF_INET6 10 /* IP version 6. */
#define PF_ROSE 11 /* Amateur Radio X.25 PLP. */
#define PF_DECnet 12 /* Reserved for DECnet project. */
#define PF_NETBEUI 13 /* Reserved for 802.2LLC project. */
#define PF_SECURITY 14 /* Security callback pseudo AF. */
#define PF_KEY 15 /* PF_KEY key management API. */
#define PF_NETLINK 16
#define PF_ROUTE PF_NETLINK /* Alias to emulate 4.4BSD. */
#define PF_PACKET 17 /* Packet family. */
#define PF_ASH 18 /* Ash. */
#define PF_ECONET 19 /* Acorn Econet. */
#define PF_ATMSVC 20 /* ATM SVCs. */
#define PF_SNA 22 /* Linux SNA Project */
#define PF_IRDA 23 /* IRDA sockets. */
#define PF_PPPOX 24 /* PPPoX sockets. */
#define PF_WANPIPE 25 /* Wanpipe API sockets. */
#define PF_BLUETOOTH 31 /* Bluetooth sockets. */
#define PF_MAX 32 /* For now.. */
/* Address families. */
#define AF_UNSPEC PF_UNSPEC
#define AF_LOCAL PF_LOCAL
#define AF_UNIX PF_UNIX
#define AF_FILE PF_FILE
#define AF_INET PF_INET
#define AF_AX25 PF_AX25
#define AF_IPX PF_IPX
#define AF_APPLETALK PF_APPLETALK
#define AF_NETROM PF_NETROM
#define AF_BRIDGE PF_BRIDGE
#define AF_ATMPVC PF_ATMPVC
#define AF_X25 PF_X25
#define AF_INET6 PF_INET6
#define AF_ROSE PF_ROSE
#define AF_DECnet PF_DECnet
#define AF_NETBEUI PF_NETBEUI
#define AF_SECURITY PF_SECURITY
#define AF_KEY PF_KEY
#define AF_NETLINK PF_NETLINK
#define AF_ROUTE PF_ROUTE
#define AF_PACKET PF_PACKET
#define AF_ASH PF_ASH
#define AF_ECONET PF_ECONET
#define AF_ATMSVC PF_ATMSVC
#define AF_SNA PF_SNA
#define AF_IRDA PF_IRDA
#define AF_PPPOX PF_PPPOX
o define AF_WANPIPE PF_WANPIPE
#define AF_BLUETOOTH PF_BLUETOOTH
#define AF_MAX PF_MAX
/* Socket level values. Others are defined in the appropriate headers.
XXX These definitions also should go into the appropriate headers as
far as they are available. */
#define SOL_RAW 255
#define SOL_DECNET 261
#define SOL_X25 262
#define SOL_PACKET 263
#define SOL_ATM 264 /* ATM layer (cell level). */
#define SOL_AAL 265 /* ATM Adaption Layer (packet level). */
#define SOL_IRDA 266
/* Maximum queue length specifiable by listen. */
#define SOMAXCONN 128
/* Get the definition of the macro to define the common sockaddr members. */
#include <bits/sockaddr.h>
/* Structure describing a generic socket address. */
struct sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
};
/* Structure large enough to hold any socket address (with the historical
exception of AF_UNIX). We reserve 128 bytes. */
#if ULONG_MAX > 0xffffffff
# define __ss_aligntype __uint64_t
#else
# define __ss_aligntype __uint32_t
#endif
#define _SS_SIZE 128
#define _SS_PADSIZE (_SS_SIZE - (2 * sizeof (__ss_aligntype)))
struct sockaddr_storage
{
__SOCKADDR_COMMON (ss_); /* Address family, etc. */
__ss_aligntype __ss_align; /* Force desired alignment. */
char __ss_padding[_SS_PADSIZE];
};
/* Bits in the FLAGS argument to `send', `recv', et al. */
enum
{
MSG_OOB = 0x01, /* Process out-of-band data. */
#define MSG_OOB MSG_OOB
MSG_PEEK = 0x02, /* Peek at incoming messages. */
#define MSG_PEEK MSG_PEEK
MSG_DONTROUTE = 0x04, /* Don't use local routing. */
#define MSG_DONTROUTE MSG_DONTROUTE
#ifdef __USE_GNU
/* DECnet uses a different name. */
MSG_TRYHARD = MSG_DONTROUTE,
# define MSG_TRYHARD MSG_DONTROUTE
#endif
MSG_CTRUNC = 0x08, /* Control data lost before delivery. */
#define MSG_CTRUNC MSG_CTRUNC
MSG_PROXY = 0x10, /* Supply or ask second address. */
#define MSG_PROXY MSG_PROXY
MSG_TRUNC = 0x20,
#define MSG_TRUNC MSG_TRUNC
MSG_DONTWAIT = 0x40, /* Nonblocking IO. */
#define MSG_DONTWAIT MSG_DONTWAIT
MSG_EOR = 0x80, /* End of record. */
#define MSG_EOR MSG_EOR
MSG_WAITALL = 0x100, /* Wait for a full request. */
#define MSG_WAITALL MSG_WAITALL
MSG_FIN = 0x200,
#define MSG_FIN MSG_FIN
MSG_SYN = 0x400,
#define MSG_SYN MSG_SYN
MSG_CONFIRM = 0x800, /* Confirm path validity. */
#define MSG_CONFIRM MSG_CONFIRM
MSG_RST = 0x1000,
#define MSG_RST MSG_RST
MSG_ERRQUEUE = 0x2000, /* Fetch message from error queue. */
#define MSG_ERRQUEUE MSG_ERRQUEUE
MSG_NOSIGNAL = 0x4000, /* Do not generate SIGPIPE. */
#define MSG_NOSIGNAL MSG_NOSIGNAL
MSG_MORE = 0x8000 /* Sender will send more. */
#define MSG_MORE MSG_MORE
};
/* Structure describing messages sent by
`sendmsg' and received by `recvmsg'. */
struct msghdr
{
void *msg_name; /* Address to send to/receive from. */
socklen_t msg_namelen; /* Length of address data. */
struct iovec *msg_iov; /* Vector of data to send/receive into. */
size_t msg_iovlen; /* Number of elements in the vector. */
void *msg_control; /* Ancillary data (eg BSD filedesc passing). */
size_t msg_controllen; /* Ancillary data buffer length.
!! The type should be socklen_t but the
definition of the kernel is incompatible
with this. */
int msg_flags; /* Flags on received message. */
};
/* Structure used for storage of ancillary data object information. */
struct cmsghdr
{
size_t cmsg_len; /* Length of data in cmsg_data plus length
of cmsghdr structure.
!! The type should be socklen_t but the
definition of the kernel is incompatible
with this. */
int cmsg_level; /* Originating protocol. */
int cmsg_type; /* Protocol specific type. */
#if (!defined __STRICT_ANSI__ && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L
__extension__ unsigned char __cmsg_data __flexarr; /* Ancillary data. */
#endif
};
/* Ancillary data object manipulation macros. */
#if (!defined __STRICT_ANSI__ && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L
# define CMSG_DATA(cmsg) ((cmsg)->__cmsg_data)
#else
# define CMSG_DATA(cmsg) ((unsigned char *) ((struct cmsghdr *) (cmsg) + 1))
#endif
#define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr (mhdr, cmsg)
#define CMSG_FIRSTHDR(mhdr) \
((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) \
? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) NULL)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) \
& (size_t) ~(sizeof (size_t) - 1))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) \
+ CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
struct cmsghdr *__cmsg) __THROW;
#ifdef __USE_EXTERN_INLINES
# ifndef _EXTERN_INLINE
# define _EXTERN_INLINE extern __inline
# endif
_EXTERN_INLINE struct cmsghdr *
__NTH (__cmsg_nxthdr (struct msghdr *__mhdr, struct cmsghdr *__cmsg))
{
if ((size_t) __cmsg->cmsg_len < sizeof (struct cmsghdr))
/* The kernel header does this so there may be a reason. */
return 0;
__cmsg = (struct cmsghdr *) ((unsigned char *) __cmsg
+ CMSG_ALIGN (__cmsg->cmsg_len));
if ((unsigned char *) (__cmsg + 1) > ((unsigned char *) __mhdr->msg_control
+ __mhdr->msg_controllen)
|| ((unsigned char *) __cmsg + CMSG_ALIGN (__cmsg->cmsg_len)
> ((unsigned char *) __mhdr->msg_control + __mhdr->msg_controllen)))
/* No more entries. */
return 0;
return __cmsg;
}
#endif /* Use `extern inline'. */
/* Socket level message types. This must match the definitions in
<linux/socket.h>. */
enum
{
SCM_RIGHTS = 0x01 /* Transfer file descriptors. */
#define SCM_RIGHTS SCM_RIGHTS
#ifdef __USE_BSD
, SCM_CREDENTIALS = 0x02 /* Credentials passing. */
# define SCM_CREDENTIALS SCM_CREDENTIALS
#endif
};
/* User visible structure for SCM_CREDENTIALS message */
struct ucred
{
pid_t pid; /* PID of sending process. */
uid_t uid; /* UID of sending process. */
gid_t gid; /* GID of sending process. */
};
/* Get socket manipulation related informations from kernel headers. */
#include <asm/socket.h>
/* Structure used to manipulate the SO_LINGER option. */
struct linger
{
int l_onoff; /* Nonzero to linger on close. */
int l_linger; /* Time to linger. */
};
#endif /* bits/socket.h */
hacking.h
#include <stdlib.h>
//This function is used to display packet data by the server program.
void dump(const unsigned char *data_buffer, const unsigned int length){
unsigned char byte;
unsigned int i, j;
for(i = 0; i < length; i++){
byte = data_buffer[i];
printf("%02x ", data_buffer[i]); //Display byte in hex
if((( i % 16) == 15 ) || ( i == length - 1)) {
for( j = 0; j < 15 - ( i % 16 ); j++ )
printf(" ");
printf("| ");
for(j = ( i - ( i % 16 )); j <= i; j++) { //Display printable bytes from line.
byte = data_buffer[j];
if((byte > 31) && (byte < 127))//Outside printable char range
printf("%c", byte);
else
printf(".");
}
printf("\n"); // End of the dump line (each line is 16 bytes)
}//End if
}//End for
}
void fatal(char *message){
char error_message[100];
strcpy(error_message, "[!!] Fatal Error");
strncat(error_message, message, 83);
perror(error_message);
exit(-1);
}
void *ec_malloc(unsigned int size){
void *ptr;
ptr = malloc(size);
if(ptr== NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
in.h
https://pastebin.com/RTjSdA2e
Ok. So the problem was in socket.h. Basically this line
o define AF_WANPIPE PF_WANPIPE
needed to be changed to this
#define AF_WANPIPE PF_WANPIPE
Thanks David Wohlferd.
the contents of the hacking.h header file are not correct.
The contents should be the 'prototypes' for those functions and the actual functions should be in a separate .c file, probably named hacking.c
I.E. the contents of the hacking.h file should be:
#ifndef HACKING_H
#define HACKING_H
//This function is used to display packet data by the server program.
void dump(const unsigned char *data_buffer, const unsigned int length);
void fatal(char *message);
void *ec_malloc(unsigned int size);
#endif // HACKING_H
Notice the 'include only once' wrapper; consisting of the #ifndef, #define, and the trailing #endif statements
notice the file only contains the prototypes (it could also contain any other #define statements and/or data definitions and/or macro definitions, although in this case there are none.)
Then there should be a separate file hacking.c that contains the actual functions (and a statement: #include "hacking.h")
in general, neither function bodies nor data declarations should be in a header file. (later on, you will learn of certain exceptions to this statement, like when declaring inline functions

FreeBSD: network interface information

I am trying to programmingly find network interfaces info in FreeBSD. In linux, the interfaces are listed at /etc/network/interfaces file.
Is there any such file in FreeBSD? How can I extract that info?
you can always use getifaddrs(3) here is an exmaple:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
int main(void) {
struct ifaddrs *ifap,*ifa;
getifaddrs(&ifap);
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
printf("%s\n",ifa->ifa_name);
}
freeifaddrs(ifap);
}
EDIT: on linux if you need to fetch the link layer address of the interface you need to look for AF_PACKET sa_family, which is found in netpacket/packet.h
on linux, *bsd its called AF_LINK and its in net/if_dl.h
#ifdef AF_LINK
# include <net/if_dl.h>
#endif
#ifdef AF_PACKET
# include <netpacket/packet.h>
#endif
#ifdef AF_LINK
#define SDL ((struct sockaddr_dl *)ifa->ifa_addr)
if (SDL->sdl_family == AF_LINK) {
bcopy(SDL->sdl_data + SDL->sdl_nlen,....,SDL->sdl_alen
}
#undef SDL
#endif
#ifdef AF_PACKET
if (ifa->ifa_addr->sa_family == AF_PACKET) {
struct sockaddr_ll *sl =
(struct sockaddr_ll*) ifa->ifa_addr;
bcopy(sl->sll_addr,....,sl->sll_halen
}
#endif

howto check a network devices status in C?

I would like to check a network devices status e.g. promiscous mode. Basically like shown with ip a command.
Maybe someone could push me in the right direction?
I want to do this in C for linux so linux specific headers are available.
You need to use the SIOCGIFFLAGS ioctl to retrieve the flags associated with an interface. You can then check if the IFF_PROMISC flag is set:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h> /* ioctl() */
#include <sys/socket.h> /* socket() */
#include <arpa/inet.h>
#include <unistd.h> /* close() */
#include <linux/if.h> /* struct ifreq */
int main(int argc, char* argv[])
{
/* this socket doesn't really matter, we just need a descriptor
* to perform the ioctl on */
int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
struct ifreq ethreq;
memset(&ethreq, 0, sizeof(ethreq));
/* set the name of the interface we wish to check */
strncpy(ethreq.ifr_name, "eth0", IFNAMSIZ);
/* grab flags associated with this interface */
ioctl(fd, SIOCGIFFLAGS, &ethreq);
if (ethreq.ifr_flags & IFF_PROMISC) {
printf("%s is in promiscuous mode\n",
ethreq.ifr_name);
} else {
printf("%s is NOT in promiscuous mode\n",
ethreq.ifr_name);
}
close(fd);
return 0;
}
If you want to set the interface to promiscuous mode, you will need root privileges, but you can simply set the field in ifr_flags and use the SIOCSIFFLAGS ioctl:
/* ... */
ethreq.ifr_flags |= IFF_PROMISC;
ioctl(fd, SIOCSIFFLAGS, &ethreq);

Linux receive signal break for ttycontrol program

I would like to receive SIGINT if my process controlling /dev/ttyS2 receives BREAK on a serial port. I run this program from a shell. From what I discovered only "the terminal is the controlling terminal of a foreground process group, it will cause a SIGINT to be sent to this foreground process group" I tried make process making controller of terminal but it fails.
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS2"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
__sighandler_t sighandle(int signum, __sighandler_t h) {
fprintf(stderr, "BREAK DETECTED\n");
signal(SIGINT, (__sighandler_t) sighandle);
return SIG_IGN;
}
volatile int STOP=FALSE;
int main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];
pid_t pid;
signal(SIGINT, (__sighandler_t) sighandle);
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); return (-1); }
tcgetattr(fd,&oldtio); /* save current port settings */
memset(&newtio, 0,sizeof(newtio));
newtio.c_cflag |= BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD | BRKINT;
newtio.c_iflag &= ~IGNBRK ;
newtio.c_oflag = 0;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 5 chars received */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
if( ioctl(fd, TIOCSCTTY, 1) <0 )
{
printf("Error number: %d\n", errno);
}
if ( tcsetpgrp(fd, tcgetpgrp(0) ) < 0 )
{
syslog(LOG_PERROR,"tcsetpgrp failed: %d " ,errno);
syslog(LOG_PERROR,"EBADF is %d " ,EBADF);
syslog(LOG_PERROR,"EINVAL is %d " ,EINVAL);
syslog(LOG_PERROR,"ENOTTY is %d " ,ENOTTY);
syslog(LOG_PERROR,"EPERM is %d " ,EPERM);
}
while (STOP==FALSE) { /* loop for input */
res = read(fd,buf,255); /* returns after 5 chars have been input */
buf[res]=0; /* so we can printf... */
printf(":%s:%d\n", buf, res);
if (buf[0]=='z') {STOP=TRUE;}
}
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
I finally found answer to my problem. I am using custom board so it don't have to work always but when serial port is configured correctly user can get number of breaks by serial port by cat /proc/tty/device/serial, that kind of functionality I was hoping to get
#define _POSIX_SOURCE 1 has no effect unless it precedes the #includes.
With a C99 compiler, you may #include <stdbool.h> instead of #define your own TRUE and FALSE.
/* from signal.h, */
typedef void(*sighandler_t)(int);
/* which means the handler must be declared thusly */
void sighandle(int);
/* as a result, no casting is necessary */
sighandler_t p_sighandle = &sighandle;
The foreground process group is set by tcsetpgrp, which you appear to be missing.

Resources