Advertisement
orzechtbg

SO2.lab1.0zad1.nl

Jun 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<netlink/netlink.h>
  4.  
  5. #define NETLINK_TEST 17
  6. #define MSG_STR 0x11
  7.  
  8. int funkcja(struct nl_sock** ns, char msg[14])
  9. {
  10.     int result = nl_connect(*ns,NETLINK_TEST);
  11.    
  12.     if(result<0) {
  13.         fprintf(stderr,"Blad wczytywania gniazda netlink: %s\t%d\t%d.\n",__FILE__,__LINE__-2,result);
  14.         nl_socket_free(*ns);
  15.         return -2;
  16.     }
  17.    
  18.     return result;
  19. }
  20.  
  21. void funkcja1(struct nl_sock** ns, char msg[14])
  22. {
  23.     printf("Moj port: %u\n",nl_socket_get_local_port(*ns));
  24.         printf("Port odbiorcy: %u\n",nl_socket_get_peer_port(*ns));
  25.         printf("Dlugosc wiadomosci: %u\n",sizeof(msg));
  26.  
  27.        
  28.     nl_socket_disable_seq_check(*ns);
  29.         nl_socket_disable_auto_ack(*ns);
  30.         nl_socket_disable_msg_peek(*ns);
  31. }
  32.  
  33.  
  34. int main(void)
  35. {
  36.    
  37.     struct nl_sock *ns;
  38.     char msg[] = "Hello netlink";
  39.     ns = nl_socket_alloc();
  40.        
  41.     if(!ns) {
  42.         fprintf(stderr,"Blad tworzenia gniazda netlink: %s\t%d.\n",__FILE__,__LINE__-2);
  43.         return -1;
  44.     }
  45.  
  46.     funkcja1(&ns, msg);
  47.  
  48.     int result = funkcja(&ns, msg);
  49.     if (result < 0)
  50.         return -2;
  51.  
  52.     result = nl_send_simple(ns,MSG_STR,NLM_F_REQUEST,(void *)msg,sizeof(msg));
  53.     if(result<0)
  54.         fprintf(stderr,"Blad wysylania wiadomosci: %s\t%d\t%d\n",__FILE__,__LINE__,result);
  55.     else
  56.         printf("Wyslano %d bajtow wiadomosci.\n",result);
  57.  
  58.  
  59.     nl_socket_free(ns);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement