WIP on libradsec: 94e3f46 Example client crafting simple packet using freeradius...
[libradsec.git] / lib / examples / client.c
index 64a4436..37601b6 100644 (file)
@@ -3,7 +3,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
-#include <freeradius/libradius.h>
 #include "../libradsec.h"
 #include "../debug.h"
 
 #define USER_PW "hemligt"
 
 int
-rsx_client ()
+rsx_client (const char *srvname, int srvport)
 {
-  fr_randctx fr_ctx;
-  struct rs_handle *ctx;
-  struct rs_connection *conn;
-  RADIUS_PACKET *pkt;
-  VALUE_PAIR *vp;
-  char user_pw[MAX_STRING_LEN];
-  uint8_t reqauth[AUTH_VECTOR_LEN];
+  struct rs_context *h;
+  struct rs_connecion *conn;
+  struct rs_packet *req, *resp;
 
-  fr_log_fp = stderr;
-  fr_debug_flag = 1;
-  fr_randinit (&fr_ctx, 0);
-  fr_rand_seed (NULL, 0);
+  if (rs_context_create (&h, "/usr/share/freeradius/dictionary"))
+    return rs_err_code (rs_ctx_err_code (h));
 
-  printf ("creating context\n");
-  if (rs_context_create(&ctx))
-    return -1;
+  if (rs_conn_new (h, &conn))
+    return rs_err_code (rs_conn_err_code (conn));
+  if (rs_conn_add_server (conn, RS_CONN_TYPE_UDP, srvname, srvport, 10, 3, SECRET))
+    return rs_err_code (rs_conn_err_code (conn));
 
-#if 0
-  printf ("reading config\n");
-  if (rs_context_config_read(ctx, "libradsec.conf"))
-    return -1;
-#endif
+  if (rs_packet_create_acc_request (conn, &req, USER_NAME, USER_PW))
+    return rs_err_code (rs_conn_err_code (conn));
 
-  printf ("init dict");
-  if (dict_init("/usr/share/freeradius", "dictionary"))
-    return -1;
+  if (rs_packet_send (req))
+    return rs_err_code (rs_conn_err_code (conn));
+  req = NULL;
 
-#if 0
-  printf ("creating connection\n");
-  if (rs_conn_create(ctx, &conn))
-    return -1;
-#endif
+  if (rs_packet_recv (conn, &resp))
+    return rs_err_code (rs_conn_err_code (conn));
 
-  printf ("creating a packet\n");
-  pkt = rad_alloc (1);
-  if (!pkt) {
-    fr_perror ("pairmake");
-    return -1;
-  }
-
-  {
-    size_t pwlen =  sizeof(USER_PW);
-    strncpy (user_pw, USER_PW, sizeof(user_pw));
-    rad_pwencode(user_pw, &pwlen, SECRET, reqauth);
-  }
-
-  printf ("creating value pairs\n");
-  vp = pairmake ("User-Name", USER_NAME, 0);
-  if (!vp) {
-    fr_perror ("paircreate");
-    return -1;
-  }
-  pairadd (&vp, pairmake ("User-Password", user_pw, 0));
-  pkt->vps = vp;
-
-  printf ("attributes:\n");
-  vp_printlist (stdout, vp);
-
-  printf ("encoding packet\n");
-  rad_encode (pkt, NULL, SECRET);
-  print_hex (pkt);             /* DEBUG */
-
-#if 0
-  rs_packet_create (&pkt, RS_ACCESS_REQUEST);
-  rs_attrib_create (&attr, RS_...);
-  rs_packet_add_attrib (pkt, attr);
-#endif
-
-  //rs_packet_send (conn, pkt, ...);
-
-  rad_free(&pkt);
-
-#if 0
-  printf ("destroying connection\n");
-  if (rs_conn_destroy(conn))
-    return -1;
-#endif
-
-  printf ("destroying context\n");
-  rs_context_destroy(ctx);
-
-  return 0;
+  rs_conn_destroy (conn);
+  rs_context_destroy (h);
 }
 
 int