nic.h

Go to the documentation of this file.
00001 /** @file nic.h
00002  *
00003  *  Network Interface Configurator (NIC) for libdhcp .
00004  * 
00005  *  Provides facilities for configuring network interfaces, addresses, and routes,
00006  *  with an interface to the libnl netlink library.
00007  *
00008  * @author       Jason Vas Dias <jvdias@redhat.com>
00009  */
00010 /*  Copyright(C) Jason Vas Dias <jvdias@redhat.com> Red Hat Inc. May 2006
00011  *
00012  *  This program is free software; you can redistribute it and/or modify
00013  *  it under the terms of the GNU General Public License as published by
00014  *  the Free Software Foundation at 
00015  *           http://www.fsf.org/licensing/licenses/gpl.txt
00016  *  and included in this software distribution as the "LICENSE" file.
00017  *
00018  *  This program is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *  GNU General Public License for more details.
00022  */
00023 #ifndef LIBDHCP_NIC_H
00024 #define LIBDHCP_NIC_H
00025 
00026 #include <sys/types.h>
00027 #include <sys/queue.h>
00028 #include <netinet/in.h>
00029 #include <net/if.h>
00030 #include <stdint.h>
00031 #include <stdarg.h>
00032 #include <ip_addr.h>
00033 
00034 
00035 /**
00036  * @addtogroup  NICI
00037  * @{
00038  */
00039 /**
00040  *  Network Interface Configuration (NIC) Link opaque type:
00041  */
00042 struct nic_s;
00043 typedef struct nic_s  *NIC_t;
00044 /**@}*/
00045 
00046 /**
00047  * @addtogroup  NICA
00048  * @{
00049  */
00050 /**
00051  *  Interface IP Address opaque type 
00052  */
00053 struct  nic_ip_address_s;
00054 typedef struct nic_ip_address_s  *IPaddr_t;
00055 
00056 typedef
00057 struct nic_ip_address_list_node_s
00058 {
00059     IPaddr_t addr;
00060     STAILQ_ENTRY( nic_ip_address_list_node_s ) link;
00061 } IPaddr_list_node_t;
00062 
00063 typedef 
00064 STAILQ_HEAD(nic_ip_address_list_s,nic_ip_address_list_node_s)
00065 IPaddr_list_t;
00066 /**@}*/
00067 
00068 /** 
00069  * @addtogroup  NICR
00070  * @{
00071  */
00072 /**
00073  * Interface Route opaque type
00074  */
00075 struct nic_route_s;
00076 typedef struct nic_route_s *IProute_t;
00077 
00078 typedef
00079 struct nic_ip_route_node_s
00080 {
00081     IProute_t route;
00082     STAILQ_ENTRY( nic_ip_route_node_s ) link;
00083 } IProute_list_node_t;
00084 
00085 typedef
00086 STAILQ_HEAD(nic_ip_route_list_s,nic_ip_route_node_s)
00087 IProute_list_t;
00088 /**@}*/
00089 
00090 /**
00091  * @addtogroup NICH
00092  * @{
00093  */
00094 
00095 /**
00096  * Opaque NIC Library Handle - users must first obtain one to create 
00097  * any NIC object. Also records error handler and log level:
00098  */
00099 typedef struct nlh_s *NLH_t; 
00100 
00101 /**
00102  * Error level - functions will call error handler
00103  * with priority set to one of these values.
00104  */
00105 typedef 
00106 enum nic_error_level_e
00107 { /* syslog priority */
00108     NIC_FATAL,    /**< cannot continue - unused!   */
00109     NIC_ERR=3,    /**< operation failed            */
00110     NIC_WARN=4,   /**< not all OK with operation   */
00111     NIC_DEBUG=7   /**< debugging                   */
00112 } NIC_Error_Level_t;
00113 
00114 /**
00115  * Result code type
00116  */
00117 typedef
00118 enum nic_result_e
00119 {   /* < -1 : -1 *netlink error code */
00120     NIC_FAIL = -1, /**< operation failed */
00121     NIC_OK   =  0, /**< success */
00122     NIC_SUCCESS=0  /**< success == NIC_OK */
00123     /* > 0: OK status  */
00124 } NIC_Res_t;
00125 
00126 /**
00127  * Error handler / logger function type:
00128  */
00129 typedef void (*NIC_Error_Handler_t)( NLH_t, NIC_Error_Level_t, char*, ...); 
00130 
00131 /**
00132  * First call this to obtain a NIC module "handle" 
00133  */
00134 extern
00135 NLH_t nic_open( NIC_Error_Handler_t );
00136 
00137 /**
00138  * Call this when finished with NIC to free resources
00139  */
00140 extern
00141 void nic_close(NLH_t*); 
00142 
00143 
00144 /* default error handlers: */
00145 
00146 extern
00147 void nic_set_logger(NLH_t, NIC_Error_Handler_t /* can be NULL: no logging - default */ );
00148 
00149 typedef int (*NIC_VA_Error_Handler_t)(void*, int priority, char *fmt, va_list va);
00150 
00151 extern
00152 void nic_set_va_logger(NLH_t, NIC_VA_Error_Handler_t handler, void *handler_arg );
00153 
00154 extern
00155 void nic_sys_logger( NLH_t, NIC_Error_Level_t, char *fmt, ... );
00156 
00157 extern
00158 void nic_stderr_logger( NLH_t, NIC_Error_Level_t, char *fmt, ... );
00159 
00160 extern
00161 NIC_Res_t nic_set_loglevel( NLH_t, NIC_Error_Level_t );
00162 /**@}*/
00163 
00164 
00165 /**
00166  * @addtogroup NICI
00167  * @{
00168  */
00169 /**
00170  *  Interface NIC (Link / IF) functions:
00171  */
00172 extern
00173 NIC_t nic_by_name(NLH_t, char * );   /* constructor */
00174 
00175 extern
00176 NIC_t nic_by_index(NLH_t, int16_t );/* constructor */
00177 
00178 typedef void (*NIC_handler_t)(NLH_t nh, NIC_t, void*);
00179 
00180 extern  void nic_foreach(NLH_t nh, NIC_handler_t handler,  void *arg); /**<
00181  * Calls handler with nic of each interface currently configured in
00182  * the kernel and user void* arg arguments. 
00183  * @warning: the callback MUST NOT free(nic) ! 
00184  * Also, only ONE thread can do a nic foreach at a time - this is enforced.
00185  */
00186 
00187 extern
00188 char  *nic_get_name( NIC_t );
00189 
00190 extern
00191 int16_t nic_get_index( NIC_t );
00192 
00193 extern
00194 ip_addr_t nic_get_link_addr(NIC_t);
00195 /* get link layer / ethernet address
00196  */
00197 
00198 extern
00199 ip_addr_t nic_get_link_broadcast(NIC_t);
00200 /* get link layer broadcast address
00201  */
00202 
00203 extern
00204 NIC_Res_t nic_update( NIC_t nic ); /**<
00205  * call nic_update(nic) after you've made
00206  * all your nic_set_*(nic,...) calls to
00207  * actually update the link with netlink.
00208  */ 
00209 
00210 extern
00211 uint32_t nic_get_flags( NIC_t );
00212 
00213 extern
00214 void nic_set_flags( NIC_t, uint32_t  );
00215 
00216 extern
00217 uint32_t nic_get_mtu( NIC_t );
00218 
00219 extern
00220 void nic_set_mtu( NIC_t, uint32_t  );
00221 
00222 extern
00223 char *nic_get_qdisc( NIC_t );
00224 
00225 extern
00226 void nic_set_qdisc( NIC_t, char * );
00227 
00228 extern
00229 uint32_t nic_get_txqlen( NIC_t );
00230 
00231 extern
00232 void nic_set_txqlen( NIC_t, uint32_t  );
00233 
00234 extern
00235 uint32_t nic_get_link( NIC_t );
00236 
00237 extern
00238 void nic_set_link( NIC_t, uint32_t  );
00239 
00240 extern
00241 uint32_t nic_get_weight( NIC_t );
00242 
00243 extern
00244 void nic_set_weight( NIC_t, uint32_t  );
00245 
00246 extern
00247 uint32_t nic_get_master( NIC_t );
00248 
00249 extern
00250 void nic_set_master( NIC_t, uint32_t  );
00251 
00252 extern
00253 uint32_t nic_get_cost( NIC_t );
00254 
00255 extern
00256 void nic_set_cost( NIC_t, uint32_t  );
00257 
00258 extern
00259 uint32_t nic_get_priority( NIC_t );
00260 
00261 extern
00262 void nic_set_priority( NIC_t, uint32_t  );
00263 
00264 extern
00265 uint32_t nic_get_protinfo( NIC_t );
00266 
00267 extern
00268 void nic_set_protinfo( NIC_t, uint32_t  );
00269 
00270 struct rtnl_link_stats;/**< include linux/rtnetlink for this*/
00271 extern
00272 struct rtnl_link_stats nic_get_stats(NIC_t);
00273 
00274 struct rtnl_link_ifmap;/**< include linux/rtnetlink for this*/
00275 extern
00276 struct rtnl_link_ifmap nic_get_ifmap(NIC_t);
00277 
00278 /**@}*/
00279 
00280 /*
00281  *  NIC IP Address functions:
00282  */
00283 /**
00284  * @addtogroup NICA
00285  * @{
00286  */
00287 
00288 typedef void (*IPaddr_Handler_t) (NLH_t nh, IPaddr_t, void *);
00289 
00290 extern
00291 void nic_addr_foreach( NLH_t, IPaddr_Handler_t, void * );
00292 /* calls handler for every address currently configured */
00293 
00294 extern
00295 IPaddr_t nic_addr_ip( NLH_t nh, ip_addr_t *);
00296 
00297 extern
00298 ip_addr_t nic_ip_addr( IPaddr_t );
00299 
00300 extern
00301 IPaddr_t nic_addr( NLH_t nh, ip_addr_t);
00302 
00303 extern
00304 IPaddr_t nic_addr_local( NLH_t nh, ip_addr_t);
00305 
00306 extern
00307 ip_addr_t nic_addr_get_local( IPaddr_t );
00308 
00309 extern
00310 void nic_addr_set_local( IPaddr_t, ip_addr_t);
00311 
00312 extern
00313 uint8_t nic_addr_get_family( IPaddr_t );
00314 
00315 extern
00316 uint8_t nic_addr_get_prefix( IPaddr_t );
00317 
00318 extern
00319 void nic_addr_set_prefix( IPaddr_t, uint8_t );
00320 
00321 extern
00322 ip_addr_t nic_addr_get_broadcast( IPaddr_t );
00323 
00324 extern
00325 void nic_addr_set_broadcast( IPaddr_t, ip_addr_t );
00326 
00327 extern
00328 ip_addr_t nic_addr_get_anycast( IPaddr_t );
00329 
00330 extern
00331 void nic_addr_set_anycast( IPaddr_t, ip_addr_t);
00332 
00333 extern
00334 ip_addr_t nic_addr_get_multicast( IPaddr_t );
00335 
00336 extern
00337 void nic_addr_set_multicast( IPaddr_t, ip_addr_t);
00338 
00339 extern
00340 int8_t nic_addr_get_scope(IPaddr_t);
00341 
00342 extern
00343 void nic_addr_set_scope(IPaddr_t, int8_t);
00344 
00345 extern
00346 uint8_t nic_addr_get_flags(IPaddr_t);
00347 
00348 extern
00349 void nic_addr_set_flags(IPaddr_t, uint8_t);
00350 
00351 extern
00352 const char *nic_addr_get_label(IPaddr_t);
00353 
00354 extern
00355 void nic_addr_set_label(IPaddr_t, const char *);
00356 
00357 struct ifa_cacheinfo;
00358 
00359 extern
00360 struct ifa_cacheinfo nic_addr_get_cacheinfo( IPaddr_t );
00361 
00362 extern
00363 void  nic_addr_set_cacheinfo( IPaddr_t, struct ifa_cacheinfo* );
00364 
00365 extern
00366 IPaddr_list_t *nic_address_list_new( IPaddr_t, ... );
00367 /* All args in list must be IPaddr_t; last arg MUST be 0
00368  */
00369 extern
00370 void nic_address_list_free( IPaddr_list_t *);
00371 
00372 extern
00373 NIC_Res_t nic_add_address( NIC_t, IPaddr_t );
00374 
00375 extern
00376 NIC_Res_t nic_remove_address( NIC_t, IPaddr_t );
00377 
00378 extern
00379 NIC_Res_t nic_add_addresses( NIC_t, IPaddr_list_t* );
00380 
00381 extern
00382 NIC_Res_t nic_remove_addresses( NIC_t, IPaddr_list_t* );
00383 
00384 void nic_addr_free(void *);
00385 /**@}*/
00386 
00387 /*
00388  *  NIC IP Route functions: 
00389  */
00390 /**
00391  * @addtogroup NICR
00392  * @{
00393  */
00394 typedef void (*IProute_handler_t) (NLH_t nh, IProute_t, void *);
00395 
00396 extern
00397 void nic_route_foreach( NLH_t, IProute_handler_t, void * );
00398 /* calls handler for every route currently configured */
00399 
00400 extern
00401 int32_t nic_route_get_table(IProute_t);
00402 
00403 extern
00404 void nic_route_set_table(IProute_t, uint8_t table);
00405 
00406 extern
00407 char *nic_route_get_table_name( int32_t, char *buf, int len );
00408 
00409 extern
00410 int32_t nic_route_get_table_number( char* );
00411 
00412 extern
00413 uint8_t nic_route_get_family( IProute_t );
00414 
00415 extern
00416 uint8_t nic_route_get_scope(IProute_t);
00417 
00418 extern
00419 void nic_route_set_scope(IProute_t, uint8_t);
00420 
00421 extern
00422 uint32_t nic_route_get_flags( IProute_t );
00423 
00424 extern
00425 void nic_route_set_flags( IProute_t, uint32_t );
00426 
00427 extern
00428 uint8_t nic_route_get_dst_len( IProute_t );
00429 
00430 extern
00431 void nic_route_set_dst_len( IProute_t, uint8_t );
00432 
00433 extern
00434 uint8_t nic_route_get_src_len( IProute_t );
00435 
00436 extern
00437 void nic_route_set_src_len( IProute_t, uint8_t );
00438 
00439 extern
00440 uint8_t nic_route_get_type( IProute_t );
00441 
00442 extern
00443 void nic_route_set_type( IProute_t, uint8_t );
00444 
00445 extern
00446 uint8_t nic_route_get_protocol( IProute_t );
00447 
00448 extern
00449 void nic_route_set_protocol( IProute_t, uint8_t );
00450 
00451 extern
00452 uint8_t nic_route_get_tos( IProute_t);
00453 
00454 extern
00455 void nic_route_set_tos( IProute_t, uint8_t);
00456 
00457 extern 
00458 ip_addr_t nic_route_get_dst( IProute_t );
00459 
00460 extern
00461 void nic_route_set_dst(IProute_t, ip_addr_t);
00462 
00463 extern 
00464 ip_addr_t nic_route_get_src( IProute_t );
00465 
00466 extern
00467 void nic_route_set_src(IProute_t, ip_addr_t);
00468 
00469 extern 
00470 ip_addr_t nic_route_get_gateway( IProute_t );
00471 
00472 extern
00473 void nic_route_set_gateway(IProute_t, ip_addr_t);
00474 
00475 extern 
00476 ip_addr_t nic_route_get_prefsrc( IProute_t );
00477 
00478 extern
00479 void nic_route_set_prefsrc(IProute_t, ip_addr_t);
00480 
00481 extern
00482 int16_t nic_route_get_oif( IProute_t );
00483 
00484 extern
00485 void nic_route_set_oif( IProute_t, uint16_t);
00486 
00487 typedef 
00488 struct nic_if_name_s
00489 { char name [ IFNAMSIZ ];
00490 } NIC_if_name_t;
00491 
00492 extern
00493 NIC_if_name_t nic_route_get_iif( IProute_t );
00494 
00495 extern
00496 void nic_route_set_iif( IProute_t, char * );
00497 
00498 extern
00499 uint32_t nic_route_get_priority( IProute_t );
00500 
00501 extern
00502 void nic_route_set_priority( IProute_t, uint32_t );
00503 
00504 extern
00505 uint32_t nic_route_get_protoinfo(IProute_t);
00506 
00507 extern
00508 void nic_route_set_protoinfo(IProute_t, uint32_t);
00509 
00510 extern
00511 uint32_t nic_route_get_session(IProute_t);
00512 
00513 extern
00514 void nic_route_set_session(IProute_t, uint32_t);
00515 
00516 extern
00517 uint32_t nic_route_get_flow(IProute_t);
00518 
00519 extern
00520 void nic_route_set_flow(IProute_t, uint32_t);
00521 
00522 extern
00523 uint32_t nic_route_get_mp_algo(IProute_t);
00524 
00525 extern
00526 void nic_route_mp_algo(IProute_t, uint32_t);
00527 
00528 extern
00529 IProute_t nic_route_new
00530 (   NLH_t,
00531     int16_t oif,          /* output interface: -1 means none */
00532     ip_addr_t *dst,       /* 0L here means DEFAULT ROUTE */ 
00533 #define      NIC_ROUTE_DEFAULT_ADDR ((ip_addr_t*)0L)
00534     uint8_t dst_len,      /* dst_len ignored for default route */
00535     ip_addr_t *gw,        /* 0L here means no gateway */
00536     int8_t type,          /* -1: default: RTN_UNICAST */
00537     int8_t protocol,      /* -1: default: RTPROT_BOOT */
00538     int8_t scope,         /* -1=UNIVERSE,LINK=253,SITE=200,HOST=254 */
00539     int32_t priority,     /* metric - -1 means no priority */
00540     int32_t table,        /* table: -1 selects default: "local" */
00541     char* iif,            /* input interface: -1 means none */
00542     ip_addr_t *src,       /* 0L here means no src route */
00543     uint8_t  src_len      /* 0 here means no src route */
00544 #define      NIC_ROUTE_NO_IF (-1)
00545 );
00546 
00547 extern
00548 void nic_route_free(void *);
00549 
00550 extern
00551 NIC_Res_t nic_add_route( IProute_t );
00552 
00553 extern
00554 NIC_Res_t nic_update_route( IProute_t );
00555 
00556 extern
00557 NIC_Res_t nic_remove_route( IProute_t );
00558 
00559 extern
00560 IProute_list_t *nic_route_list_new( IProute_t, ... );
00561 /* All args in list must be IProute_t; last arg MUST be 0
00562  */
00563 void nic_route_list_free( IProute_list_t* );
00564 
00565 extern
00566 NIC_Res_t nic_add_routes( IProute_list_t* );
00567 
00568 extern
00569 NIC_Res_t nic_remove_routes( IProute_list_t* );
00570 
00571 extern
00572 NIC_Res_t nic_update_routes( IProute_list_t* );
00573 
00574 struct rtnl_route *nic_rtnl_route_get(IProute_t);
00575 void nic_rtnl_route_put(struct rtnl_route *);
00576 /* Every other RTA attribute can be set with 
00577  * the libnl rtnl_route module - include the
00578  * libnl headers to use, and call rtnl_route_put
00579  * when finished with the pointer.
00580  */
00581 extern void nic_free_route( IProute_t );
00582 /**@}*/
00583 
00584 /*
00585  * Global Configuration Operations:
00586  */
00587 
00588 /**
00589  * @addtogroup NICG
00590  * @{
00591  */
00592 
00593 extern
00594 NIC_Res_t 
00595 nic_configure_resolver
00596 (
00597     NLH_t          nh,
00598     IPaddr_list_t  *dns_servers,
00599     char           *search_list
00600 ); /**< DNS resolv.conf configuration */
00601 
00602 extern
00603 NIC_Res_t 
00604 nic_set_hostname
00605 ( NLH_t nh,
00606   char *hostname
00607 );
00608 
00609 extern
00610 NIC_Res_t 
00611 nic_configure
00612 (
00613     NLH_t          nh,
00614     NIC_t          nic,
00615     IPaddr_list_t  *addresses,
00616     IProute_list_t *routes,
00617     IPaddr_list_t  *dns_servers,
00618     char           *search_list,
00619     char           *host_name
00620 );
00621 /**@}*/
00622 #endif

Generated on Thu Aug 10 21:26:26 2006 for libdhcp by  doxygen 1.4.7